-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Normalize name field #8360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Normalize name field #8360
Changes from all commits
2c5b51e
fff5a61
48bebad
98147da
c928355
1c2aedb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need a custom import here? Is it only to support importing with the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's there to shorten the |
||
| 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. | ||
|
|
||
| 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 |
|---|---|---|
| @@ -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 |
|---|---|---|
| @@ -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 | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need an upgrade state function to make sure any of these resources that were created with the
v1/prefix are moved to the correct id format