Skip to content

Commit d91a1eb

Browse files
Adding ServiceConnectionPolicies resource in NetworkConnectivity. (#8273) (#15381)
* adding both resources, basic test for serviceClass it's working, need to fix the test scenario for policy * fix the test scenario and the service class resource properties * adding a testing update, just adding the labels for the resource * added the resource service connection map, and it's basic test, need to evaluate * Revert "added the resource service connection map, and it's basic test, need to evaluate" This reverts commit 4240e1d81e5901221cee1f1db7c03c8fbc58e3a6. * removing the service class in one test scenario and using a static value * fix a typo * removing serviceClass and fix the tests * removing ga tag and updatinga etag for fingerprint type * updating the code, to remove the static value from the docs, need to test * adding a file to tpgtools override at product level * adding function to help with env var, but the patch on tests it's failing * adding the variable to be used in the docs * fixing the test, the network field cannot be immutable * removing the env_var service class and adding a static value * updating the value used from tests and documentation * removing unused resources for docs and tests * fix typos, not used variables and run the fmt * adding description to the basic test * fixed yaml variable override * removing unused resources for tests, and making the test fails with network as immutable * adding encoder to help network field be immutable and be sent on patch method Signed-off-by: Modular Magician <[email protected]>
1 parent 1dced78 commit d91a1eb

15 files changed

+1277
-23
lines changed

.changelog/8273.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
google_network_connectivity_service_connection_policy
3+
```

google/config_test_utils.go

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func configureTestBasePaths(c *transport_tpg.Config, url string) {
9090
c.MemcacheBasePath = url
9191
c.MLEngineBasePath = url
9292
c.MonitoringBasePath = url
93+
c.NetworkConnectivityBasePath = url
9394
c.NetworkManagementBasePath = url
9495
c.NetworkSecurityBasePath = url
9596
c.NetworkServicesBasePath = url

google/fwmodels/provider_model.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ type ProviderModel struct {
9191
MemcacheCustomEndpoint types.String `tfsdk:"memcache_custom_endpoint"`
9292
MLEngineCustomEndpoint types.String `tfsdk:"ml_engine_custom_endpoint"`
9393
MonitoringCustomEndpoint types.String `tfsdk:"monitoring_custom_endpoint"`
94+
NetworkConnectivityCustomEndpoint types.String `tfsdk:"network_connectivity_custom_endpoint"`
9495
NetworkManagementCustomEndpoint types.String `tfsdk:"network_management_custom_endpoint"`
9596
NetworkSecurityCustomEndpoint types.String `tfsdk:"network_security_custom_endpoint"`
9697
NetworkServicesCustomEndpoint types.String `tfsdk:"network_services_custom_endpoint"`
@@ -141,7 +142,6 @@ type ProviderModel struct {
141142
CloudResourceManagerCustomEndpoint types.String `tfsdk:"cloud_resource_manager_custom_endpoint"`
142143
EventarcCustomEndpoint types.String `tfsdk:"eventarc_custom_endpoint"`
143144
FirebaserulesCustomEndpoint types.String `tfsdk:"firebaserules_custom_endpoint"`
144-
NetworkConnectivityCustomEndpoint types.String `tfsdk:"network_connectivity_custom_endpoint"`
145145
OrgPolicyCustomEndpoint types.String `tfsdk:"org_policy_custom_endpoint"`
146146
RecaptchaEnterpriseCustomEndpoint types.String `tfsdk:"recaptcha_enterprise_custom_endpoint"`
147147
}

google/fwprovider/framework_provider.go

+6
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,12 @@ func (p *FrameworkProvider) Schema(_ context.Context, _ provider.SchemaRequest,
515515
transport_tpg.CustomEndpointValidator(),
516516
},
517517
},
518+
"network_connectivity_custom_endpoint": &schema.StringAttribute{
519+
Optional: true,
520+
Validators: []validator.String{
521+
transport_tpg.CustomEndpointValidator(),
522+
},
523+
},
518524
"network_management_custom_endpoint": &schema.StringAttribute{
519525
Optional: true,
520526
Validators: []validator.String{

google/fwtransport/framework_config.go

+10
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ type FrameworkProviderConfig struct {
115115
MemcacheBasePath string
116116
MLEngineBasePath string
117117
MonitoringBasePath string
118+
NetworkConnectivityBasePath string
118119
NetworkManagementBasePath string
119120
NetworkSecurityBasePath string
120121
NetworkServicesBasePath string
@@ -251,6 +252,7 @@ func (p *FrameworkProviderConfig) LoadAndValidateFramework(ctx context.Context,
251252
p.MemcacheBasePath = data.MemcacheCustomEndpoint.ValueString()
252253
p.MLEngineBasePath = data.MLEngineCustomEndpoint.ValueString()
253254
p.MonitoringBasePath = data.MonitoringCustomEndpoint.ValueString()
255+
p.NetworkConnectivityBasePath = data.NetworkConnectivityCustomEndpoint.ValueString()
254256
p.NetworkManagementBasePath = data.NetworkManagementCustomEndpoint.ValueString()
255257
p.NetworkSecurityBasePath = data.NetworkSecurityCustomEndpoint.ValueString()
256258
p.NetworkServicesBasePath = data.NetworkServicesCustomEndpoint.ValueString()
@@ -935,6 +937,14 @@ func (p *FrameworkProviderConfig) HandleDefaults(ctx context.Context, data *fwmo
935937
data.MonitoringCustomEndpoint = types.StringValue(customEndpoint.(string))
936938
}
937939
}
940+
if data.NetworkConnectivityCustomEndpoint.IsNull() {
941+
customEndpoint := transport_tpg.MultiEnvDefault([]string{
942+
"GOOGLE_NETWORK_CONNECTIVITY_CUSTOM_ENDPOINT",
943+
}, transport_tpg.DefaultBasePaths[transport_tpg.NetworkConnectivityBasePathKey])
944+
if customEndpoint != nil {
945+
data.NetworkConnectivityCustomEndpoint = types.StringValue(customEndpoint.(string))
946+
}
947+
}
938948
if data.NetworkManagementCustomEndpoint.IsNull() {
939949
customEndpoint := transport_tpg.MultiEnvDefault([]string{
940950
"GOOGLE_NETWORK_MANAGEMENT_CUSTOM_ENDPOINT",

google/gcp_sweeper_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import (
7474
_ "github.com/hashicorp/terraform-provider-google/google/services/memcache"
7575
_ "github.com/hashicorp/terraform-provider-google/google/services/mlengine"
7676
_ "github.com/hashicorp/terraform-provider-google/google/services/monitoring"
77+
_ "github.com/hashicorp/terraform-provider-google/google/services/networkconnectivity"
7778
_ "github.com/hashicorp/terraform-provider-google/google/services/networkmanagement"
7879
_ "github.com/hashicorp/terraform-provider-google/google/services/networksecurity"
7980
_ "github.com/hashicorp/terraform-provider-google/google/services/networkservices"

google/provider/provider.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import (
7979
"github.com/hashicorp/terraform-provider-google/google/services/memcache"
8080
"github.com/hashicorp/terraform-provider-google/google/services/mlengine"
8181
"github.com/hashicorp/terraform-provider-google/google/services/monitoring"
82+
"github.com/hashicorp/terraform-provider-google/google/services/networkconnectivity"
8283
"github.com/hashicorp/terraform-provider-google/google/services/networkmanagement"
8384
"github.com/hashicorp/terraform-provider-google/google/services/networksecurity"
8485
"github.com/hashicorp/terraform-provider-google/google/services/networkservices"
@@ -554,6 +555,11 @@ func Provider() *schema.Provider {
554555
Optional: true,
555556
ValidateFunc: transport_tpg.ValidateCustomEndpoint,
556557
},
558+
"network_connectivity_custom_endpoint": {
559+
Type: schema.TypeString,
560+
Optional: true,
561+
ValidateFunc: transport_tpg.ValidateCustomEndpoint,
562+
},
557563
"network_management_custom_endpoint": {
558564
Type: schema.TypeString,
559565
Optional: true,
@@ -953,9 +959,9 @@ func DatasourceMapWithErrors() (map[string]*schema.Resource, error) {
953959
})
954960
}
955961

956-
// Generated resources: 305
962+
// Generated resources: 306
957963
// Generated IAM resources: 204
958-
// Total generated resources: 509
964+
// Total generated resources: 510
959965
func ResourceMap() map[string]*schema.Resource {
960966
resourceMap, _ := ResourceMapWithErrors()
961967
return resourceMap
@@ -1363,6 +1369,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
13631369
"google_monitoring_custom_service": monitoring.ResourceMonitoringService(),
13641370
"google_monitoring_slo": monitoring.ResourceMonitoringSlo(),
13651371
"google_monitoring_uptime_check_config": monitoring.ResourceMonitoringUptimeCheckConfig(),
1372+
"google_network_connectivity_service_connection_policy": networkconnectivity.ResourceNetworkConnectivityServiceConnectionPolicy(),
13661373
"google_network_management_connectivity_test": networkmanagement.ResourceNetworkManagementConnectivityTest(),
13671374
"google_network_security_address_group": networksecurity.ResourceNetworkSecurityAddressGroup(),
13681375
"google_network_security_gateway_security_policy": networksecurity.ResourceNetworkSecurityGatewaySecurityPolicy(),
@@ -1764,6 +1771,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr
17641771
config.MemcacheBasePath = d.Get("memcache_custom_endpoint").(string)
17651772
config.MLEngineBasePath = d.Get("ml_engine_custom_endpoint").(string)
17661773
config.MonitoringBasePath = d.Get("monitoring_custom_endpoint").(string)
1774+
config.NetworkConnectivityBasePath = d.Get("network_connectivity_custom_endpoint").(string)
17671775
config.NetworkManagementBasePath = d.Get("network_management_custom_endpoint").(string)
17681776
config.NetworkSecurityBasePath = d.Get("network_security_custom_endpoint").(string)
17691777
config.NetworkServicesBasePath = d.Get("network_services_custom_endpoint").(string)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package google
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-provider-google/google/acctest"
11+
)
12+
13+
func TestAccNetworkConnectivityServiceConnectionPolicy_update(t *testing.T) {
14+
t.Parallel()
15+
16+
context := map[string]interface{}{
17+
"networkProducerName": fmt.Sprintf("tf-test-network-%s", RandString(t, 10)),
18+
"subnetworkProducerName1": fmt.Sprintf("tf-test-subnet-producer-%s", RandString(t, 10)),
19+
"subnetworkProducerName2": fmt.Sprintf("tf-test-subnet-producer-%s", RandString(t, 10)),
20+
"serviceConnectionPolicyName": fmt.Sprintf("tf-test-service-connection-policy-%s", RandString(t, 10)),
21+
}
22+
23+
VcrTest(t, resource.TestCase{
24+
PreCheck: func() { acctest.AccTestPreCheck(t) },
25+
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
26+
CheckDestroy: testAccCheckNetworkConnectivityServiceConnectionPolicyDestroyProducer(t),
27+
Steps: []resource.TestStep{
28+
{
29+
Config: testAccNetworkConnectivityServiceConnectionPolicy_basic(context),
30+
},
31+
{
32+
ResourceName: "google_network_connectivity_service_connection_policy.default",
33+
ImportState: true,
34+
ImportStateVerify: true,
35+
},
36+
{
37+
Config: testAccNetworkConnectivityServiceConnectionPolicy_update(context),
38+
},
39+
{
40+
ResourceName: "google_network_connectivity_service_connection_policy.default",
41+
ImportState: true,
42+
ImportStateVerify: true,
43+
},
44+
{
45+
Config: testAccNetworkConnectivityServiceConnectionPolicy_basic(context),
46+
},
47+
{
48+
ResourceName: "google_network_connectivity_service_connection_policy.default",
49+
ImportState: true,
50+
ImportStateVerify: true,
51+
},
52+
},
53+
})
54+
}
55+
56+
func testAccNetworkConnectivityServiceConnectionPolicy_basic(context map[string]interface{}) string {
57+
return acctest.Nprintf(`
58+
resource "google_compute_network" "producer_net" {
59+
name = "%{networkProducerName}"
60+
auto_create_subnetworks = false
61+
}
62+
63+
resource "google_compute_subnetwork" "producer_subnet" {
64+
name = "%{subnetworkProducerName1}"
65+
ip_cidr_range = "10.0.0.0/16"
66+
region = "us-central1"
67+
network = google_compute_network.producer_net.id
68+
}
69+
70+
resource "google_network_connectivity_service_connection_policy" "default" {
71+
name = "%{serviceConnectionPolicyName}"
72+
location = "us-central1"
73+
service_class = "gcp-memorystore-redis"
74+
network = google_compute_network.producer_net.id
75+
psc_config {
76+
subnetworks = [google_compute_subnetwork.producer_subnet.id]
77+
limit = 2
78+
}
79+
}
80+
`, context)
81+
}
82+
83+
func testAccNetworkConnectivityServiceConnectionPolicy_update(context map[string]interface{}) string {
84+
return acctest.Nprintf(`
85+
resource "google_compute_network" "producer_net" {
86+
name = "%{networkProducerName}"
87+
auto_create_subnetworks = false
88+
}
89+
90+
resource "google_compute_subnetwork" "producer_subnet1" {
91+
name = "%{subnetworkProducerName2}"
92+
ip_cidr_range = "10.1.0.0/16"
93+
region = "us-central1"
94+
network = google_compute_network.producer_net.id
95+
}
96+
97+
resource "google_network_connectivity_service_connection_policy" "default" {
98+
name = "%{serviceConnectionPolicyName}"
99+
location = "us-central1"
100+
service_class = "gcp-memorystore-redis"
101+
network = google_compute_network.producer_net.id
102+
psc_config {
103+
subnetworks = [google_compute_subnetwork.producer_subnet1.id]
104+
limit = 4
105+
}
106+
labels = {
107+
foo = "bar"
108+
}
109+
}
110+
`, context)
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
// ----------------------------------------------------------------------------
5+
//
6+
// *** AUTO GENERATED CODE *** Type: MMv1 ***
7+
//
8+
// ----------------------------------------------------------------------------
9+
//
10+
// This file is automatically generated by Magic Modules and manual
11+
// changes will be clobbered when the file is regenerated.
12+
//
13+
// Please read more about how to change this file in
14+
// .github/CONTRIBUTING.md.
15+
//
16+
// ----------------------------------------------------------------------------
17+
18+
package google
19+
20+
import (
21+
"fmt"
22+
"strings"
23+
"testing"
24+
25+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
26+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
27+
28+
"github.com/hashicorp/terraform-provider-google/google/acctest"
29+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
30+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
31+
)
32+
33+
func TestAccNetworkConnectivityServiceConnectionPolicy_networkConnectivityPolicyBasicExample(t *testing.T) {
34+
t.Parallel()
35+
36+
context := map[string]interface{}{
37+
"service_class_name": "gcp-memorystore-redis",
38+
"random_suffix": acctest.RandString(t, 10),
39+
}
40+
41+
acctest.VcrTest(t, resource.TestCase{
42+
PreCheck: func() { acctest.AccTestPreCheck(t) },
43+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
44+
CheckDestroy: testAccCheckNetworkConnectivityServiceConnectionPolicyDestroyProducer(t),
45+
Steps: []resource.TestStep{
46+
{
47+
Config: testAccNetworkConnectivityServiceConnectionPolicy_networkConnectivityPolicyBasicExample(context),
48+
},
49+
{
50+
ResourceName: "google_network_connectivity_service_connection_policy.default",
51+
ImportState: true,
52+
ImportStateVerify: true,
53+
ImportStateVerifyIgnore: []string{"name", "location"},
54+
},
55+
},
56+
})
57+
}
58+
59+
func testAccNetworkConnectivityServiceConnectionPolicy_networkConnectivityPolicyBasicExample(context map[string]interface{}) string {
60+
return acctest.Nprintf(`
61+
resource "google_compute_network" "producer_net" {
62+
name = "tf-test-producer-net%{random_suffix}"
63+
auto_create_subnetworks = false
64+
}
65+
66+
resource "google_compute_subnetwork" "producer_subnet" {
67+
name = "tf-test-producer-subnet%{random_suffix}"
68+
ip_cidr_range = "10.0.0.0/16"
69+
region = "us-central1"
70+
network = google_compute_network.producer_net.id
71+
}
72+
73+
resource "google_network_connectivity_service_connection_policy" "default" {
74+
name = "tf-test-my-network-connectivity-policy%{random_suffix}"
75+
location = "us-central1"
76+
service_class = "%{service_class_name}"
77+
description = "my basic service connection policy"
78+
network = google_compute_network.producer_net.id
79+
psc_config {
80+
subnetworks = [google_compute_subnetwork.producer_subnet.id]
81+
limit = 2
82+
}
83+
}
84+
`, context)
85+
}
86+
87+
func testAccCheckNetworkConnectivityServiceConnectionPolicyDestroyProducer(t *testing.T) func(s *terraform.State) error {
88+
return func(s *terraform.State) error {
89+
for name, rs := range s.RootModule().Resources {
90+
if rs.Type != "google_network_connectivity_service_connection_policy" {
91+
continue
92+
}
93+
if strings.HasPrefix(name, "data.") {
94+
continue
95+
}
96+
97+
config := acctest.GoogleProviderConfig(t)
98+
99+
url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{NetworkConnectivityBasePath}}projects/{{project}}/locations/{{location}}/serviceConnectionPolicies/{{name}}")
100+
if err != nil {
101+
return err
102+
}
103+
104+
billingProject := ""
105+
106+
if config.BillingProject != "" {
107+
billingProject = config.BillingProject
108+
}
109+
110+
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
111+
Config: config,
112+
Method: "GET",
113+
Project: billingProject,
114+
RawURL: url,
115+
UserAgent: config.UserAgent,
116+
})
117+
if err == nil {
118+
return fmt.Errorf("NetworkConnectivityServiceConnectionPolicy still exists at %s", url)
119+
}
120+
}
121+
122+
return nil
123+
}
124+
}

0 commit comments

Comments
 (0)