Skip to content

Commit 6e986cd

Browse files
modular-magicianmelinath
authored andcommitted
Normalize name field (#8360) (#5905)
* Normalize name field * Fix import * Support previous import id * Also shorten name in encoder * Add long form test * Add upgrade state function Signed-off-by: Modular Magician <[email protected]>
1 parent 3030f3c commit 6e986cd

File tree

4 files changed

+113
-9
lines changed

4 files changed

+113
-9
lines changed

.changelog/8360.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
monitoring: fixed an issue which occurred when `name` field of `google_monitoring_monitored_project` was long-form
3+
```

google-beta/resource_monitoring_monitored_project_generated_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,48 @@ resource "google_project" "basic" {
7474
`, context)
7575
}
7676

77+
func TestAccMonitoringMonitoredProject_monitoringMonitoredProjectLongFormExample(t *testing.T) {
78+
t.Parallel()
79+
80+
context := map[string]interface{}{
81+
"org_id": envvar.GetTestOrgFromEnv(t),
82+
"project_id": envvar.GetTestProjectFromEnv(),
83+
"random_suffix": acctest.RandString(t, 10),
84+
}
85+
86+
acctest.VcrTest(t, resource.TestCase{
87+
PreCheck: func() { acctest.AccTestPreCheck(t) },
88+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
89+
CheckDestroy: testAccCheckMonitoringMonitoredProjectDestroyProducer(t),
90+
Steps: []resource.TestStep{
91+
{
92+
Config: testAccMonitoringMonitoredProject_monitoringMonitoredProjectLongFormExample(context),
93+
},
94+
{
95+
ResourceName: "google_monitoring_monitored_project.primary",
96+
ImportState: true,
97+
ImportStateVerify: true,
98+
ImportStateVerifyIgnore: []string{"metrics_scope"},
99+
},
100+
},
101+
})
102+
}
103+
104+
func testAccMonitoringMonitoredProject_monitoringMonitoredProjectLongFormExample(context map[string]interface{}) string {
105+
return acctest.Nprintf(`
106+
resource "google_monitoring_monitored_project" "primary" {
107+
metrics_scope = "%{project_id}"
108+
name = "locations/global/metricsScopes/%{project_id}/projects/${google_project.basic.project_id}"
109+
}
110+
111+
resource "google_project" "basic" {
112+
project_id = "tf-test-m-id%{random_suffix}"
113+
name = "tf-test-m-id%{random_suffix}-display"
114+
org_id = "%{org_id}"
115+
}
116+
`, context)
117+
}
118+
77119
func testAccCheckMonitoringMonitoredProjectDestroyProducer(t *testing.T) func(s *terraform.State) error {
78120
return func(s *terraform.State) error {
79121
for name, rs := range s.RootModule().Resources {

google-beta/services/monitoring/resource_monitoring_monitored_project.go

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package monitoring
1919

2020
import (
21+
"context"
2122
"fmt"
2223
"log"
2324
"reflect"
@@ -45,6 +46,16 @@ func ResourceMonitoringMonitoredProject() *schema.Resource {
4546
Delete: schema.DefaultTimeout(20 * time.Minute),
4647
},
4748

49+
SchemaVersion: 1,
50+
51+
StateUpgraders: []schema.StateUpgrader{
52+
{
53+
Type: resourceMonitoringMonitoredProjectResourceV0().CoreConfigSchema().ImpliedType(),
54+
Upgrade: ResourceMonitoringMonitoredProjectUpgradeV0,
55+
Version: 0,
56+
},
57+
},
58+
4859
Schema: map[string]*schema.Schema{
4960
"metrics_scope": {
5061
Type: schema.TypeString,
@@ -54,10 +65,11 @@ func ResourceMonitoringMonitoredProject() *schema.Resource {
5465
Description: `Required. The resource name of the existing Metrics Scope that will monitor this project. Example: locations/global/metricsScopes/{SCOPING_PROJECT_ID_OR_NUMBER}`,
5566
},
5667
"name": {
57-
Type: schema.TypeString,
58-
Required: true,
59-
ForceNew: true,
60-
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}'`,
68+
Type: schema.TypeString,
69+
Required: true,
70+
ForceNew: true,
71+
DiffSuppressFunc: tpgresource.CompareResourceNames,
72+
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}'`,
6173
},
6274
"create_time": {
6375
Type: schema.TypeString,
@@ -118,7 +130,7 @@ func resourceMonitoringMonitoredProjectCreate(d *schema.ResourceData, meta inter
118130
}
119131

120132
// Store the ID now
121-
id, err := tpgresource.ReplaceVars(d, config, "v1/locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}}")
133+
id, err := tpgresource.ReplaceVars(d, config, "locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}}")
122134
if err != nil {
123135
return fmt.Errorf("Error constructing id: %s", err)
124136
}
@@ -150,6 +162,9 @@ func resourceMonitoringMonitoredProjectRead(d *schema.ResourceData, meta interfa
150162
billingProject = bp
151163
}
152164

165+
name := d.Get("name").(string)
166+
name = tpgresource.GetResourceNameFromSelfLink(name)
167+
d.Set("name", name)
153168
metricsScope := d.Get("metrics_scope").(string)
154169
metricsScope = tpgresource.GetResourceNameFromSelfLink(metricsScope)
155170
d.Set("metrics_scope", metricsScope)
@@ -245,16 +260,23 @@ func resourceMonitoringMonitoredProjectDelete(d *schema.ResourceData, meta inter
245260
}
246261

247262
func resourceMonitoringMonitoredProjectImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
263+
name := d.Get("name").(string)
264+
name = tpgresource.GetResourceNameFromSelfLink(name)
265+
d.Set("name", name)
266+
metricsScope := d.Get("metrics_scope").(string)
267+
metricsScope = tpgresource.GetResourceNameFromSelfLink(metricsScope)
268+
d.Set("metrics_scope", metricsScope)
248269
config := meta.(*transport_tpg.Config)
249270
if err := tpgresource.ParseImportId([]string{
271+
"locations/global/metricsScopes/(?P<metrics_scope>[^/]+)/projects/(?P<name>[^/]+)",
250272
"v1/locations/global/metricsScopes/(?P<metrics_scope>[^/]+)/projects/(?P<name>[^/]+)",
251273
"(?P<metrics_scope>[^/]+)/(?P<name>[^/]+)",
252274
}, d, config); err != nil {
253275
return nil, err
254276
}
255277

256278
// Replace import id for the resource id
257-
id, err := tpgresource.ReplaceVars(d, config, "v1/locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}}")
279+
id, err := tpgresource.ReplaceVars(d, config, "locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}}")
258280
if err != nil {
259281
return nil, fmt.Errorf("Error constructing id: %s", err)
260282
}
@@ -279,6 +301,7 @@ func expandNestedMonitoringMonitoredProjectName(v interface{}, d tpgresource.Ter
279301
func resourceMonitoringMonitoredProjectEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
280302
name := d.Get("name").(string)
281303
name = tpgresource.GetResourceNameFromSelfLink(name)
304+
d.Set("name", name)
282305
metricsScope := d.Get("metrics_scope").(string)
283306
metricsScope = tpgresource.GetResourceNameFromSelfLink(metricsScope)
284307
d.Set("metrics_scope", metricsScope)
@@ -356,3 +379,39 @@ func resourceMonitoringMonitoredProjectDecoder(d *schema.ResourceData, meta inte
356379
}
357380
return res, nil
358381
}
382+
383+
func resourceMonitoringMonitoredProjectResourceV0() *schema.Resource {
384+
return &schema.Resource{
385+
Schema: map[string]*schema.Schema{
386+
"metrics_scope": {
387+
Type: schema.TypeString,
388+
Required: true,
389+
ForceNew: true,
390+
DiffSuppressFunc: tpgresource.CompareResourceNames,
391+
Description: `Required. The resource name of the existing Metrics Scope that will monitor this project. Example: locations/global/metricsScopes/{SCOPING_PROJECT_ID_OR_NUMBER}`,
392+
},
393+
"name": {
394+
Type: schema.TypeString,
395+
Required: true,
396+
ForceNew: true,
397+
DiffSuppressFunc: tpgresource.CompareResourceNames,
398+
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}'`,
399+
},
400+
"create_time": {
401+
Type: schema.TypeString,
402+
Computed: true,
403+
Description: `Output only. The time when this 'MonitoredProject' was created.`,
404+
},
405+
},
406+
UseJSONNumber: true,
407+
}
408+
}
409+
410+
func ResourceMonitoringMonitoredProjectUpgradeV0(_ context.Context, rawState map[string]any, meta any) (map[string]any, error) {
411+
log.Printf("[DEBUG] Attributes before migration: %#v", rawState)
412+
413+
rawState["id"] = strings.TrimPrefix(rawState["id"].(string), "v1/")
414+
415+
log.Printf("[DEBUG] Attributes after migration: %#v", rawState)
416+
return rawState, nil
417+
}

website/docs/r/monitoring_monitored_project.html.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ The following arguments are supported:
6666

6767
In addition to the arguments listed above, the following computed attributes are exported:
6868

69-
* `id` - an identifier for the resource with format `v1/locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}}`
69+
* `id` - an identifier for the resource with format `locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}}`
7070

7171
* `create_time` -
7272
Output only. The time when this `MonitoredProject` was created.
@@ -86,6 +86,6 @@ This resource provides the following
8686
MonitoredProject can be imported using any of these accepted formats:
8787

8888
```
89-
$ terraform import google_monitoring_monitored_project.default v1/locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}}
90-
$ terraform import google_monitoring_monitored_project.default {{metrics_scope}}/{{name}}
89+
$ terraform import google_monitoring_monitored_project.default v1/locations/global/metricsScopes/{{name}}
90+
$ terraform import google_monitoring_monitored_project.default {{name}}
9191
```

0 commit comments

Comments
 (0)