Skip to content

Commit

Permalink
validate sa creation
Browse files Browse the repository at this point in the history
  • Loading branch information
q2w committed Jan 28, 2025
1 parent a5d2dd9 commit c3c1852
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion modules/instance_template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ See the [simple](../../examples/instance_template/simple) for a usage example.
| 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 |
| can\_ip\_forward | Enable IP forwarding, for NAT instances for example | `string` | `"false"` | no |
| 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 |
| 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 |
| 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 |
| description | The template's description | `string` | `""` | no |
| 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 |
| disk\_labels | Labels to be assigned to boot disk, provided as a map | `map(string)` | `{}` | no |
Expand Down
5 changes: 4 additions & 1 deletion modules/instance_template/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,16 @@ locals {

# Service account
resource "google_service_account" "sa" {
count = local.create_service_account ? 1 : 0
provider = google-beta
count = local.create_service_account ? 1 : 0

project = var.project_id
account_id = "${local.service_account_prefix}-sa"
display_name = "Service account for ${var.name_prefix} in ${var.region}"
}

resource "google_project_iam_member" "roles" {
provider = google-beta
for_each = toset(distinct(var.service_account_project_roles))

project = var.project_id
Expand Down
1 change: 1 addition & 0 deletions modules/instance_template/metadata.display.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ spec:
source_image_project:
name: source_image_project
title: Source Image Project
level: 1
spot:
name: spot
title: Spot
Expand Down
4 changes: 1 addition & 3 deletions modules/instance_template/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ spec:
outputExpr: email
inputPath: email
- name: create_service_account
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!
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.
varType: bool
defaultValue: true
- name: service_account_project_roles
Expand Down Expand Up @@ -424,7 +424,5 @@ spec:
- compute.googleapis.com
- iam.googleapis.com
providerVersions:
- source: hashicorp/google
version: ">= 5.36, < 7"
- source: hashicorp/google-beta
version: ">= 5.36, < 7"
2 changes: 1 addition & 1 deletion modules/instance_template/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ variable "service_account" {

variable "create_service_account" {
type = bool
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!"
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."
default = true
}

Expand Down
5 changes: 0 additions & 5 deletions modules/instance_template/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
terraform {
required_version = ">=1.3"
required_providers {
google = {
source = "hashicorp/google"
version = ">= 5.36, < 7"
}

google-beta = {
source = "hashicorp/google-beta"
version = ">= 5.36, < 7"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ func TestInstanceTemplateSimpleSAModule(t *testing.T) {

const instanceNamePrefix = "it-simple-sa"
const expected_templates = 1
const expected_sa = 1

insSimpleT := tft.NewTFBlueprintTest(t)
insSimpleT.DefineVerify(func(assert *assert.Assertions) {
insSimpleT.DefaultVerify(assert)

instance_templates := gcloud.Run(t, fmt.Sprintf("compute instance-templates list --project %s --filter name~%s", insSimpleT.GetStringOutput("project_id"), instanceNamePrefix))
assert.Equal(expected_templates, len(instance_templates.Array()), fmt.Sprintf("should have %d instance_templates", expected_templates))

service_accounts := gcloud.Run(t, fmt.Sprintf("iam service-accounts list --project %s --filter email~%s", insSimpleT.GetStringOutput("project_id"), instanceNamePrefix))
assert.Equal(expected_sa, len(service_accounts.Array()), fmt.Sprintf("should have %d service_accounts", expected_sa))
})
insSimpleT.Test()
}

0 comments on commit c3c1852

Please sign in to comment.