|
| 1 | +package acctest |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 11 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 12 | +) |
| 13 | + |
| 14 | +// Hardcode the Terraform resource name -> API service name mapping temporarily. |
| 15 | +// TODO: [tgc] read the mapping from the resource metadata files. |
| 16 | +var ApiServiceNames = map[string]string{ |
| 17 | + "google_compute_instance": "compute.googleapis.com", |
| 18 | + "google_project": "cloudresourcemanager.googleapis.com", |
| 19 | +} |
| 20 | + |
| 21 | +// Gets the test metadata for tgc: |
| 22 | +// - test config |
| 23 | +// - cai asset name |
| 24 | +// For example: //compute.googleapis.com/projects/ci-test-188019/zones/us-central1-a/instances/tf-test-mi3fqaucf8 |
| 25 | +func GetTestMetadataForTgc(service, address, config string) resource.TestCheckFunc { |
| 26 | + return func(s *terraform.State) error { |
| 27 | + splits := strings.Split(address, ".") |
| 28 | + if splits == nil || len(splits) < 2 { |
| 29 | + return fmt.Errorf("The resource address %s is invalid.", address) |
| 30 | + } |
| 31 | + resourceType := splits[0] |
| 32 | + resourceName := splits[1] |
| 33 | + |
| 34 | + rState := s.RootModule().Resources[address] |
| 35 | + if rState == nil || rState.Primary == nil { |
| 36 | + return fmt.Errorf("The resource state is unavailable. Please check if the address %s.%s is correct.", resourceType, resourceName) |
| 37 | + } |
| 38 | + |
| 39 | + // Convert the resource ID into CAI asset name |
| 40 | + // and then print out the CAI asset name in the logs |
| 41 | + if apiServiceName, ok := ApiServiceNames[resourceType]; !ok { |
| 42 | + return fmt.Errorf("The Cai product backend name for resource %s doesn't exist.", resourceType) |
| 43 | + } else { |
| 44 | + var rName string |
| 45 | + switch resourceType { |
| 46 | + case "google_project": |
| 47 | + rName = fmt.Sprintf("projects/%s", rState.Primary.Attributes["number"]) |
| 48 | + default: |
| 49 | + rName = rState.Primary.ID |
| 50 | + } |
| 51 | + |
| 52 | + caiAssetName := fmt.Sprintf("//%s/%s", apiServiceName, rName) |
| 53 | + log.Printf("[DEBUG]TGC CAI asset name: %s", caiAssetName) |
| 54 | + } |
| 55 | + |
| 56 | + // The acceptance tests names will be also used for the tgc tests. |
| 57 | + // "service" is logged and will be used to put the tgc tests into specific service packages. |
| 58 | + log.Printf("[DEBUG]TGC Terraform service: %s", service) |
| 59 | + log.Printf("[DEBUG]TGC Terraform resource: %s", resourceType) |
| 60 | + |
| 61 | + re := regexp.MustCompile(`\"(tf[-_]?test[-_]?.*?)([a-z0-9]+)\"`) |
| 62 | + config = re.ReplaceAllString(config, `"${1}tgc"`) |
| 63 | + |
| 64 | + // Replace resource name with the resource's real name, |
| 65 | + // which is used to get the main resource object by checking the address after parsing raw config. |
| 66 | + // For example, replace `"google_compute_instance" "foobar"` with `"google_compute_instance" "tf-test-mi3fqaucf8"` |
| 67 | + n := tpgresource.GetResourceNameFromSelfLink(rState.Primary.ID) |
| 68 | + old := fmt.Sprintf(`"%s" "%s"`, resourceType, resourceName) |
| 69 | + new := fmt.Sprintf(`"%s" "%s"`, resourceType, n) |
| 70 | + config = strings.Replace(config, old, new, 1) |
| 71 | + |
| 72 | + log.Printf("[DEBUG]TGC raw_config starts %sEnd of TGC raw_config", config) |
| 73 | + return nil |
| 74 | + } |
| 75 | +} |
0 commit comments