Skip to content

Commit 7641ebc

Browse files
add network_url attribute in consumer_accept_list block of google_compute_service_attachment resource (#9895) (#7047)
* add network_url attribute in consumer_accept_list block of google_compute_service_attachment resource * Bugfix: Use SelfLinkRelativePath check to prevent false positive resource changes [upstream:0249d74bdb046afe63b6562f40b7cfa315eeb8b2] Signed-off-by: Modular Magician <[email protected]>
1 parent d1cb9d3 commit 7641ebc

File tree

4 files changed

+310
-6
lines changed

4 files changed

+310
-6
lines changed

.changelog/9895.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 the `network_url` attribute to the `consumer_accept_list`-block of the `google_compute_service_attachment` resource.
3+
```

google-beta/services/compute/resource_compute_service_attachment.go

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

2020
import (
21+
"bytes"
2122
"fmt"
2223
"log"
2324
"reflect"
@@ -30,6 +31,42 @@ import (
3031
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
3132
)
3233

34+
// Hash based on key, which is either project_id_or_num or network_url.
35+
func computeServiceAttachmentConsumerAcceptListsHash(v interface{}) int {
36+
if v == nil {
37+
return 0
38+
}
39+
40+
var buf bytes.Buffer
41+
m := v.(map[string]interface{})
42+
log.Printf("[DEBUG] hashing %v", m)
43+
44+
if v, ok := m["project_id_or_num"]; ok {
45+
if v == nil {
46+
v = ""
47+
}
48+
49+
buf.WriteString(fmt.Sprintf("%v-", v))
50+
}
51+
52+
if v, ok := m["network_url"]; ok {
53+
if v == nil {
54+
v = ""
55+
} else {
56+
if networkUrl, err := tpgresource.GetRelativePath(v.(string)); err != nil {
57+
log.Printf("[WARN] Error on retrieving relative path of network url: %s", err)
58+
} else {
59+
v = networkUrl
60+
}
61+
}
62+
63+
buf.WriteString(fmt.Sprintf("%v-", v))
64+
}
65+
66+
log.Printf("[DEBUG] computed hash value of %v from %v", tpgresource.Hashcode(buf.String()), buf.String())
67+
return tpgresource.Hashcode(buf.String())
68+
}
69+
3370
func ResourceComputeServiceAttachment() *schema.Resource {
3471
return &schema.Resource{
3572
Create: resourceComputeServiceAttachmentCreate,
@@ -100,7 +137,7 @@ this service attachment.`,
100137
Description: `An array of projects that are allowed to connect to this service
101138
attachment.`,
102139
Elem: computeServiceAttachmentConsumerAcceptListsSchema(),
103-
// Default schema.HashSchema is used.
140+
Set: computeServiceAttachmentConsumerAcceptListsHash,
104141
},
105142
"consumer_reject_lists": {
106143
Type: schema.TypeList,
@@ -195,11 +232,19 @@ func computeServiceAttachmentConsumerAcceptListsSchema() *schema.Resource {
195232
Required: true,
196233
Description: `The number of consumer forwarding rules the consumer project can
197234
create.`,
235+
},
236+
"network_url": {
237+
Type: schema.TypeString,
238+
Optional: true,
239+
DiffSuppressFunc: tpgresource.CompareSelfLinkRelativePaths,
240+
Description: `The network that is allowed to connect to this service attachment.
241+
Only one of project_id_or_num and network_url may be set.`,
198242
},
199243
"project_id_or_num": {
200-
Type: schema.TypeString,
201-
Required: true,
202-
Description: `A project that is allowed to connect to this service attachment.`,
244+
Type: schema.TypeString,
245+
Optional: true,
246+
Description: `A project that is allowed to connect to this service attachment.
247+
Only one of project_id_or_num and network_url may be set.`,
203248
},
204249
},
205250
}
@@ -688,7 +733,7 @@ func flattenComputeServiceAttachmentConsumerAcceptLists(v interface{}, d *schema
688733
return v
689734
}
690735
l := v.([]interface{})
691-
transformed := schema.NewSet(schema.HashResource(computeServiceAttachmentConsumerAcceptListsSchema()), []interface{}{})
736+
transformed := schema.NewSet(computeServiceAttachmentConsumerAcceptListsHash, []interface{}{})
692737
for _, raw := range l {
693738
original := raw.(map[string]interface{})
694739
if len(original) < 1 {
@@ -697,6 +742,7 @@ func flattenComputeServiceAttachmentConsumerAcceptLists(v interface{}, d *schema
697742
}
698743
transformed.Add(map[string]interface{}{
699744
"project_id_or_num": flattenComputeServiceAttachmentConsumerAcceptListsProjectIdOrNum(original["projectIdOrNum"], d, config),
745+
"network_url": flattenComputeServiceAttachmentConsumerAcceptListsNetworkUrl(original["networkUrl"], d, config),
700746
"connection_limit": flattenComputeServiceAttachmentConsumerAcceptListsConnectionLimit(original["connectionLimit"], d, config),
701747
})
702748
}
@@ -706,6 +752,10 @@ func flattenComputeServiceAttachmentConsumerAcceptListsProjectIdOrNum(v interfac
706752
return v
707753
}
708754

755+
func flattenComputeServiceAttachmentConsumerAcceptListsNetworkUrl(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
756+
return v
757+
}
758+
709759
func flattenComputeServiceAttachmentConsumerAcceptListsConnectionLimit(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
710760
// Handles the string fixed64 format
711761
if strVal, ok := v.(string); ok {
@@ -797,6 +847,13 @@ func expandComputeServiceAttachmentConsumerAcceptLists(v interface{}, d tpgresou
797847
transformed["projectIdOrNum"] = transformedProjectIdOrNum
798848
}
799849

850+
transformedNetworkUrl, err := expandComputeServiceAttachmentConsumerAcceptListsNetworkUrl(original["network_url"], d, config)
851+
if err != nil {
852+
return nil, err
853+
} else if val := reflect.ValueOf(transformedNetworkUrl); val.IsValid() && !tpgresource.IsEmptyValue(val) {
854+
transformed["networkUrl"] = transformedNetworkUrl
855+
}
856+
800857
transformedConnectionLimit, err := expandComputeServiceAttachmentConsumerAcceptListsConnectionLimit(original["connection_limit"], d, config)
801858
if err != nil {
802859
return nil, err
@@ -813,6 +870,10 @@ func expandComputeServiceAttachmentConsumerAcceptListsProjectIdOrNum(v interface
813870
return v, nil
814871
}
815872

873+
func expandComputeServiceAttachmentConsumerAcceptListsNetworkUrl(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
874+
return v, nil
875+
}
876+
816877
func expandComputeServiceAttachmentConsumerAcceptListsConnectionLimit(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
817878
return v, nil
818879
}

google-beta/services/compute/resource_compute_service_attachment_generated_test.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,133 @@ resource "google_compute_subnetwork" "psc_ilb_nat" {
255255
`, context)
256256
}
257257

258+
func TestAccComputeServiceAttachment_serviceAttachmentExplicitNetworksExample(t *testing.T) {
259+
t.Parallel()
260+
261+
context := map[string]interface{}{
262+
"random_suffix": acctest.RandString(t, 10),
263+
}
264+
265+
acctest.VcrTest(t, resource.TestCase{
266+
PreCheck: func() { acctest.AccTestPreCheck(t) },
267+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
268+
CheckDestroy: testAccCheckComputeServiceAttachmentDestroyProducer(t),
269+
Steps: []resource.TestStep{
270+
{
271+
Config: testAccComputeServiceAttachment_serviceAttachmentExplicitNetworksExample(context),
272+
},
273+
{
274+
ResourceName: "google_compute_service_attachment.psc_ilb_service_attachment",
275+
ImportState: true,
276+
ImportStateVerify: true,
277+
ImportStateVerifyIgnore: []string{"target_service", "region"},
278+
},
279+
},
280+
})
281+
}
282+
283+
func testAccComputeServiceAttachment_serviceAttachmentExplicitNetworksExample(context map[string]interface{}) string {
284+
return acctest.Nprintf(`
285+
resource "google_compute_service_attachment" "psc_ilb_service_attachment" {
286+
name = "tf-test-my-psc-ilb%{random_suffix}"
287+
region = "us-west2"
288+
description = "A service attachment configured with Terraform"
289+
290+
enable_proxy_protocol = false
291+
292+
connection_preference = "ACCEPT_MANUAL"
293+
nat_subnets = [google_compute_subnetwork.psc_ilb_nat.id]
294+
target_service = google_compute_forwarding_rule.psc_ilb_target_service.id
295+
296+
consumer_accept_lists {
297+
network_url = google_compute_network.psc_ilb_consumer_network.self_link
298+
connection_limit = 1
299+
}
300+
}
301+
302+
resource "google_compute_network" "psc_ilb_consumer_network" {
303+
name = "tf-test-psc-ilb-consumer-network%{random_suffix}"
304+
auto_create_subnetworks = false
305+
}
306+
307+
resource "google_compute_subnetwork" "psc_ilb_consumer_subnetwork" {
308+
name = "tf-test-psc-ilb-consumer-network%{random_suffix}"
309+
ip_cidr_range = "10.0.0.0/16"
310+
region = "us-west2"
311+
network = google_compute_network.psc_ilb_consumer_network.id
312+
}
313+
314+
resource "google_compute_address" "psc_ilb_consumer_address" {
315+
name = "tf-test-psc-ilb-consumer-address%{random_suffix}"
316+
region = "us-west2"
317+
318+
subnetwork = google_compute_subnetwork.psc_ilb_consumer_subnetwork.id
319+
address_type = "INTERNAL"
320+
}
321+
322+
resource "google_compute_forwarding_rule" "psc_ilb_consumer" {
323+
name = "tf-test-psc-ilb-consumer-forwarding-rule%{random_suffix}"
324+
region = "us-west2"
325+
326+
target = google_compute_service_attachment.psc_ilb_service_attachment.id
327+
load_balancing_scheme = "" # need to override EXTERNAL default when target is a service attachment
328+
network = google_compute_network.psc_ilb_consumer_network.id
329+
subnetwork = google_compute_subnetwork.psc_ilb_consumer_subnetwork.id
330+
ip_address = google_compute_address.psc_ilb_consumer_address.id
331+
}
332+
333+
resource "google_compute_forwarding_rule" "psc_ilb_target_service" {
334+
name = "tf-test-producer-forwarding-rule%{random_suffix}"
335+
region = "us-west2"
336+
337+
load_balancing_scheme = "INTERNAL"
338+
backend_service = google_compute_region_backend_service.producer_service_backend.id
339+
all_ports = true
340+
network = google_compute_network.psc_ilb_network.name
341+
subnetwork = google_compute_subnetwork.psc_ilb_producer_subnetwork.name
342+
}
343+
344+
resource "google_compute_region_backend_service" "producer_service_backend" {
345+
name = "tf-test-producer-service%{random_suffix}"
346+
region = "us-west2"
347+
348+
health_checks = [google_compute_health_check.producer_service_health_check.id]
349+
}
350+
351+
resource "google_compute_health_check" "producer_service_health_check" {
352+
name = "tf-test-producer-service-health-check%{random_suffix}"
353+
354+
check_interval_sec = 1
355+
timeout_sec = 1
356+
tcp_health_check {
357+
port = "80"
358+
}
359+
}
360+
361+
resource "google_compute_network" "psc_ilb_network" {
362+
name = "tf-test-psc-ilb-network%{random_suffix}"
363+
auto_create_subnetworks = false
364+
}
365+
366+
resource "google_compute_subnetwork" "psc_ilb_producer_subnetwork" {
367+
name = "tf-test-psc-ilb-producer-subnetwork%{random_suffix}"
368+
region = "us-west2"
369+
370+
network = google_compute_network.psc_ilb_network.id
371+
ip_cidr_range = "10.0.0.0/16"
372+
}
373+
374+
resource "google_compute_subnetwork" "psc_ilb_nat" {
375+
name = "tf-test-psc-ilb-nat%{random_suffix}"
376+
region = "us-west2"
377+
378+
network = google_compute_network.psc_ilb_network.id
379+
purpose = "PRIVATE_SERVICE_CONNECT"
380+
ip_cidr_range = "10.1.0.0/16"
381+
}
382+
`, context)
383+
}
384+
258385
func TestAccComputeServiceAttachment_serviceAttachmentReconcileConnectionsExample(t *testing.T) {
259386
t.Parallel()
260387

0 commit comments

Comments
 (0)