From 30e7b928e2f326b57c597fc69dbaec39b99f1e90 Mon Sep 17 00:00:00 2001 From: Thomas Rodgers Date: Sat, 15 Jul 2023 00:55:05 +0000 Subject: [PATCH] Normalize name field (#8360) * Normalize name field * Fix import * Support previous import id * Also shorten name in encoder * Add long form test * Add upgrade state function --- .../products/monitoring/MonitoredProject.yaml | 17 +++++++-- .../monitoring_monitored_project.go.erb | 37 +++++++++++++++++++ .../monitoring_monitored_project.go.erb | 1 + ...itoring_monitored_project_long_form.tf.erb | 10 +++++ .../monitoring_monitored_project.go.erb | 3 ++ .../monitoring_monitored_project.go.erb | 35 ++++++++++++++++++ 6 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 mmv1/templates/terraform/custom_import/monitoring_monitored_project.go.erb create mode 100644 mmv1/templates/terraform/examples/monitoring_monitored_project_long_form.tf.erb create mode 100644 mmv1/templates/terraform/state_migrations/monitoring_monitored_project.go.erb diff --git a/mmv1/products/monitoring/MonitoredProject.yaml b/mmv1/products/monitoring/MonitoredProject.yaml index 0eb8a7b128b7..919e7567b19f 100644 --- a/mmv1/products/monitoring/MonitoredProject.yaml +++ b/mmv1/products/monitoring/MonitoredProject.yaml @@ -17,9 +17,9 @@ base_url: v1/locations/global/metricsScopes create_url: v1/locations/global/metricsScopes/{{metrics_scope}}/projects delete_url: v1/locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}} self_link: v1/locations/global/metricsScopes/{{metrics_scope}} -id_format: v1/locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}} -import_format: - - v1/locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}} +id_format: locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}} +schema_version: 1 +state_upgraders: true references: !ruby/object:Api::Resource::ReferenceLinks guides: 'Official Documentation': 'https://cloud.google.com/monitoring/settings/manage-api' @@ -31,6 +31,7 @@ description: "A [project being monitored](https://cloud.google.com/monitoring/se immutable: true error_retry_predicates: ['transport_tpg.IsMonitoringPermissionError'] custom_code: !ruby/object:Provider::Terraform::CustomCode + custom_import: templates/terraform/custom_import/monitoring_monitored_project.go.erb encoder: templates/terraform/encoders/monitoring_monitored_project.go.erb decoder: templates/terraform/decoders/monitoring_monitored_project.go.erb pre_read: templates/terraform/pre_read/monitoring_monitored_project.go.erb @@ -44,6 +45,15 @@ examples: test_env_vars: org_id: :ORG_ID project_id: :PROJECT_NAME + - !ruby/object:Provider::Terraform::Examples + name: 'monitoring_monitored_project_long_form' + primary_resource_id: 'primary' + vars: + monitored_project: 'm-id' + test_env_vars: + org_id: :ORG_ID + project_id: :PROJECT_NAME + skip_docs: true parameters: - !ruby/object:Api::Type::String name: metricsScope @@ -57,6 +67,7 @@ properties: name: name description: 'Immutable. The resource name of the `MonitoredProject`. On input, the resource name includes the scoping project ID and monitored project ID. On output, it contains the equivalent project numbers. Example: `locations/global/metricsScopes/{SCOPING_PROJECT_ID_OR_NUMBER}/projects/{MONITORED_PROJECT_ID_OR_NUMBER}`' required: true + diff_suppress_func: 'tpgresource.CompareResourceNames' - !ruby/object:Api::Type::String name: createTime description: Output only. The time when this `MonitoredProject` was created. diff --git a/mmv1/templates/terraform/custom_import/monitoring_monitored_project.go.erb b/mmv1/templates/terraform/custom_import/monitoring_monitored_project.go.erb new file mode 100644 index 000000000000..8510dd4817ef --- /dev/null +++ b/mmv1/templates/terraform/custom_import/monitoring_monitored_project.go.erb @@ -0,0 +1,37 @@ +<%- # the license inside this block applies to this file + # Copyright 2023 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/encoders/monitoring_monitored_project.go.erb b/mmv1/templates/terraform/encoders/monitoring_monitored_project.go.erb index 48215bb25fd1..94012980b429 100644 --- a/mmv1/templates/terraform/encoders/monitoring_monitored_project.go.erb +++ b/mmv1/templates/terraform/encoders/monitoring_monitored_project.go.erb @@ -14,6 +14,7 @@ -%> 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) diff --git a/mmv1/templates/terraform/examples/monitoring_monitored_project_long_form.tf.erb b/mmv1/templates/terraform/examples/monitoring_monitored_project_long_form.tf.erb new file mode 100644 index 000000000000..877056db134a --- /dev/null +++ b/mmv1/templates/terraform/examples/monitoring_monitored_project_long_form.tf.erb @@ -0,0 +1,10 @@ +resource "google_monitoring_monitored_project" "<%= ctx[:primary_resource_id] %>" { + metrics_scope = "<%= ctx[:test_env_vars]['project_id'] %>" + name = "locations/global/metricsScopes/<%= ctx[:test_env_vars]['project_id'] %>/projects/${google_project.basic.project_id}" +} + +resource "google_project" "basic" { + project_id = "<%= ctx[:vars]['monitored_project'] %>" + name = "<%= ctx[:vars]['monitored_project'] %>-display" + org_id = "<%= ctx[:test_env_vars]['org_id'] %>" +} diff --git a/mmv1/templates/terraform/pre_read/monitoring_monitored_project.go.erb b/mmv1/templates/terraform/pre_read/monitoring_monitored_project.go.erb index 820d19ceeeea..0c73fb8b4eea 100644 --- a/mmv1/templates/terraform/pre_read/monitoring_monitored_project.go.erb +++ b/mmv1/templates/terraform/pre_read/monitoring_monitored_project.go.erb @@ -12,6 +12,9 @@ # 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) diff --git a/mmv1/templates/terraform/state_migrations/monitoring_monitored_project.go.erb b/mmv1/templates/terraform/state_migrations/monitoring_monitored_project.go.erb new file mode 100644 index 000000000000..9dc51b7fd2f7 --- /dev/null +++ b/mmv1/templates/terraform/state_migrations/monitoring_monitored_project.go.erb @@ -0,0 +1,35 @@ +func resourceMonitoringMonitoredProjectResourceV0() *schema.Resource { + return &schema.Resource{ + Schema: map[string]*schema.Schema{ + "metrics_scope": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + DiffSuppressFunc: tpgresource.CompareResourceNames, + Description: `Required. The resource name of the existing Metrics Scope that will monitor this project. Example: locations/global/metricsScopes/{SCOPING_PROJECT_ID_OR_NUMBER}`, + }, + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + DiffSuppressFunc: tpgresource.CompareResourceNames, + Description: `Immutable. The resource name of the 'MonitoredProject'. On input, the resource name includes the scoping project ID and monitored project ID. On output, it contains the equivalent project numbers. Example: 'locations/global/metricsScopes/{SCOPING_PROJECT_ID_OR_NUMBER}/projects/{MONITORED_PROJECT_ID_OR_NUMBER}'`, + }, + "create_time": { + Type: schema.TypeString, + Computed: true, + Description: `Output only. The time when this 'MonitoredProject' was created.`, + }, + }, + UseJSONNumber: true, + } +} + +func ResourceMonitoringMonitoredProjectUpgradeV0(_ context.Context, rawState map[string]any, meta any) (map[string]any, error) { + log.Printf("[DEBUG] Attributes before migration: %#v", rawState) + + rawState["id"] = strings.TrimPrefix(rawState["id"].(string), "v1/") + + log.Printf("[DEBUG] Attributes after migration: %#v", rawState) + return rawState, nil +}