Allow manage existing SSM instance (#2595)

* added option to manage an existing SSM instance

* switched depends on to try for instance iam

* added resource instance keys

* add instance keys to tests

* changed label default to null

* updated readme

* replaced try functions with conditional expressions for IAM
This commit is contained in:
Liam Nesteroff
2024-09-27 20:13:30 +10:00
committed by GitHub
parent 3fad96436c
commit 163a0eab2b
11 changed files with 43 additions and 36 deletions

View File

@@ -41,9 +41,7 @@ module "ssm_instance" {
location = var.region
kms_key = "projects/another-project-id/locations/${var.region}/keyRings/my-key-ring/cryptoKeys/my-key"
repositories = {
my-repository = {
location = var.region
}
my-repository = {}
}
}
# tftest modules=1 resources=2 inventory=public-instance-with-cmek.yaml
@@ -59,9 +57,7 @@ module "ssm_instance" {
location = var.region
ca_pool = "projects/another-project/locations/${var.region}/caPools/my-ca-pool"
repositories = {
my-repository = {
location = var.region
}
my-repository = {}
}
}
# tftest modules=1 resources=2 inventory=private-instance.yaml
@@ -82,7 +78,6 @@ module "ssm_instance" {
}
repositories = {
my-repository = {
location = var.region
iam = {
"roles/securesourcemanager.repoAdmin" = [
"group:my-repo-admins@myorg.com"
@@ -109,7 +104,6 @@ module "ssm_instance" {
}
repositories = {
my-repository = {
location = var.region
iam_bindings_additive = {
my-repository-admin = {
role = "roles/securesourcemanager.repoAdmin"
@@ -138,7 +132,6 @@ module "ssm_instance" {
}
repositories = {
my-repository = {
location = var.region
iam_bindings = {
my-repository-admin = {
role = "roles/securesourcemanager.repoAdmin"
@@ -157,16 +150,17 @@ module "ssm_instance" {
| name | description | type | required | default |
|---|---|:---:|:---:|:---:|
| [instance_id](variables.tf#L23) | Instance ID. | <code>string</code> | ✓ | |
| [location](variables.tf#L40) | Location. | <code>string</code> | ✓ | |
| [project_id](variables.tf#L45) | Project ID. | <code>string</code> | ✓ | |
| [repositories](variables.tf#L50) | Repositories. | <code title="map&#40;object&#40;&#123;&#10; description &#61; optional&#40;string&#41;&#10; iam &#61; optional&#40;map&#40;list&#40;string&#41;&#41;, &#123;&#125;&#41;&#10; iam_bindings &#61; optional&#40;map&#40;object&#40;&#123;&#10; role &#61; string&#10; members &#61; list&#40;string&#41;&#10; &#125;&#41;&#41;, &#123;&#125;&#41;&#10; iam_bindings_additive &#61; optional&#40;map&#40;object&#40;&#123;&#10; role &#61; string&#10; member &#61; string&#10; &#125;&#41;&#41;, &#123;&#125;&#41;&#10; initial_config &#61; optional&#40;object&#40;&#123;&#10; default_branch &#61; optional&#40;string&#41;&#10; gitignores &#61; optional&#40;string&#41;&#10; license &#61; optional&#40;string&#41;&#10; readme &#61; optional&#40;string&#41;&#10; &#125;&#41;&#41;&#10; location &#61; string&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | ✓ | |
| [instance_id](variables.tf#L29) | Instance ID. | <code>string</code> | ✓ | |
| [location](variables.tf#L46) | Location. | <code>string</code> | ✓ | |
| [project_id](variables.tf#L51) | Project ID. | <code>string</code> | ✓ | |
| [repositories](variables.tf#L56) | Repositories. | <code title="map&#40;object&#40;&#123;&#10; description &#61; optional&#40;string&#41;&#10; iam &#61; optional&#40;map&#40;list&#40;string&#41;&#41;, &#123;&#125;&#41;&#10; iam_bindings &#61; optional&#40;map&#40;object&#40;&#123;&#10; role &#61; string&#10; members &#61; list&#40;string&#41;&#10; &#125;&#41;&#41;, &#123;&#125;&#41;&#10; iam_bindings_additive &#61; optional&#40;map&#40;object&#40;&#123;&#10; role &#61; string&#10; member &#61; string&#10; &#125;&#41;&#41;, &#123;&#125;&#41;&#10; initial_config &#61; optional&#40;object&#40;&#123;&#10; default_branch &#61; optional&#40;string&#41;&#10; gitignores &#61; optional&#40;string&#41;&#10; license &#61; optional&#40;string&#41;&#10; readme &#61; optional&#40;string&#41;&#10; &#125;&#41;&#41;&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | ✓ | |
| [ca_pool](variables.tf#L17) | CA pool. | <code>string</code> | | <code>null</code> |
| [iam](variables-iam.tf#L17) | IAM bindings. | <code>map&#40;list&#40;string&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [iam_bindings](variables-iam.tf#L23) | IAM bindings. | <code title="map&#40;object&#40;&#123;&#10; role &#61; string&#10; members &#61; list&#40;string&#41;&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [iam_bindings_additive](variables-iam.tf#L32) | IAM bindings. | <code title="map&#40;object&#40;&#123;&#10; role &#61; string&#10; member &#61; string&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [kms_key](variables.tf#L28) | KMS key. | <code>string</code> | | <code>null</code> |
| [labels](variables.tf#L34) | Instance labels. | <code>map&#40;string&#41;</code> | | <code>&#123;&#125;</code> |
| [instance_create](variables.tf#L23) | Create SSM Instance. When set to false, uses instance_id to reference existing SSM instance. | <code>bool</code> | | <code>true</code> |
| [kms_key](variables.tf#L34) | KMS key. | <code>string</code> | | <code>null</code> |
| [labels](variables.tf#L40) | Instance labels. | <code>map&#40;string&#41;</code> | | <code>null</code> |
## Outputs

View File

@@ -29,31 +29,37 @@ locals {
"${k1}.${k2}" => merge(v2, {
repository = k1
}) }]...)
iam_instance_values = {
project = var.instance_create ? google_secure_source_manager_instance.instance[0].project : var.project_id
location = var.instance_create ? google_secure_source_manager_instance.instance[0].location : var.location
instance_id = var.instance_create ? google_secure_source_manager_instance.instance[0].instance_id : var.instance_id
}
}
resource "google_secure_source_manager_instance_iam_binding" "authoritative" {
for_each = var.iam
project = google_secure_source_manager_instance.instance.project
location = google_secure_source_manager_instance.instance.location
instance_id = google_secure_source_manager_instance.instance.instance_id
project = local.iam_instance_values["project"]
location = local.iam_instance_values["location"]
instance_id = local.iam_instance_values["instance_id"]
role = each.key
members = each.value
}
resource "google_secure_source_manager_instance_iam_binding" "bindings" {
for_each = var.iam_bindings
project = google_secure_source_manager_instance.instance.project
location = google_secure_source_manager_instance.instance.location
instance_id = google_secure_source_manager_instance.instance.instance_id
project = local.iam_instance_values["project"]
location = local.iam_instance_values["location"]
instance_id = local.iam_instance_values["instance_id"]
role = each.value.role
members = each.value.members
}
resource "google_secure_source_manager_instance_iam_member" "bindings" {
for_each = var.iam_bindings_additive
project = google_secure_source_manager_instance.instance.project
location = google_secure_source_manager_instance.instance.location
instance_id = google_secure_source_manager_instance.instance.instance_id
project = local.iam_instance_values["project"]
location = local.iam_instance_values["location"]
instance_id = local.iam_instance_values["instance_id"]
role = each.value.role
member = each.value.member
}

View File

@@ -15,6 +15,7 @@
*/
resource "google_secure_source_manager_instance" "instance" {
count = var.instance_create ? 1 : 0
instance_id = var.instance_id
project = var.project_id
location = var.location
@@ -32,9 +33,10 @@ resource "google_secure_source_manager_instance" "instance" {
resource "google_secure_source_manager_repository" "repositories" {
for_each = var.repositories
repository_id = each.key
instance = google_secure_source_manager_instance.instance.name
instance = try(google_secure_source_manager_instance.instance[0].name, "projects/${var.project_id}/locations/${var.location}/instances/${var.instance_id}")
project = var.project_id
location = each.value.location
location = var.location
description = each.value.description
dynamic "initial_config" {
for_each = each.value.initial_config == null ? [] : [""]
content {

View File

@@ -16,12 +16,12 @@
output "instance" {
description = "Instance."
value = google_secure_source_manager_instance.instance
value = try(google_secure_source_manager_instance.instance[0], null)
}
output "instance_id" {
description = "Instance id."
value = google_secure_source_manager_instance.instance.id
value = try(google_secure_source_manager_instance.instance[0].id, null)
}
output "repositories" {

View File

@@ -20,6 +20,12 @@ variable "ca_pool" {
default = null
}
variable "instance_create" {
description = "Create SSM Instance. When set to false, uses instance_id to reference existing SSM instance."
type = bool
default = true
}
variable "instance_id" {
description = "Instance ID."
type = string
@@ -34,7 +40,7 @@ variable "kms_key" {
variable "labels" {
description = "Instance labels."
type = map(string)
default = {}
default = null
}
variable "location" {
@@ -66,6 +72,5 @@ variable "repositories" {
license = optional(string)
readme = optional(string)
}))
location = string
}))
}