diff --git a/mmv1/api/resource.go b/mmv1/api/resource.go index 50612df74cbf..6cf68221653b 100644 --- a/mmv1/api/resource.go +++ b/mmv1/api/resource.go @@ -937,3 +937,15 @@ func (r Resource) IgnoreReadPropertiesToString(e resource.Examples) string { func (r *Resource) SetCompiler(t string) { r.Compiler = fmt.Sprintf("%s-codegen", strings.ToLower(t)) } + +// Returns the id format of an object, or self_link_uri if none is explicitly defined +// We prefer the long name of a resource as the id so that users can reference +// resources in a standard way, and most APIs accept short name, long name or self_link +// def id_format(object) +func (r Resource) GetIdFormat() string { + idFormat := r.IdFormat + if idFormat == "" { + idFormat = r.SelfLinkUri() + } + return idFormat +} diff --git a/mmv1/products/identityplatform/Config.yaml b/mmv1/products/identityplatform/Config.yaml index bf6e71aea9dd..97bfb4f98354 100644 --- a/mmv1/products/identityplatform/Config.yaml +++ b/mmv1/products/identityplatform/Config.yaml @@ -67,7 +67,7 @@ examples: - "client.0.api_key" - "client.0.firebase_subdomain" custom_code: !ruby/object:Provider::Terraform::CustomCode - custom_create: 'templates/terraform/custom_create/identity_platform_config.go' + custom_create: 'templates/terraform/custom_create/identity_platform_config.go.erb' properties: - !ruby/object:Api::Type::String name: 'name' diff --git a/mmv1/provider/template_data.go b/mmv1/provider/template_data.go index 58f6d8d1e9ff..6dadb1492558 100644 --- a/mmv1/provider/template_data.go +++ b/mmv1/provider/template_data.go @@ -50,7 +50,9 @@ var TemplateFunctions = template.FuncMap{ "replace": strings.Replace, "camelize": google.Camelize, "underscore": google.Underscore, + "plural": google.Plural, "contains": strings.Contains, + "join": strings.Join, } var GA_VERSION = "ga" diff --git a/mmv1/templates/terraform/constants/go/access_approval.go.tmpl b/mmv1/templates/terraform/constants/go/access_approval.go.tmpl index d42733706681..c2f528549bee 100644 --- a/mmv1/templates/terraform/constants/go/access_approval.go.tmpl +++ b/mmv1/templates/terraform/constants/go/access_approval.go.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} var accessApprovalCloudProductMapping = map[string]string{ "appengine.googleapis.com": "App Engine", "bigquery.googleapis.com": "BigQuery", diff --git a/mmv1/templates/terraform/constants/go/artifact_registry_repository.go.tmpl b/mmv1/templates/terraform/constants/go/artifact_registry_repository.go.tmpl index 1c310327a25d..80d98ba92f2f 100644 --- a/mmv1/templates/terraform/constants/go/artifact_registry_repository.go.tmpl +++ b/mmv1/templates/terraform/constants/go/artifact_registry_repository.go.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} func upstreamPoliciesDiffSuppress(k, old, new string, d *schema.ResourceData) bool { o, n := d.GetChange("virtual_repository_config.0.upstream_policies") oldPolicies, ok := o.([]any) diff --git a/mmv1/templates/terraform/constants/go/backend_service.go.tmpl b/mmv1/templates/terraform/constants/go/backend_service.go.tmpl index 84d13fc9ff96..b58dedb4d505 100644 --- a/mmv1/templates/terraform/constants/go/backend_service.go.tmpl +++ b/mmv1/templates/terraform/constants/go/backend_service.go.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} // suppress changes on sample_rate if log_config is set to disabled. func suppressWhenDisabled(k, old, new string, d *schema.ResourceData) bool { _, n := d.GetChange("log_config.0.enable") diff --git a/mmv1/templates/terraform/constants/go/billing_budget.tmpl b/mmv1/templates/terraform/constants/go/billing_budget.tmpl index 0b4a87957d44..fa5c7990bf40 100644 --- a/mmv1/templates/terraform/constants/go/billing_budget.tmpl +++ b/mmv1/templates/terraform/constants/go/billing_budget.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} // Check to see if a specified value in the config exists and suppress diffs if so. Otherwise run EmptyOrDefaultStringSuppress. diff --git a/mmv1/templates/terraform/constants/go/binaryauthorization_policy.tmpl b/mmv1/templates/terraform/constants/go/binaryauthorization_policy.tmpl index 6a704187ed6f..5d5ad5833fae 100644 --- a/mmv1/templates/terraform/constants/go/binaryauthorization_policy.tmpl +++ b/mmv1/templates/terraform/constants/go/binaryauthorization_policy.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} func DefaultBinaryAuthorizationPolicy(project string) map[string]interface{} { return map[string]interface{}{ "name": fmt.Sprintf("projects/%s/policy", project), diff --git a/mmv1/templates/terraform/constants/go/cert_manager.tmpl b/mmv1/templates/terraform/constants/go/cert_manager.tmpl index 6faa077d79b8..700544afb1ed 100644 --- a/mmv1/templates/terraform/constants/go/cert_manager.tmpl +++ b/mmv1/templates/terraform/constants/go/cert_manager.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} func certManagerDefaultScopeDiffSuppress(_, old, new string, diff *schema.ResourceData) bool { if old == "" && new == "DEFAULT" || old == "DEFAULT" && new == "" { diff --git a/mmv1/templates/terraform/constants/go/compute_service_attachment.go.tmpl b/mmv1/templates/terraform/constants/go/compute_service_attachment.go.tmpl index 733488b0cb58..c98c75a0daef 100644 --- a/mmv1/templates/terraform/constants/go/compute_service_attachment.go.tmpl +++ b/mmv1/templates/terraform/constants/go/compute_service_attachment.go.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} // Hash based on key, which is either project_id_or_num or network_url. func computeServiceAttachmentConsumerAcceptListsHash(v interface{}) int { diff --git a/mmv1/templates/terraform/constants/go/datastream_stream.go.tmpl b/mmv1/templates/terraform/constants/go/datastream_stream.go.tmpl index 4943f413e6b1..49683bc145b9 100644 --- a/mmv1/templates/terraform/constants/go/datastream_stream.go.tmpl +++ b/mmv1/templates/terraform/constants/go/datastream_stream.go.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} func resourceDatastreamStreamCustomDiffFunc(diff tpgresource.TerraformResourceDiff) error { if diff.HasChange("desired_state") { old, new := diff.GetChange("desired_state") diff --git a/mmv1/templates/terraform/constants/go/dlp_stored_info_type.go.tmpl b/mmv1/templates/terraform/constants/go/dlp_stored_info_type.go.tmpl index d749adc381ca..5a121eba82b6 100644 --- a/mmv1/templates/terraform/constants/go/dlp_stored_info_type.go.tmpl +++ b/mmv1/templates/terraform/constants/go/dlp_stored_info_type.go.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} // This customizeDiff allows updating the dictionary, regex, and large_custom_dictionary fields, but // it recreates the resource if changing between these fields. e.g., updating the regex field should // be allowed, while changing from regex to dictionary should trigger the recreation of the resource. diff --git a/mmv1/templates/terraform/constants/go/firebase_database_instance.go.tmpl b/mmv1/templates/terraform/constants/go/firebase_database_instance.go.tmpl index 2a4107669e53..56b904e98b12 100644 --- a/mmv1/templates/terraform/constants/go/firebase_database_instance.go.tmpl +++ b/mmv1/templates/terraform/constants/go/firebase_database_instance.go.tmpl @@ -1,5 +1,5 @@ func enableRTDB(config *transport_tpg.Config, d *schema.ResourceData, project string, billingProject string, userAgent string) error { - url, err := tpgresource.ReplaceVars(d, config, "{{FirebaseDatabaseBasePath}}projects/{{project}}/locations/{{region}}/instances/{{instance_id}}:reenable") + url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}FirebaseDatabaseBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}region{{"}}"}}/instances/{{"{{"}}instance_id{{"}}"}}:reenable") if err != nil { return err } @@ -20,7 +20,7 @@ func enableRTDB(config *transport_tpg.Config, d *schema.ResourceData, project st } func disableRTDB(config *transport_tpg.Config, d *schema.ResourceData, project string, billingProject string, userAgent string) error { - url, err := tpgresource.ReplaceVars(d, config, "{{FirebaseDatabaseBasePath}}projects/{{project}}/locations/{{region}}/instances/{{instance_id}}:disable") + url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}FirebaseDatabaseBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}region{{"}}"}}/instances/{{"{{"}}instance_id{{"}}"}}:disable") if err != nil { return err } diff --git a/mmv1/templates/terraform/constants/go/firebase_project.go.tmpl b/mmv1/templates/terraform/constants/go/firebase_project.go.tmpl index 0a12218e36dc..7c4be2f6e49f 100644 --- a/mmv1/templates/terraform/constants/go/firebase_project.go.tmpl +++ b/mmv1/templates/terraform/constants/go/firebase_project.go.tmpl @@ -1,5 +1,5 @@ func getExistingFirebaseProjectId(config *transport_tpg.Config, d *schema.ResourceData, billingProject string, userAgent string) (string, error) { - url, err := tpgresource.ReplaceVars(d, config, "{{FirebaseBasePath}}projects/{{project}}") + url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}FirebaseBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}") if err != nil { return "", err } @@ -12,7 +12,7 @@ func getExistingFirebaseProjectId(config *transport_tpg.Config, d *schema.Resour UserAgent: userAgent, }) if err == nil { - id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}") + id, err := tpgresource.ReplaceVars(d, config, "projects/{{"{{"}}project{{"}}"}}") if err != nil { return "", fmt.Errorf("Error constructing id: %s", err) } diff --git a/mmv1/templates/terraform/constants/go/firewall.tmpl b/mmv1/templates/terraform/constants/go/firewall.tmpl index f1453a001070..76db259b9d41 100644 --- a/mmv1/templates/terraform/constants/go/firewall.tmpl +++ b/mmv1/templates/terraform/constants/go/firewall.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} func resourceComputeFirewallRuleHash(v interface{}) int { var buf bytes.Buffer m := v.(map[string]interface{}) diff --git a/mmv1/templates/terraform/constants/go/monitoring_alert_policy.go.tmpl b/mmv1/templates/terraform/constants/go/monitoring_alert_policy.go.tmpl index 4c190ebef9cb..5f652cc41414 100644 --- a/mmv1/templates/terraform/constants/go/monitoring_alert_policy.go.tmpl +++ b/mmv1/templates/terraform/constants/go/monitoring_alert_policy.go.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} // API does not return a value for REDUCE_NONE func crossSeriesReducerDiffSuppress(k, old, new string, d *schema.ResourceData) bool { diff --git a/mmv1/templates/terraform/constants/go/netapp_volume_replication.go.tmpl b/mmv1/templates/terraform/constants/go/netapp_volume_replication.go.tmpl index 10ec7b806253..5037739e6f5e 100644 --- a/mmv1/templates/terraform/constants/go/netapp_volume_replication.go.tmpl +++ b/mmv1/templates/terraform/constants/go/netapp_volume_replication.go.tmpl @@ -6,7 +6,7 @@ func NetAppVolumeReplicationWaitForMirror(d *schema.ResourceData, meta interface return err } - url, err := tpgresource.ReplaceVars(d, config, "{{NetappBasePath}}projects/{{project}}/locations/{{location}}/volumes/{{volume_name}}/replications/{{name}}") + url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}NetappBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/volumes/{{"{{"}}volume_name{{"}}"}}/replications/{{"{{"}}name{{"}}"}}") if err != nil { return err } diff --git a/mmv1/templates/terraform/constants/go/network_endpoints.go.tmpl b/mmv1/templates/terraform/constants/go/network_endpoints.go.tmpl index 1a9bcda902b5..62d1dfe961ef 100644 --- a/mmv1/templates/terraform/constants/go/network_endpoints.go.tmpl +++ b/mmv1/templates/terraform/constants/go/network_endpoints.go.tmpl @@ -10,7 +10,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} type NetworkEndpointsNetworkEndpoint struct { IPAddress string @@ -64,7 +64,7 @@ func networkEndpointsPaginatedRead(d *schema.ResourceData, config *transport_tpg func networkEndpointsPaginatedMutate(d *schema.ResourceData, endpoints []interface{}, config *transport_tpg.Config, userAgent, url, project, billingProject string, chunkSize int, returnLastPage bool) ([]interface{}, error) { // Pull out what this mutation is doing - either attachNetworkEndpoints or detachNetworkEndpoints verb := url[len(url)-len("attachNetworkEndpoints"):] - id, err := tpgresource.ReplaceVars(d, config, "{{project}}/{{zone}}/{{network_endpoint_group}}/endpoints") + id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}project{{"}}"}}/{{"{{"}}zone{{"}}"}}/{{"{{"}}network_endpoint_group{{"}}"}}/endpoints") if err != nil { return nil, fmt.Errorf("Error constructing id: %s", err) } diff --git a/mmv1/templates/terraform/constants/go/network_services_gateway.go.tmpl b/mmv1/templates/terraform/constants/go/network_services_gateway.go.tmpl index 40371dc1d068..b5cb33458554 100644 --- a/mmv1/templates/terraform/constants/go/network_services_gateway.go.tmpl +++ b/mmv1/templates/terraform/constants/go/network_services_gateway.go.tmpl @@ -9,13 +9,13 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} // Checks if there is another gateway under the same location. func gatewaysSameLocation(d *schema.ResourceData, config *transport_tpg.Config, billingProject, userAgent string) ([]interface{}, error) { log.Print("[DEBUG] Looking for gateways under the same location.") var gateways []interface{} - gatewaysUrl, err := tpgresource.ReplaceVars(d, config, "{{NetworkServicesBasePath}}projects/{{project}}/locations/{{location}}/gateways") + gatewaysUrl, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}NetworkServicesBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/gateways") if err != nil { return gateways, err } @@ -77,7 +77,7 @@ func isLastSWGGateway(gateways []interface{}, network string) bool { func deleteSWGAutoGenRouter(d *schema.ResourceData, config *transport_tpg.Config, billingProject, userAgent string) error { log.Printf("[DEBUG] Searching the network id by name %q.", d.Get("network")) - networkPath := fmt.Sprintf("{{ComputeBasePath}}%s", d.Get("network")) + networkPath := fmt.Sprintf("{{"{{"}}ComputeBasePath{{"}}"}}%s", d.Get("network")) networkUrl, err := tpgresource.ReplaceVars(d, config, networkPath) if err != nil { return err @@ -98,7 +98,7 @@ func deleteSWGAutoGenRouter(d *schema.ResourceData, config *transport_tpg.Config routerId := fmt.Sprintf("swg-autogen-router-%s", resp["id"]) log.Printf("[DEBUG] Deleting the auto generated router %q.", routerId) - routerPath := fmt.Sprintf("{{ComputeBasePath}}projects/{{project}}/regions/{{location}}/routers/%s", routerId) + routerPath := fmt.Sprintf("{{"{{"}}ComputeBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/regions/{{"{{"}}location{{"}}"}}/routers/%s", routerId) routerUrl, err := tpgresource.ReplaceVars(d, config, routerPath) if err != nil { return err diff --git a/mmv1/templates/terraform/constants/go/region_backend_service.go.tmpl b/mmv1/templates/terraform/constants/go/region_backend_service.go.tmpl index 3721e4623d72..b411dd178914 100644 --- a/mmv1/templates/terraform/constants/go/region_backend_service.go.tmpl +++ b/mmv1/templates/terraform/constants/go/region_backend_service.go.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} // Fields in "backends" that are not allowed for non-managed backend services // (loadBalancingScheme) - the API returns an error if they are set at all // in the request. diff --git a/mmv1/templates/terraform/constants/go/region_ssl_policy.tmpl b/mmv1/templates/terraform/constants/go/region_ssl_policy.tmpl index 8ba15c466265..3a399d347362 100644 --- a/mmv1/templates/terraform/constants/go/region_ssl_policy.tmpl +++ b/mmv1/templates/terraform/constants/go/region_ssl_policy.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} func regionSslPolicyCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v interface{}) error { profile := diff.Get("profile") customFeaturesCount := diff.Get("custom_features.#") diff --git a/mmv1/templates/terraform/constants/go/resource_dns_resource_record_set.go.tmpl b/mmv1/templates/terraform/constants/go/resource_dns_resource_record_set.go.tmpl index 3fe62eb8782b..6b8fdffae7d3 100644 --- a/mmv1/templates/terraform/constants/go/resource_dns_resource_record_set.go.tmpl +++ b/mmv1/templates/terraform/constants/go/resource_dns_resource_record_set.go.tmpl @@ -25,7 +25,7 @@ func rrdatasDnsDiffSuppress(k, old, new string, d *schema.ResourceData) bool { // suppress on a list when 1) its items have dups that need to be ignored // and 2) string comparison on the items may need a special parse function -// example of usage can be found ../../../third_party/terraform/services/dns/resource_dns_record_set_test.go.erb +// example of usage can be found ../../../third_party/terraform/services/dns/resource_dns_record_set_test.go.tmpl func RrdatasListDiffSuppress(oldList, newList []string, fun func(x string) string, _ *schema.ResourceData) bool { // compare two lists of unordered records diff := make(map[string]bool, len(oldList)) diff --git a/mmv1/templates/terraform/constants/go/router.go.tmpl b/mmv1/templates/terraform/constants/go/router.go.tmpl index 9fec9b5eb964..30527bd6cde7 100644 --- a/mmv1/templates/terraform/constants/go/router.go.tmpl +++ b/mmv1/templates/terraform/constants/go/router.go.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} // customizeDiff func for additional checks on google_compute_router properties: func resourceComputeRouterCustomDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error { diff --git a/mmv1/templates/terraform/constants/go/router_nat.go.tmpl b/mmv1/templates/terraform/constants/go/router_nat.go.tmpl index 1f82d0fda2d2..b5be0c8ce0f6 100644 --- a/mmv1/templates/terraform/constants/go/router_nat.go.tmpl +++ b/mmv1/templates/terraform/constants/go/router_nat.go.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} func resourceNameSetFromSelfLinkSet(v interface{}) *schema.Set { if v == nil { return schema.NewSet(schema.HashString, nil) diff --git a/mmv1/templates/terraform/constants/go/source_repo_repository.go.tmpl b/mmv1/templates/terraform/constants/go/source_repo_repository.go.tmpl index b4e0fcbdafb2..cd2da0a330e4 100644 --- a/mmv1/templates/terraform/constants/go/source_repo_repository.go.tmpl +++ b/mmv1/templates/terraform/constants/go/source_repo_repository.go.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} func resourceSourceRepoRepositoryPubSubConfigsHash(v interface{}) int { if v == nil { return 0 diff --git a/mmv1/templates/terraform/constants/go/spanner_database.go.tmpl b/mmv1/templates/terraform/constants/go/spanner_database.go.tmpl index 7da72a828e4b..76492397dcf8 100644 --- a/mmv1/templates/terraform/constants/go/spanner_database.go.tmpl +++ b/mmv1/templates/terraform/constants/go/spanner_database.go.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} // customizeDiff func for additional checks on google_spanner_database properties: func resourceSpannerDBDdlCustomDiffFunc(diff tpgresource.TerraformResourceDiff) error { old, new := diff.GetChange("ddl") diff --git a/mmv1/templates/terraform/constants/go/spanner_instance.go.tmpl b/mmv1/templates/terraform/constants/go/spanner_instance.go.tmpl index 9b5de2f4915f..71ea57489506 100644 --- a/mmv1/templates/terraform/constants/go/spanner_instance.go.tmpl +++ b/mmv1/templates/terraform/constants/go/spanner_instance.go.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} func deleteSpannerBackups(d *schema.ResourceData, config *transport_tpg.Config, res map[string]interface{}, userAgent string, billingProject string) error { var v interface{} var ok bool @@ -30,7 +30,7 @@ func deleteSpannerBackups(d *schema.ResourceData, config *transport_tpg.Config, log.Printf("[DEBUG] Found backups for resource %q: %#v)", d.Id(), item) - path := "{{SpannerBasePath}}" + backupName + path := "{{"{{"}}SpannerBasePath{{"}}"}}" + backupName url, err := tpgresource.ReplaceVars(d, config, path) if err != nil { diff --git a/mmv1/templates/terraform/constants/go/ssl_policy.tmpl b/mmv1/templates/terraform/constants/go/ssl_policy.tmpl index 9b5341a497ca..6e2a907a33d9 100644 --- a/mmv1/templates/terraform/constants/go/ssl_policy.tmpl +++ b/mmv1/templates/terraform/constants/go/ssl_policy.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} func sslPolicyCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v interface{}) error { profile := diff.Get("profile") customFeaturesCount := diff.Get("custom_features.#") diff --git a/mmv1/templates/terraform/constants/go/subscription.go.tmpl b/mmv1/templates/terraform/constants/go/subscription.go.tmpl index 0b3aaf7cb205..e12735491aef 100644 --- a/mmv1/templates/terraform/constants/go/subscription.go.tmpl +++ b/mmv1/templates/terraform/constants/go/subscription.go.tmpl @@ -9,7 +9,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} func comparePubsubSubscriptionExpirationPolicy(_, old, new string, _ *schema.ResourceData) bool { trimmedNew := strings.TrimLeft(new, "0") diff --git a/mmv1/templates/terraform/constants/go/tagtemplate_fields.go.tmpl b/mmv1/templates/terraform/constants/go/tagtemplate_fields.go.tmpl index d2c3fc2e4280..1657ab0d399e 100644 --- a/mmv1/templates/terraform/constants/go/tagtemplate_fields.go.tmpl +++ b/mmv1/templates/terraform/constants/go/tagtemplate_fields.go.tmpl @@ -9,12 +9,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -*/}} +*/ -}} //Use it to delete TagTemplate Field func deleteTagTemplateField(d *schema.ResourceData, config *transport_tpg.Config, name, billingProject, userAgent string) (error) { - url_delete, err := tpgresource.ReplaceVars(d, config, "{{DataCatalogBasePath}}{{name}}/fields/"+name+"?force={{force_delete}}") + url_delete, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}DataCatalogBasePath{{"}}"}}{{"{{"}}name{{"}}"}}/fields/"+name+"?force={{"{{"}}force_delete{{"}}"}}") if err != nil { return err } @@ -39,7 +39,7 @@ func deleteTagTemplateField(d *schema.ResourceData, config *transport_tpg.Config //Use it to create TagTemplate Field func createTagTemplateField(d *schema.ResourceData, config *transport_tpg.Config, body map[string]interface{}, name, billingProject, userAgent string) (error) { - url_create, err := tpgresource.ReplaceVars(d, config, "{{DataCatalogBasePath}}{{name}}/fields") + url_create, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}DataCatalogBasePath{{"}}"}}{{"{{"}}name{{"}}"}}/fields") if err != nil { return err } diff --git a/mmv1/templates/terraform/custom_check_destroy/go/apigee_addons_override.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/apigee_addons_override.go.tmpl new file mode 100644 index 000000000000..52d22054bb7e --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/apigee_addons_override.go.tmpl @@ -0,0 +1,35 @@ +config := acctest.GoogleProviderConfig(t) + +url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}ApigeeBasePath{{"}}"}}organizations/{{"{{"}}org{{"}}"}}") +if err != nil { + return err +} + +billingProject := "" + +if config.BillingProject != "" { + billingProject = config.BillingProject +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: billingProject, + RawURL: url, + UserAgent: config.UserAgent, +}) + +if err != nil { + // If the Apigee org doesn't exist, then a 403 can also be returned. + if transport_tpg.IsGoogleApiErrorWithCode(err, 403) || transport_tpg.IsGoogleApiErrorWithCode(err, 404) { + return nil + } else { + return err + } +} + +v, ok := res["addonsConfig"] + +if ok || v != nil { + return fmt.Errorf("ApigeeAddonConfig still exists at %s", url) +} diff --git a/mmv1/templates/terraform/custom_check_destroy/go/appengine.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/appengine.go.tmpl new file mode 100644 index 000000000000..07e0d309886f --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/appengine.go.tmpl @@ -0,0 +1 @@ +log.Printf("[DEBUG] Ignoring destroy during test") diff --git a/mmv1/templates/terraform/custom_check_destroy/go/bigquery_bi_reservation.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/bigquery_bi_reservation.go.tmpl new file mode 100644 index 000000000000..6449ec3f91a1 --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/bigquery_bi_reservation.go.tmpl @@ -0,0 +1,35 @@ +// BI reservation is a singleton resource and can't be deleted. +// We will check if the reservation info has been cleared + +config := acctest.GoogleProviderConfig(t) + +url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}BigqueryReservationBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/biReservation") +if err != nil { + return err +} + +billingProject := "" + +if config.BillingProject != "" { + billingProject = config.BillingProject +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: billingProject, + RawURL: url, + UserAgent: config.UserAgent, +}) +if err != nil { + return nil +} + +if _, ok := res["size"]; ok { + return fmt.Errorf("BIReservation was not cleared") +} +if _, ok := res["preferredTables"]; ok { + return fmt.Errorf("BIReservation was not cleared") +} + +return nil diff --git a/mmv1/templates/terraform/custom_check_destroy/go/billing_project_info.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/billing_project_info.go.tmpl new file mode 100644 index 000000000000..ebd8a74ea63d --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/billing_project_info.go.tmpl @@ -0,0 +1,27 @@ +// After deleting a project, you can still query its billing account +// (it will be empty). We change the destroy check to ensure the +// project has no billing account linked after destroying the +// google_billing_project_info resource + +config := acctest.GoogleProviderConfig(t) + +url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}CoreBillingBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/billingInfo") +if err != nil { + return err +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + RawURL: url, + UserAgent: config.UserAgent, +}) +if err != nil { + return nil +} + +if ba := res["billingAccountName"]; ba == "" { + return nil +} + +return fmt.Errorf("Billing account still linked at %s", url) diff --git a/mmv1/templates/terraform/custom_check_destroy/go/consumer_quota_override.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/consumer_quota_override.go.tmpl new file mode 100644 index 000000000000..fdfb09e126a4 --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/consumer_quota_override.go.tmpl @@ -0,0 +1,26 @@ +config := acctest.GoogleProviderConfig(t) + +url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}ServiceUsageBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/services/{{"{{"}}service{{"}}"}}/consumerQuotaMetrics/{{"{{"}}metric{{"}}"}}/limits/{{"{{"}}limit{{"}}"}}/consumerOverrides/") +if err != nil { + return err +} + +billingProject := "" + +if config.BillingProject != "" { + billingProject = config.BillingProject +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: billingProject, + RawURL: url, + UserAgent: config.UserAgent, +}) +if err == nil { + // Sometimes the API returns an empty response instead of erroring, treat empty as nonexistent + if len(res) != 0 { + return fmt.Errorf("ServiceUsageConsumerQuotaOverride still exists at %s", url) + } +} \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_check_destroy/go/firebase_database_instance.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/firebase_database_instance.go.tmpl new file mode 100644 index 000000000000..a8f8b99502b7 --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/firebase_database_instance.go.tmpl @@ -0,0 +1,34 @@ +config := acctest.GoogleProviderConfig(t) + +url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}FirebaseDatabaseBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}region{{"}}"}}/instances/{{"{{"}}instance_id{{"}}"}}") +if err != nil { + return err +} + +billingProject := "" + +if config.BillingProject != "" { + billingProject = config.BillingProject +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: billingProject, + RawURL: url, + UserAgent: config.UserAgent, +}) +if err != nil { + return err // RTDB only supports soft-delete. +} + +dbState := res["state"] +if dbState == "DELETED" { + return nil // USER_DATABASE soft deleted. +} +dbType := res["type"] +if dbState == "DISABLED" && dbType == "DEFAULT_DATABASE" { + return nil // DEFAULT_DATABASE is left in a DISABLED state because it cannot be deleted. +} + +return fmt.Errorf("firebase_database_instance %s got state=%s, want DELETED", url, s) diff --git a/mmv1/templates/terraform/custom_check_destroy/go/firebasehosting_custom_domain_soft_delete.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/firebasehosting_custom_domain_soft_delete.go.tmpl new file mode 100644 index 000000000000..e6d1df322f6a --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/firebasehosting_custom_domain_soft_delete.go.tmpl @@ -0,0 +1,23 @@ +config := acctest.GoogleProviderConfig(t) + +url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}FirebaseHostingBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/sites/{{"{{"}}site_id{{"}}"}}/customDomains/{{"{{"}}custom_domain{{"}}"}}") +if err != nil { + return err +} + +billingProject := "" + +if config.BillingProject != "" { + billingProject = config.BillingProject +} + +resp, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: billingProject, + RawURL: url, + UserAgent: config.UserAgent, +}) +if err == nil && resp["deleteTime"].(string) == "" { + return fmt.Errorf("FirebaseHostingCustomDomain still exists at %s", url) +} \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_check_destroy/go/firestore_field.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/firestore_field.go.tmpl new file mode 100644 index 000000000000..e42823418c84 --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/firestore_field.go.tmpl @@ -0,0 +1,43 @@ +// Firestore fields are not deletable. We consider the field deleted if: +// 1) the index configuration has no overrides and matches the ancestor configuration. +// 2) the ttl configuration is unset. + +config := acctest.GoogleProviderConfig(t) + +url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}FirestoreBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/databases/{{"{{"}}database{{"}}"}}/collectionGroups/{{"{{"}}collection{{"}}"}}/fields/{{"{{"}}field{{"}}"}}") +if err != nil { + return err +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + RawURL: url, + UserAgent: config.UserAgent, +}) +if err != nil { + e := err.(*googleapi.Error) + if e.Code == 403 && strings.Contains(e.Message, "Cloud Firestore API has not been used in project") { + // The acceptance test has provisioned the resources under test in a new project, and the destory check is seeing the + // effects of the project not existing. This means the service isn't enabled, and that the resource is definitely destroyed. + // We do not return the error in this case - destroy was successful + return nil + } + + // Return err in all other cases + return err +} + +if v := res["indexConfig"]; v != nil { + indexConfig := v.(map[string]interface{}) + + usesAncestorConfig, ok := indexConfig["usesAncestorConfig"].(bool) + + if !ok || !usesAncestorConfig { + return fmt.Errorf("Index configuration is not using the ancestor config %s.", url) + } +} + +if res["ttlConfig"] != nil { + return fmt.Errorf("TTL configuration was not deleted at %s.", url) +} diff --git a/mmv1/templates/terraform/custom_check_destroy/go/iam_workforce_pool.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/iam_workforce_pool.go.tmpl new file mode 100644 index 000000000000..e96ee2427b1e --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/iam_workforce_pool.go.tmpl @@ -0,0 +1,22 @@ +config := acctest.GoogleProviderConfig(t) + +url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}IAMWorkforcePoolBasePath{{"}}"}}locations/{{"{{"}}location{{"}}"}}/workforcePools/{{"{{"}}workforce_pool_id{{"}}"}}") +if err != nil { + return err +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + RawURL: url, + UserAgent: config.UserAgent, +}) +if err != nil { + return nil +} + +if v := res["state"]; v == "DELETED" { + return nil +} + +return fmt.Errorf("IAMWorkforcePool still exists at %s", url) diff --git a/mmv1/templates/terraform/custom_check_destroy/go/iam_workload_identity_pool.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/iam_workload_identity_pool.go.tmpl new file mode 100644 index 000000000000..66f7217401f9 --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/iam_workload_identity_pool.go.tmpl @@ -0,0 +1,22 @@ +config := acctest.GoogleProviderConfig(t) + +url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}IAMBetaBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/global/workloadIdentityPools/{{"{{"}}workload_identity_pool_id{{"}}"}}") +if err != nil { + return err +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + RawURL: url, + UserAgent: config.UserAgent, +}) +if err != nil { + return nil +} + +if v := res["state"]; v == "DELETED" { + return nil +} + +return fmt.Errorf("IAMBetaWorkloadIdentityPool still exists at %s", url) diff --git a/mmv1/templates/terraform/custom_check_destroy/go/iam_workload_identity_pool_provider.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/iam_workload_identity_pool_provider.go.tmpl new file mode 100644 index 000000000000..9ddbfa3c899a --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/iam_workload_identity_pool_provider.go.tmpl @@ -0,0 +1,22 @@ +config := acctest.GoogleProviderConfig(t) + +url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}IAMBetaBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/global/workloadIdentityPools/{{"{{"}}workload_identity_pool_id{{"}}"}}/providers/{{"{{"}}workload_identity_pool_provider_id{{"}}"}}") +if err != nil { + return err +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + RawURL: url, + UserAgent: config.UserAgent, +}) +if err != nil { + return nil +} + +if v := res["state"]; v == "DELETED" { + return nil +} + +return fmt.Errorf("IAMBetaWorkloadIdentityPoolProvider still exists at %s", url) diff --git a/mmv1/templates/terraform/custom_check_destroy/go/identity_platform_project_default_config.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/identity_platform_project_default_config.go.tmpl new file mode 100644 index 000000000000..4c201190faba --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/identity_platform_project_default_config.go.tmpl @@ -0,0 +1,45 @@ +config := acctest.GoogleProviderConfig(t) + +url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}IdentityPlatformBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/config") +if err != nil { + return err +} + +billingProject := "" + +if config.BillingProject != "" { + billingProject = config.BillingProject +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: billingProject, + RawURL: url, + UserAgent: config.UserAgent, +}) +if err != nil { + return fmt.Errorf("something went wrong trying to get the IdentityPlatformProjectDefaultConfig at %s", url) +} + +signIn := res["signIn"] +if signIn == nil { + return nil +} + +original := signIn.(map[string]interface{}) +if len(original) == 0 { + return nil +} + +transformed := make(map[string]interface{}) +transformed["email"] = + flattenIdentityPlatformProjectDefaultConfigSignInEmail(original["email"], nil, nil) +transformed["phone_number"] = + flattenIdentityPlatformProjectDefaultConfigSignInPhoneNumber(original["phoneNumber"], nil, nil) +transformed["anonymous"] = + flattenIdentityPlatformProjectDefaultConfigSignInAnonymous(original["anonymous"], nil, nil) + +if transformed["email"] != nil || transformed["phone_number"] != nil || transformed["anonymous"] != nil { + return fmt.Errorf("IdentityPlatformProjectDefaultConfig still exists at %s", url) +} \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_check_destroy/go/monitoring_monitored_project.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/monitoring_monitored_project.go.tmpl new file mode 100644 index 000000000000..e7972802d72e --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/monitoring_monitored_project.go.tmpl @@ -0,0 +1,42 @@ +{{/* + The license inside this block applies to this file + Copyright 2024 Google Inc. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ -}} +config := acctest.GoogleProviderConfig(t) + +url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}MonitoringBasePath{{"}}"}}v1/locations/global/metricsScopes/{{"{{"}}metrics_scope{{"}}"}}") +if err != nil { + return err +} + +billingProject := "" + +if config.BillingProject != "" { + billingProject = config.BillingProject +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: billingProject, + RawURL: url, + UserAgent: config.UserAgent, +}) + +rName := tpgresource.GetResourceNameFromSelfLink(rs.Primary.Attributes["name"]) +project, err := config.NewResourceManagerClient(config.UserAgent).Projects.Get(rName).Do() +rName = strconv.FormatInt(project.ProjectNumber, 10) + +for _, monitoredProject := range res["monitoredProjects"].([]any) { + if strings.HasSuffix(monitoredProject.(map[string]any)["name"].(string), rName) { + return fmt.Errorf("MonitoringMonitoredProject still exists at %s", url) + } +} diff --git a/mmv1/templates/terraform/custom_check_destroy/go/privateca_certificate.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/privateca_certificate.go.tmpl new file mode 100644 index 000000000000..2f5143f8c828 --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/privateca_certificate.go.tmpl @@ -0,0 +1,24 @@ +config := acctest.GoogleProviderConfig(t) + +url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}PrivatecaBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/caPools/{{"{{"}}pool{{"}}"}}/certificates/{{"{{"}}name{{"}}"}}") + +if err != nil { + return err +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + RawURL: url, + UserAgent: config.UserAgent, +}) +if err != nil { + return err +} + + +if _, ok := res["revocationDetails"]; !ok { + return fmt.Errorf("CertificateAuthority.Certificate Revocation expected %s got %s, want revocationDetails.revocationTime", url, s) +} + +return nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_check_destroy/go/privateca_certificate_authority.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/privateca_certificate_authority.go.tmpl new file mode 100644 index 000000000000..be08015674ba --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/privateca_certificate_authority.go.tmpl @@ -0,0 +1,20 @@ +config := acctest.GoogleProviderConfig(t) + +url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}PrivatecaBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/caPools/{{"{{"}}pool{{"}}"}}/certificateAuthorities/{{"{{"}}certificate_authority_id{{"}}"}}") +if err != nil { + return err +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + RawURL: url, + UserAgent: config.UserAgent, +}) +if err != nil { + return nil +} + +if s := res["state"]; s != "DELETED" { + return fmt.Errorf("CertificateAuthority %s got %s, want DELETED", url, s) +} \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_check_destroy/go/skip_delete_during_test.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/skip_delete_during_test.go.tmpl new file mode 100644 index 000000000000..07e0d309886f --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/skip_delete_during_test.go.tmpl @@ -0,0 +1 @@ +log.Printf("[DEBUG] Ignoring destroy during test") diff --git a/mmv1/templates/terraform/custom_check_destroy/go/storage_hmac_key.go.tmpl b/mmv1/templates/terraform/custom_check_destroy/go/storage_hmac_key.go.tmpl new file mode 100644 index 000000000000..f4e09436a9b6 --- /dev/null +++ b/mmv1/templates/terraform/custom_check_destroy/go/storage_hmac_key.go.tmpl @@ -0,0 +1,22 @@ +config := acctest.GoogleProviderConfig(t) + +url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}StorageBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/hmacKeys/{{"{{"}}access_id{{"}}"}}") +if err != nil { + return err +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + RawURL: url, + UserAgent: config.UserAgent, +}) +if err != nil { + return nil +} + +if v := res["state"]; v == "DELETED" { + return nil +} + +return fmt.Errorf("StorageHmacKey still exists at %s", url) diff --git a/mmv1/templates/terraform/custom_create/go/identity_platform_config.go.tmpl b/mmv1/templates/terraform/custom_create/go/identity_platform_config.go.tmpl new file mode 100644 index 000000000000..0479fcd874df --- /dev/null +++ b/mmv1/templates/terraform/custom_create/go/identity_platform_config.go.tmpl @@ -0,0 +1,53 @@ +userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) +if err != nil { + return err +} + +url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}IdentityPlatformBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/identityPlatform:initializeAuth") +if err != nil { + return err +} + +billingProject := "" + +project, err := tpgresource.GetProject(d, config) +if err != nil { + return fmt.Errorf("Error fetching project for Config: %s", err) +} +billingProject = project + +// err == nil indicates that the billing_project value was found +if bp, err := tpgresource.GetBillingProject(d, config); err == nil { + billingProject = bp +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "POST", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Timeout: d.Timeout(schema.TimeoutCreate), +}) +if err != nil { + return fmt.Errorf("Error creating Config: %s", err) +} +if err := d.Set("name", flattenIdentityPlatformConfigName(res["name"], d, config)); err != nil { + return fmt.Errorf(`Error setting computed identity field "name": %s`, err) +} + +// Store the ID now +id, err := tpgresource.ReplaceVars(d, config, "projects/{{"{{"}}project{{"}}"}}/config") +if err != nil { + return fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +// Update the resource after initializing auth to set fields. +if err := resourceIdentityPlatformConfigUpdate(d, meta); err != nil { + return err +} + +log.Printf("[DEBUG] Finished creating Config %q: %#v", d.Id(), res) + +return resourceIdentityPlatformConfigRead(d, meta) diff --git a/mmv1/templates/terraform/custom_create/identity_platform_config.go b/mmv1/templates/terraform/custom_create/identity_platform_config.go.erb similarity index 100% rename from mmv1/templates/terraform/custom_create/identity_platform_config.go rename to mmv1/templates/terraform/custom_create/identity_platform_config.go.erb diff --git a/mmv1/templates/terraform/custom_delete/go/active_directory_domain_trust.go.tmpl b/mmv1/templates/terraform/custom_delete/go/active_directory_domain_trust.go.tmpl new file mode 100644 index 000000000000..2b9dfd7bbce6 --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/active_directory_domain_trust.go.tmpl @@ -0,0 +1,80 @@ + project, err := tpgresource.GetProject(d, config) + if err != nil { + return err + } + + url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}ActiveDirectoryBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/global/domains/{{"{{"}}domain{{"}}"}}:detachTrust") + if err != nil { + return err + } + + {{/* The generate DELETE method isn't including the {trust: } object in the response body thus custom_delete is needed */ -}} + + obj := make(map[string]interface{}) + targetDomainNameProp, err := expandNestedActiveDirectoryDomainTrustTargetDomainName(d.Get("target_domain_name"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("target_domain_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, targetDomainNameProp)) { + obj["targetDomainName"] = targetDomainNameProp + } + trustTypeProp, err := expandNestedActiveDirectoryDomainTrustTrustType(d.Get("trust_type"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("trust_type"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, trustTypeProp)) { + obj["trustType"] = trustTypeProp + } + trustDirectionProp, err := expandNestedActiveDirectoryDomainTrustTrustDirection(d.Get("trust_direction"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("trust_direction"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, trustDirectionProp)) { + obj["trustDirection"] = trustDirectionProp + } + selectiveAuthenticationProp, err := expandNestedActiveDirectoryDomainTrustSelectiveAuthentication(d.Get("selective_authentication"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("selective_authentication"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, selectiveAuthenticationProp)) { + obj["selectiveAuthentication"] = selectiveAuthenticationProp + } + targetDnsIpAddressesProp, err := expandNestedActiveDirectoryDomainTrustTargetDnsIpAddresses(d.Get("target_dns_ip_addresses"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("target_dns_ip_addresses"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, targetDnsIpAddressesProp)) { + obj["targetDnsIpAddresses"] = targetDnsIpAddressesProp + } + trustHandshakeSecretProp, err := expandNestedActiveDirectoryDomainTrustTrustHandshakeSecret(d.Get("trust_handshake_secret"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("trust_handshake_secret"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, trustHandshakeSecretProp)) { + obj["trustHandshakeSecret"] = trustHandshakeSecretProp + } + + obj, err = resourceActiveDirectoryDomainTrustEncoder(d, meta, obj) + if err != nil { + return err + } + + log.Printf("[DEBUG] Deleting DomainTrust %q", d.Id()) + + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "POST", + Project: project, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutDelete), + }) + if err != nil { + return transport_tpg.HandleNotFoundError(err, d, "DomainTrust") + } + + err = ActiveDirectoryOperationWaitTime( + config, res, project, "Deleting DomainTrust", userAgent, + d.Timeout(schema.TimeoutDelete)) + + if err != nil { + return err + } + + log.Printf("[DEBUG] Finished deleting DomainTrust %q: %#v", d.Id(), res) + return nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_delete/go/appversion_delete.go.tmpl b/mmv1/templates/terraform/custom_delete/go/appversion_delete.go.tmpl new file mode 100644 index 000000000000..1e20232b0482 --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/appversion_delete.go.tmpl @@ -0,0 +1,89 @@ + +if d.Get("noop_on_destroy") == true { + log.Printf("[DEBUG] Keeping the AppVersion %q", d.Id()) + return nil +} + +project, err := tpgresource.GetProject(d, config) +if err != nil { + return err +} + +lockName, err := tpgresource.ReplaceVars(d, config, "apps/{{"{{"}}project{{"}}"}}/services/{{"{{"}}service{{"}}"}}") +if err != nil { + return err +} +transport_tpg.MutexStore.Lock(lockName) +defer transport_tpg.MutexStore.Unlock(lockName) + +if d.Get("delete_service_on_destroy") == true { + url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}AppEngineBasePath{{"}}"}}apps/{{"{{"}}project{{"}}"}}/services/{{"{{"}}service{{"}}"}}") + if err != nil { + return err + } + var obj map[string]interface{} + log.Printf("[DEBUG] Deleting Service %q", d.Id()) + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "DELETE", + Project: project, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutDelete), +{{ if $.ErrorRetryPredicates -}} + ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{ {{- join $.ErrorRetryPredicates "," -}} }, +{{ end -}} +{{ if $.ErrorAbortPredicates -}} + ErrorAbortPredicates: []transport_tpg.RetryErrorPredicateFunc{ {{- join $.ErrorAbortPredicates "," -}} }, +{{ end -}} + }) + if err != nil { + return transport_tpg.HandleNotFoundError(err, d, "Service") + } + err = AppEngineOperationWaitTime( + config, res, project, "Deleting Service", userAgent, + d.Timeout(schema.TimeoutDelete)) + + if err != nil { + return err + } + log.Printf("[DEBUG] Finished deleting Service %q: %#v", d.Id(), res) + return nil +} else { + url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}AppEngineBasePath{{"}}"}}apps/{{"{{"}}project{{"}}"}}/services/{{"{{"}}service{{"}}"}}/versions/{{"{{"}}version_id{{"}}"}}") + if err != nil { + return err + } + var obj map[string]interface{} + log.Printf("[DEBUG] Deleting AppVersion %q", d.Id()) + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "DELETE", + Project: project, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutDelete), +{{ if $.ErrorRetryPredicates -}} + ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{ {{- join $.ErrorRetryPredicates "," -}} }, +{{ end -}} +{{ if $.ErrorAbortPredicates -}} + ErrorAbortPredicates: []transport_tpg.RetryErrorPredicateFunc{ {{- join $.ErrorAbortPredicates "," -}} }, +{{ end -}} + }) + if err != nil { + return transport_tpg.HandleNotFoundError(err, d, "AppVersion") + } + err = AppEngineOperationWaitTime( + config, res, project, "Deleting AppVersion", userAgent, + d.Timeout(schema.TimeoutDelete)) + + if err != nil { + return err + } + log.Printf("[DEBUG] Finished deleting AppVersion %q: %#v", d.Id(), res) + return nil + + +} diff --git a/mmv1/templates/terraform/custom_delete/go/clear_bigquery_bi_reservation.go.tmpl b/mmv1/templates/terraform/custom_delete/go/clear_bigquery_bi_reservation.go.tmpl new file mode 100644 index 000000000000..35b277bf03a4 --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/clear_bigquery_bi_reservation.go.tmpl @@ -0,0 +1,38 @@ +obj := make(map[string]interface{}) +obj["preferredTables"] = []string{} +obj["size"] = 0 + +url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}BigqueryReservationBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/biReservation") +if err != nil { + return err +} + +log.Printf("[DEBUG] Clearing BIReservation %q: %#v", d.Id(), obj) +updateMask := []string{} + +updateMask = append(updateMask, "size") +updateMask = append(updateMask, "preferredTables") + +// updateMask is a URL parameter but not present in the schema, so ReplaceVars +// won't set it +url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) +if err != nil { + return err +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "PATCH", + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutUpdate), +}) + +if err != nil { + return fmt.Errorf("Error clearing BIReservation %q: %s", d.Id(), err) +} else { + log.Printf("[DEBUG] Finished clearing BIReservation %q: %#v", d.Id(), res) +} + +return nil diff --git a/mmv1/templates/terraform/custom_delete/go/clear_folder_access_approval_settings.go.tmpl b/mmv1/templates/terraform/custom_delete/go/clear_folder_access_approval_settings.go.tmpl new file mode 100644 index 000000000000..5da573c92fcb --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/clear_folder_access_approval_settings.go.tmpl @@ -0,0 +1,40 @@ +obj := make(map[string]interface{}) +obj["notificationEmails"] = []string{} +obj["enrolledServices"] = []string{} +obj["activeKeyVersion"] = "" + +url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}AccessApprovalBasePath{{"}}"}}folders/{{"{{"}}folder_id{{"}}"}}/accessApprovalSettings") +if err != nil { + return err +} + +log.Printf("[DEBUG] Emptying FolderSettings %q: %#v", d.Id(), obj) +updateMask := []string{} + +updateMask = append(updateMask, "notificationEmails") +updateMask = append(updateMask, "enrolledServices") +updateMask = append(updateMask, "activeKeyVersion") + +// updateMask is a URL parameter but not present in the schema, so ReplaceVars +// won't set it +url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) +if err != nil { + return err +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "PATCH", + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutUpdate), +}) + +if err != nil { + return fmt.Errorf("Error emptying FolderSettings %q: %s", d.Id(), err) +} else { + log.Printf("[DEBUG] Finished emptying FolderSettings %q: %#v", d.Id(), res) +} + +return nil diff --git a/mmv1/templates/terraform/custom_delete/go/clear_instance_settings.go.tmpl b/mmv1/templates/terraform/custom_delete/go/clear_instance_settings.go.tmpl new file mode 100644 index 000000000000..05ae8e236abe --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/clear_instance_settings.go.tmpl @@ -0,0 +1,49 @@ +url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}ComputeBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/zones/{{"{{"}}zone{{"}}"}}/instanceSettings") +if err != nil { + return err +} + +project, err := tpgresource.GetProject(d, config) +if err != nil { + return fmt.Errorf("Error fetching project for InstanceSettings: %s", err) +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "GET", + Project: project, + RawURL: url, + UserAgent: userAgent, +}) + +if err != nil { + return err +} + +obj := map[string]interface {}{"fingerprint":res["fingerprint"], "metadata":map[string]interface {}{"items":map[string]string{}}} +log.Printf("[DEBUG] Emptying InstanceSettings %#v", obj) + +url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": "*"}) +if err != nil { + return err +} + +res, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "PATCH", + Project: project, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutUpdate), +}) + +if err != nil { + return fmt.Errorf("Error emptying InstanceSettings %s", err) +} else { + log.Printf("[DEBUG] Finished emptying InstanceSettings %#v", res) +} + +time.Sleep(1 * time.Minute) + +return nil diff --git a/mmv1/templates/terraform/custom_delete/go/clear_organization_access_approval_settings.go.tmpl b/mmv1/templates/terraform/custom_delete/go/clear_organization_access_approval_settings.go.tmpl new file mode 100644 index 000000000000..6586e7f50fea --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/clear_organization_access_approval_settings.go.tmpl @@ -0,0 +1,40 @@ +obj := make(map[string]interface{}) +obj["notificationEmails"] = []string{} +obj["enrolledServices"] = []string{} +obj["activeKeyVersion"] = "" + +url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}AccessApprovalBasePath{{"}}"}}organizations/{{"{{"}}organization_id{{"}}"}}/accessApprovalSettings") +if err != nil { + return err +} + +log.Printf("[DEBUG] Emptying OrganizationSettings %q: %#v", d.Id(), obj) +updateMask := []string{} + +updateMask = append(updateMask, "notificationEmails") +updateMask = append(updateMask, "enrolledServices") +updateMask = append(updateMask, "activeKeyVersion") + +// updateMask is a URL parameter but not present in the schema, so ReplaceVars +// won't set it +url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) +if err != nil { + return err +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "PATCH", + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutUpdate), +}) + +if err != nil { + return fmt.Errorf("Error emptying OrganizationSettings %q: %s", d.Id(), err) +} else { + log.Printf("[DEBUG] Finished emptying OrganizationSettings %q: %#v", d.Id(), res) +} + +return nil diff --git a/mmv1/templates/terraform/custom_delete/go/clear_project_access_approval_settings.go.tmpl b/mmv1/templates/terraform/custom_delete/go/clear_project_access_approval_settings.go.tmpl new file mode 100644 index 000000000000..08f5bf7df9fb --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/clear_project_access_approval_settings.go.tmpl @@ -0,0 +1,40 @@ +obj := make(map[string]interface{}) +obj["notificationEmails"] = []string{} +obj["enrolledServices"] = []string{} +obj["activeKeyVersion"] = "" + +url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}AccessApprovalBasePath{{"}}"}}projects/{{"{{"}}project_id{{"}}"}}/accessApprovalSettings") +if err != nil { + return err +} + +log.Printf("[DEBUG] Emptying ProjectSettings %q: %#v", d.Id(), obj) +updateMask := []string{} + +updateMask = append(updateMask, "notificationEmails") +updateMask = append(updateMask, "enrolledServices") +updateMask = append(updateMask, "activeKeyVersion") + +// updateMask is a URL parameter but not present in the schema, so ReplaceVars +// won't set it +url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) +if err != nil { + return err +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "PATCH", + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutUpdate), +}) + +if err != nil { + return fmt.Errorf("Error emptying ProjectSettings %q: %s", d.Id(), err) +} else { + log.Printf("[DEBUG] Finished emptying ProjectSettings %q: %#v", d.Id(), res) +} + +return nil diff --git a/mmv1/templates/terraform/custom_delete/go/firebase_app_deletion_policy.tmpl b/mmv1/templates/terraform/custom_delete/go/firebase_app_deletion_policy.tmpl new file mode 100644 index 000000000000..a59a9c5cbd21 --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/firebase_app_deletion_policy.tmpl @@ -0,0 +1,57 @@ + // Handwritten + obj := make(map[string]interface{}) + if d.Get("deletion_policy") == "DELETE" { + obj["immediate"] = true + } else { + fmt.Printf("Skip deleting App %q due to deletion_policy: %q\n", d.Id(), d.Get("deletion_policy")) + return nil + } + // End of Handwritten + billingProject := "" + + project, err := tpgresource.GetProject(d, config) + if err != nil { + return fmt.Errorf("Error fetching project for App: %s", err) + } + billingProject = project + + url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}FirebaseBasePath{{"}}"}}{{"{{"}}name{{"}}"}}:remove") + if err != nil { + return err + } + + log.Printf("[DEBUG] Deleting App %q", d.Id()) + + // err == nil indicates that the billing_project value was found + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { + billingProject = bp + } + + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "POST", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutDelete), + }) + if err != nil { + return transport_tpg.HandleNotFoundError(err, d, "App") + } + + err = FirebaseOperationWaitTime( + config, res, project, "Deleting App", userAgent, + d.Timeout(schema.TimeoutDelete)) + + if err != nil { + return err + } + + log.Printf("[DEBUG] Finished deleting App %q: %#v", d.Id(), res) + + // This is useful if the Delete operation returns before the Get operation + // during post-test destroy shows the completed state of the resource. + time.Sleep(5 * time.Second) + + return nil diff --git a/mmv1/templates/terraform/custom_delete/go/firestore_field_delete.go.tmpl b/mmv1/templates/terraform/custom_delete/go/firestore_field_delete.go.tmpl new file mode 100644 index 000000000000..fb893e9b7bf1 --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/firestore_field_delete.go.tmpl @@ -0,0 +1,56 @@ +// Firestore fields cannot be deleted, instead we clear the indexConfig and ttlConfig. + +log.Printf("[DEBUG] Deleting Field %q", d.Id()) + +billingProject := "" + +project, err := tpgresource.GetProject(d, config) +if err != nil { + return fmt.Errorf("Error fetching project for App: %s", err) +} +billingProject = project + +url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}FirestoreBasePath{{"}}"}}{{"{{"}}name{{"}}"}}") +if err != nil { + return err +} + +updateMask := []string{"indexConfig", "ttlConfig"} + +url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) +if err != nil { + return err +} + + +// err == nil indicates that the billing_project value was found +if bp, err := tpgresource.GetBillingProject(d, config); err == nil { + billingProject = bp +} + +// Clear fields by sending an empty PATCH request with appropriate update mask. +req := make(map[string]interface{}) +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "PATCH", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Body: req, + Timeout: d.Timeout(schema.TimeoutUpdate), +}) + +if err != nil { + return fmt.Errorf("Error deleting Field %q: %s", d.Id(), err) +} + +err = FirestoreOperationWaitTime( + config, res, project, "Deleting Field", userAgent, + d.Timeout(schema.TimeoutDelete)) + +if err != nil { + return err +} + +log.Printf("[DEBUG] Finished deleting Field %q", d.Id()) +return nil diff --git a/mmv1/templates/terraform/custom_delete/go/kms_crypto_key.tmpl b/mmv1/templates/terraform/custom_delete/go/kms_crypto_key.tmpl new file mode 100644 index 000000000000..12ecf3ef4d1d --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/kms_crypto_key.tmpl @@ -0,0 +1,25 @@ + cryptoKeyId, err := ParseKmsCryptoKeyId(d.Id(), config) + if err != nil { + return err + } + + log.Printf(` +[WARNING] KMS CryptoKey resources cannot be deleted from GCP. The CryptoKey %s will be removed from Terraform state, +and all its CryptoKeyVersions will be destroyed, but it will still be present in the project.`, cryptoKeyId.CryptoKeyId()) + + // Delete all versions of the key + if err := clearCryptoKeyVersions(cryptoKeyId, userAgent, config); err != nil { + return err + } + + // Make sure automatic key rotation is disabled if set + if d.Get("rotation_period") != "" { + if err := disableCryptoKeyRotation(cryptoKeyId, userAgent, config); err != nil { + return fmt.Errorf( + "While cryptoKeyVersions were cleared, Terraform was unable to disable automatic rotation of key due to an error: %s."+ + "Please retry or manually disable automatic rotation to prevent creation of a new version of this key.", err) + } + } + + d.SetId("") + return nil diff --git a/mmv1/templates/terraform/custom_delete/go/kms_crypto_key_version.tmpl b/mmv1/templates/terraform/custom_delete/go/kms_crypto_key_version.tmpl new file mode 100644 index 000000000000..a1683a05dd3e --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/kms_crypto_key_version.tmpl @@ -0,0 +1,13 @@ + + cryptoKeyVersionId, err := parseKmsCryptoKeyVersionId(d.Id(), config) + if err != nil { + return err + } + if cryptoKeyVersionId == nil { + return nil + } + if err := deleteCryptoKeyVersions(cryptoKeyVersionId, d, userAgent, config); err != nil { + return nil + } + d.SetId("") + return nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_delete/go/monitoring_uptime_check_config.go.tmpl b/mmv1/templates/terraform/custom_delete/go/monitoring_uptime_check_config.go.tmpl new file mode 100644 index 000000000000..30dc2bf22c6a --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/monitoring_uptime_check_config.go.tmpl @@ -0,0 +1,53 @@ +billingProject := "" + +project, err := tpgresource.GetProject(d, config) +if err != nil { + return fmt.Errorf("Error fetching project for UptimeCheckConfig: %s", err) +} +billingProject = project + +lockName, err := tpgresource.ReplaceVars(d, config, "stackdriver/groups/{{"{{"}}project{{"}}"}}") +if err != nil { + return err +} +transport_tpg.MutexStore.Lock(lockName) +defer transport_tpg.MutexStore.Unlock(lockName) + +url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}MonitoringBasePath{{"}}"}}v3/{{"{{"}}name{{"}}"}}") +if err != nil { + return err +} + +var obj map[string]interface{} +log.Printf("[DEBUG] Deleting UptimeCheckConfig %q", d.Id()) + +// err == nil indicates that the billing_project value was found +if bp, err := tpgresource.GetBillingProject(d, config); err == nil { + billingProject = bp +} + +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "DELETE", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutDelete), + ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.IsMonitoringConcurrentEditError}, +}) + + +{{/* The generated DELETE method returns a generic error for 400. + Need to include a help message about deleting associated Alert Policies. */ -}} + +if err != nil { + if transport_tpg.IsGoogleApiErrorWithCode(err, 400) { + err := fmt.Errorf("%w - please ensure all associated Alert Policies are deleted.", err) + return errwrap.Wrapf("Error when reading or editing UptimeCheckConfig: {{"{{"}}err{{"}}"}}", err) + } + return transport_tpg.HandleNotFoundError(err, d, "UptimeCheckConfig") +} + +log.Printf("[DEBUG] Finished deleting UptimeCheckConfig %q: %#v", d.Id(), res) +return nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_delete/go/per_instance_config.go.tmpl b/mmv1/templates/terraform/custom_delete/go/per_instance_config.go.tmpl new file mode 100644 index 000000000000..1063876a5063 --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/per_instance_config.go.tmpl @@ -0,0 +1,115 @@ + project, err := tpgresource.GetProject(d, config) + if err != nil { + return err + } + + lockName, err := tpgresource.ReplaceVars(d, config, "instanceGroupManager/{{"{{"}}project{{"}}"}}/{{"{{"}}zone{{"}}"}}/{{"{{"}}instance_group_manager{{"}}"}}") + if err != nil { + return err + } + transport_tpg.MutexStore.Lock(lockName) + defer transport_tpg.MutexStore.Unlock(lockName) + + var url string + if d.Get("remove_instance_on_destroy").(bool) { + url, err = tpgresource.ReplaceVars(d, config, "{{"{{"}}ComputeBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/zones/{{"{{"}}zone{{"}}"}}/instanceGroupManagers/{{"{{"}}instance_group_manager{{"}}"}}/deleteInstances") + } else { + url, err = tpgresource.ReplaceVars(d, config, "{{"{{"}}ComputeBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/zones/{{"{{"}}zone{{"}}"}}/instanceGroupManagers/{{"{{"}}instance_group_manager{{"}}"}}/deletePerInstanceConfigs") + } + if err != nil { + return err + } + + var obj map[string]interface{} + if d.Get("remove_instance_on_destroy").(bool) { + // Instance name in deleteInstances request must include zone + instanceName, err := tpgresource.ReplaceVars(d, config, "zones/{{"{{"}}zone{{"}}"}}/instances/{{"{{"}}name{{"}}"}}") + if err != nil { + return err + } + + obj = map[string]interface{}{ + "instances": [1]string{instanceName}, + } + } else { + obj = map[string]interface{}{ + "names": [1]string{d.Get("name").(string)}, + } + } + log.Printf("[DEBUG] Deleting PerInstanceConfig %q", d.Id()) + + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "POST", + Project: project, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutDelete), + }) + if err != nil { + return transport_tpg.HandleNotFoundError(err, d, "PerInstanceConfig") + } + + err = ComputeOperationWaitTime( + config, res, project, "Deleting PerInstanceConfig", userAgent, + d.Timeout(schema.TimeoutDelete)) + + if err != nil { + return err + } + + if d.Get("remove_instance_on_destroy").(bool) { + err = transport_tpg.PollingWaitTime(resourceComputePerInstanceConfigInstancePollRead(d, meta, d.Get("name").(string)), PollCheckInstanceConfigInstanceDeleted, "Deleting PerInstanceConfig", d.Timeout(schema.TimeoutDelete), 1) + if err != nil { + return fmt.Errorf("Error waiting for instance delete on PerInstanceConfig %q: %s", d.Id(), err) + } + } else if d.Get("remove_instance_state_on_destroy").(bool) { + // Potentially delete the state managed by this config + + // Instance name in applyUpdatesToInstances request must include zone + instanceName, err := tpgresource.ReplaceVars(d, config, "zones/{{"{{"}}zone{{"}}"}}/instances/{{"{{"}}name{{"}}"}}") + if err != nil { + return err + } + + obj = make(map[string]interface{}) + obj["instances"] = []string{instanceName} + + // The deletion must be applied to the instance after the PerInstanceConfig is deleted + url, err = tpgresource.ReplaceVars(d, config, "{{"{{"}}ComputeBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/zones/{{"{{"}}zone{{"}}"}}/instanceGroupManagers/{{"{{"}}instance_group_manager{{"}}"}}/applyUpdatesToInstances") + if err != nil { + return err + } + + log.Printf("[DEBUG] Applying updates to PerInstanceConfig %q: %#v", d.Id(), obj) + res, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "POST", + Project: project, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutUpdate), + }) + + if err != nil { + return fmt.Errorf("Error deleting PerInstanceConfig %q: %s", d.Id(), err) + } + + err = ComputeOperationWaitTime( + config, res, project, "Applying update to PerInstanceConfig", userAgent, + d.Timeout(schema.TimeoutUpdate)) + if err != nil { + return fmt.Errorf("Error deleting PerInstanceConfig %q: %s", d.Id(), err) + } + + // PerInstanceConfig goes into "DELETING" state while the instance is actually deleted + err = transport_tpg.PollingWaitTime(resourceComputePerInstanceConfigPollRead(d, meta), PollCheckInstanceConfigDeleted, "Deleting PerInstanceConfig", d.Timeout(schema.TimeoutDelete), 1) + if err != nil { + return fmt.Errorf("Error waiting for delete on PerInstanceConfig %q: %s", d.Id(), err) + } + } + + log.Printf("[DEBUG] Finished deleting PerInstanceConfig %q: %#v", d.Id(), res) + return nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_delete/go/region_per_instance_config.go.tmpl b/mmv1/templates/terraform/custom_delete/go/region_per_instance_config.go.tmpl new file mode 100644 index 000000000000..57027d277ddb --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/region_per_instance_config.go.tmpl @@ -0,0 +1,115 @@ + project, err := tpgresource.GetProject(d, config) + if err != nil { + return err + } + + lockName, err := tpgresource.ReplaceVars(d, config, "instanceGroupManager/{{"{{"}}project{{"}}"}}/{{"{{"}}region{{"}}"}}/{{"{{"}}region_instance_group_manager{{"}}"}}") + if err != nil { + return err + } + transport_tpg.MutexStore.Lock(lockName) + defer transport_tpg.MutexStore.Unlock(lockName) + + var url string + if d.Get("remove_instance_on_destroy").(bool) { + url, err = tpgresource.ReplaceVars(d, config, "{{"{{"}}ComputeBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/regions/{{"{{"}}region{{"}}"}}/instanceGroupManagers/{{"{{"}}region_instance_group_manager{{"}}"}}/deleteInstances") + } else { + url, err = tpgresource.ReplaceVars(d, config, "{{"{{"}}ComputeBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/regions/{{"{{"}}region{{"}}"}}/instanceGroupManagers/{{"{{"}}region_instance_group_manager{{"}}"}}/deletePerInstanceConfigs") + } + if err != nil { + return err + } + + var obj map[string]interface{} + if d.Get("remove_instance_on_destroy").(bool) { + // Instance name in deleteInstances request must include zone + instanceName, err := findInstanceName(d, config) + if err != nil { + return err + } + + obj = map[string]interface{}{ + "instances": [1]string{instanceName}, + } + } else { + obj = map[string]interface{}{ + "names": [1]string{d.Get("name").(string)}, + } + } + log.Printf("[DEBUG] Deleting RegionPerInstanceConfig %q", d.Id()) + + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "POST", + Project: project, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutDelete), + }) + if err != nil { + return transport_tpg.HandleNotFoundError(err, d, "RegionPerInstanceConfig") + } + + err = ComputeOperationWaitTime( + config, res, project, "Deleting RegionPerInstanceConfig", userAgent, + d.Timeout(schema.TimeoutDelete)) + + if err != nil { + return err + } + + if d.Get("remove_instance_on_destroy").(bool) { + err = transport_tpg.PollingWaitTime(resourceComputeRegionPerInstanceConfigInstancePollRead(d, meta, d.Get("name").(string)), PollCheckInstanceConfigInstanceDeleted, "Deleting RegionPerInstanceConfig", d.Timeout(schema.TimeoutDelete), 1) + if err != nil { + return fmt.Errorf("Error waiting for instance delete on RegionPerInstanceConfig %q: %s", d.Id(), err) + } + } else if d.Get("remove_instance_state_on_destroy").(bool) { + // Potentially delete the state managed by this config + // Instance name in applyUpdatesToInstances request must include zone + instanceName, err := findInstanceName(d, config) + if err != nil { + return err + } + + obj = make(map[string]interface{}) + obj["instances"] = []string{instanceName} + + // Updates must be applied to the instance after deleting the PerInstanceConfig + url, err = tpgresource.ReplaceVars(d, config, "{{"{{"}}ComputeBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/regions/{{"{{"}}region{{"}}"}}/instanceGroupManagers/{{"{{"}}region_instance_group_manager{{"}}"}}/applyUpdatesToInstances") + if err != nil { + return err + } + + log.Printf("[DEBUG] Applying updates to PerInstanceConfig %q: %#v", d.Id(), obj) + res, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "POST", + Project: project, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutUpdate), + }) + + if err != nil { + return fmt.Errorf("Error updating PerInstanceConfig %q: %s", d.Id(), err) + } + + err = ComputeOperationWaitTime( + config, res, project, "Applying update to PerInstanceConfig", userAgent, + d.Timeout(schema.TimeoutUpdate)) + + if err != nil { + return fmt.Errorf("Error deleting PerInstanceConfig %q: %s", d.Id(), err) + } + + // RegionPerInstanceConfig goes into "DELETING" state while the instance is actually deleted + err = transport_tpg.PollingWaitTime(resourceComputeRegionPerInstanceConfigPollRead(d, meta), PollCheckInstanceConfigDeleted, "Deleting RegionPerInstanceConfig", d.Timeout(schema.TimeoutDelete), 1) + if err != nil { + return fmt.Errorf("Error waiting for delete on RegionPerInstanceConfig %q: %s", d.Id(), err) + } + } + + log.Printf("[DEBUG] Finished deleting RegionPerInstanceConfig %q: %#v", d.Id(), res) + return nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_delete/go/replace_all_access_levels_empty_list.go.tmpl b/mmv1/templates/terraform/custom_delete/go/replace_all_access_levels_empty_list.go.tmpl new file mode 100644 index 000000000000..5d3f716fc2cb --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/replace_all_access_levels_empty_list.go.tmpl @@ -0,0 +1,33 @@ +obj := make(map[string]interface{}) +obj["accessLevels"] = []string{} + +url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}AccessContextManagerBasePath{{"}}"}}{{"{{"}}parent{{"}}"}}/accessLevels:replaceAll") +if err != nil { + return err +} + +log.Printf("[DEBUG] Deleting AccessLevels %q: %#v", d.Id(), obj) +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "POST", + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutUpdate), +}) + +if err != nil { + return fmt.Errorf("Error deleting AccessLevels %q: %s", d.Id(), err) +} else { + log.Printf("[DEBUG] Finished deleting AccessLevels %q: %#v", d.Id(), res) +} + +err = AccessContextManagerOperationWaitTime( + config, res, "Updating AccessLevels", userAgent, + d.Timeout(schema.TimeoutUpdate)) + +if err != nil { + return err +} + +return nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_delete/go/replace_all_service_perimeters_empty_list.go.tmpl b/mmv1/templates/terraform/custom_delete/go/replace_all_service_perimeters_empty_list.go.tmpl new file mode 100644 index 000000000000..d8cd95f965ce --- /dev/null +++ b/mmv1/templates/terraform/custom_delete/go/replace_all_service_perimeters_empty_list.go.tmpl @@ -0,0 +1,33 @@ +obj := make(map[string]interface{}) +obj["servicePerimeters"] = []string{} + +url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}AccessContextManagerBasePath{{"}}"}}{{"{{"}}parent{{"}}"}}/servicePerimeters:replaceAll") +if err != nil { + return err +} + +log.Printf("[DEBUG] Deleting servicePerimeters %q: %#v", d.Id(), obj) +res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "POST", + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: d.Timeout(schema.TimeoutUpdate), +}) + +if err != nil { + return fmt.Errorf("Error deleting ServicePerimeters %q: %s", d.Id(), err) +} else { + log.Printf("[DEBUG] Finished deleting ServicePerimeters %q: %#v", d.Id(), res) +} + +err = AccessContextManagerOperationWaitTime( + config, res, "Updating ServicePerimeters", userAgent, + d.Timeout(schema.TimeoutUpdate)) + +if err != nil { + return err +} + +return nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/access_context_manager_service_perimeter_egress_policy.go.tmpl b/mmv1/templates/terraform/custom_import/go/access_context_manager_service_perimeter_egress_policy.go.tmpl new file mode 100644 index 000000000000..a1d96883938d --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/access_context_manager_service_perimeter_egress_policy.go.tmpl @@ -0,0 +1,24 @@ +{{/* + The license inside this block applies to this file + Copyright 2024 Google Inc. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ -}} + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + parts, err := tpgresource.GetImportIdQualifiers([]string{"accessPolicies/(?P[^/]+)/servicePerimeters/(?P[^/]+)"}, d, config, d.Id()) + if err != nil { + return nil, err + } + + if err := d.Set("perimeter", fmt.Sprintf("accessPolicies/%s/servicePerimeters/%s", parts["accessPolicy"], parts["perimeter"])); err != nil { + return nil, fmt.Errorf("Error setting perimeter: %s", err) + } + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/access_context_manager_service_perimeter_ingress_policy.go.tmpl b/mmv1/templates/terraform/custom_import/go/access_context_manager_service_perimeter_ingress_policy.go.tmpl new file mode 100644 index 000000000000..a1d96883938d --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/access_context_manager_service_perimeter_ingress_policy.go.tmpl @@ -0,0 +1,24 @@ +{{/* + The license inside this block applies to this file + Copyright 2024 Google Inc. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ -}} + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + parts, err := tpgresource.GetImportIdQualifiers([]string{"accessPolicies/(?P[^/]+)/servicePerimeters/(?P[^/]+)"}, d, config, d.Id()) + if err != nil { + return nil, err + } + + if err := d.Set("perimeter", fmt.Sprintf("accessPolicies/%s/servicePerimeters/%s", parts["accessPolicy"], parts["perimeter"])); err != nil { + return nil, fmt.Errorf("Error setting perimeter: %s", err) + } + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/access_context_manager_service_perimeter_resource.go.tmpl b/mmv1/templates/terraform/custom_import/go/access_context_manager_service_perimeter_resource.go.tmpl new file mode 100644 index 000000000000..32a802fb4a45 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/access_context_manager_service_perimeter_resource.go.tmpl @@ -0,0 +1,27 @@ +{{/* + The license inside this block applies to this file + Copyright 2024 Google Inc. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ -}} + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + parts, err := tpgresource.GetImportIdQualifiers([]string{"accessPolicies/(?P[^/]+)/servicePerimeters/(?P[^/]+)/(?P.+)"}, d, config, d.Id()) + if err != nil { + return nil, err + } + + if err := d.Set("perimeter_name", fmt.Sprintf("accessPolicies/%s/servicePerimeters/%s", parts["accessPolicy"], parts["perimeter"])); err != nil { + return nil, fmt.Errorf("Error setting perimeter_name: %s", err) + } + if err := d.Set("resource", parts["resource"]); err != nil { + return nil, fmt.Errorf("Error setting resource: %s", err) + } + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/alloydb_instance.go.tmpl b/mmv1/templates/terraform/custom_import/go/alloydb_instance.go.tmpl new file mode 100644 index 000000000000..177c9f1f3e35 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/alloydb_instance.go.tmpl @@ -0,0 +1,17 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats can't import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{ + "(?P.+)/instances/(?P[^/]+)", +}, d, config); err != nil { + return nil, err +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}cluster{{"}}"}}/instances/{{"{{"}}instance_id{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/alloydb_user.go.tmpl b/mmv1/templates/terraform/custom_import/go/alloydb_user.go.tmpl new file mode 100644 index 000000000000..3beaa7454e21 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/alloydb_user.go.tmpl @@ -0,0 +1,17 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats can't import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{ + "(?P.+)/users/(?P[^/]+)", +}, d, config); err != nil { + return nil, err +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}cluster{{"}}"}}/users/{{"{{"}}user_id{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/apigee_addons.go.tmpl b/mmv1/templates/terraform/custom_import/go/apigee_addons.go.tmpl new file mode 100644 index 000000000000..4cefb8fa8373 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/apigee_addons.go.tmpl @@ -0,0 +1,36 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats can't import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +parts := strings.Split(d.Get("org").(string), "/") + +var projectId string +switch len(parts) { + case 1: + projectId = parts[0] + case 2: + projectId = parts[1] + default: + return nil, fmt.Errorf( + "Saw %s when the org is expected to have shape %s or %s", + d.Get("org"), + "{{"{{"}}org{{"}}"}}", + "organizations/{{"{{"}}org{{"}}"}}", + ) +} + +if err := d.Set("org", projectId); err != nil { + return nil, fmt.Errorf("Error setting organization: %s", err) +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "organizations/{{"{{"}}org{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/apigee_endpoint_attachment.go.tmpl b/mmv1/templates/terraform/custom_import/go/apigee_endpoint_attachment.go.tmpl new file mode 100644 index 000000000000..2e0afd971f9d --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/apigee_endpoint_attachment.go.tmpl @@ -0,0 +1,33 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats cannot import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +nameParts := strings.Split(d.Get("name").(string), "/") +if len(nameParts) == 4 { + // `organizations/{{"{{"}}org_name{{"}}"}}/endpointAttachment/{{"{{"}}endpoint_attachment_id{{"}}"}}` + orgId := fmt.Sprintf("organizations/%s", nameParts[1]) + if err := d.Set("org_id", orgId); err != nil { + return nil, fmt.Errorf("Error setting org_id: %s", err) + } + if err := d.Set("endpoint_attachment_id", nameParts[3]); err != nil { + return nil, fmt.Errorf("Error setting endpoint_attachment_id: %s", err) + } +} else { + return nil, fmt.Errorf( + "Saw %s when the name is expected to have shape %s", + d.Get("name"), + "organizations/{{"{{"}}org_name{{"}}"}}/environments/{{"{{"}}name{{"}}"}}") +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil + diff --git a/mmv1/templates/terraform/custom_import/go/apigee_env_keystore_alias_self_signed_cert.go.tmpl b/mmv1/templates/terraform/custom_import/go/apigee_env_keystore_alias_self_signed_cert.go.tmpl new file mode 100644 index 000000000000..9f4fad4c441d --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/apigee_env_keystore_alias_self_signed_cert.go.tmpl @@ -0,0 +1,18 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats cannot import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{ + "organizations/(?P[^/]+)/environments/(?P[^/]+)/keystores/(?P[^/]+)/aliases/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", + }, d, config); err != nil { + return nil, err + } + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "organizations/{{"{{"}}org_id{{"}}"}}/environments/{{"{{"}}environment{{"}}"}}/keystores/{{"{{"}}keystore{{"}}"}}/aliases/{{"{{"}}alias{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/apigee_environment.go.tmpl b/mmv1/templates/terraform/custom_import/go/apigee_environment.go.tmpl new file mode 100644 index 000000000000..bf637ce84657 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/apigee_environment.go.tmpl @@ -0,0 +1,42 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats cannot import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +nameParts := strings.Split(d.Get("name").(string), "/") +if len(nameParts) == 4 { + // `organizations/{{"{{"}}org_name{{"}}"}}/environments/{{"{{"}}name{{"}}"}}` + orgId := fmt.Sprintf("organizations/%s", nameParts[1]) + if err := d.Set("org_id", orgId); err != nil { + return nil, fmt.Errorf("Error setting org_id: %s", err) + } + if err := d.Set("name", nameParts[3]); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } +} else if len(nameParts) == 3 { + // `organizations/{{"{{"}}org_name{{"}}"}}/{{"{{"}}name{{"}}"}}` + orgId := fmt.Sprintf("organizations/%s", nameParts[1]) + if err := d.Set("org_id", orgId); err != nil { + return nil, fmt.Errorf("Error setting org_id: %s", err) + } + if err := d.Set("name", nameParts[2]); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } +} else { + return nil, fmt.Errorf( + "Saw %s when the name is expected to have shape %s or %s", + d.Get("name"), + "organizations/{{"{{"}}org_name{{"}}"}}/environments/{{"{{"}}name{{"}}"}}", + "organizations/{{"{{"}}org_name{{"}}"}}/{{"{{"}}name{{"}}"}}") +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}org_id{{"}}"}}/environments/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/apigee_environment_group.go.tmpl b/mmv1/templates/terraform/custom_import/go/apigee_environment_group.go.tmpl new file mode 100644 index 000000000000..1afb318c6f34 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/apigee_environment_group.go.tmpl @@ -0,0 +1,42 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats cannot import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +nameParts := strings.Split(d.Get("name").(string), "/") +if len(nameParts) == 4 { + // `organizations/{{"{{"}}org_name{{"}}"}}/envgroups/{{"{{"}}name{{"}}"}}` + orgId := fmt.Sprintf("organizations/%s", nameParts[1]) + if err := d.Set("org_id", orgId); err != nil { + return nil, fmt.Errorf("Error setting org_id: %s", err) + } + if err := d.Set("name", nameParts[3]); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } +} else if len(nameParts) == 3 { + // `organizations/{{"{{"}}org_name{{"}}"}}/{{"{{"}}name{{"}}"}}` + orgId := fmt.Sprintf("organizations/%s", nameParts[1]) + if err := d.Set("org_id", orgId); err != nil { + return nil, fmt.Errorf("Error setting org_id: %s", err) + } + if err := d.Set("name", nameParts[2]); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } +} else { + return nil, fmt.Errorf( + "Saw %s when the name is expected to have shape %s or %s", + d.Get("name"), + "organizations/{{"{{"}}org_name{{"}}"}}/envgroups/{{"{{"}}name{{"}}"}}", + "organizations/{{"{{"}}org_name{{"}}"}}/{{"{{"}}name{{"}}"}}") +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}org_id{{"}}"}}/envgroups/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/apigee_environment_group_attachment.go.tmpl b/mmv1/templates/terraform/custom_import/go/apigee_environment_group_attachment.go.tmpl new file mode 100644 index 000000000000..24bfa13ebca4 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/apigee_environment_group_attachment.go.tmpl @@ -0,0 +1,18 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats cannot import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{ + "(?P.+)/attachments/(?P.+)", + "(?P.+)/(?P.+)", + }, d, config); err != nil { + return nil, err + } + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}envgroup_id{{"}}"}}/attachments/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/apigee_environment_keystore.go.tmpl b/mmv1/templates/terraform/custom_import/go/apigee_environment_keystore.go.tmpl new file mode 100644 index 000000000000..05adf74c9a42 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/apigee_environment_keystore.go.tmpl @@ -0,0 +1,18 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats cannot import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{ + "(?P.+)/keystores/(?P.+)", + "(?P.+)/(?P.+)", + }, d, config); err != nil { + return nil, err + } + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}env_id{{"}}"}}/keystores/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/apigee_environment_reference.go.tmpl b/mmv1/templates/terraform/custom_import/go/apigee_environment_reference.go.tmpl new file mode 100644 index 000000000000..37eb0dfaa1d1 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/apigee_environment_reference.go.tmpl @@ -0,0 +1,18 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats cannot import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{ + "(?P.+)/references/(?P.+)", + "(?P.+)/(?P.+)", + }, d, config); err != nil { + return nil, err + } + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}env_id{{"}}"}}/references/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/apigee_instance.go.tmpl b/mmv1/templates/terraform/custom_import/go/apigee_instance.go.tmpl new file mode 100644 index 000000000000..a06fe9353325 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/apigee_instance.go.tmpl @@ -0,0 +1,42 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats cannot import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +nameParts := strings.Split(d.Get("name").(string), "/") +if len(nameParts) == 4 { + // `organizations/{{"{{"}}org_name{{"}}"}}/instances/{{"{{"}}name{{"}}"}}` + orgId := fmt.Sprintf("organizations/%s", nameParts[1]) + if err := d.Set("org_id", orgId); err != nil { + return nil, fmt.Errorf("Error setting org_id: %s", err) + } + if err := d.Set("name", nameParts[3]); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } +} else if len(nameParts) == 3 { + // `organizations/{{"{{"}}org_name{{"}}"}}/{{"{{"}}name{{"}}"}}` + orgId := fmt.Sprintf("organizations/%s", nameParts[1]) + if err := d.Set("org_id", orgId); err != nil { + return nil, fmt.Errorf("Error setting org_id: %s", err) + } + if err := d.Set("name", nameParts[2]); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } +} else { + return nil, fmt.Errorf( + "Saw %s when the name is expected to have shape %s or %s", + d.Get("name"), + "organizations/{{"{{"}}org_name{{"}}"}}/instances/{{"{{"}}name{{"}}"}}", + "organizations/{{"{{"}}org_name{{"}}"}}/{{"{{"}}name{{"}}"}}") +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}org_id{{"}}"}}/instances/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/apigee_instance_attachment.go.tmpl b/mmv1/templates/terraform/custom_import/go/apigee_instance_attachment.go.tmpl new file mode 100644 index 000000000000..ef29ac333c5f --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/apigee_instance_attachment.go.tmpl @@ -0,0 +1,18 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats cannot import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{ + "(?P.+)/attachments/(?P.+)", + "(?P.+)/(?P.+)", + }, d, config); err != nil { + return nil, err + } + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}instance_id{{"}}"}}/attachments/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/apigee_nat_address.go.tmpl b/mmv1/templates/terraform/custom_import/go/apigee_nat_address.go.tmpl new file mode 100644 index 000000000000..cbf60f11f90b --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/apigee_nat_address.go.tmpl @@ -0,0 +1,18 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats cannot import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{ + "(?P.+)/natAddresses/(?P.+)", + "(?P.+)/(?P.+)", + }, d, config); err != nil { + return nil, err + } + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}instance_id{{"}}"}}/natAddresses/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/apigee_organization.go.tmpl b/mmv1/templates/terraform/custom_import/go/apigee_organization.go.tmpl new file mode 100644 index 000000000000..ffcad2c9c2fc --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/apigee_organization.go.tmpl @@ -0,0 +1,40 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats can't import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +parts := strings.Split(d.Get("name").(string), "/") + +var projectId string +switch len(parts) { + case 1: + projectId = parts[0] + case 2: + projectId = parts[1] + default: + return nil, fmt.Errorf( + "Saw %s when the name is expected to have shape %s or %s", + d.Get("name"), + "{{"{{"}}name{{"}}"}}", + "organizations/{{"{{"}}name{{"}}"}}", + ) +} + +if err := d.Set("name", projectId); err != nil { + return nil, fmt.Errorf("Error setting organization: %s", err) +} + +if err := d.Set("project_id", projectId); err != nil { + return nil, fmt.Errorf("Error setting organization: %s", err) +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "organizations/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/apigee_target_server.go.tmpl b/mmv1/templates/terraform/custom_import/go/apigee_target_server.go.tmpl new file mode 100644 index 000000000000..890c263a23de --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/apigee_target_server.go.tmpl @@ -0,0 +1,18 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats cannot import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{ + "(?P.+)/targetservers/(?P.+)", + "(?P.+)/(?P.+)", + }, d, config); err != nil { + return nil, err + } + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}env_id{{"}}"}}/targetservers/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/bigquery_data_transfer_self_link_as_name_set_location.go.tmpl b/mmv1/templates/terraform/custom_import/go/bigquery_data_transfer_self_link_as_name_set_location.go.tmpl new file mode 100644 index 000000000000..315ffb1dc13e --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/bigquery_data_transfer_self_link_as_name_set_location.go.tmpl @@ -0,0 +1,21 @@ + +config := meta.(*transport_tpg.Config) + +// current import_formats can't import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P[^ ]+) (?P[^ ]+)", "(?P[^ ]+)"}, d, config); err != nil { + return nil, err +} + +// import location if the name format follows: projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/transferConfigs/{{"{{"}}config_id{{"}}"}} +name := d.Get("name").(string) +stringParts := strings.Split(name, "/") +if len(stringParts) == 6 { + if err := d.Set("location", stringParts[3]); err != nil { + return nil, fmt.Errorf("Error setting location: %s", err) + } +} else { + log.Printf("[INFO] Transfer config location not imported as it is not included in the name: %s", name) +} + + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/bigquery_reservation_capacity_commitment_set_id.go.tmpl b/mmv1/templates/terraform/custom_import/go/bigquery_reservation_capacity_commitment_set_id.go.tmpl new file mode 100644 index 000000000000..a50db3aec431 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/bigquery_reservation_capacity_commitment_set_id.go.tmpl @@ -0,0 +1,22 @@ +config := meta.(*transport_tpg.Config) +if err := tpgresource.ParseImportId([]string{ + "^projects/(?P[^/]+)/locations/(?P[^/]+)/capacityCommitments/(?P[^/]+)$", + "^(?P[^/]+)/(?P[^/]+)/(?P[^/]+)$", + "^(?P[^/]+)/(?P[^/]+)$", +}, d, config); err != nil { + return nil, err +} + +// Set name based on the components +if err := d.Set("name", "projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/capacityCommitments/{{"{{"}}capacity_commitment_id{{"}}"}}"); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, d.Get("name").(string)) +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/cloud_asset_feed.go.tmpl b/mmv1/templates/terraform/custom_import/go/cloud_asset_feed.go.tmpl new file mode 100644 index 000000000000..a089f93aee9a --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/cloud_asset_feed.go.tmpl @@ -0,0 +1,4 @@ +if err := d.Set("name", d.Id()); err != nil { + return nil, err +} +return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/cloud_identity_group_import.go.tmpl b/mmv1/templates/terraform/custom_import/go/cloud_identity_group_import.go.tmpl new file mode 100644 index 000000000000..91b8528c1980 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/cloud_identity_group_import.go.tmpl @@ -0,0 +1,18 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats can't import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +name := d.Get("name").(string) + +if d.Get("initial_group_config") == nil { + d.Set("initial_group_config", "EMPTY") +} + +if err := d.Set("name", name); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) +} +d.SetId(name) +return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/compute_global_network_endpoint.go.tmpl b/mmv1/templates/terraform/custom_import/go/compute_global_network_endpoint.go.tmpl new file mode 100644 index 000000000000..d18acbeeb4d0 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/compute_global_network_endpoint.go.tmpl @@ -0,0 +1,18 @@ +config := meta.(*transport_tpg.Config) +// FQDN, port and ip_address are optional, so use * instead of + when reading the import id +if err := tpgresource.ParseImportId([]string{ + "projects/(?P[^/]+)/global/networkEndpointGroups/(?P[^/]+)/(?P[^/]*)/(?P[^/]*)/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)/(?P[^/]*)/(?P[^/]*)/(?P[^/]*)", + "(?P[^/]+)/(?P[^/]*)/(?P[^/]*)/(?P[^/]*)", +}, d, config); err != nil { + return nil, err +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}project{{"}}"}}/{{"{{"}}global_network_endpoint_group{{"}}"}}/{{"{{"}}ip_address{{"}}"}}/{{"{{"}}fqdn{{"}}"}}/{{"{{"}}port{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/compute_instance_group_membership.go.tmpl b/mmv1/templates/terraform/custom_import/go/compute_instance_group_membership.go.tmpl new file mode 100644 index 000000000000..30bab333777d --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/compute_instance_group_membership.go.tmpl @@ -0,0 +1,19 @@ +config := meta.(*transport_tpg.Config) + +if err := tpgresource.ParseImportId([]string{ + "projects/(?P[^/]+)/zones/(?P[^/]+)/instanceGroups/(?P[^/]+)/(?P.+)", + "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)/(?P.+)", + "(?P[^/]+)/(?P[^/]+)/(?P.+)", + "(?P[^/]+)/(?P.+)", +}, d, config); err != nil { + return nil, err +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}project{{"}}"}}/{{"{{"}}zone{{"}}"}}/{{"{{"}}instance_group{{"}}"}}/{{"{{"}}instance{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/compute_network_endpoint.go.tmpl b/mmv1/templates/terraform/custom_import/go/compute_network_endpoint.go.tmpl new file mode 100644 index 000000000000..600a834818eb --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/compute_network_endpoint.go.tmpl @@ -0,0 +1,19 @@ +config := meta.(*transport_tpg.Config) +// instance is optional, so use * instead of + when reading the import id +if err := tpgresource.ParseImportId([]string{ + "projects/(?P[^/]+)/zones/(?P[^/]+)/networkEndpointGroups/(?P[^/]+)/(?P[^/]*)/(?P[^/]+)/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)/(?P[^/]*)/(?P[^/]+)/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)/(?P[^/]*)/(?P[^/]+)/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]*)/(?P[^/]+)/(?P[^/]+)", +}, d, config); err != nil { + return nil, err +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}project{{"}}"}}/{{"{{"}}zone{{"}}"}}/{{"{{"}}network_endpoint_group{{"}}"}}/{{"{{"}}instance{{"}}"}}/{{"{{"}}ip_address{{"}}"}}/{{"{{"}}port{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/compute_region_network_endpoint.go.tmpl b/mmv1/templates/terraform/custom_import/go/compute_region_network_endpoint.go.tmpl new file mode 100644 index 000000000000..1b7094a9dc09 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/compute_region_network_endpoint.go.tmpl @@ -0,0 +1,19 @@ +config := meta.(*transport_tpg.Config) +// instance is optional, so use * instead of + when reading the import id +if err := tpgresource.ParseImportId([]string{ + "projects/(?P[^/]+)/regions/(?P[^/]+)/networkEndpointGroups/(?P[^/]+)/(?P[^/]*)/(?P[^/]*)/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)/(?P[^/]*)/(?P[^/]*)/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)/(?P[^/]*)/(?P[^/]*)/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]*)/(?P[^/]*)/(?P[^/]+)", +}, d, config); err != nil { + return nil, err +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}project{{"}}"}}/{{"{{"}}region{{"}}"}}/{{"{{"}}region_network_endpoint_group{{"}}"}}/{{"{{"}}ip_address{{"}}"}}/{{"{{"}}fqdn{{"}}"}}/{{"{{"}}port{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/data_catalog_entry.go.tmpl b/mmv1/templates/terraform/custom_import/go/data_catalog_entry.go.tmpl new file mode 100644 index 000000000000..f05ad3b9ac1a --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/data_catalog_entry.go.tmpl @@ -0,0 +1,21 @@ + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err + } + + name := d.Get("name").(string) + egRegex := regexp.MustCompile("(projects/.+/locations/.+/entryGroups/.+)/entries/(.+)") + + parts := egRegex.FindStringSubmatch(name) + if len(parts) != 3 { + return nil, fmt.Errorf("entry name does not fit the format %s", egRegex) + } + if err := d.Set("entry_group", parts[1]); err != nil { + return nil, fmt.Errorf("Error setting entry_group: %s", err) + } + if err := d.Set("entry_id", parts[2]); err != nil { + return nil, fmt.Errorf("Error setting entry_id: %s", err) + } + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/data_catalog_entry_group.go.tmpl b/mmv1/templates/terraform/custom_import/go/data_catalog_entry_group.go.tmpl new file mode 100644 index 000000000000..440c83396f2a --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/data_catalog_entry_group.go.tmpl @@ -0,0 +1,24 @@ + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err + } + + name := d.Get("name").(string) + egRegex := regexp.MustCompile("projects/(.+)/locations/(.+)/entryGroups/(.+)") + + parts := egRegex.FindStringSubmatch(name) + if len(parts) != 4 { + return nil, fmt.Errorf("entry group name does not fit the format %s", egRegex) + } + if err := d.Set("project", parts[1]); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) + } + if err := d.Set("region", parts[2]); err != nil { + return nil, fmt.Errorf("Error setting region: %s", err) + } + if err := d.Set("entry_group_id", parts[3]); err != nil { + return nil, fmt.Errorf("Error setting entry_group_id: %s", err) + } + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/data_catalog_tag.go.tmpl b/mmv1/templates/terraform/custom_import/go/data_catalog_tag.go.tmpl new file mode 100644 index 000000000000..cf427c3b5b55 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/data_catalog_tag.go.tmpl @@ -0,0 +1,19 @@ + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err + } + + name := d.Get("name").(string) + egRegex := regexp.MustCompile("(.+)/tags") + + parts := egRegex.FindStringSubmatch(name) + if len(parts) != 2 { + return nil, fmt.Errorf("entry name does not fit the format %s", egRegex) + } + + if err := d.Set("parent", parts[1]); err != nil { + return nil, fmt.Errorf("Error setting parent: %s", err) + } + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/data_catalog_tag_template.go.tmpl b/mmv1/templates/terraform/custom_import/go/data_catalog_tag_template.go.tmpl new file mode 100644 index 000000000000..753503ef6649 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/data_catalog_tag_template.go.tmpl @@ -0,0 +1,24 @@ + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err + } + + name := d.Get("name").(string) + egRegex := regexp.MustCompile("projects/(.+)/locations/(.+)/tagTemplates/(.+)") + + parts := egRegex.FindStringSubmatch(name) + if len(parts) != 4 { + return nil, fmt.Errorf("tag template name does not fit the format %s", egRegex) + } + if err := d.Set("project", parts[1]); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) + } + if err := d.Set("region", parts[2]); err != nil { + return nil, fmt.Errorf("Error setting region: %s", err) + } + if err := d.Set("tag_template_id", parts[3]); err != nil { + return nil, fmt.Errorf("Error setting tag_template_id: %s", err) + } + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/data_catalog_taxonomy.go.tmpl b/mmv1/templates/terraform/custom_import/go/data_catalog_taxonomy.go.tmpl new file mode 100644 index 000000000000..1450c6fa00c7 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/data_catalog_taxonomy.go.tmpl @@ -0,0 +1,17 @@ + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err + } + + name := d.Get("name").(string) + d.SetId(name) + + re := regexp.MustCompile("projects/(.+)/(?:locations|regions)/(.+)/taxonomies/(.+)") + if matches := re.FindStringSubmatch(name); matches != nil { + d.Set("project", matches[1]) + d.Set("region", matches[2]) + } + + return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/dialogflowcx_entity_type.go.tmpl b/mmv1/templates/terraform/custom_import/go/dialogflowcx_entity_type.go.tmpl new file mode 100644 index 000000000000..f9d25f996f68 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/dialogflowcx_entity_type.go.tmpl @@ -0,0 +1,18 @@ + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value and parent contains slashes + if err := tpgresource.ParseImportId([]string{ + "(?P.+)/entityTypes/(?P[^/]+)", + "(?P.+)/(?P[^/]+)", + }, d, config); err != nil { + return nil, err + } + + // Replace import id for the resource id + id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}parent{{"}}"}}/entityTypes/{{"{{"}}name{{"}}"}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/dialogflowcx_environment.go.tmpl b/mmv1/templates/terraform/custom_import/go/dialogflowcx_environment.go.tmpl new file mode 100644 index 000000000000..6b747275df25 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/dialogflowcx_environment.go.tmpl @@ -0,0 +1,18 @@ + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value and parent contains slashes + if err := tpgresource.ParseImportId([]string{ + "(?P.+)/environments/(?P[^/]+)", + "(?P.+)/(?P[^/]+)", + }, d, config); err != nil { + return nil, err + } + + // Replace import id for the resource id + id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}parent{{"}}"}}/environments/{{"{{"}}name{{"}}"}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/dialogflowcx_flow.go.tmpl b/mmv1/templates/terraform/custom_import/go/dialogflowcx_flow.go.tmpl new file mode 100644 index 000000000000..a69dac07a32a --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/dialogflowcx_flow.go.tmpl @@ -0,0 +1,23 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats can't import fields with forward slashes in their value and parent contains slashes +if err := tpgresource.ParseImportId([]string{ + "(?P.+)/flows/(?P[^/]+)", + "(?P.+)/(?P[^/]+)", +}, d, config); err != nil { + return nil, err +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}parent{{"}}"}}/flows/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +// Set is_default_start_flow if the resource is actually the Default Start Flow +if d.Get("name").(string) == "00000000-0000-0000-0000-000000000000" { + d.Set("is_default_start_flow", true) +} + +return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/dialogflowcx_intent.go.tmpl b/mmv1/templates/terraform/custom_import/go/dialogflowcx_intent.go.tmpl new file mode 100644 index 000000000000..50535d725ace --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/dialogflowcx_intent.go.tmpl @@ -0,0 +1,26 @@ + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value and parent contains slashes + if err := tpgresource.ParseImportId([]string{ + "(?P.+)/intents/(?P[^/]+)", + "(?P.+)/(?P[^/]+)", + }, d, config); err != nil { + return nil, err + } + + // Replace import id for the resource id + id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}parent{{"}}"}}/intents/{{"{{"}}name{{"}}"}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + // Set is_default_X if the resource is actually a Default Intent + if d.Get("name").(string) == "00000000-0000-0000-0000-000000000000" { + d.Set("is_default_welcome_intent", true) + } + if d.Get("name").(string) == "00000000-0000-0000-0000-000000000001" { + d.Set("is_default_negative_intent", true) + } + + return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/dialogflowcx_page.go.tmpl b/mmv1/templates/terraform/custom_import/go/dialogflowcx_page.go.tmpl new file mode 100644 index 000000000000..2ed9dd7a0409 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/dialogflowcx_page.go.tmpl @@ -0,0 +1,18 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats can't import fields with forward slashes in their value and parent contains slashes +if err := tpgresource.ParseImportId([]string{ + "(?P.+)/pages/(?P[^/]+)", + "(?P.+)/(?P[^/]+)", +}, d, config); err != nil { + return nil, err +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}parent{{"}}"}}/pages/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/dialogflowcx_version.go.tmpl b/mmv1/templates/terraform/custom_import/go/dialogflowcx_version.go.tmpl new file mode 100644 index 000000000000..b5f2069f0193 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/dialogflowcx_version.go.tmpl @@ -0,0 +1,18 @@ + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value and parent contains slashes + if err := tpgresource.ParseImportId([]string{ + "(?P.+)/versions/(?P[^/]+)", + "(?P.+)/(?P[^/]+)", + }, d, config); err != nil { + return nil, err + } + + // Replace import id for the resource id + id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}parent{{"}}"}}/versions/{{"{{"}}name{{"}}"}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/dialogflowcx_webhook.go.tmpl b/mmv1/templates/terraform/custom_import/go/dialogflowcx_webhook.go.tmpl new file mode 100644 index 000000000000..443ee07879da --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/dialogflowcx_webhook.go.tmpl @@ -0,0 +1,18 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats can't import fields with forward slashes in their value and parent contains slashes +if err := tpgresource.ParseImportId([]string{ + "(?P.+)/webhooks/(?P[^/]+)", + "(?P.+)/(?P[^/]+)", +}, d, config); err != nil { + return nil, err +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}parent{{"}}"}}/webhooks/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/dlp_import.go.tmpl b/mmv1/templates/terraform/custom_import/go/dlp_import.go.tmpl new file mode 100644 index 000000000000..73547fda9ced --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/dlp_import.go.tmpl @@ -0,0 +1,32 @@ + config := meta.(*transport_tpg.Config) + + // Custom import to handle parent possibilities + if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err + } + parts := strings.Split(d.Get("name").(string), "/") + if len(parts) == 6 { + if err := d.Set("name", parts[5]); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + } else if len(parts) == 4 { + if err := d.Set("name", parts[3]); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + } else { + return nil, fmt.Errorf("Unexpected import id: %s, expected form {{"{{"}}parent{{"}}"}}/{{camelize $.Name "lower"}}/{{"{{"}}name{{"}}"}}", d.Get("name").(string)) + } + // Remove "/{{camelize $.Name "lower"}}/{{"{{"}}name{{"}}"}}" from the id + parts = parts[:len(parts)-2] + if err := d.Set("parent", strings.Join(parts, "/")); err != nil { + return nil, fmt.Errorf("Error setting parent: %s", err) + } + + // Replace import id for the resource id + id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}parent{{"}}"}}/{{camelize $.Name "lower"}}s/{{"{{"}}name{{"}}"}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/document_ai_warehouse_document_schema.go.tmpl b/mmv1/templates/terraform/custom_import/go/document_ai_warehouse_document_schema.go.tmpl new file mode 100644 index 000000000000..f24c80976052 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/document_ai_warehouse_document_schema.go.tmpl @@ -0,0 +1,17 @@ +config := meta.(*transport_tpg.Config) + +if err := tpgresource.ParseImportId([]string{ + "projects/(?P[^/]+)/locations/(?P[^/]+)/documentSchemas/(?P[^/]+)"}, d, config); err != nil { + return nil, err +} + +originalName := d.Get("name").(string) +originalLocation := d.Get("location").(string) +originalProject := d.Get("project_number").(string) +name := fmt.Sprintf("projects/%s/locations/%s/documentSchemas/%s", originalProject, originalLocation, originalName) + +if err := d.Set("name", name); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) +} +d.SetId(name) +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/extract_taxonomy.go.tmpl b/mmv1/templates/terraform/custom_import/go/extract_taxonomy.go.tmpl new file mode 100644 index 000000000000..c99cf546f939 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/extract_taxonomy.go.tmpl @@ -0,0 +1,16 @@ + config := meta.(*transport_tpg.Config) + + if err := tpgresource.ParseImportId([]string{ + "(?Pprojects/[^/]+/locations/[^/]+/taxonomies/[^/]+)/policyTags/(?P.+)"}, d, config); err != nil { + return nil, err + } + + originalName := d.Get("name").(string) + originalTaxonomy := d.Get("taxonomy").(string) + name := fmt.Sprintf("%s/policyTags/%s", originalTaxonomy, originalName) + + if err := d.Set("name", name); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + d.SetId(name) + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/firestore_document.go.tmpl b/mmv1/templates/terraform/custom_import/go/firestore_document.go.tmpl new file mode 100644 index 000000000000..b484f459092e --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/firestore_document.go.tmpl @@ -0,0 +1,28 @@ + + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err + } + + re := regexp.MustCompile("^projects/([^/]+)/databases/([^/]+)/documents/(.+)/([^/]+)$") + match := re.FindStringSubmatch(d.Get("name").(string)) + if len(match) > 0{ + if err := d.Set("project", match[1]); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) + } + if err := d.Set("database", match[2]); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) + } + if err := d.Set("collection", match[3]); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) + } + if err := d.Set("document_id", match[4]); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) + } + } else { + return nil, fmt.Errorf("import did not match the regex ^projects/([^/]+)/databases/([^/]+)/documents/(.+)/([^/]+)$") + } + + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/firestore_field.go.tmpl b/mmv1/templates/terraform/custom_import/go/firestore_field.go.tmpl new file mode 100644 index 000000000000..b15d8131060d --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/firestore_field.go.tmpl @@ -0,0 +1,29 @@ + +config := meta.(*transport_tpg.Config) + +// current import_formats can't import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +// Re-populate split fields from the name. +re := regexp.MustCompile("^projects/([^/]+)/databases/([^/]+)/collectionGroups/([^/]+)/fields/(.+)$") +match := re.FindStringSubmatch(d.Get("name").(string)) +if len(match) > 0 { + if err := d.Set("project", match[1]); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) + } + if err := d.Set("database", match[2]); err != nil { + return nil, fmt.Errorf("Error setting database: %s", err) + } + if err := d.Set("collection", match[3]); err != nil { + return nil, fmt.Errorf("Error setting collection: %s", err) + } + if err := d.Set("field", match[4]); err != nil { + return nil, fmt.Errorf("Error setting field: %s", err) + } +} else { + return nil, fmt.Errorf("import did not match the regex ^projects/([^/]+)/databases/([^/]+)/collectionGroups/([^/]+)/fields/(.+)$") +} + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/healthcare_dicom_store.go.tmpl b/mmv1/templates/terraform/custom_import/go/healthcare_dicom_store.go.tmpl new file mode 100644 index 000000000000..a2c0fabce7ba --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/healthcare_dicom_store.go.tmpl @@ -0,0 +1,16 @@ + + config := meta.(*transport_tpg.Config) + + dicomStoreId, err := ParseHealthcareDicomStoreId(d.Id(), config) + if err != nil { + return nil, err + } + + if err := d.Set("dataset", dicomStoreId.DatasetId.DatasetId()); err != nil { + return nil, fmt.Errorf("Error setting dataset: %s", err) + } + if err := d.Set("name", dicomStoreId.Name); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/healthcare_fhir_store.go.tmpl b/mmv1/templates/terraform/custom_import/go/healthcare_fhir_store.go.tmpl new file mode 100644 index 000000000000..f3e7ed477669 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/healthcare_fhir_store.go.tmpl @@ -0,0 +1,16 @@ + + config := meta.(*transport_tpg.Config) + + fhirStoreId, err := ParseHealthcareFhirStoreId(d.Id(), config) + if err != nil { + return nil, err + } + + if err := d.Set("dataset", fhirStoreId.DatasetId.DatasetId()); err != nil { + return nil, fmt.Errorf("Error setting dataset: %s", err) + } + if err := d.Set("name", fhirStoreId.Name); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/healthcare_hl7_v2_store.go.tmpl b/mmv1/templates/terraform/custom_import/go/healthcare_hl7_v2_store.go.tmpl new file mode 100644 index 000000000000..86768ee920a6 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/healthcare_hl7_v2_store.go.tmpl @@ -0,0 +1,16 @@ + + config := meta.(*transport_tpg.Config) + + hl7v2StoreId, err := ParseHealthcareHl7V2StoreId(d.Id(), config) + if err != nil { + return nil, err + } + + if err := d.Set("dataset", hl7v2StoreId.DatasetId.DatasetId()); err != nil { + return nil, fmt.Errorf("Error setting dataset: %s", err) + } + if err := d.Set("name", hl7v2StoreId.Name); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/iap_brand.go.tmpl b/mmv1/templates/terraform/custom_import/go/iap_brand.go.tmpl new file mode 100644 index 000000000000..e31def29c84b --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/iap_brand.go.tmpl @@ -0,0 +1,37 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats can't import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +nameParts := strings.Split(d.Get("name").(string), "/") +if len(nameParts) != 4 && len(nameParts) != 2 { + return nil, fmt.Errorf( + "Saw %s when the name is expected to have either shape %s or %s", + d.Get("name"), + "projects/{{"{{"}}project{{"}}"}}/brands/{{"{{"}}name{{"}}"}}", + "{{"{{"}}project{{"}}"}}/{{"{{"}}name{{"}}"}}", + ) +} + +var project string +if len(nameParts) == 4 { + project = nameParts[1] +} +if len(nameParts) == 2 { + project = nameParts[0] // Different index + + // Set `name` (and `id`) as a 4-part format so Read func produces valid URL + brand := nameParts[1] + name := fmt.Sprintf("projects/%s/brands/%s", project, brand) + if err := d.Set("name", name); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + d.SetId(name) +} + +if err := d.Set("project", project); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) +} +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/iap_client.go.tmpl b/mmv1/templates/terraform/custom_import/go/iap_client.go.tmpl new file mode 100644 index 000000000000..e3be84e350b0 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/iap_client.go.tmpl @@ -0,0 +1,23 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats can't import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +nameParts := strings.Split(d.Get("brand").(string), "/") +if len(nameParts) != 6 { + return nil, fmt.Errorf( + "Saw %s when the name is expected to have shape %s", + d.Get("brand").(string), + "projects/{{"{{"}}project_number{{"}}"}}/brands/{{"{{"}}brand_id{{"}}"}}/identityAwareProxyClients/{{"{{"}}client_id{{"}}"}}", + ) +} + +if err := d.Set("brand", fmt.Sprintf("projects/%s/brands/%s", nameParts[1], nameParts[3])); err != nil { + return nil, fmt.Errorf("Error setting brand: %s", err) +} +if err := d.Set("client_id", nameParts[5]); err != nil { + return nil, fmt.Errorf("Error setting client_id: %s", err) +} +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/index_self_link_as_name_set_project.go.tmpl b/mmv1/templates/terraform/custom_import/go/index_self_link_as_name_set_project.go.tmpl new file mode 100644 index 000000000000..3fa636cbf14a --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/index_self_link_as_name_set_project.go.tmpl @@ -0,0 +1,27 @@ + + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err + } + + stringParts := strings.Split(d.Get("name").(string), "/") + if len(stringParts) != 8 { + return nil, fmt.Errorf( + "Saw %s when the name is expected to have shape %s", + d.Get("name"), + "projects/{{"{{"}}project{{"}}"}}/databases/{{"{{"}}database{{"}}"}}/collectionGroups/{{"{{"}}collection{{"}}"}}/indexes/{{"{{"}}server_generated_id{{"}}"}}", + ) + } + + if err := d.Set("project", stringParts[1]); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) + } + if err := d.Set("database", stringParts[3]); err != nil { + return nil, fmt.Errorf("Error setting database: %s", err) + } + if err := d.Set("collection", stringParts[5]); err != nil { + return nil, fmt.Errorf("Error setting collection: %s", err) + } + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/kms_crypto_key.go.tmpl b/mmv1/templates/terraform/custom_import/go/kms_crypto_key.go.tmpl new file mode 100644 index 000000000000..db6e302250d3 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/kms_crypto_key.go.tmpl @@ -0,0 +1,26 @@ + + config := meta.(*transport_tpg.Config) + + cryptoKeyId, err := ParseKmsCryptoKeyId(d.Id(), config) + if err != nil { + return nil, err + } + + if err := d.Set("key_ring", cryptoKeyId.KeyRingId.KeyRingId()); err != nil { + return nil, fmt.Errorf("Error setting key_ring: %s", err) + } + if err := d.Set("name", cryptoKeyId.Name); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + + if err := d.Set("skip_initial_version_creation", false); err != nil { + return nil, fmt.Errorf("Error setting skip_initial_version_creation: %s", err) + } + + id, err := tpgresource.ReplaceVars(d, config, "{{$.GetIdFormat}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/kms_crypto_key_version.go.tmpl b/mmv1/templates/terraform/custom_import/go/kms_crypto_key_version.go.tmpl new file mode 100644 index 000000000000..f29594e126b2 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/kms_crypto_key_version.go.tmpl @@ -0,0 +1,20 @@ + + config := meta.(*transport_tpg.Config) + + cryptoKeyVersionId, err := parseKmsCryptoKeyVersionId(d.Id(), config) + if err != nil { + return nil, err + } + if err := d.Set("crypto_key", cryptoKeyVersionId.CryptoKeyId.CryptoKeyId()); err != nil { + return nil, fmt.Errorf("Error setting key_ring: %s", err) + } + if err := d.Set("name", cryptoKeyVersionId.Name); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + id, err := tpgresource.ReplaceVars(d, config, "{{$.GetIdFormat}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/kms_key_ring_import_job.go.tmpl b/mmv1/templates/terraform/custom_import/go/kms_key_ring_import_job.go.tmpl new file mode 100644 index 000000000000..875780ed2ca8 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/kms_key_ring_import_job.go.tmpl @@ -0,0 +1,24 @@ + + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err + } + + stringParts := strings.Split(d.Get("name").(string), "/") + if len(stringParts) != 8 { + return nil, fmt.Errorf( + "Saw %s when the name is expected to have shape %s", + d.Get("name"), + "projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/keyRings/{{"{{"}}keyRing{{"}}"}}/importJobs/{{"{{"}}importJobId{{"}}"}}", + ) + } + + if err := d.Set("key_ring", stringParts[3]); err != nil { + return nil, fmt.Errorf("Error setting key_ring: %s", err) + } + if err := d.Set("import_job_id", stringParts[5]); err != nil { + return nil, fmt.Errorf("Error setting import_job_id: %s", err) + } + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/monitoring_monitored_project.go.tmpl b/mmv1/templates/terraform/custom_import/go/monitoring_monitored_project.go.tmpl new file mode 100644 index 000000000000..816910ecd0ab --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/monitoring_monitored_project.go.tmpl @@ -0,0 +1,35 @@ +{{/* + The license inside this block applies to this file + Copyright 2024 Google Inc. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ -}} +name := d.Get("name").(string) +name = tpgresource.GetResourceNameFromSelfLink(name) +d.Set("name", name) +metricsScope := d.Get("metrics_scope").(string) +metricsScope = tpgresource.GetResourceNameFromSelfLink(metricsScope) +d.Set("metrics_scope", metricsScope) +config := meta.(*transport_tpg.Config) +if err := tpgresource.ParseImportId([]string{ + "locations/global/metricsScopes/(?P[^/]+)/projects/(?P[^/]+)", + "v1/locations/global/metricsScopes/(?P[^/]+)/projects/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)", +}, d, config); err != nil { + return nil, err +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "locations/global/metricsScopes/{{"{{"}}metrics_scope{{"}}"}}/projects/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/scc_mute_config.go.tmpl b/mmv1/templates/terraform/custom_import/go/scc_mute_config.go.tmpl new file mode 100644 index 000000000000..9f00fba3e2cb --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/scc_mute_config.go.tmpl @@ -0,0 +1,34 @@ +config := meta.(*transport_tpg.Config) + +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +// current import_formats can't import fields with forward slashes in their value +name := d.Get("name").(string) + +matched, err := regexp.MatchString("(organizations|folders|projects)/.+/muteConfigs/.+", name) +if err != nil { + return nil, fmt.Errorf("error validating import name: %s", err) +} + +if !matched { + return nil, fmt.Errorf("error validating import name: %s does not fit naming for muteConfigs. Expected %s", + name, "organizations/{organization}/muteConfigs/{configId}, folders/{folder}/muteConfigs/{configId} or projects/{project}/muteConfigs/{configId}") +} + +if err := d.Set("name", name); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) +} + +// mute_config_id and parent are not returned by the API and therefore need to be set manually +stringParts := strings.Split(d.Get("name").(string), "/") +if err := d.Set("mute_config_id", stringParts[3]); err != nil { + return nil, fmt.Errorf("Error setting mute_config_id: %s", err) +} + +if err := d.Set("parent", fmt.Sprintf("%s/%s", stringParts[0], stringParts[1])); err != nil { + return nil, fmt.Errorf("Error setting mute_config_id: %s", err) +} + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/scc_source_self_link_as_name_set_organization.go.tmpl b/mmv1/templates/terraform/custom_import/go/scc_source_self_link_as_name_set_organization.go.tmpl new file mode 100644 index 000000000000..bd54b46f70d8 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/scc_source_self_link_as_name_set_organization.go.tmpl @@ -0,0 +1,20 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats can't import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +stringParts := strings.Split(d.Get("name").(string), "/") +if len(stringParts) != 4 { + return nil, fmt.Errorf( + "Saw %s when the name is expected to have shape %s", + d.Get("name"), + "organizations/{{"{{"}}organization{{"}}"}}/sources/{{"{{"}}source{{"}}"}}", + ) +} + +if err := d.Set("organization", stringParts[1]); err != nil { + return nil, fmt.Errorf("Error setting organization: %s", err) +} +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/secret_version.go.tmpl b/mmv1/templates/terraform/custom_import/go/secret_version.go.tmpl new file mode 100644 index 000000000000..0c5ab7aee59f --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/secret_version.go.tmpl @@ -0,0 +1,31 @@ + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err + } + + name := d.Get("name").(string) + secretRegex := regexp.MustCompile("(projects/.+/secrets/.+)/versions/.+$") + versionRegex := regexp.MustCompile("projects/(.+)/secrets/(.+)/versions/(.+)$") + + parts := secretRegex.FindStringSubmatch(name) + if len(parts) != 2 { + return nil, fmt.Errorf("Version name does not fit the format `projects/{{"{{"}}project{{"}}"}}/secrets/{{"{{"}}secret{{"}}"}}/versions/{{"{{"}}version{{"}}"}}`") + } + if err := d.Set("secret", parts[1]); err != nil { + return nil, fmt.Errorf("Error setting secret: %s", err) + } + + parts = versionRegex.FindStringSubmatch(name) + + if err := d.Set("version", parts[3]); err != nil { + return nil, fmt.Errorf("Error setting version: %s", err) + } + + // Explicitly set virtual fields to default values on import + if err := d.Set("deletion_policy", "DELETE"); err != nil { + return nil, fmt.Errorf("Error setting version: %s", err) + } + + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/self_link_as_name.tmpl b/mmv1/templates/terraform/custom_import/go/self_link_as_name.tmpl new file mode 100644 index 000000000000..d83bf4faa636 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/self_link_as_name.tmpl @@ -0,0 +1,9 @@ + + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + if err := tpgresource.ParseImportId([]string{"(?P[^ ]+) (?P[^ ]+)", "(?P[^ ]+)"}, d, config); err != nil { + return nil, err + } + + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/self_link_as_name_set_project.go.tmpl b/mmv1/templates/terraform/custom_import/go/self_link_as_name_set_project.go.tmpl new file mode 100644 index 000000000000..c2de0c59fbfb --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/self_link_as_name_set_project.go.tmpl @@ -0,0 +1,20 @@ + + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err + } + + stringParts := strings.Split(d.Get("name").(string), "/") + if len(stringParts) < 2 { + return nil, fmt.Errorf( + "Could not split project from name: %s", + d.Get("name"), + ) + } + + if err := d.Set("project", stringParts[1]); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) + } + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/service_directory_endpoint.go.tmpl b/mmv1/templates/terraform/custom_import/go/service_directory_endpoint.go.tmpl new file mode 100644 index 000000000000..4d070d0c250f --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/service_directory_endpoint.go.tmpl @@ -0,0 +1,55 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats cannot import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +nameParts := strings.Split(d.Get("name").(string), "/") +if len(nameParts) == 10 { + // `projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/namespaces/{{"{{"}}namespace_id{{"}}"}}/services/{{"{{"}}service_id{{"}}"}}/endpoints/{{"{{"}}endpoint_id{{"}}"}}` + if err := d.Set("service", fmt.Sprintf("projects/%s/locations/%s/namespaces/%s/services/%s", nameParts[1], nameParts[3], nameParts[5], nameParts[7])); err != nil { + return nil, fmt.Errorf("Error setting service: %s", err) + } + if err := d.Set("endpoint_id", nameParts[9]); err != nil { + return nil, fmt.Errorf("Error setting endpoint_id: %s", err) + } +} else if len(nameParts) == 5 { + // `{{"{{"}}project{{"}}"}}/{{"{{"}}location{{"}}"}}/{{"{{"}}namespace_id{{"}}"}}/{{"{{"}}service_id{{"}}"}}/{{"{{"}}endpoint_id{{"}}"}}` + if err := d.Set("service", fmt.Sprintf("projects/%s/locations/%s/namespaces/%s/services/%s", nameParts[0], nameParts[1], nameParts[2], nameParts[3])); err != nil { + return nil, fmt.Errorf("Error setting service: %s", err) + } + if err := d.Set("endpoint_id", nameParts[4]); err != nil { + return nil, fmt.Errorf("Error setting endpoint_id: %s", err) + } + id := fmt.Sprintf("projects/%s/locations/%s/namespaces/%s/services/%s/endpoints/%s", nameParts[0], nameParts[1], nameParts[2], nameParts[3], nameParts[4]) + if err := d.Set("name", id); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + d.SetId(id) +} else if len(nameParts) == 4 { + // `{{"{{"}}location{{"}}"}}/{{"{{"}}namespace_id{{"}}"}}/{{"{{"}}service_id{{"}}"}}/{{"{{"}}endpoint_id{{"}}"}}` + project, err := tpgresource.GetProject(d, config) + if err != nil { + return nil, err + } + if err := d.Set("service", fmt.Sprintf("projects/%s/locations/%s/namespaces/%s/services/%s", project, nameParts[0], nameParts[1], nameParts[2])); err != nil { + return nil, fmt.Errorf("Error setting service: %s", err) + } + if err := d.Set("endpoint_id", nameParts[3]); err != nil { + return nil, fmt.Errorf("Error setting endpoint_id: %s", err) + } + id := fmt.Sprintf("projects/%s/locations/%s/namespaces/%s/services/%s/endpoints/%s", project, nameParts[0], nameParts[1], nameParts[2], nameParts[3]) + if err := d.Set("name", id); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + d.SetId(id) +} else { + return nil, fmt.Errorf( + "Saw %s when the name is expected to have shape %s, %s or %s", + d.Get("name"), + "projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/namespaces/{{"{{"}}namespace_id{{"}}"}}/services/{{"{{"}}service_id{{"}}"}}/endpoints/{{"{{"}}endpoint_id{{"}}"}}", + "{{"{{"}}project{{"}}"}}/{{"{{"}}location{{"}}"}}/{{"{{"}}namespace_id{{"}}"}}/{{"{{"}}service_id{{"}}"}}/{{"{{"}}endpoint_id{{"}}"}}", + "{{"{{"}}location{{"}}"}}/{{"{{"}}namespace_id{{"}}"}}/{{"{{"}}service_id{{"}}"}}/{{"{{"}}endpoint_id{{"}}"}}") +} +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/service_directory_namespace.go.tmpl b/mmv1/templates/terraform/custom_import/go/service_directory_namespace.go.tmpl new file mode 100644 index 000000000000..a76f6793f51a --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/service_directory_namespace.go.tmpl @@ -0,0 +1,64 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats cannot import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +nameParts := strings.Split(d.Get("name").(string), "/") +if len(nameParts) == 6 { + // `projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/namespaces/{{"{{"}}namespace_id{{"}}"}}` + if err := d.Set("project", nameParts[1]); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) + } + if err := d.Set("location", nameParts[3]); err != nil { + return nil, fmt.Errorf("Error setting location: %s", err) + } + if err := d.Set("namespace_id", nameParts[5]); err != nil { + return nil, fmt.Errorf("Error setting namespace_id: %s", err) + } +} else if len(nameParts) == 3 { + // `{{"{{"}}project{{"}}"}}/{{"{{"}}location{{"}}"}}/{{"{{"}}namespace_id{{"}}"}}` + if err := d.Set("project", nameParts[0]); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) + } + if err := d.Set("location", nameParts[1]); err != nil { + return nil, fmt.Errorf("Error setting location: %s", err) + } + if err := d.Set("namespace_id", nameParts[2]); err != nil { + return nil, fmt.Errorf("Error setting namespace_id: %s", err) + } + id := fmt.Sprintf("projects/%s/locations/%s/namespaces/%s", nameParts[0], nameParts[1], nameParts[2]) + if err := d.Set("name", id); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + d.SetId(id) +} else if len(nameParts) == 2 { + // `{{"{{"}}location{{"}}"}}/{{"{{"}}namespace_id{{"}}"}}` + project, err := tpgresource.GetProject(d, config) + if err != nil { + return nil, err + } + if err := d.Set("project", project); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) + } + if err := d.Set("location", nameParts[0]); err != nil { + return nil, fmt.Errorf("Error setting location: %s", err) + } + if err := d.Set("namespace_id", nameParts[1]); err != nil { + return nil, fmt.Errorf("Error setting namespace_id: %s", err) + } + id := fmt.Sprintf("projects/%s/locations/%s/namespaces/%s", project, nameParts[0], nameParts[1]) + if err := d.Set("name", id); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + d.SetId(id) +} else { + return nil, fmt.Errorf( + "Saw %s when the name is expected to have shape %s, %s or %s", + d.Get("name"), + "projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/namespaces/{{"{{"}}namespace_id{{"}}"}}", + "{{"{{"}}project{{"}}"}}/{{"{{"}}location{{"}}"}}/{{"{{"}}namespace_id{{"}}"}}", + "{{"{{"}}location{{"}}"}}/{{"{{"}}namespace_id{{"}}"}}") +} +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/service_directory_service.go.tmpl b/mmv1/templates/terraform/custom_import/go/service_directory_service.go.tmpl new file mode 100644 index 000000000000..54e6d0f587d0 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/service_directory_service.go.tmpl @@ -0,0 +1,56 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats cannot import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +nameParts := strings.Split(d.Get("name").(string), "/") +if len(nameParts) == 8 { + // `projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/namespaces/{{"{{"}}namespace_id{{"}}"}}/services/{{"{{"}}service_id{{"}}"}}` + if err := d.Set("namespace", fmt.Sprintf("projects/%s/locations/%s/namespaces/%s", nameParts[1], nameParts[3], nameParts[5])); err != nil { + return nil, fmt.Errorf("Error setting namespace: %s", err) + } + if err := d.Set("service_id", nameParts[7]); err != nil { + return nil, fmt.Errorf("Error setting service_id: %s", err) + } +} else if len(nameParts) == 4 { + // `{{"{{"}}project{{"}}"}}/{{"{{"}}location{{"}}"}}/{{"{{"}}namespace_id{{"}}"}}/{{"{{"}}service_id{{"}}"}}` + if err := d.Set("namespace", fmt.Sprintf("projects/%s/locations/%s/namespaces/%s", nameParts[0], nameParts[1], nameParts[2])); err != nil { + return nil, fmt.Errorf("Error setting namespace: %s", err) + } + if err := d.Set("service_id", nameParts[3]); err != nil { + return nil, fmt.Errorf("Error setting service_id: %s", err) + } + id := fmt.Sprintf("projects/%s/locations/%s/namespaces/%s/services/%s", nameParts[0], nameParts[1], nameParts[2], nameParts[3]) + if err := d.Set("name", id); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + d.SetId(id) +} else if len(nameParts) == 3 { + // `{{"{{"}}location{{"}}"}}/{{"{{"}}namespace_id{{"}}"}}/{{"{{"}}service_id{{"}}"}}` + project, err := tpgresource.GetProject(d, config) + if err != nil { + return nil, err + } + if err := d.Set("namespace", fmt.Sprintf("projects/%s/locations/%s/namespaces/%s", project, nameParts[0], nameParts[1])); err != nil { + return nil, fmt.Errorf("Error setting namespace: %s", err) + } + if err := d.Set("service_id", nameParts[2]); err != nil { + return nil, fmt.Errorf("Error setting service_id: %s", err) + } + id := fmt.Sprintf("projects/%s/locations/%s/namespaces/%s/services/%s", project, nameParts[0], nameParts[1], nameParts[2]) + if err := d.Set("name", id); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) + } + d.SetId(id) +} else { + return nil, fmt.Errorf( + "Saw %s when the name is expected to have shape %s, %s or %s", + d.Get("name"), + "projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/namespaces/{{"{{"}}namespace_id{{"}}"}}/services/{{"{{"}}service_id{{"}}"}}", + "{{"{{"}}project{{"}}"}}/{{"{{"}}location{{"}}"}}/{{"{{"}}namespace_id{{"}}"}}/{{"{{"}}service_id{{"}}"}}", + "{{"{{"}}location{{"}}"}}/{{"{{"}}namespace_id{{"}}"}}/{{"{{"}}service_id{{"}}"}}") +} +return []*schema.ResourceData{d}, nil + diff --git a/mmv1/templates/terraform/custom_import/go/set_access_policy_parent_from_access_policy.go.tmpl b/mmv1/templates/terraform/custom_import/go/set_access_policy_parent_from_access_policy.go.tmpl new file mode 100644 index 000000000000..d0c36880504b --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/set_access_policy_parent_from_access_policy.go.tmpl @@ -0,0 +1,24 @@ +{{/* + The license inside this block applies to this file + Copyright 2024 Google Inc. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ -}} + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + parts, err := tpgresource.GetImportIdQualifiers([]string{"accessPolicies/(?P[^/]+)/(.+)"}, d, config, d.Id()) + if err != nil { + return nil, err + } + + if err := d.Set("parent", fmt.Sprintf("accessPolicies/%s", parts["accessPolicy"])); err != nil { + return nil, fmt.Errorf("Error setting parent: %s", err) + } + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/set_access_policy_parent_from_self_link.go.tmpl b/mmv1/templates/terraform/custom_import/go/set_access_policy_parent_from_self_link.go.tmpl new file mode 100644 index 000000000000..8d0d10bbd47a --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/set_access_policy_parent_from_self_link.go.tmpl @@ -0,0 +1,26 @@ +{{/* + The license inside this block applies to this file + Copyright 2024 Google Inc. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ -}} + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err + } + stringParts := strings.Split(d.Get("name").(string), "/") + if len(stringParts) < 2 { + return nil, fmt.Errorf("Error parsing parent name. Should be in form accessPolicies/{{"{{"}}policy_id{{"}}"}}/{{camelize (plural $.Name) "lower"}}/{{"{{"}}short_name{{"}}"}}") + } + if err := d.Set("parent", fmt.Sprintf("%s/%s", stringParts[0], stringParts[1])); err != nil { + return nil, fmt.Errorf("Error setting parent, %s", err) + } + return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/set_id_name_with_slashes.go.tmpl b/mmv1/templates/terraform/custom_import/go/set_id_name_with_slashes.go.tmpl new file mode 100644 index 000000000000..244fc59d8bd8 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/set_id_name_with_slashes.go.tmpl @@ -0,0 +1,14 @@ +config := meta.(*transport_tpg.Config) + +// current import_formats can't import fields with forward slashes in their value +if err := tpgresource.ParseImportId([]string{"(?P.+)"}, d, config); err != nil { + return nil, err +} + +name := d.Get("name").(string) + +if err := d.Set("name", name); err != nil { + return nil, fmt.Errorf("Error setting name: %s", err) +} +d.SetId(name) +return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/tags_tag_binding.go.tmpl b/mmv1/templates/terraform/custom_import/go/tags_tag_binding.go.tmpl new file mode 100644 index 000000000000..df9ea77c0aff --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/tags_tag_binding.go.tmpl @@ -0,0 +1,22 @@ + config := meta.(*transport_tpg.Config) + + // current import_formats can't import fields with forward slashes in their value + if err := tpgresource.ParseImportId([]string{ + "tagBindings/(?P.+)", + "(?P.+)", + }, d, config); err != nil { + return nil, err + } + + stringParts := strings.Split(d.Get("name").(string), "/") + if len(stringParts) < 3 { + return nil, fmt.Errorf("Error parsing parent name. Should be in form {{"{{"}}parent{{"}}"}}/tagValues/{{"{{"}}tag_value{{"}}"}}") + } + if err := d.Set("parent", stringParts[0]); err != nil { + return nil, fmt.Errorf("Error setting parent, %s", err) + } + + name := d.Get("name").(string) + d.SetId(name) + + return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/vertex_ai_featurestore_entitytype.go.tmpl b/mmv1/templates/terraform/custom_import/go/vertex_ai_featurestore_entitytype.go.tmpl new file mode 100644 index 000000000000..01b58abc78ba --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/vertex_ai_featurestore_entitytype.go.tmpl @@ -0,0 +1,22 @@ +config := meta.(*transport_tpg.Config) +if err := tpgresource.ParseImportId([]string{ + "(?P.+)/entityTypes/(?P[^/]+)", +}, d, config); err != nil { + return nil, err +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}featurestore{{"}}"}}/entityTypes/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +featurestore := d.Get("featurestore").(string) + +re := regexp.MustCompile("^projects/(.+)/locations/(.+)/featurestores/(.+)$") +if parts := re.FindStringSubmatch(featurestore); parts != nil { + d.Set("region", parts[2]) +} + +return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/custom_import/go/vertex_ai_featurestore_entitytype_feature.go.tmpl b/mmv1/templates/terraform/custom_import/go/vertex_ai_featurestore_entitytype_feature.go.tmpl new file mode 100644 index 000000000000..e4b84a1b5aaf --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/vertex_ai_featurestore_entitytype_feature.go.tmpl @@ -0,0 +1,22 @@ +config := meta.(*transport_tpg.Config) +if err := tpgresource.ParseImportId([]string{ + "(?P.+)/features/(?P[^/]+)", +}, d, config); err != nil { + return nil, err +} + +// Replace import id for the resource id +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}entitytype{{"}}"}}/features/{{"{{"}}name{{"}}"}}") +if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) +} +d.SetId(id) + +entitytype := d.Get("entitytype").(string) + +re := regexp.MustCompile("^projects/(.+)/locations/(.+)/featurestores/(.+)/entityTypes/(.+)$") +if parts := re.FindStringSubmatch(entitytype); parts != nil { + d.Set("region", parts[2]) +} + +return []*schema.ResourceData{d}, nil diff --git a/mmv1/templates/terraform/custom_import/go/vertex_ai_tensorboard_import.go.tmpl b/mmv1/templates/terraform/custom_import/go/vertex_ai_tensorboard_import.go.tmpl new file mode 100644 index 000000000000..be6cd588a588 --- /dev/null +++ b/mmv1/templates/terraform/custom_import/go/vertex_ai_tensorboard_import.go.tmpl @@ -0,0 +1,22 @@ + config := meta.(*transport_tpg.Config) + if err := tpgresource.ParseImportId([]string{ + "projects/(?P[^/]+)/locations/(?P[^/]+)/tensorboards/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)", + "(?P[^/]+)", + }, d, config); err != nil { + return nil, err + } + + // Replace import id for the resource id + id, err := tpgresource.ReplaceVars(d, config, "projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}region{{"}}"}}/tensorboards/{{"{{"}}name{{"}}"}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + if err := d.Set("name", id); err != nil { + return nil, fmt.Errorf("Error setting name for import: %s", err) + } + + return []*schema.ResourceData{d}, nil \ No newline at end of file diff --git a/mmv1/templates/terraform/resource.html.markdown.tmpl b/mmv1/templates/terraform/resource.html.markdown.tmpl index 9c9488900e45..14d62b73323b 100644 --- a/mmv1/templates/terraform/resource.html.markdown.tmpl +++ b/mmv1/templates/terraform/resource.html.markdown.tmpl @@ -188,4 +188,4 @@ $ terraform import {{$.TerraformName}}.default {{$idFormat}} ## User Project Overrides This resource supports [User Project Overrides](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#user_project_override). -{{- end }} +{{- end }} \ No newline at end of file