Skip to content

Commit 8854827

Browse files
authored
replaced deprecated template_file data source with templatefile function (#28)
* added support for private bastion, changed default shape to E4.Flex * replaced deprecated template_file data source with templatefile function, renamed variables * Update CHANGELOG.adoc * moved templating function into locals * minor typo * changed cloud init provider * Update versions.tf
1 parent 717692d commit 8854827

14 files changed

+101
-88
lines changed

CHANGELOG.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ The format is based on {uri-changelog}[Keep a Changelog].
1010
= Unreleased
1111

1212
== New features
13+
* Renamed variable bastion_upgrade --> upgrade_bastion
14+
* Renamed variable timezone --> bastion_timezone
15+
* AD lookup mechanism reimplemented to remove dependency on deprecated template_file data source
16+
* Replaced deprecated template_file data source with templatefile function
1317
* New variable (`bastion_operating_system_version`) to specify Autonomous Linux version (#15)
1418
* Added sort_order on images (#16)
1519
* New variable (`bastion_state`) to specify state of bastion host (#17)

cloudinit/autonomous.template.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
33

44
#cloud-config
5-
timezone: ${timezone}
5+
package_upgrade: ${upgrade_bastion}
6+
timezone: ${bastion_timezone}
67

78
write_files:
89
# setup script

compute.tf

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
33

44
resource "oci_core_instance" "bastion" {
5-
availability_domain = element(local.ad_names, (var.availability_domain - 1))
5+
availability_domain = data.oci_identity_availability_domain.ad.name
66
compartment_id = var.compartment_id
77
freeform_tags = var.tags
88

9+
agent_config {
10+
11+
are_all_plugins_disabled = false
12+
is_management_disabled = false
13+
is_monitoring_disabled = false
14+
15+
plugins_config {
16+
desired_state = "DISABLED"
17+
name = "Bastion"
18+
}
19+
}
20+
921
create_vnic_details {
1022
assign_public_ip = var.bastion_type == "public" ? true : false
1123
display_name = var.label_prefix == "none" ? "bastion-vnic" : "${var.label_prefix}-bastion-vnic"
@@ -27,7 +39,7 @@ resource "oci_core_instance" "bastion" {
2739

2840
metadata = {
2941
ssh_authorized_keys = var.ssh_public_key != "" ? var.ssh_public_key : file(var.ssh_public_key_path)
30-
user_data = data.template_cloudinit_config.bastion[0].rendered
42+
user_data = data.cloudinit_config.bastion[0].rendered
3143
}
3244

3345
shape = lookup(var.bastion_shape, "shape", "VM.Standard.E2.2")
@@ -47,10 +59,10 @@ resource "oci_core_instance" "bastion" {
4759
}
4860

4961
state = var.bastion_state
50-
62+
5163
timeouts {
5264
create = "60m"
5365
}
5466

55-
count = var.bastion_enabled == true ? 1 : 0
67+
count = var.create_bastion == true ? 1 : 0
5668
}

datasources.tf

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
# Copyright 2019, 2020 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-
data "oci_identity_availability_domains" "ad_list" {
4+
data "oci_identity_availability_domain" "ad" {
55
compartment_id = var.tenancy_id
6-
}
76

8-
data "template_file" "ad_names" {
9-
count = length(data.oci_identity_availability_domains.ad_list.availability_domains)
10-
template = lookup(data.oci_identity_availability_domains.ad_list.availability_domains[count.index], "name")
7+
ad_number = var.availability_domain
118
}
129

1310
data "oci_identity_tenancy" "tenancy" {
@@ -26,28 +23,6 @@ data "oci_core_vcn" "vcn" {
2623
vcn_id = var.vcn_id
2724
}
2825

29-
data "template_file" "autonomous_template" {
30-
template = file("${path.module}/scripts/notification.template.sh")
31-
32-
vars = {
33-
notification_enabled = var.notification_enabled
34-
topic_id = var.notification_enabled == true ? oci_ons_notification_topic.bastion_notification[0].topic_id : "null"
35-
}
36-
37-
count = (var.bastion_enabled == true && var.bastion_image_id == "Autonomous") ? 1 : 0
38-
}
39-
40-
data "template_file" "autonomous_cloud_init_file" {
41-
template = file("${path.module}/cloudinit/autonomous.template.yaml")
42-
43-
vars = {
44-
notification_sh_content = base64gzip(data.template_file.autonomous_template[0].rendered)
45-
timezone = var.timezone
46-
}
47-
48-
count = (var.bastion_enabled == true && var.bastion_image_id == "Autonomous") ? 1 : 0
49-
}
50-
5126
data "oci_core_images" "autonomous_images" {
5227
compartment_id = var.compartment_id
5328
operating_system = "Oracle Autonomous Linux"
@@ -58,45 +33,51 @@ data "oci_core_images" "autonomous_images" {
5833
}
5934

6035
# cloud init for bastion
61-
data "template_cloudinit_config" "bastion" {
36+
data "cloudinit_config" "bastion" {
6237
gzip = true
6338
base64_encode = true
6439

6540
part {
6641
filename = "bastion.yaml"
6742
content_type = "text/cloud-config"
68-
content = data.template_file.autonomous_cloud_init_file[0].rendered
43+
content = templatefile(
44+
local.autonomous_template, {
45+
bastion_timezone = var.bastion_timezone,
46+
notification_sh_content = local.notification_template,
47+
upgrade_bastion = var.upgrade_bastion
48+
}
49+
)
6950
}
70-
count = var.bastion_enabled == true ? 1 : 0
51+
count = var.create_bastion == true ? 1 : 0
7152
}
7253

7354
# Gets a list of VNIC attachments on the bastion instance
7455
data "oci_core_vnic_attachments" "bastion_vnics_attachments" {
75-
availability_domain = element(local.ad_names, (var.availability_domain - 1))
56+
availability_domain = data.oci_identity_availability_domain.ad.name
7657
compartment_id = var.compartment_id
7758
depends_on = [oci_core_instance.bastion]
7859
instance_id = oci_core_instance.bastion[0].id
7960

80-
count = var.bastion_enabled == true ? 1 : 0
61+
count = var.create_bastion == true ? 1 : 0
8162
}
8263

8364
# Gets the OCID of the first (default) VNIC on the bastion instance
8465
data "oci_core_vnic" "bastion_vnic" {
8566
depends_on = [oci_core_instance.bastion]
8667
vnic_id = lookup(data.oci_core_vnic_attachments.bastion_vnics_attachments[0].vnic_attachments[0], "vnic_id")
8768

88-
count = var.bastion_enabled == true ? 1 : 0
69+
count = var.create_bastion == true ? 1 : 0
8970
}
9071

9172
data "oci_core_instance" "bastion" {
9273
depends_on = [oci_core_instance.bastion]
9374
instance_id = oci_core_instance.bastion[0].id
9475

95-
count = var.bastion_enabled == true ? 1 : 0
76+
count = var.create_bastion == true ? 1 : 0
9677
}
9778

9879
data "oci_ons_notification_topic" "bastion_notification" {
9980
topic_id = oci_ons_notification_topic.bastion_notification[0].topic_id
10081

101-
count = (var.bastion_enabled == true && var.notification_enabled == true) ? 1 : 0
82+
count = (var.create_bastion == true && var.enable_notification == true) ? 1 : 0
10283
}

docs/quickstart.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ provider "oci" {
8181
. Optional parameters to override:
8282

8383
* `bastion_shape`
84-
* `bastion_upgrade`
85-
* `notification_enabled`
84+
* `upgrade_bastion`
85+
* `enable_notification`
8686
* `notification_endpoint`
8787

8888
. Run Terraform:

docs/terraformoptions.adoc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Ensure you review the {uri-terraform-dependencies}[dependencies].
128128
|Values
129129
|Default
130130

131-
|`bastion_enabled`
131+
|`create_bastion`
132132
|whether to create the bastion
133133
| true/false
134134
|true
@@ -163,16 +163,16 @@ Ensure you review the {uri-terraform-dependencies}[dependencies].
163163
|RUNNING|STOPPED
164164
|RUNNING
165165

166+
|`bastion_timezone`
167+
|The preferred timezone for the bastion host. {uri-timezones}[List of timezones]
168+
|e.g. Australia/Sydney
169+
|The preferred timezone for the bastion host. {uri-timezones}[List of timezones]
170+
166171
|`bastion_type`
167172
|Whether to make the bastion host public or private.
168173
|public|private
169174
|public
170175

171-
|`bastion_upgrade`
172-
|Whether to upgrade the bastion host packages after provisioning. It's useful to set this to false during development/testing so the bastion is provisioned faster.
173-
|true/false
174-
|true
175-
176176
|`ssh_public_key`
177177
|the content of the ssh public key used to access the bastion. set this or the ssh_public_key_path
178178
|
@@ -183,10 +183,10 @@ Ensure you review the {uri-terraform-dependencies}[dependencies].
183183
|""
184184
|
185185

186-
|`timezone`
187-
|The preferred timezone for the bastion host. {uri-timezones}[List of timezones]
188-
|e.g. Australia/Sydney
189-
|The preferred timezone for the bastion host. {uri-timezones}[List of timezones]
186+
|`upgrade_bastion`
187+
|Whether to upgrade the bastion host packages after provisioning. It's useful to set this to false during development/testing so the bastion is provisioned faster.
188+
|true/false
189+
|true
190190

191191
|===
192192

@@ -200,13 +200,13 @@ Ensure you review the {uri-terraform-dependencies}[dependencies].
200200
|Values
201201
|Default
202202

203-
|`notification_enabled`
203+
|`enable_notification`
204204
|Whether to enable ONS notification for the bastion host.
205205
|true/false
206206
|false
207207

208208
|`notification_endpoint`
209-
|The subscription notification endpoint. Email address to be notified. *Required if notification_enabled = true* ..
209+
|The subscription notification endpoint. Email address to be notified. *Required if enable_notification = true* ..
210210
|
211211
|Autonomous
212212

locals.tf

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,26 @@
55
# https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
66

77
locals {
8-
all_protocols = "all"
9-
ad_names = data.template_file.ad_names.*.rendered
10-
anywhere = "0.0.0.0/0"
11-
ssh_port = 22
12-
tcp_protocol = 6
13-
bastion_image_id = var.bastion_image_id == "Autonomous" ? data.oci_core_images.autonomous_images.images.0.id : var.bastion_image_id
14-
vcn_cidr = data.oci_core_vcn.vcn.cidr_block
8+
all_protocols = "all"
9+
10+
anywhere = "0.0.0.0/0"
11+
12+
autonomous_template = "${path.module}/cloudinit/autonomous.template.yaml"
13+
14+
bastion_image_id = var.bastion_image_id == "Autonomous" ? data.oci_core_images.autonomous_images.images.0.id : var.bastion_image_id
15+
16+
notification_template = base64gzip(
17+
templatefile("${path.module}/scripts/notification.template.sh",
18+
{
19+
enable_notification = var.enable_notification,
20+
topic_id = var.enable_notification == true ? oci_ons_notification_topic.bastion_notification[0].topic_id : "null"
21+
}
22+
)
23+
)
24+
25+
ssh_port = 22
26+
27+
tcp_protocol = 6
28+
29+
vcn_cidr = data.oci_core_vcn.vcn.cidr_block
1530
}

ons.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ resource "oci_ons_notification_topic" "bastion_notification" {
1414
compartment_id = var.compartment_id
1515
name = var.label_prefix == "none" ? var.notification_topic : "${var.label_prefix}-${var.notification_topic}"
1616

17-
count = (var.bastion_enabled == true && var.notification_enabled == true) ? 1 : 0
17+
count = (var.create_bastion == true && var.enable_notification == true) ? 1 : 0
1818
}
1919

2020
resource "oci_ons_subscription" "bastion_notification" {
@@ -23,7 +23,7 @@ resource "oci_ons_subscription" "bastion_notification" {
2323
protocol = var.notification_protocol
2424
topic_id = oci_ons_notification_topic.bastion_notification[0].topic_id
2525

26-
count = (var.bastion_enabled == true && var.notification_enabled == true) ? 1 : 0
26+
count = (var.create_bastion == true && var.enable_notification == true) ? 1 : 0
2727
}
2828

2929
resource "oci_identity_dynamic_group" "bastion_notification" {
@@ -35,7 +35,7 @@ resource "oci_identity_dynamic_group" "bastion_notification" {
3535
matching_rule = "ALL {instance.id = '${join(",", data.oci_core_instance.bastion.*.id)}'}"
3636
name = var.label_prefix == "none" ? "bastion-notification" : "${var.label_prefix}-bastion-notification"
3737

38-
count = (var.bastion_enabled == true && var.notification_enabled == true) ? 1 : 0
38+
count = (var.create_bastion == true && var.enable_notification == true) ? 1 : 0
3939
}
4040

4141
resource "oci_identity_policy" "bastion_notification" {
@@ -47,5 +47,5 @@ resource "oci_identity_policy" "bastion_notification" {
4747
name = var.label_prefix == "none" ? "bastion-notification" : "${var.label_prefix}-bastion-notification"
4848
statements = ["Allow dynamic-group ${oci_identity_dynamic_group.bastion_notification[0].name} to use ons-topic in compartment id ${var.compartment_id} where request.permission='ONS_TOPIC_PUBLISH'"]
4949

50-
count = (var.bastion_enabled == true && var.notification_enabled == true) ? 1 : 0
50+
count = (var.create_bastion == true && var.enable_notification == true) ? 1 : 0
5151
}

scripts/notification.template.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright 2019, 2020 Oracle Corporation and/or affiliates. All rights reserved.
44
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
55

6-
if [ ${notification_enabled} ]; then
6+
if [ ${enable_notification} ]; then
77
sudo al-config -T ${topic_id}
88
else
99
echo 'ONS notification not enabled'

security.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ resource "oci_core_security_list" "bastion" {
2323
}
2424
vcn_id = var.vcn_id
2525

26-
count = var.bastion_enabled == true ? 1 : 0
26+
count = var.create_bastion == true ? 1 : 0
2727
}

0 commit comments

Comments
 (0)