Skip to content

Commit

Permalink
Normalize name field (GoogleCloudPlatform#8360)
Browse files Browse the repository at this point in the history
* Normalize name field

* Fix import

* Support previous import id

* Also shorten name in encoder

* Add long form test

* Add upgrade state function
  • Loading branch information
trodge authored and DanielRieske committed Aug 2, 2023
1 parent 3fc3700 commit 30e7b92
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 3 deletions.
17 changes: 14 additions & 3 deletions mmv1/products/monitoring/MonitoredProject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<metrics_scope>[^/]+)/projects/(?P<name>[^/]+)",
"v1/locations/global/metricsScopes/(?P<metrics_scope>[^/]+)/projects/(?P<name>[^/]+)",
"(?P<metrics_scope>[^/]+)/(?P<name>[^/]+)",
}, 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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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'] %>"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 30e7b92

Please sign in to comment.