Skip to content

Commit

Permalink
fix assured workloads tests (GoogleCloudPlatform#4976)
Browse files Browse the repository at this point in the history
* fix assured workloads tests

* consume folder dynamically and fix style
  • Loading branch information
ScottSuarez authored and khajduczenia committed Oct 12, 2021
1 parent 61aca85 commit 4445d91
Showing 1 changed file with 130 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ package google
import (
"context"
"fmt"
"log"
"strconv"
"strings"
"testing"
"time"

dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
<% unless version == 'ga' -%>
Expand All @@ -14,13 +17,11 @@ import (
assuredworkloads "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/assuredworkloads"
<% end -%>
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccAssuredWorkloadsWorkload_basic(t *testing.T) {
// skipping vcr testing intermitently while tests are resolved.
// resource is confirmed working... hitting quota issues.
skipIfVcr(t)
t.Parallel()

context := map[string]interface{}{
Expand All @@ -41,23 +42,23 @@ func TestAccAssuredWorkloadsWorkload_basic(t *testing.T) {
ResourceName: "google_assured_workloads_workload.meep",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"billing_account"},
ImportStateVerifyIgnore: []string{"billing_account", "kms_settings", "resource_settings", "provisioned_resources_parent"},
},
{
Config: testAccAssuredWorkloadsWorkload_basicUpdate(context),
Check: deleteAssuredWorkloadProvisionedResources(t),
},
{
ResourceName: "google_assured_workloads_workload.meep",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"billing_account"},
ImportStateVerifyIgnore: []string{"billing_account", "kms_settings", "resource_settings", "provisioned_resources_parent"},
},
},
})
}

func TestAccAssuredWorkloadsWorkload_full(t *testing.T) {
skipIfVcr(t)
t.Parallel()

context := map[string]interface{}{
Expand All @@ -73,6 +74,7 @@ func TestAccAssuredWorkloadsWorkload_full(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccAssuredWorkloadsWorkload_full(context),
Check: resource.ComposeTestCheckFunc(deleteAssuredWorkloadProvisionedResources(t)),
},
{
ResourceName: "google_assured_workloads_workload.meep",
Expand All @@ -84,51 +86,145 @@ func TestAccAssuredWorkloadsWorkload_full(t *testing.T) {
})
}

// deleteAssuredWorkloadProvisionedResources deletes the resources provisioned by
// assured workloads.. this is needed in order to delete the parent resource
func deleteAssuredWorkloadProvisionedResources(t *testing.T) resource.TestCheckFunc {
return func(s *terraform.State) error {
config := googleProviderConfig(t)
timeout := *schema.DefaultTimeout(4 * time.Minute)
for _, rs := range s.RootModule().Resources {
if rs.Type != "google_assured_workloads_workload" {
continue
}
resourceAttributes := rs.Primary.Attributes
n, err := strconv.Atoi(resourceAttributes["resources.#"])
log.Printf("[DEBUG]found %v resources\n", n)
log.Println(resourceAttributes)
if err != nil {
return err
}

// first delete the projects
for i := 0; i < n; i++ {
typee := resourceAttributes[fmt.Sprintf("resources.%d.resource_type", i)]
if !strings.Contains(typee, "PROJECT") {
continue
}
resource_id := resourceAttributes[fmt.Sprintf("resources.%d.resource_id", i)]
log.Printf("[DEBUG] searching for project %s\n", resource_id)
err := retryTimeDuration(func() (reqErr error) {
_, reqErr = config.NewResourceManagerClient(config.userAgent).Projects.Get(resource_id).Do()
return reqErr
}, timeout)
if err != nil {
log.Printf("[DEBUG] did not find project %sn", resource_id)
continue
}
log.Printf("[DEBUG] found project %s\n", resource_id)

err = retryTimeDuration(func() error {
_, delErr := config.NewResourceManagerClient(config.userAgent).Projects.Delete(resource_id).Do()
return delErr
}, timeout)
if err != nil {
log.Printf("Error deleting project '%s': %s\n ", resource_id, err)
continue
}
log.Printf("[DEBUG] deleted project %s\n", resource_id)
}

// Then delete the folders
for i := 0; i < n; i++ {
typee := resourceAttributes[fmt.Sprintf("resources.%d.resource_type", i)]
if typee != "CONSUMER_FOLDER" {
continue
}
resource_id := "folders/" + resourceAttributes[fmt.Sprintf("resources.%d.resource_id", i)]
err := retryTimeDuration(func() error {
var reqErr error
_, reqErr = config.NewResourceManagerV2Client(config.userAgent).Folders.Get(resource_id).Do()
return reqErr
}, timeout)
log.Printf("[DEBUG] searching for folder %s\n", resource_id)
if err != nil {
log.Printf("[DEBUG] did not find folder %sn", resource_id)
continue
}
log.Printf("[DEBUG] found folder %s\n", resource_id)
err = retryTimeDuration(func() error {
_, reqErr := config.NewResourceManagerV2Client(config.userAgent).Folders.Delete(resource_id).Do()
return reqErr
}, timeout)
if err != nil {
return fmt.Errorf("Error deleting folder '%s': %s\n ", resource_id, err)
}
log.Printf("[DEBUG] deleted folder %s\n", resource_id)
}
}
return nil
}
}

func testAccAssuredWorkloadsWorkload_basic(context map[string]interface{}) string {
return Nprintf(`
resource "google_assured_workloads_workload" "meep" {
display_name = "workloadExample"
labels = {
a = "a"
}
billing_account = "billingAccounts/%{billing_account}"
compliance_regime = "FEDRAMP_MODERATE"
organization = "%{org_id}"
location = "us-central1"
provisioned_resources_parent = "folders/177863664720"
display_name = "workloadExample-%{random_suffix}"
labels = {
a = "a"
}
billing_account = "billingAccounts/%{billing_account}"
compliance_regime = "FEDRAMP_MODERATE"
provisioned_resources_parent = google_folder.folder1.name
organization = "%{org_id}"
location = "us-central1"
}

resource "google_folder" "folder1" {
display_name = "tf-test-%{random_suffix}"
parent = "organizations/%{org_id}"
}
`, context)
}

func testAccAssuredWorkloadsWorkload_basicUpdate(context map[string]interface{}) string {
return Nprintf(`
resource "google_assured_workloads_workload" "meep" {
display_name = "updatedExample"
labels = {
a = "b"
}
billing_account = "billingAccounts/%{billing_account}"
compliance_regime = "FEDRAMP_MODERATE"
organization = "%{org_id}"
location = "us-central1"
provisioned_resources_parent = "folders/177863664720"
display_name = "updatedExample-%{random_suffix}"
labels = {
a = "b"
}
billing_account = "billingAccounts/%{billing_account}"
compliance_regime = "FEDRAMP_MODERATE"
provisioned_resources_parent = google_folder.folder1.name
organization = "%{org_id}"
location = "us-central1"
}

resource "google_folder" "folder1" {
display_name = "tf-test-%{random_suffix}"
parent = "organizations/%{org_id}"
}
`, context)
}

func testAccAssuredWorkloadsWorkload_full(context map[string]interface{}) string {
return Nprintf(`
resource "google_assured_workloads_workload" "meep" {
display_name = "workloadExample"
billing_account = "billingAccounts/%{billing_account}"
compliance_regime = "FEDRAMP_MODERATE"
organization = "%{org_id}"
location = "us-central1"
kms_settings {
next_rotation_time = "2021-10-02T15:01:23Z"
rotation_period = "864000s"
}
provisioned_resources_parent = "folders/177863664720"
display_name = "workloadExample-%{random_suffix}"
billing_account = "billingAccounts/%{billing_account}"
compliance_regime = "FEDRAMP_MODERATE"
organization = "%{org_id}"
location = "us-central1"
kms_settings {
next_rotation_time = "2021-10-02T15:01:23Z"
rotation_period = "864000s"
}
provisioned_resources_parent = google_folder.folder1.name
}

resource "google_folder" "folder1" {
display_name = "tf-test-%{random_suffix}"
parent = "organizations/%{org_id}"
}
`, context)
}
Expand Down

0 comments on commit 4445d91

Please sign in to comment.