Skip to content

Commit 22008e5

Browse files
CodeRepositoryIndex cascade deletion (#12601)
Co-authored-by: Nick Elliot <[email protected]>
1 parent 27bceb0 commit 22008e5

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

mmv1/products/gemini/CodeRepositoryIndex.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,18 @@ async:
5151
result:
5252
resource_inside_response: true
5353
include_project: false
54+
custom_code:
55+
pre_delete: templates/terraform/pre_delete/code_repository_index_force_delete.go.tmpl
5456
error_retry_predicates:
5557
- 'transport_tpg.IsCodeRepositoryIndexUnreadyError'
5658
- 'transport_tpg.IsRepositoryGroupQueueError'
59+
virtual_fields:
60+
- name: 'force_destroy'
61+
description:
62+
If set to true, will allow deletion of the CodeRepositoryIndex even if there are existing
63+
RepositoryGroups for the resource. These RepositoryGroups will also be deleted.
64+
type: Boolean
65+
default_value: false
5766
parameters:
5867
- name: location
5968
type: String
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{{- if ne $.TargetVersionName "ga" -}}
2+
obj = make(map[string]interface{})
3+
if v, ok := d.GetOk("force_destroy"); ok {
4+
if v == true {
5+
obj["force"] = true
6+
}
7+
}
8+
{{- end }}

mmv1/third_party/terraform/services/gemini/resource_gemini_code_repository_index_test.go.tmpl

+131
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gemini_test
22
{{- if ne $.TargetVersionName "ga" }}
33

44
import (
5+
"fmt"
56
"os"
67
"testing"
78

@@ -44,6 +45,136 @@ func TestAccGeminiCodeRepositoryIndex_update(t *testing.T) {
4445
})
4546
}
4647

48+
// TestAccGeminiCodeRepositoryIndex_delete checks if there is no error in deleting CRI along with children resource
49+
// note: this is an example of a bad usage, where RGs refer to the CRI using a string id, not a reference, as they
50+
// will be force-removed upon CRI deletion, because the CRI provider uses --force option by default
51+
// The plan after the _delete function should not be empty due to the child resource in plan
52+
func TestAccGeminiCodeRepositoryIndex_delete(t *testing.T) {
53+
bootstrappedKMS := acctest.BootstrapKMSKeyInLocation(t, "us-central1")
54+
randomSuffix := acctest.RandString(t, 10)
55+
context := map[string]interface{}{
56+
"random_suffix": randomSuffix,
57+
"project_id": os.Getenv("GOOGLE_PROJECT"),
58+
"kms_key": bootstrappedKMS.CryptoKey.Name,
59+
"cri_id": fmt.Sprintf("tf-test-cri-index-delete-example-%s", randomSuffix),
60+
}
61+
62+
acctest.VcrTest(t, resource.TestCase{
63+
PreCheck: func() { acctest.AccTestPreCheck(t) },
64+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
65+
Steps: []resource.TestStep{
66+
{
67+
Config: testAccGeminiCodeRepositoryIndex_withChildren_basic(context),
68+
},
69+
{
70+
ResourceName: "google_gemini_code_repository_index.example",
71+
ImportState: true,
72+
ImportStateVerify: true,
73+
ImportStateVerifyIgnore: []string{"code_repository_index_id", "labels", "location", "terraform_labels", "force_destroy"},
74+
},
75+
{
76+
Config: testAccGeminiCodeRepositoryIndex_withChildren_delete(context),
77+
ExpectNonEmptyPlan: true,
78+
PlanOnly: true,
79+
},
80+
},
81+
})
82+
}
83+
84+
func testAccGeminiCodeRepositoryIndex_withChildren_basic(context map[string]interface{}) string {
85+
return acctest.Nprintf(`
86+
resource "google_gemini_code_repository_index" "example" {
87+
provider = google-beta
88+
labels = {"ccfe_debug_note": "terraform_e2e_should_be_deleted"}
89+
location = "us-central1"
90+
code_repository_index_id = "%{cri_id}"
91+
force_destroy = true
92+
}
93+
94+
resource "google_gemini_repository_group" "example" {
95+
provider = google-beta
96+
location = "us-central1"
97+
code_repository_index = "%{cri_id}"
98+
repository_group_id = "tf-test-rg-repository-group-id-%{random_suffix}"
99+
repositories {
100+
resource = "projects/%{project_id}/locations/us-central1/connections/${google_developer_connect_connection.github_conn.connection_id}/gitRepositoryLinks/${google_developer_connect_git_repository_link.conn.git_repository_link_id}"
101+
branch_pattern = "main"
102+
}
103+
labels = {"label1": "value1"}
104+
depends_on = [
105+
google_gemini_code_repository_index.example
106+
]
107+
}
108+
109+
resource "google_developer_connect_git_repository_link" "conn" {
110+
provider = google-beta
111+
git_repository_link_id = "tf-test-repository-conn-delete"
112+
parent_connection = google_developer_connect_connection.github_conn.connection_id
113+
clone_uri = "https://github.com/CC-R-github-robot/tf-test.git"
114+
location = "us-central1"
115+
annotations = {}
116+
}
117+
118+
resource "google_developer_connect_connection" "github_conn" {
119+
provider = google-beta
120+
location = "us-central1"
121+
connection_id = "tf-test-cloudaicompanion-delete-%{random_suffix}"
122+
disabled = false
123+
124+
github_config {
125+
github_app = "DEVELOPER_CONNECT"
126+
app_installation_id = 54180648
127+
128+
authorizer_credential {
129+
oauth_token_secret_version = "projects/502367051001/secrets/tf-test-cloudaicompanion-github-oauthtoken-c42e5c/versions/1"
130+
}
131+
}
132+
}
133+
`, context)
134+
}
135+
136+
// Removed depends_on to not break plan test
137+
func testAccGeminiCodeRepositoryIndex_withChildren_delete(context map[string]interface{}) string {
138+
return acctest.Nprintf(`
139+
resource "google_gemini_repository_group" "example" {
140+
provider = google-beta
141+
location = "us-central1"
142+
code_repository_index = "%{cri_id}"
143+
repository_group_id = "tf-test-rg-repository-group-id-%{random_suffix}"
144+
repositories {
145+
resource = "projects/%{project_id}/locations/us-central1/connections/${google_developer_connect_connection.github_conn.connection_id}/gitRepositoryLinks/${google_developer_connect_git_repository_link.conn.git_repository_link_id}"
146+
branch_pattern = "main"
147+
}
148+
labels = {"label1": "value1"}
149+
}
150+
151+
resource "google_developer_connect_git_repository_link" "conn" {
152+
provider = google-beta
153+
git_repository_link_id = "tf-test-repository-conn-delete"
154+
parent_connection = google_developer_connect_connection.github_conn.connection_id
155+
clone_uri = "https://github.com/CC-R-github-robot/tf-test.git"
156+
location = "us-central1"
157+
annotations = {}
158+
}
159+
160+
resource "google_developer_connect_connection" "github_conn" {
161+
provider = google-beta
162+
location = "us-central1"
163+
connection_id = "tf-test-cloudaicompanion-delete-%{random_suffix}"
164+
disabled = false
165+
166+
github_config {
167+
github_app = "DEVELOPER_CONNECT"
168+
app_installation_id = 54180648
169+
170+
authorizer_credential {
171+
oauth_token_secret_version = "projects/502367051001/secrets/tf-test-cloudaicompanion-github-oauthtoken-c42e5c/versions/1"
172+
}
173+
}
174+
}
175+
`, context)
176+
}
177+
47178
func testAccGeminiCodeRepositoryIndex_basic(context map[string]interface{}) string {
48179
return acctest.Nprintf(`
49180
resource "google_gemini_code_repository_index" "example" {

0 commit comments

Comments
 (0)