Skip to content

Commit 709da91

Browse files
modular-magicianc2thorn
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 a70e0dd commit 709da91

File tree

4 files changed

+114
-9
lines changed

4 files changed

+114
-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: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
package monitoring
1919

2020
import (
21+
"context"
2122
"fmt"
2223
"log"
2324
"reflect"
25+
"strings"
2426
"time"
2527

2628
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -44,6 +46,16 @@ func ResourceMonitoringMonitoredProject() *schema.Resource {
4446
Delete: schema.DefaultTimeout(20 * time.Minute),
4547
},
4648

49+
SchemaVersion: 1,
50+
51+
StateUpgraders: []schema.StateUpgrader{
52+
{
53+
Type: resourceMonitoringMonitoredProjectResourceV0().CoreConfigSchema().ImpliedType(),
54+
Upgrade: ResourceMonitoringMonitoredProjectUpgradeV0,
55+
Version: 0,
56+
},
57+
},
58+
4759
Schema: map[string]*schema.Schema{
4860
"metrics_scope": {
4961
Type: schema.TypeString,
@@ -53,10 +65,11 @@ func ResourceMonitoringMonitoredProject() *schema.Resource {
5365
Description: `Required. The resource name of the existing Metrics Scope that will monitor this project. Example: locations/global/metricsScopes/{SCOPING_PROJECT_ID_OR_NUMBER}`,
5466
},
5567
"name": {
56-
Type: schema.TypeString,
57-
Required: true,
58-
ForceNew: true,
59-
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}'`,
6073
},
6174
"create_time": {
6275
Type: schema.TypeString,
@@ -116,7 +129,7 @@ func resourceMonitoringMonitoredProjectCreate(d *schema.ResourceData, meta inter
116129
}
117130

118131
// Store the ID now
119-
id, err := tpgresource.ReplaceVars(d, config, "v1/locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}}")
132+
id, err := tpgresource.ReplaceVars(d, config, "locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}}")
120133
if err != nil {
121134
return fmt.Errorf("Error constructing id: %s", err)
122135
}
@@ -146,6 +159,9 @@ func resourceMonitoringMonitoredProjectRead(d *schema.ResourceData, meta interfa
146159
billingProject = bp
147160
}
148161

162+
name := d.Get("name").(string)
163+
name = tpgresource.GetResourceNameFromSelfLink(name)
164+
d.Set("name", name)
149165
metricsScope := d.Get("metrics_scope").(string)
150166
metricsScope = tpgresource.GetResourceNameFromSelfLink(metricsScope)
151167
d.Set("metrics_scope", metricsScope)
@@ -240,16 +256,23 @@ func resourceMonitoringMonitoredProjectDelete(d *schema.ResourceData, meta inter
240256
}
241257

242258
func resourceMonitoringMonitoredProjectImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
259+
name := d.Get("name").(string)
260+
name = tpgresource.GetResourceNameFromSelfLink(name)
261+
d.Set("name", name)
262+
metricsScope := d.Get("metrics_scope").(string)
263+
metricsScope = tpgresource.GetResourceNameFromSelfLink(metricsScope)
264+
d.Set("metrics_scope", metricsScope)
243265
config := meta.(*transport_tpg.Config)
244266
if err := tpgresource.ParseImportId([]string{
267+
"locations/global/metricsScopes/(?P<metrics_scope>[^/]+)/projects/(?P<name>[^/]+)",
245268
"v1/locations/global/metricsScopes/(?P<metrics_scope>[^/]+)/projects/(?P<name>[^/]+)",
246269
"(?P<metrics_scope>[^/]+)/(?P<name>[^/]+)",
247270
}, d, config); err != nil {
248271
return nil, err
249272
}
250273

251274
// Replace import id for the resource id
252-
id, err := tpgresource.ReplaceVars(d, config, "v1/locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}}")
275+
id, err := tpgresource.ReplaceVars(d, config, "locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}}")
253276
if err != nil {
254277
return nil, fmt.Errorf("Error constructing id: %s", err)
255278
}
@@ -273,6 +296,7 @@ func expandNestedMonitoringMonitoredProjectName(v interface{}, d tpgresource.Ter
273296
func resourceMonitoringMonitoredProjectEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
274297
name := d.Get("name").(string)
275298
name = tpgresource.GetResourceNameFromSelfLink(name)
299+
d.Set("name", name)
276300
metricsScope := d.Get("metrics_scope").(string)
277301
metricsScope = tpgresource.GetResourceNameFromSelfLink(metricsScope)
278302
d.Set("metrics_scope", metricsScope)
@@ -350,3 +374,39 @@ func resourceMonitoringMonitoredProjectDecoder(d *schema.ResourceData, meta inte
350374
}
351375
return res, nil
352376
}
377+
378+
func resourceMonitoringMonitoredProjectResourceV0() *schema.Resource {
379+
return &schema.Resource{
380+
Schema: map[string]*schema.Schema{
381+
"metrics_scope": {
382+
Type: schema.TypeString,
383+
Required: true,
384+
ForceNew: true,
385+
DiffSuppressFunc: tpgresource.CompareResourceNames,
386+
Description: `Required. The resource name of the existing Metrics Scope that will monitor this project. Example: locations/global/metricsScopes/{SCOPING_PROJECT_ID_OR_NUMBER}`,
387+
},
388+
"name": {
389+
Type: schema.TypeString,
390+
Required: true,
391+
ForceNew: true,
392+
DiffSuppressFunc: tpgresource.CompareResourceNames,
393+
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}'`,
394+
},
395+
"create_time": {
396+
Type: schema.TypeString,
397+
Computed: true,
398+
Description: `Output only. The time when this 'MonitoredProject' was created.`,
399+
},
400+
},
401+
UseJSONNumber: true,
402+
}
403+
}
404+
405+
func ResourceMonitoringMonitoredProjectUpgradeV0(_ context.Context, rawState map[string]any, meta any) (map[string]any, error) {
406+
log.Printf("[DEBUG] Attributes before migration: %#v", rawState)
407+
408+
rawState["id"] = strings.TrimPrefix(rawState["id"].(string), "v1/")
409+
410+
log.Printf("[DEBUG] Attributes after migration: %#v", rawState)
411+
return rawState, nil
412+
}

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)