Skip to content

Commit 6ad3e75

Browse files
Add resource_manager_tags support to Route api (#14395) (#23489)
[upstream:1f87d93c4f1c1b848e213265f2c49b55fd655ee1] Signed-off-by: Modular Magician <[email protected]>
1 parent 74a59c5 commit 6ad3e75

File tree

6 files changed

+131
-2
lines changed

6 files changed

+131
-2
lines changed

.changelog/14395.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: added `params.resourceManagerTags` field to the `google_compute_route`
3+
```

google/services/compute/resource_compute_route.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,29 @@ You can specify this as a full or partial URL. For example:
168168
Description: `URL to a VpnTunnel that should handle matching packets.`,
169169
ExactlyOneOf: []string{"next_hop_gateway", "next_hop_instance", "next_hop_ip", "next_hop_vpn_tunnel", "next_hop_ilb"},
170170
},
171+
"params": {
172+
Type: schema.TypeList,
173+
Optional: true,
174+
ForceNew: true,
175+
Description: `Additional params passed with the request, but not persisted as part of resource payload`,
176+
MaxItems: 1,
177+
Elem: &schema.Resource{
178+
Schema: map[string]*schema.Schema{
179+
"resource_manager_tags": {
180+
Type: schema.TypeMap,
181+
Optional: true,
182+
ForceNew: true,
183+
Description: `Resource manager tags to be bound to the route. Tag keys and values have the
184+
same definition as resource manager tags. Keys must be in the format tagKeys/{tag_key_id},
185+
and values are in the format tagValues/456. The field is ignored when empty.
186+
The field is immutable and causes resource replacement when mutated. This field is only
187+
set at create time and modifying this field after creation will trigger recreation.
188+
To apply tags to an existing resource, see the google_tags_tag_binding resource.`,
189+
Elem: &schema.Schema{Type: schema.TypeString},
190+
},
191+
},
192+
},
193+
},
171194
"priority": {
172195
Type: schema.TypeInt,
173196
Optional: true,
@@ -404,6 +427,12 @@ func resourceComputeRouteCreate(d *schema.ResourceData, meta interface{}) error
404427
} else if v, ok := d.GetOkExists("next_hop_ilb"); !tpgresource.IsEmptyValue(reflect.ValueOf(nextHopIlbProp)) && (ok || !reflect.DeepEqual(v, nextHopIlbProp)) {
405428
obj["nextHopIlb"] = nextHopIlbProp
406429
}
430+
paramsProp, err := expandComputeRouteParams(d.Get("params"), d, config)
431+
if err != nil {
432+
return err
433+
} else if v, ok := d.GetOkExists("params"); !tpgresource.IsEmptyValue(reflect.ValueOf(paramsProp)) && (ok || !reflect.DeepEqual(v, paramsProp)) {
434+
obj["params"] = paramsProp
435+
}
407436

408437
lockName, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/global/networks/{{network}}/peerings")
409438
if err != nil {
@@ -941,6 +970,36 @@ func expandComputeRouteNextHopIlb(v interface{}, d tpgresource.TerraformResource
941970
return v, nil
942971
}
943972

973+
func expandComputeRouteParams(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
974+
l := v.([]interface{})
975+
if len(l) == 0 || l[0] == nil {
976+
return nil, nil
977+
}
978+
raw := l[0]
979+
original := raw.(map[string]interface{})
980+
transformed := make(map[string]interface{})
981+
982+
transformedResourceManagerTags, err := expandComputeRouteParamsResourceManagerTags(original["resource_manager_tags"], d, config)
983+
if err != nil {
984+
return nil, err
985+
} else if val := reflect.ValueOf(transformedResourceManagerTags); val.IsValid() && !tpgresource.IsEmptyValue(val) {
986+
transformed["resourceManagerTags"] = transformedResourceManagerTags
987+
}
988+
989+
return transformed, nil
990+
}
991+
992+
func expandComputeRouteParamsResourceManagerTags(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
993+
if v == nil {
994+
return map[string]string{}, nil
995+
}
996+
m := make(map[string]string)
997+
for k, val := range v.(map[string]interface{}) {
998+
m[k] = val.(string)
999+
}
1000+
return m, nil
1001+
}
1002+
9441003
func resourceComputeRouteDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) {
9451004
if v, ok := res["nextHopInstance"]; ok {
9461005
val, err := tpgresource.ParseZonalFieldValue("instances", v.(string), "project", "next_hop_instance_zone", d, meta.(*transport_tpg.Config), true)

google/services/compute/resource_compute_route_generated_meta.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fields:
2323
- field: 'next_hop_origin'
2424
- field: 'next_hop_peering'
2525
- field: 'next_hop_vpn_tunnel'
26+
- field: 'params.resource_manager_tags'
2627
- field: 'priority'
2728
- field: 'route_status'
2829
- field: 'route_type'

google/services/compute/resource_compute_route_generated_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestAccComputeRoute_routeBasicExample(t *testing.T) {
4949
ResourceName: "google_compute_route.default",
5050
ImportState: true,
5151
ImportStateVerify: true,
52-
ImportStateVerifyIgnore: []string{"network", "next_hop_instance", "next_hop_vpn_tunnel"},
52+
ImportStateVerifyIgnore: []string{"network", "next_hop_instance", "next_hop_vpn_tunnel", "params"},
5353
},
5454
},
5555
})
@@ -90,7 +90,7 @@ func TestAccComputeRoute_routeIlbExample(t *testing.T) {
9090
ResourceName: "google_compute_route.route-ilb",
9191
ImportState: true,
9292
ImportStateVerify: true,
93-
ImportStateVerifyIgnore: []string{"network", "next_hop_instance", "next_hop_vpn_tunnel"},
93+
ImportStateVerifyIgnore: []string{"network", "next_hop_instance", "next_hop_vpn_tunnel", "params"},
9494
},
9595
},
9696
})

google/services/compute/resource_compute_route_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
2424
"github.com/hashicorp/terraform-provider-google/google/acctest"
25+
"github.com/hashicorp/terraform-provider-google/google/envvar"
2526
)
2627

2728
func TestAccComputeRoute_defaultInternetGateway(t *testing.T) {
@@ -65,6 +66,55 @@ func TestAccComputeRoute_hopInstance(t *testing.T) {
6566
})
6667
}
6768

69+
func TestAccComputeRoute_resourceManagerTags(t *testing.T) {
70+
71+
org := envvar.GetTestOrgFromEnv(t)
72+
73+
routeName := fmt.Sprintf("tf-test-route-resource-manager-tags-%s", acctest.RandString(t, 10))
74+
tagKeyResult := acctest.BootstrapSharedTestTagKeyDetails(t, "crm-nroute-tagkey", "organizations/"+org, make(map[string]interface{}))
75+
sharedTagkey, _ := tagKeyResult["shared_tag_key"]
76+
tagValueResult := acctest.BootstrapSharedTestTagValueDetails(t, "crm-route-tagvalue", sharedTagkey, org)
77+
context := map[string]interface{}{
78+
"route_name": routeName,
79+
"tag_key_id": tagKeyResult["name"],
80+
"tag_value_id": tagValueResult["name"],
81+
}
82+
83+
acctest.VcrTest(t, resource.TestCase{
84+
PreCheck: func() { acctest.AccTestPreCheck(t) },
85+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
86+
CheckDestroy: testAccCheckComputeRouteDestroyProducer(t),
87+
Steps: []resource.TestStep{
88+
{
89+
Config: testAccComputeRoute_resourceManagerTags(context),
90+
},
91+
{
92+
ResourceName: "google_compute_route.acc_route_with_resource_manager_tags",
93+
ImportState: true,
94+
ImportStateVerify: true,
95+
ImportStateVerifyIgnore: []string{"params"}, // we don't read tags back. The whole params block is input only
96+
},
97+
},
98+
})
99+
}
100+
101+
func testAccComputeRoute_resourceManagerTags(context map[string]interface{}) string {
102+
return acctest.Nprintf(`
103+
resource "google_compute_route" "acc_route_with_resource_manager_tags" {
104+
name = "%{route_name}"
105+
dest_range = "0.0.0.0/0"
106+
network = "default"
107+
next_hop_gateway = "default-internet-gateway"
108+
priority = 100
109+
params {
110+
resource_manager_tags = {
111+
"%{tag_key_id}" = "%{tag_value_id}"
112+
}
113+
}
114+
}
115+
`, context)
116+
}
117+
68118
func testAccComputeRoute_defaultInternetGateway(suffix string) string {
69119
return fmt.Sprintf(`
70120
resource "google_compute_route" "foobar" {

website/docs/r/compute_route.html.markdown

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ The following arguments are supported:
310310
Note that this can only be used when the destinationRange is
311311
a public (non-RFC 1918) IP CIDR range.
312312

313+
* `params` -
314+
(Optional)
315+
Additional params passed with the request, but not persisted as part of resource payload
316+
Structure is [documented below](#nested_params).
317+
313318
* `project` - (Optional) The ID of the project in which the resource belongs.
314319
If it is not provided, the provider project is used.
315320

@@ -319,6 +324,17 @@ The following arguments are supported:
319324
a URL.
320325

321326

327+
<a name="nested_params"></a>The `params` block supports:
328+
329+
* `resource_manager_tags` -
330+
(Optional)
331+
Resource manager tags to be bound to the route. Tag keys and values have the
332+
same definition as resource manager tags. Keys must be in the format tagKeys/{tag_key_id},
333+
and values are in the format tagValues/456. The field is ignored when empty.
334+
The field is immutable and causes resource replacement when mutated. This field is only
335+
set at create time and modifying this field after creation will trigger recreation.
336+
To apply tags to an existing resource, see the google_tags_tag_binding resource.
337+
322338
## Attributes Reference
323339

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

0 commit comments

Comments
 (0)