Skip to content

Commit d9422e5

Browse files
Added abandon policy (#9765) (#6830)
* Added abandon policy * Fixed linting errors on line 68 and 262 * added test coverage * Fixed test case to pass if network is destroyed successfully * Disabled testing for state import. Deletion policy isn't an API attribute so it will never import. * Added documentation for the additional argument [upstream:76d03c3ad1652dc12fb5f738a907719aa01e0d8e] Signed-off-by: Modular Magician <[email protected]>
1 parent ba796fa commit d9422e5

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

.changelog/9765.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
service: added field `deletion_policy` to `google_service_networking_connection`
3+
```

google-beta/services/servicenetworking/resource_service_networking_connection.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/hashicorp/errwrap"
1717
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1819
"google.golang.org/api/servicenetworking/v1"
1920
)
2021

@@ -60,6 +61,12 @@ func ResourceServiceNetworkingConnection() *schema.Resource {
6061
Elem: &schema.Schema{Type: schema.TypeString},
6162
Description: `Named IP address range(s) of PEERING type reserved for this service provider. Note that invoking this method with a different range when connection is already established will not reallocate already provisioned service producer subnetworks.`,
6263
},
64+
"deletion_policy": {
65+
Type: schema.TypeString,
66+
Optional: true,
67+
ValidateFunc: validation.StringInSlice([]string{"ABANDON", ""}, false),
68+
Description: `When set to ABANDON, terraform will abandon management of the resource instead of deleting it. Prevents terraform apply failures with CloudSQL. Note: The resource will still exist.`,
69+
},
6370
"peering": {
6471
Type: schema.TypeString,
6572
Computed: true,
@@ -250,6 +257,12 @@ func resourceServiceNetworkingConnectionUpdate(d *schema.ResourceData, meta inte
250257

251258
func resourceServiceNetworkingConnectionDelete(d *schema.ResourceData, meta interface{}) error {
252259
config := meta.(*transport_tpg.Config)
260+
261+
if deletionPolicy := d.Get("deletion_policy"); deletionPolicy == "ABANDON" {
262+
log.Printf("[WARN] The service networking connection has been abandoned")
263+
return nil
264+
}
265+
253266
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
254267
if err != nil {
255268
return err

google-beta/services/servicenetworking/resource_service_networking_connection_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,32 @@ func TestAccServiceNetworkingConnection_create(t *testing.T) {
3838
})
3939
}
4040

41+
func TestAccServiceNetworkingConnection_abandon(t *testing.T) {
42+
t.Parallel()
43+
44+
network := fmt.Sprintf("tf-test-service-networking-connection-abandon-%s", acctest.RandString(t, 10))
45+
addr := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
46+
service := "servicenetworking.googleapis.com"
47+
org_id := envvar.GetTestOrgFromEnv(t)
48+
billing_account := envvar.GetTestBillingAccountFromEnv(t)
49+
50+
acctest.VcrTest(t, resource.TestCase{
51+
PreCheck: func() { acctest.AccTestPreCheck(t) },
52+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
53+
CheckDestroy: testServiceNetworkingConnectionDestroyAbandon(t, service, network),
54+
Steps: []resource.TestStep{
55+
{
56+
Config: testAccServiceNetworkingConnectionToBeAbandoned(network, addr, "servicenetworking.googleapis.com", org_id, billing_account),
57+
},
58+
{
59+
ResourceName: "google_service_networking_connection.foobar",
60+
ImportState: true,
61+
ImportStateVerify: false,
62+
},
63+
},
64+
})
65+
}
66+
4167
func TestAccServiceNetworkingConnection_update(t *testing.T) {
4268
t.Parallel()
4369

@@ -98,6 +124,30 @@ func testServiceNetworkingConnectionDestroy(t *testing.T, parent, network string
98124
}
99125
}
100126

127+
func testServiceNetworkingConnectionDestroyAbandon(t *testing.T, parent, network string) resource.TestCheckFunc {
128+
return func(s *terraform.State) error {
129+
config := acctest.GoogleProviderConfig(t)
130+
parentService := "services/" + parent
131+
networkName := fmt.Sprintf("projects/%s/global/networks/%s", envvar.GetTestProjectFromEnv(), network)
132+
listCall := config.NewServiceNetworkingClient(config.UserAgent).Services.Connections.List(parentService).Network(networkName)
133+
if config.UserProjectOverride {
134+
listCall.Header().Add("X-Goog-User-Project", envvar.GetTestProjectFromEnv())
135+
}
136+
response, err := listCall.Do()
137+
if err != nil {
138+
return err
139+
}
140+
141+
for _, c := range response.Connections {
142+
if c.Network == networkName {
143+
return fmt.Errorf("Found %s which should have been destroyed.", networkName)
144+
}
145+
}
146+
147+
return nil
148+
}
149+
}
150+
101151
func testAccServiceNetworkingConnection(networkName, addressRangeName, serviceName, org_id, billing_account string) string {
102152
return fmt.Sprintf(`
103153
resource "google_project" "project" {
@@ -134,3 +184,41 @@ resource "google_service_networking_connection" "foobar" {
134184
}
135185
`, addressRangeName, addressRangeName, org_id, billing_account, networkName, addressRangeName, serviceName)
136186
}
187+
188+
func testAccServiceNetworkingConnectionToBeAbandoned(networkName, addressRangeName, serviceName, org_id, billing_account string) string {
189+
return fmt.Sprintf(`
190+
resource "google_project" "project" {
191+
project_id = "%s"
192+
name = "%s"
193+
org_id = "%s"
194+
billing_account = "%s"
195+
}
196+
197+
resource "google_project_service" "servicenetworking" {
198+
project = google_project.project.project_id
199+
service = "servicenetworking.googleapis.com"
200+
}
201+
202+
resource "google_compute_network" "servicenet" {
203+
name = "%s"
204+
depends_on = [google_project_service.servicenetworking]
205+
}
206+
207+
resource "google_compute_global_address" "foobar" {
208+
name = "%s"
209+
purpose = "VPC_PEERING"
210+
address_type = "INTERNAL"
211+
prefix_length = 16
212+
network = google_compute_network.servicenet.self_link
213+
depends_on = [google_project_service.servicenetworking]
214+
}
215+
216+
resource "google_service_networking_connection" "foobar" {
217+
network = google_compute_network.servicenet.self_link
218+
service = "%s"
219+
reserved_peering_ranges = [google_compute_global_address.foobar.name]
220+
depends_on = [google_project_service.servicenetworking]
221+
deletion_policy = "ABANDON"
222+
}
223+
`, addressRangeName, addressRangeName, org_id, billing_account, networkName, addressRangeName, serviceName)
224+
}

website/docs/r/service_networking_connection.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ The following arguments are supported:
5959
this service provider. Note that invoking this method with a different range when connection
6060
is already established will not reallocate already provisioned service producer subnetworks.
6161

62+
* `deletion_policy` - (Optional) The deletion policy for the service networking connection. Setting to ABANDON allows the resource to be abandoned rather than deleted. This will enable a successful terraform destroy when destroying CloudSQL instances. Use with care as it can lead to dangling resources.
63+
6264
## Attributes Reference
6365

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

0 commit comments

Comments
 (0)