|
4 | 4 | "testing"
|
5 | 5 |
|
6 | 6 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
|
| 7 | + "google.golang.org/api/iam/v1" |
7 | 8 | )
|
8 | 9 |
|
9 | 10 | func TestAccCloudIdentityGroupMembership_update(t *testing.T) {
|
@@ -174,3 +175,87 @@ resource "google_cloud_identity_group_membership" "basic" {
|
174 | 175 | }
|
175 | 176 | `, context)
|
176 | 177 | }
|
| 178 | + |
| 179 | +func TestAccCloudIdentityGroupMembership_membershipDoesNotExist(t *testing.T) { |
| 180 | + // Skip VCR because the service account needs to be created/deleted out of |
| 181 | + // band, and so those calls aren't recorded |
| 182 | + skipIfVcr(t) |
| 183 | + t.Parallel() |
| 184 | + |
| 185 | + context := map[string]interface{}{ |
| 186 | + "org_domain": getTestOrgDomainFromEnv(t), |
| 187 | + "cust_id": getTestCustIdFromEnv(t), |
| 188 | + "random_suffix": randString(t, 10), |
| 189 | + } |
| 190 | + |
| 191 | + saId := "tf-test-sa-" + randString(t, 10) |
| 192 | + project := getTestProjectFromEnv() |
| 193 | + config := BootstrapConfig(t) |
| 194 | + |
| 195 | + r := &iam.CreateServiceAccountRequest{ |
| 196 | + AccountId: saId, |
| 197 | + ServiceAccount: &iam.ServiceAccount{}, |
| 198 | + } |
| 199 | + |
| 200 | + sa, err := config.NewIamClient(config.userAgent).Projects.ServiceAccounts.Create("projects/"+project, r).Do() |
| 201 | + if err != nil { |
| 202 | + t.Fatalf("Error creating service account: %s", err) |
| 203 | + } |
| 204 | + |
| 205 | + context["member_id"] = sa.Email |
| 206 | + |
| 207 | + vcrTest(t, resource.TestCase{ |
| 208 | + PreCheck: func() { testAccPreCheck(t) }, |
| 209 | + Providers: testAccProviders, |
| 210 | + CheckDestroy: testAccCheckCloudIdentityGroupMembershipDestroyProducer(t), |
| 211 | + Steps: []resource.TestStep{ |
| 212 | + { |
| 213 | + Config: testAccCloudIdentityGroupMembership_dne(context), |
| 214 | + }, |
| 215 | + { |
| 216 | + PreConfig: func() { |
| 217 | + config := googleProviderConfig(t) |
| 218 | + |
| 219 | + _, err := config.NewIamClient(config.userAgent).Projects.ServiceAccounts.Delete(sa.Name).Do() |
| 220 | + if err != nil { |
| 221 | + t.Errorf("cannot delete service account %s: %v", sa.Name, err) |
| 222 | + return |
| 223 | + } |
| 224 | + }, |
| 225 | + Config: testAccCloudIdentityGroupMembership_dne(context), |
| 226 | + PlanOnly: true, |
| 227 | + ExpectNonEmptyPlan: true, |
| 228 | + }, |
| 229 | + }, |
| 230 | + }) |
| 231 | +} |
| 232 | + |
| 233 | +func testAccCloudIdentityGroupMembership_dne(context map[string]interface{}) string { |
| 234 | + return Nprintf(` |
| 235 | +resource "google_cloud_identity_group" "group" { |
| 236 | + display_name = "tf-test-my-identity-group-%{random_suffix}" |
| 237 | +
|
| 238 | + parent = "customers/%{cust_id}" |
| 239 | +
|
| 240 | + group_key { |
| 241 | + id = "tf-test-my-identity-group-%{random_suffix}@%{org_domain}" |
| 242 | + } |
| 243 | +
|
| 244 | + labels = { |
| 245 | + "cloudidentity.googleapis.com/groups.discussion_forum" = "" |
| 246 | + } |
| 247 | +} |
| 248 | +
|
| 249 | +resource "google_cloud_identity_group_membership" "basic" { |
| 250 | + group = google_cloud_identity_group.group.id |
| 251 | +
|
| 252 | + preferred_member_key { |
| 253 | + id = "%{member_id}" |
| 254 | + } |
| 255 | +
|
| 256 | + roles { |
| 257 | + name = "MEMBER" |
| 258 | + } |
| 259 | +} |
| 260 | +`, context) |
| 261 | +} |
0 commit comments