forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Normalize name field (GoogleCloudPlatform#8360)
* 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
1 parent
3fc3700
commit 30e7b92
Showing
6 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
mmv1/templates/terraform/custom_import/monitoring_monitored_project.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
mmv1/templates/terraform/examples/monitoring_monitored_project_long_form.tf.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] %>" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
mmv1/templates/terraform/state_migrations/monitoring_monitored_project.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |