Skip to content

Commit 7157b85

Browse files
committed
validate sa creation
1 parent a5d2dd9 commit 7157b85

File tree

7 files changed

+255
-252
lines changed

7 files changed

+255
-252
lines changed

build/int.cloudbuild.yaml

Lines changed: 230 additions & 234 deletions
Large diffs are not rendered by default.

modules/instance_template/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ See the [simple](../../examples/instance_template/simple) for a usage example.
2121
| automatic\_restart | (Optional) Specifies whether the instance should be automatically restarted if it is terminated by Compute Engine (not terminated by a user). | `bool` | `true` | no |
2222
| can\_ip\_forward | Enable IP forwarding, for NAT instances for example | `string` | `"false"` | no |
2323
| confidential\_instance\_type | Defines the confidential computing technology the instance uses. If this is set to "SEV\_SNP", var.min\_cpu\_platform will be automatically set to "AMD Milan". See https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance#confidential_instance_type. | `string` | `null` | no |
24-
| create\_service\_account | Create a new service account to attach to the instance. This is alternate to providing the service\_account input variable. Please provide the service\_account input if setting this to false! | `bool` | `true` | no |
24+
| create\_service\_account | Create a new service account to attach to the instance. This is alternate to providing the service\_account input variable. Please provide the service\_account input if setting this to false. | `bool` | `true` | no |
2525
| description | The template's description | `string` | `""` | no |
2626
| disk\_encryption\_key | The id of the encryption key that is stored in Google Cloud KMS to use to encrypt all the disks on this instance | `string` | `null` | no |
2727
| disk\_labels | Labels to be assigned to boot disk, provided as a map | `map(string)` | `{}` | no |

modules/instance_template/main.tf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,16 @@ locals {
8585

8686
# Service account
8787
resource "google_service_account" "sa" {
88-
count = local.create_service_account ? 1 : 0
88+
provider = google-beta
89+
count = local.create_service_account ? 1 : 0
90+
8991
project = var.project_id
9092
account_id = "${local.service_account_prefix}-sa"
9193
display_name = "Service account for ${var.name_prefix} in ${var.region}"
9294
}
9395

9496
resource "google_project_iam_member" "roles" {
97+
provider = google-beta
9598
for_each = toset(distinct(var.service_account_project_roles))
9699

97100
project = var.project_id

modules/instance_template/metadata.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ spec:
302302
outputExpr: email
303303
inputPath: email
304304
- name: create_service_account
305-
description: Create a new service account to attach to the instance. This is alternate to providing the service_account input variable. Please provide the service_account input if setting this to false!
305+
description: Create a new service account to attach to the instance. This is alternate to providing the service_account input variable. Please provide the service_account input if setting this to false.
306306
varType: bool
307307
defaultValue: true
308308
- name: service_account_project_roles
@@ -424,7 +424,5 @@ spec:
424424
- compute.googleapis.com
425425
- iam.googleapis.com
426426
providerVersions:
427-
- source: hashicorp/google
428-
version: ">= 5.36, < 7"
429427
- source: hashicorp/google-beta
430428
version: ">= 5.36, < 7"

modules/instance_template/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ variable "service_account" {
333333

334334
variable "create_service_account" {
335335
type = bool
336-
description = "Create a new service account to attach to the instance. This is alternate to providing the service_account input variable. Please provide the service_account input if setting this to false!"
336+
description = "Create a new service account to attach to the instance. This is alternate to providing the service_account input variable. Please provide the service_account input if setting this to false."
337337
default = true
338338
}
339339

modules/instance_template/versions.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717
terraform {
1818
required_version = ">=1.3"
1919
required_providers {
20-
google = {
21-
source = "hashicorp/google"
22-
version = ">= 5.36, < 7"
23-
}
24-
2520
google-beta = {
2621
source = "hashicorp/google-beta"
2722
version = ">= 5.36, < 7"

test/integration/it_simple_with_sa_creation/it_simple_with_sa_creation_test.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,25 @@ import (
2626
func TestInstanceTemplateSimpleSAModule(t *testing.T) {
2727

2828
const instanceNamePrefix = "it-simple-sa"
29-
const expected_templates = 1
29+
const expectedTemplates = 1
30+
const expectedServiceAccounts = 1
3031

31-
insSimpleT := tft.NewTFBlueprintTest(t)
32-
insSimpleT.DefineVerify(func(assert *assert.Assertions) {
33-
insSimpleT.DefaultVerify(assert)
32+
instanceSimpleTest := tft.NewTFBlueprintTest(t)
33+
instanceSimpleTest.DefineVerify(func(assert *assert.Assertions) {
34+
instanceSimpleTest.DefaultVerify(assert)
3435

35-
instance_templates := gcloud.Run(t, fmt.Sprintf("compute instance-templates list --project %s --filter name~%s", insSimpleT.GetStringOutput("project_id"), instanceNamePrefix))
36-
assert.Equal(expected_templates, len(instance_templates.Array()), fmt.Sprintf("should have %d instance_templates", expected_templates))
36+
projectID := instanceSimpleTest.GetStringOutput("project_id")
37+
instanceTemplates := gcloud.Run(t, fmt.Sprintf("compute instance-templates list --project %s --filter name~%s", projectID, instanceNamePrefix))
38+
assert.Equal(expectedTemplates, len(instanceTemplates.Array()), fmt.Sprintf("should have %d instance templates", expectedTemplates))
39+
40+
serviceAccounts := gcloud.Run(t, fmt.Sprintf("iam service-accounts list --project %s --filter email~%s", projectID, instanceNamePrefix))
41+
assert.Equal(expectedServiceAccounts, len(serviceAccounts.Array()), fmt.Sprintf("should have %d service accounts", expectedServiceAccounts))
42+
43+
for _, it := range instanceTemplates.Array() {
44+
instanceTemplateName := it.Get("name").String()
45+
instanceTemplateServiceAccount := gcloud.Run(t, fmt.Sprintf("compute instance-templates describe %s --project %s", instanceTemplateName, projectID), gcloud.WithCommonArgs([]string{"--format", "get(properties.serviceAccounts)"}))
46+
assert.Contains(instanceTemplateServiceAccount.Get("email").String(), instanceNamePrefix, fmt.Sprintf("Instance template service account %s should start with %s", instanceTemplateServiceAccount, instanceNamePrefix))
47+
}
3748
})
38-
insSimpleT.Test()
49+
instanceSimpleTest.Test()
3950
}

0 commit comments

Comments
 (0)