@@ -1101,6 +1101,54 @@ func TestAccComputeInstance_serviceAccount(t *testing.T) {
1101
1101
})
1102
1102
}
1103
1103
1104
+ func TestAccComputeInstance_noServiceAccount (t * testing.T ) {
1105
+ t .Parallel ()
1106
+
1107
+ var instance compute.Instance
1108
+ var instanceName = fmt .Sprintf ("tf-test-%s" , acctest .RandString (t , 10 ))
1109
+
1110
+ acctest .VcrTest (t , resource.TestCase {
1111
+ PreCheck : func () { acctest .AccTestPreCheck (t ) },
1112
+ ProtoV5ProviderFactories : acctest .ProtoV5ProviderFactories (t ),
1113
+ CheckDestroy : testAccCheckComputeInstanceDestroyProducer (t ),
1114
+ Steps : []resource.TestStep {
1115
+ {
1116
+ Config : testAccComputeInstance_noServiceAccount (instanceName ),
1117
+ Check : resource .ComposeTestCheckFunc (
1118
+ testAccCheckComputeInstanceExists (
1119
+ t , "google_compute_instance.foobar" , & instance ),
1120
+ testAccCheckComputeInstanceNoServiceAccount (& instance ),
1121
+ ),
1122
+ },
1123
+ computeInstanceImportStep ("us-central1-a" , instanceName , []string {}),
1124
+ },
1125
+ })
1126
+ }
1127
+
1128
+ func TestAccComputeInstance_serviceAccountEmail_0scopes (t * testing.T ) {
1129
+ t .Parallel ()
1130
+
1131
+ var instance compute.Instance
1132
+ var instanceName = fmt .Sprintf ("tf-test-%s" , acctest .RandString (t , 10 ))
1133
+
1134
+ acctest .VcrTest (t , resource.TestCase {
1135
+ PreCheck : func () { acctest .AccTestPreCheck (t ) },
1136
+ ProtoV5ProviderFactories : acctest .ProtoV5ProviderFactories (t ),
1137
+ CheckDestroy : testAccCheckComputeInstanceDestroyProducer (t ),
1138
+ Steps : []resource.TestStep {
1139
+ {
1140
+ Config : testAccComputeInstance_serviceAccountEmail_0scopes (instanceName ),
1141
+ Check : resource .ComposeTestCheckFunc (
1142
+ testAccCheckComputeInstanceExists (
1143
+ t , "google_compute_instance.foobar" , & instance ),
1144
+ testAccCheckComputeInstanceMatchServiceAccount (& instance , "\\ d+-compute@developer.gserviceaccount.com" ),
1145
+ ),
1146
+ },
1147
+ computeInstanceImportStep ("us-central1-a" , instanceName , []string {}),
1148
+ },
1149
+ })
1150
+ }
1151
+
1104
1152
func TestAccComputeInstance_serviceAccount_updated (t * testing.T ) {
1105
1153
t .Parallel ()
1106
1154
@@ -1117,6 +1165,7 @@ func TestAccComputeInstance_serviceAccount_updated(t *testing.T) {
1117
1165
Check : resource .ComposeTestCheckFunc (
1118
1166
testAccCheckComputeInstanceExists (
1119
1167
t , "google_compute_instance.foobar" , & instance ),
1168
+ testAccCheckComputeInstanceNoServiceAccount (& instance ),
1120
1169
testAccCheckComputeInstanceScopes (& instance , 0 ),
1121
1170
),
1122
1171
},
@@ -1126,6 +1175,7 @@ func TestAccComputeInstance_serviceAccount_updated(t *testing.T) {
1126
1175
Check : resource .ComposeTestCheckFunc (
1127
1176
testAccCheckComputeInstanceExists (
1128
1177
t , "google_compute_instance.foobar" , & instance ),
1178
+ testAccCheckComputeInstanceNoServiceAccount (& instance ),
1129
1179
testAccCheckComputeInstanceScopes (& instance , 0 ),
1130
1180
),
1131
1181
},
@@ -1135,6 +1185,7 @@ func TestAccComputeInstance_serviceAccount_updated(t *testing.T) {
1135
1185
Check : resource .ComposeTestCheckFunc (
1136
1186
testAccCheckComputeInstanceExists (
1137
1187
t , "google_compute_instance.foobar" , & instance ),
1188
+ testAccCheckComputeInstanceMatchServiceAccount (& instance , "\\ d+-compute@developer.gserviceaccount.com" ),
1138
1189
testAccCheckComputeInstanceScopes (& instance , 0 ),
1139
1190
),
1140
1191
},
@@ -1144,6 +1195,7 @@ func TestAccComputeInstance_serviceAccount_updated(t *testing.T) {
1144
1195
Check : resource .ComposeTestCheckFunc (
1145
1196
testAccCheckComputeInstanceExists (
1146
1197
t , "google_compute_instance.foobar" , & instance ),
1198
+ testAccCheckComputeInstanceMatchServiceAccount (& instance , "\\ d+-compute@developer.gserviceaccount.com" ),
1147
1199
testAccCheckComputeInstanceScopes (& instance , 3 ),
1148
1200
),
1149
1201
},
@@ -1168,6 +1220,7 @@ func TestAccComputeInstance_serviceAccount_updated0to1to0scopes(t *testing.T) {
1168
1220
Check : resource .ComposeTestCheckFunc (
1169
1221
testAccCheckComputeInstanceExists (
1170
1222
t , "google_compute_instance.foobar" , & instance ),
1223
+ testAccCheckComputeInstanceNoServiceAccount (& instance ),
1171
1224
testAccCheckComputeInstanceScopes (& instance , 0 ),
1172
1225
),
1173
1226
},
@@ -1177,6 +1230,7 @@ func TestAccComputeInstance_serviceAccount_updated0to1to0scopes(t *testing.T) {
1177
1230
Check : resource .ComposeTestCheckFunc (
1178
1231
testAccCheckComputeInstanceExists (
1179
1232
t , "google_compute_instance.foobar" , & instance ),
1233
+ testAccCheckComputeInstanceMatchServiceAccount (& instance , "\\ d+-compute@developer.gserviceaccount.com" ),
1180
1234
testAccCheckComputeInstanceScopes (& instance , 1 ),
1181
1235
),
1182
1236
},
@@ -1186,6 +1240,7 @@ func TestAccComputeInstance_serviceAccount_updated0to1to0scopes(t *testing.T) {
1186
1240
Check : resource .ComposeTestCheckFunc (
1187
1241
testAccCheckComputeInstanceExists (
1188
1242
t , "google_compute_instance.foobar" , & instance ),
1243
+ testAccCheckComputeInstanceNoServiceAccount (& instance ),
1189
1244
testAccCheckComputeInstanceScopes (& instance , 0 ),
1190
1245
),
1191
1246
},
@@ -3306,6 +3361,30 @@ func testAccCheckComputeInstanceServiceAccount(instance *compute.Instance, scope
3306
3361
}
3307
3362
}
3308
3363
3364
+ func testAccCheckComputeInstanceNoServiceAccount (instance * compute.Instance ) resource.TestCheckFunc {
3365
+ return func (s * terraform.State ) error {
3366
+ if count := len (instance .ServiceAccounts ); count != 0 {
3367
+ return fmt .Errorf ("Wrong number of ServiceAccounts: expected 0, got %d" , count )
3368
+ }
3369
+ return nil
3370
+ }
3371
+ }
3372
+
3373
+ func testAccCheckComputeInstanceMatchServiceAccount (instance * compute.Instance , serviceAcctRegexp string ) resource.TestCheckFunc {
3374
+ return func (s * terraform.State ) error {
3375
+ if count := len (instance .ServiceAccounts ); count != 1 {
3376
+ return fmt .Errorf ("Wrong number of ServiceAccounts: expected 1, got %d" , count )
3377
+ }
3378
+
3379
+ email := instance .ServiceAccounts [0 ].Email
3380
+ if ! regexp .MustCompile (serviceAcctRegexp ).MatchString (email ) {
3381
+ return fmt .Errorf ("ServiceAccount email didn't match:\" %s\" , got \" %s\" " , serviceAcctRegexp , email )
3382
+ }
3383
+
3384
+ return nil
3385
+ }
3386
+ }
3387
+
3309
3388
func testAccCheckComputeInstanceScopes (instance * compute.Instance , scopeCount int ) resource.TestCheckFunc {
3310
3389
return func (s * terraform.State ) error {
3311
3390
@@ -5277,6 +5356,70 @@ resource "google_compute_instance" "foobar" {
5277
5356
` , instance )
5278
5357
}
5279
5358
5359
+ func testAccComputeInstance_noServiceAccount (instance string ) string {
5360
+ return fmt .Sprintf (`
5361
+ data "google_compute_image" "my_image" {
5362
+ family = "debian-11"
5363
+ project = "debian-cloud"
5364
+ }
5365
+
5366
+ resource "google_compute_instance" "foobar" {
5367
+ name = "%s"
5368
+ machine_type = "e2-medium"
5369
+ zone = "us-central1-a"
5370
+
5371
+ boot_disk {
5372
+ initialize_params {
5373
+ image = data.google_compute_image.my_image.self_link
5374
+ }
5375
+ }
5376
+
5377
+ network_interface {
5378
+ network = "default"
5379
+ }
5380
+
5381
+ service_account {
5382
+ scopes = []
5383
+ }
5384
+ }
5385
+ ` , instance )
5386
+ }
5387
+
5388
+ func testAccComputeInstance_serviceAccountEmail_0scopes (instance string ) string {
5389
+ return fmt .Sprintf (`
5390
+ data "google_project" "project" {}
5391
+
5392
+ data "google_compute_image" "my_image" {
5393
+ family = "debian-11"
5394
+ project = "debian-cloud"
5395
+ }
5396
+
5397
+ resource "google_compute_instance" "foobar" {
5398
+ name = "%s"
5399
+ machine_type = "e2-medium"
5400
+ zone = "us-central1-a"
5401
+
5402
+ boot_disk {
5403
+ initialize_params {
5404
+ image = data.google_compute_image.my_image.self_link
5405
+ }
5406
+ }
5407
+
5408
+ network_interface {
5409
+ network = "default"
5410
+ }
5411
+
5412
+ service_account {
5413
+ email = data.google_compute_default_service_account.default.email
5414
+ scopes = []
5415
+ }
5416
+ }
5417
+
5418
+ data "google_compute_default_service_account" "default" {
5419
+ }
5420
+ ` , instance )
5421
+ }
5422
+
5280
5423
func testAccComputeInstance_serviceAccount_update0 (instance string ) string {
5281
5424
return fmt .Sprintf (`
5282
5425
data "google_compute_image" "my_image" {
0 commit comments