Skip to content

Commit fc952e8

Browse files
Add resource_manager_tags support to Network api (#14119) (#23421)
[upstream:524ca0e0edbab9d7ebf5b2d0dfc4d31ff890b7b0] Signed-off-by: Modular Magician <[email protected]>
1 parent 729859d commit fc952e8

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/acctest/bootstrap_test_utils.go

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

17581758
func BootstrapSharedTestProjectTagKey(t *testing.T, testId string, obj map[string]interface{}) string {
17591759
pid := envvar.GetTestProjectFromEnv()
1760-
return bootstrapSharedTestTagKey(t, testId, "projects/"+pid, obj)
1760+
return BootstrapSharedTestTagKeyDetails(t, testId, "projects/"+pid, obj)["shared_tag_key"]
17611761
}
17621762

17631763
func BootstrapSharedTestOrganizationTagKey(t *testing.T, testId string, obj map[string]interface{}) string {
17641764
org := envvar.GetTestOrgFromEnv(t)
1765-
return bootstrapSharedTestTagKey(t, testId, "organizations/"+org, obj)
1765+
return BootstrapSharedTestTagKeyDetails(t, testId, "organizations/"+org, obj)["shared_tag_key"]
17661766
}
17671767

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

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

17811781
config := BootstrapConfig(t)
17821782
if config == nil {
1783-
return ""
1783+
return make(map[string]string)
17841784
}
17851785

17861786
log.Printf("[DEBUG] Getting shared test tag key %q", sharedTagKey)
@@ -1829,7 +1829,7 @@ func bootstrapSharedTestTagKey(t *testing.T, testId, parent string, obj map[stri
18291829
}
18301830
}
18311831

1832-
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
1832+
getTagKeyResponse, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
18331833
Config: config,
18341834
Method: "GET",
18351835
Project: config.Project,
@@ -1842,29 +1842,32 @@ func bootstrapSharedTestTagKey(t *testing.T, testId, parent string, obj map[stri
18421842
t.Fatalf("Error getting shared tag key %q: %s", sharedTagKey, err)
18431843
}
18441844

1845-
return sharedTagKey
1845+
return map[string]string{
1846+
"name": getTagKeyResponse["name"].(string),
1847+
"shared_tag_key": sharedTagKey,
1848+
}
18461849
}
18471850

18481851
const sharedTagValuePrefix = "tf-bootstrap-tagvalue"
18491852

18501853
func BootstrapSharedTestProjectTagValue(t *testing.T, testId string, tagKey string) string {
18511854
pid := envvar.GetTestProjectFromEnv()
1852-
return BootstrapSharedTestTagValue(t, testId, tagKey, pid)
1855+
return BootstrapSharedTestTagValueDetails(t, testId, tagKey, pid)["shared_tag_value"]
18531856
}
18541857

18551858
func BootstrapSharedTestOrganizationTagValue(t *testing.T, testId string, tagKey string) string {
18561859
org := envvar.GetTestOrgFromEnv(t)
1857-
return BootstrapSharedTestTagValue(t, testId, tagKey, org)
1860+
return BootstrapSharedTestTagValueDetails(t, testId, tagKey, org)["shared_tag_value"]
18581861
}
18591862

1860-
func BootstrapSharedTestTagValue(t *testing.T, testId string, tagKey, parentId string) string {
1863+
func BootstrapSharedTestTagValueDetails(t *testing.T, testId string, tagKey, parentId string) map[string]string {
18611864
sharedTagValue := fmt.Sprintf("%s-%s", sharedTagValuePrefix, testId)
18621865
tagKeyName := fmt.Sprintf("%s/%s", parentId, tagKey)
18631866
tagValueName := fmt.Sprintf("%s/%s", tagKeyName, sharedTagValue)
18641867

18651868
config := BootstrapConfig(t)
18661869
if config == nil {
1867-
return ""
1870+
return make(map[string]string)
18681871
}
18691872

18701873
log.Printf("[DEBUG] Getting shared test tag value %q", sharedTagValue)
@@ -1923,8 +1926,7 @@ func BootstrapSharedTestTagValue(t *testing.T, testId string, tagKey, parentId s
19231926
t.Fatalf("Error waiting to create TagValue: %s", err)
19241927
}
19251928
}
1926-
1927-
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
1929+
getTagValueResponse, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
19281930
Config: config,
19291931
Method: "GET",
19301932
Project: config.Project,
@@ -1937,7 +1939,10 @@ func BootstrapSharedTestTagValue(t *testing.T, testId string, tagKey, parentId s
19371939
t.Fatalf("Error getting shared tag value %q: %s", sharedTagValue, err)
19381940
}
19391941

1940-
return sharedTagValue
1942+
return map[string]string{
1943+
"name": getTagValueResponse["name"].(string),
1944+
"shared_tag_value": sharedTagValue,
1945+
}
19411946
}
19421947

19431948
type BootstrapClient struct {

google/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/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/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/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)