Skip to content

Commit 93ea8ab

Browse files
authored
Use stable name for dynamic group (#69)
Signed-off-by: Devon Crouse <[email protected]> Signed-off-by: Devon Crouse <[email protected]>
1 parent d55a936 commit 93ea8ab

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

instance_principal.tf

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
1-
# Copyright 2017, 2021 Oracle Corporation and/or affiliates. All rights reserved.
1+
# Copyright (c) 2022, Oracle Corporation and/or affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl
33

4-
resource "oci_identity_dynamic_group" "enable_operator_instance_principal" {
4+
resource "random_id" "dynamic_group_suffix" {
5+
keepers = {
6+
# Generate a new suffix only when variables are changed
7+
label_prefix = local.dynamic_group_prefix
8+
tenancy_id = var.tenancy_id
9+
}
10+
11+
byte_length = 8
12+
}
13+
14+
resource "oci_identity_dynamic_group" "operator_group" {
515
provider = oci.home
616

7-
compartment_id = var.tenancy_id
8-
description = var.label_prefix == "none" ? "dynamic group to allow operator instance to invoke services" : "dynamic group with label ${var.label_prefix} to allow operator to invoke services"
17+
compartment_id = random_id.dynamic_group_suffix.keepers.tenancy_id
18+
description = "dynamic group %{ if var.label_prefix != "none" }with label ${var.label_prefix}%{ endif } to allow operator to invoke services"
919

1020
lifecycle {
11-
ignore_changes = [name, defined_tags]
21+
ignore_changes = [defined_tags]
1222
}
1323

1424
matching_rule = "ALL {instance.id = '${join(",", data.oci_core_instance.operator.*.id)}'}"
15-
name = "operator-instance-principal-${substr(uuid(), 0, 8)}"
25+
name = join("-", compact([
26+
random_id.dynamic_group_suffix.keepers.label_prefix,
27+
"operator-instance-principal",
28+
random_id.dynamic_group_suffix.hex
29+
]))
1630

1731
count = var.enable_operator_instance_principal == true ? 1 : 0
1832
}
1933

20-
resource "oci_identity_policy" "enable_operator_instance_principal" {
34+
resource "oci_identity_policy" "operator_group_policy" {
2135
provider = oci.home
2236

2337
compartment_id = var.compartment_id
2438
description = "policy to allow operator host to call services"
25-
name = var.label_prefix == "none" ? "operator-instance-principal" : "${var.label_prefix}-operator-instance-principal"
26-
statements = ["Allow dynamic-group ${oci_identity_dynamic_group.enable_operator_instance_principal[0].name} to manage all-resources in compartment id ${var.compartment_id}"]
39+
name = join("-", compact([ local.dynamic_group_prefix, "operator-instance-principal" ]))
40+
statements = ["Allow dynamic-group ${oci_identity_dynamic_group.operator_group[0].name} to manage all-resources in compartment id ${var.compartment_id}"]
2741

2842
count = var.enable_operator_instance_principal == true ? 1 : 0
2943
}

locals.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2017, 2021 Oracle Corporation and/or affiliates. All rights reserved.
1+
# Copyright (c) 2022, Oracle Corporation and/or affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
33

44
# Protocols are specified as protocol numbers.
@@ -9,6 +9,8 @@ locals {
99

1010
anywhere = "0.0.0.0/0"
1111

12+
dynamic_group_prefix = (var.label_prefix == "none") ? "" : "${var.label_prefix}"
13+
1214
operator_image_id = var.operator_image_id == "Oracle" ? data.oci_core_images.oracle_images[0].images.0.id : var.operator_image_id
1315

1416
operator_subnet = cidrsubnet(local.vcn_cidr, var.newbits, var.netnum)

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Copyright 2017, 2021 Oracle Corporation and/or affiliates. All rights reserved.
1+
# Copyright (c) 2022, Oracle Corporation and/or affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl
33

44
output "operator_private_ip" {
55
value = join(",", data.oci_core_vnic.operator_vnic.*.private_ip_address)
66
}
77

88
output "operator_instance_principal_group_name" {
9-
value = var.enable_operator_instance_principal == true ? oci_identity_dynamic_group.enable_operator_instance_principal[0].name : null
9+
value = var.enable_operator_instance_principal == true ? oci_identity_dynamic_group.operator_group[0].name : null
1010
}
1111

1212
output "operator_subnet_id" {

0 commit comments

Comments
 (0)