Skip to content

Commit 31ea014

Browse files
Add resource_manager_tags support to Network api (#14119) (#10266)
[upstream:524ca0e0edbab9d7ebf5b2d0dfc4d31ff890b7b0] Signed-off-by: Modular Magician <[email protected]>
1 parent 35906d4 commit 31ea014

File tree

7 files changed

+163
-31
lines changed

7 files changed

+163
-31
lines changed

.changelog/14119.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: add `params.resourceManagerTags` field to the `google_compute_network`
3+
```

google-beta/acctest/bootstrap_test_utils.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,16 +2061,16 @@ const sharedTagKeyParentErr = "Parent %q is not valid. Should be in format: 'org
20612061

20622062
func BootstrapSharedTestProjectTagKey(t *testing.T, testId string, obj map[string]interface{}) string {
20632063
pid := envvar.GetTestProjectFromEnv()
2064-
return bootstrapSharedTestTagKey(t, testId, "projects/"+pid, obj)
2064+
return BootstrapSharedTestTagKeyDetails(t, testId, "projects/"+pid, obj)["shared_tag_key"]
20652065
}
20662066

20672067
func BootstrapSharedTestOrganizationTagKey(t *testing.T, testId string, obj map[string]interface{}) string {
20682068
org := envvar.GetTestOrgFromEnv(t)
2069-
return bootstrapSharedTestTagKey(t, testId, "organizations/"+org, obj)
2069+
return BootstrapSharedTestTagKeyDetails(t, testId, "organizations/"+org, obj)["shared_tag_key"]
20702070
}
20712071

20722072
// parent should be in format: {"organization" OR "projects"}/{id}
2073-
func bootstrapSharedTestTagKey(t *testing.T, testId, parent string, obj map[string]interface{}) string {
2073+
func BootstrapSharedTestTagKeyDetails(t *testing.T, testId string, parent string, obj map[string]interface{}) map[string]string {
20742074
sharedTagKey := fmt.Sprintf("%s-%s", sharedTagKeyPrefix, testId)
20752075

20762076
parentSplit := strings.Split(parent, "/")
@@ -2084,7 +2084,7 @@ func bootstrapSharedTestTagKey(t *testing.T, testId, parent string, obj map[stri
20842084

20852085
config := BootstrapConfig(t)
20862086
if config == nil {
2087-
return ""
2087+
return make(map[string]string)
20882088
}
20892089

20902090
log.Printf("[DEBUG] Getting shared test tag key %q", sharedTagKey)
@@ -2133,7 +2133,7 @@ func bootstrapSharedTestTagKey(t *testing.T, testId, parent string, obj map[stri
21332133
}
21342134
}
21352135

2136-
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
2136+
getTagKeyResponse, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
21372137
Config: config,
21382138
Method: "GET",
21392139
Project: config.Project,
@@ -2146,29 +2146,32 @@ func bootstrapSharedTestTagKey(t *testing.T, testId, parent string, obj map[stri
21462146
t.Fatalf("Error getting shared tag key %q: %s", sharedTagKey, err)
21472147
}
21482148

2149-
return sharedTagKey
2149+
return map[string]string{
2150+
"name": getTagKeyResponse["name"].(string),
2151+
"shared_tag_key": sharedTagKey,
2152+
}
21502153
}
21512154

21522155
const sharedTagValuePrefix = "tf-bootstrap-tagvalue"
21532156

21542157
func BootstrapSharedTestProjectTagValue(t *testing.T, testId string, tagKey string) string {
21552158
pid := envvar.GetTestProjectFromEnv()
2156-
return BootstrapSharedTestTagValue(t, testId, tagKey, pid)
2159+
return BootstrapSharedTestTagValueDetails(t, testId, tagKey, pid)["shared_tag_value"]
21572160
}
21582161

21592162
func BootstrapSharedTestOrganizationTagValue(t *testing.T, testId string, tagKey string) string {
21602163
org := envvar.GetTestOrgFromEnv(t)
2161-
return BootstrapSharedTestTagValue(t, testId, tagKey, org)
2164+
return BootstrapSharedTestTagValueDetails(t, testId, tagKey, org)["shared_tag_value"]
21622165
}
21632166

2164-
func BootstrapSharedTestTagValue(t *testing.T, testId string, tagKey, parentId string) string {
2167+
func BootstrapSharedTestTagValueDetails(t *testing.T, testId string, tagKey, parentId string) map[string]string {
21652168
sharedTagValue := fmt.Sprintf("%s-%s", sharedTagValuePrefix, testId)
21662169
tagKeyName := fmt.Sprintf("%s/%s", parentId, tagKey)
21672170
tagValueName := fmt.Sprintf("%s/%s", tagKeyName, sharedTagValue)
21682171

21692172
config := BootstrapConfig(t)
21702173
if config == nil {
2171-
return ""
2174+
return make(map[string]string)
21722175
}
21732176

21742177
log.Printf("[DEBUG] Getting shared test tag value %q", sharedTagValue)
@@ -2227,8 +2230,7 @@ func BootstrapSharedTestTagValue(t *testing.T, testId string, tagKey, parentId s
22272230
t.Fatalf("Error waiting to create TagValue: %s", err)
22282231
}
22292232
}
2230-
2231-
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
2233+
getTagValueResponse, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
22322234
Config: config,
22332235
Method: "GET",
22342236
Project: config.Project,
@@ -2241,7 +2243,10 @@ func BootstrapSharedTestTagValue(t *testing.T, testId string, tagKey, parentId s
22412243
t.Fatalf("Error getting shared tag value %q: %s", sharedTagValue, err)
22422244
}
22432245

2244-
return sharedTagValue
2246+
return map[string]string{
2247+
"name": getTagValueResponse["name"].(string),
2248+
"shared_tag_value": sharedTagValue,
2249+
}
22452250
}
22462251

22472252
type BootstrapClient struct {

google-beta/services/compute/resource_compute_network.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,26 @@ following are valid URLs:
136136
* https://www.googleapis.com/compute/v1/projects/{projectId}/global/networkProfiles/{network_profile_name}
137137
* projects/{projectId}/global/networkProfiles/{network_profile_name}`,
138138
},
139+
"params": {
140+
Type: schema.TypeList,
141+
Optional: true,
142+
ForceNew: true,
143+
Description: `Additional params passed with the request, but not persisted as part of resource payload`,
144+
MaxItems: 1,
145+
Elem: &schema.Resource{
146+
Schema: map[string]*schema.Schema{
147+
"resource_manager_tags": {
148+
Type: schema.TypeMap,
149+
Optional: true,
150+
ForceNew: true,
151+
Description: `Resource manager tags to be bound to the network. Tag keys and values have the
152+
same definition as resource manager tags. Keys must be in the format tagKeys/{tag_key_id},
153+
and values are in the format tagValues/456.`,
154+
Elem: &schema.Schema{Type: schema.TypeString},
155+
},
156+
},
157+
},
158+
},
139159
"bgp_always_compare_med": {
140160
Type: schema.TypeBool,
141161
Computed: true,
@@ -270,6 +290,12 @@ func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) erro
270290
} else if v, ok := d.GetOkExists("network_profile"); !tpgresource.IsEmptyValue(reflect.ValueOf(networkProfileProp)) && (ok || !reflect.DeepEqual(v, networkProfileProp)) {
271291
obj["networkProfile"] = networkProfileProp
272292
}
293+
paramsProp, err := expandComputeNetworkParams(d.Get("params"), d, config)
294+
if err != nil {
295+
return err
296+
} else if v, ok := d.GetOkExists("params"); !tpgresource.IsEmptyValue(reflect.ValueOf(paramsProp)) && (ok || !reflect.DeepEqual(v, paramsProp)) {
297+
obj["params"] = paramsProp
298+
}
273299

274300
obj, err = resourceComputeNetworkEncoder(d, meta, obj)
275301
if err != nil {
@@ -885,6 +911,36 @@ func expandComputeNetworkNetworkProfile(v interface{}, d tpgresource.TerraformRe
885911
return v, nil
886912
}
887913

914+
func expandComputeNetworkParams(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
915+
l := v.([]interface{})
916+
if len(l) == 0 || l[0] == nil {
917+
return nil, nil
918+
}
919+
raw := l[0]
920+
original := raw.(map[string]interface{})
921+
transformed := make(map[string]interface{})
922+
923+
transformedResourceManagerTags, err := expandComputeNetworkParamsResourceManagerTags(original["resource_manager_tags"], d, config)
924+
if err != nil {
925+
return nil, err
926+
} else if val := reflect.ValueOf(transformedResourceManagerTags); val.IsValid() && !tpgresource.IsEmptyValue(val) {
927+
transformed["resourceManagerTags"] = transformedResourceManagerTags
928+
}
929+
930+
return transformed, nil
931+
}
932+
933+
func expandComputeNetworkParamsResourceManagerTags(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
934+
if v == nil {
935+
return map[string]string{}, nil
936+
}
937+
m := make(map[string]string)
938+
for k, val := range v.(map[string]interface{}) {
939+
m[k] = val.(string)
940+
}
941+
return m, nil
942+
}
943+
888944
func resourceComputeNetworkEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
889945
delete(obj, "numeric_id") // Field doesn't exist in the API
890946
return obj, nil

google-beta/services/compute/resource_compute_network_generated_meta.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ fields:
2020
api_field: 'id'
2121
- field: 'network_profile'
2222
- field: 'numeric_id'
23+
- field: 'params.resource_manager_tags'
2324
- field: 'routing_config.bgp_always_compare_med'
2425
- field: 'routing_config.bgp_best_path_selection_mode'
2526
- field: 'routing_config.bgp_inter_region_cost'

google-beta/services/compute/resource_compute_network_generated_test.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ func TestAccComputeNetwork_networkBasicExample(t *testing.T) {
4747
Config: testAccComputeNetwork_networkBasicExample(context),
4848
},
4949
{
50-
ResourceName: "google_compute_network.vpc_network",
51-
ImportState: true,
52-
ImportStateVerify: true,
50+
ResourceName: "google_compute_network.vpc_network",
51+
ImportState: true,
52+
ImportStateVerify: true,
53+
ImportStateVerifyIgnore: []string{"params"},
5354
},
5455
},
5556
})
@@ -80,9 +81,10 @@ func TestAccComputeNetwork_networkCustomMtuExample(t *testing.T) {
8081
Config: testAccComputeNetwork_networkCustomMtuExample(context),
8182
},
8283
{
83-
ResourceName: "google_compute_network.vpc_network",
84-
ImportState: true,
85-
ImportStateVerify: true,
84+
ResourceName: "google_compute_network.vpc_network",
85+
ImportState: true,
86+
ImportStateVerify: true,
87+
ImportStateVerifyIgnore: []string{"params"},
8688
},
8789
},
8890
})
@@ -116,9 +118,10 @@ func TestAccComputeNetwork_networkCustomFirewallEnforcementOrderExample(t *testi
116118
Config: testAccComputeNetwork_networkCustomFirewallEnforcementOrderExample(context),
117119
},
118120
{
119-
ResourceName: "google_compute_network.vpc_network",
120-
ImportState: true,
121-
ImportStateVerify: true,
121+
ResourceName: "google_compute_network.vpc_network",
122+
ImportState: true,
123+
ImportStateVerify: true,
124+
ImportStateVerifyIgnore: []string{"params"},
122125
},
123126
},
124127
})
@@ -152,9 +155,10 @@ func TestAccComputeNetwork_networkBgpBestPathSelectionModeExample(t *testing.T)
152155
Config: testAccComputeNetwork_networkBgpBestPathSelectionModeExample(context),
153156
},
154157
{
155-
ResourceName: "google_compute_network.vpc_network",
156-
ImportState: true,
157-
ImportStateVerify: true,
158+
ResourceName: "google_compute_network.vpc_network",
159+
ImportState: true,
160+
ImportStateVerify: true,
161+
ImportStateVerifyIgnore: []string{"params"},
158162
},
159163
},
160164
})
@@ -187,9 +191,10 @@ func TestAccComputeNetwork_networkBgpBestPathSelectionModeStandardExample(t *tes
187191
Config: testAccComputeNetwork_networkBgpBestPathSelectionModeStandardExample(context),
188192
},
189193
{
190-
ResourceName: "google_compute_network.vpc_network",
191-
ImportState: true,
192-
ImportStateVerify: true,
194+
ResourceName: "google_compute_network.vpc_network",
195+
ImportState: true,
196+
ImportStateVerify: true,
197+
ImportStateVerifyIgnore: []string{"params"},
193198
},
194199
},
195200
})
@@ -223,9 +228,10 @@ func TestAccComputeNetwork_networkBgpBestPathSelectionModeStandardCustomFieldsEx
223228
Config: testAccComputeNetwork_networkBgpBestPathSelectionModeStandardCustomFieldsExample(context),
224229
},
225230
{
226-
ResourceName: "google_compute_network.vpc_network",
227-
ImportState: true,
228-
ImportStateVerify: true,
231+
ResourceName: "google_compute_network.vpc_network",
232+
ImportState: true,
233+
ImportStateVerify: true,
234+
ImportStateVerifyIgnore: []string{"params"},
229235
},
230236
},
231237
})

google-beta/services/compute/resource_compute_network_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,40 @@ func TestAccComputeNetwork_networkFirewallPolicyEnforcementOrderAndUpdate(t *tes
516516
})
517517
}
518518

519+
func TestAccComputeNetwork_resourceManagerTags(t *testing.T) {
520+
521+
t.Parallel()
522+
523+
var network compute.Network
524+
org := envvar.GetTestOrgFromEnv(t)
525+
526+
suffixName := acctest.RandString(t, 10)
527+
tagKeyResult := acctest.BootstrapSharedTestTagKeyDetails(t, "crm-networks-tagkey", "organizations/"+org, make(map[string]interface{}))
528+
sharedTagkey, _ := tagKeyResult["shared_tag_key"]
529+
tagValueResult := acctest.BootstrapSharedTestTagValueDetails(t, "crm-networks-tagvalue", sharedTagkey, org)
530+
networkName := fmt.Sprintf("tf-test-network-resource-manager-tags-%s", suffixName)
531+
context := map[string]interface{}{
532+
"network_name": networkName,
533+
"tag_key_id": tagKeyResult["name"],
534+
"tag_value_id": tagValueResult["name"],
535+
}
536+
537+
acctest.VcrTest(t, resource.TestCase{
538+
PreCheck: func() { acctest.AccTestPreCheck(t) },
539+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
540+
CheckDestroy: testAccCheckComputeNetworkDestroyProducer(t),
541+
Steps: []resource.TestStep{
542+
{
543+
Config: testAccComputeNetwork_resourceManagerTags(context),
544+
Check: resource.ComposeTestCheckFunc(
545+
testAccCheckComputeNetworkExists(
546+
t, "google_compute_network.acc_network_with_resource_manager_tags", &network),
547+
),
548+
},
549+
},
550+
})
551+
}
552+
519553
func testAccCheckComputeNetworkExists(t *testing.T, n string, network *compute.Network) resource.TestCheckFunc {
520554
return func(s *terraform.State) error {
521555
rs, ok := s.RootModule().Resources[n]
@@ -845,3 +879,17 @@ resource "google_compute_network" "acc_network_firewall_policy_enforcement_order
845879
}
846880
`, networkName, order)
847881
}
882+
883+
func testAccComputeNetwork_resourceManagerTags(context map[string]interface{}) string {
884+
return acctest.Nprintf(`
885+
resource "google_compute_network" "acc_network_with_resource_manager_tags" {
886+
name = "%{network_name}"
887+
auto_create_subnetworks = false
888+
params {
889+
resource_manager_tags = {
890+
"%{tag_key_id}" = "%{tag_value_id}"
891+
}
892+
}
893+
}
894+
`, context)
895+
}

website/docs/r/compute_network.html.markdown

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,26 @@ The following arguments are supported:
191191
* https://www.googleapis.com/compute/v1/projects/{projectId}/global/networkProfiles/{network_profile_name}
192192
* projects/{projectId}/global/networkProfiles/{network_profile_name}
193193

194+
* `params` -
195+
(Optional)
196+
Additional params passed with the request, but not persisted as part of resource payload
197+
Structure is [documented below](#nested_params).
198+
194199
* `project` - (Optional) The ID of the project in which the resource belongs.
195200
If it is not provided, the provider project is used.
196201

197202
* `delete_default_routes_on_create` - (Optional) If set to `true`, default routes (`0.0.0.0/0`) will be deleted
198203
immediately after network creation. Defaults to `false`.
199204

200205

206+
<a name="nested_params"></a>The `params` block supports:
207+
208+
* `resource_manager_tags` -
209+
(Optional)
210+
Resource manager tags to be bound to the network. Tag keys and values have the
211+
same definition as resource manager tags. Keys must be in the format tagKeys/{tag_key_id},
212+
and values are in the format tagValues/456.
213+
201214
## Attributes Reference
202215

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

0 commit comments

Comments
 (0)