Skip to content

Commit 5623354

Browse files
authored
replaced deprecated template_file data source with templatefile function (#39)
* replaced deprecated template_file data source with templatefile function * renamed variables * Update README.adoc * moved templating into locals * change cloud init provider
1 parent c64fae1 commit 5623354

File tree

10 files changed

+81
-70
lines changed

10 files changed

+81
-70
lines changed

CHANGELOG.adoc

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

1212
== New features
13+
* Renamed variable operator_upgrade --> upgrade_operator
14+
* Renamed variable timezone --> operator_timezone
15+
* Added support for Bastion service
16+
* AD lookup mechanism reimplemented to remove dependency on deprecated template_file data source
17+
* Replaced deprecated template_file data source with templatefile function
1318
* Set minimum Terraform version to 1.0.0
1419
* Renamed var.operator_enabled --> var.create_operator
1520
* New variable (`operator_state`) to specify state of operator host

README.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
:idseparator: -
55

66
:uri-repo: https://github.com/oracle-terraform-modules/terraform-oci-operator
7+
:uri-bastion-repo: https://github.com/oracle-terraform-modules/terraform-oci-bastion
78

89
:uri-rel-file-base: link:{uri-repo}/blob/master
910
:uri-rel-tree-base: link:{uri-repo}/tree/master
@@ -21,6 +22,7 @@
2122
:uri-networks-subnets-cidr: https://erikberg.com/notes/networks.html
2223
:uri-oci: https://cloud.oracle.com/cloud-infrastructure
2324
:uri-oci-documentation: https://docs.cloud.oracle.com/iaas/Content/home.htm
25+
:uri-oci-bastion: https://docs.oracle.com/en-us/iaas/Content/Bastion/home.htm
2426
:uri-oracle: https://www.oracle.com
2527
:uri-prereqs: {uri-docs}/prerequisites.adoc
2628
:uri-quickstart: {uri-docs}/quickstart.adoc
@@ -46,6 +48,8 @@ This module is primarily meant to be reusable and the operator instance is used
4648
1. performing post-provisioning tasks with Terraform or other automation tools
4749
2. provide administrators access without the need to upload api authentication keys (instance_principal)
4850
51+
It can be accessed either through a Bastion host (e.g. {uri-bastion-repo}[terraform-oci-bastion]) or through the {uri-oci-bastion}[OCI Bastion service].
52+
4953
You can further use it as part of higher level Terraform modules.
5054

5155
== {uri-docs}[Documentation]

cloudinit/operator.template.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +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-
package_upgrade: ${operator_upgrade}
6-
timezone: ${timezone}
5+
package_upgrade: ${upgrade_operator}
6+
timezone: ${operator_timezone}
77
write_files:
88
# setup script
99
- path: "/root/operator/operator.sh"

compute.tf

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,29 @@
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" "operator" {
5-
availability_domain = element(local.ad_names, (var.availability_domain - 1))
5+
availability_domain = data.oci_identity_availability_domain.ad.name
66

77
agent_config {
8-
is_management_disabled = true
9-
}
108

9+
are_all_plugins_disabled = false
10+
is_management_disabled = false
11+
is_monitoring_disabled = false
12+
13+
plugins_config {
14+
desired_state = "ENABLED"
15+
name = "Bastion"
16+
}
17+
}
18+
1119
compartment_id = var.compartment_id
1220
freeform_tags = var.tags
1321

1422
create_vnic_details {
15-
assign_public_ip = false
16-
display_name = var.label_prefix == "none" ? "operator-vnic" : "${var.label_prefix}-operator-vnic"
17-
hostname_label = var.label_prefix == "none" ? "operator" : "${var.label_prefix}-operator"
18-
nsg_ids = concat(var.nsg_ids,[oci_core_network_security_group.operator[0].id])
19-
subnet_id = oci_core_subnet.operator[0].id
23+
assign_public_ip = false
24+
display_name = var.label_prefix == "none" ? "operator-vnic" : "${var.label_prefix}-operator-vnic"
25+
hostname_label = var.label_prefix == "none" ? "operator" : "${var.label_prefix}-operator"
26+
nsg_ids = concat(var.nsg_ids, [oci_core_network_security_group.operator[0].id])
27+
subnet_id = oci_core_subnet.operator[0].id
2028
}
2129

2230
display_name = var.label_prefix == "none" ? "operator" : "${var.label_prefix}-operator"
@@ -33,7 +41,7 @@ resource "oci_core_instance" "operator" {
3341

3442
metadata = {
3543
ssh_authorized_keys = var.ssh_public_key != "" ? var.ssh_public_key : file(var.ssh_public_key_path)
36-
user_data = data.template_cloudinit_config.operator[0].rendered
44+
user_data = data.cloudinit_config.operator[0].rendered
3745
}
3846

3947
shape = lookup(var.operator_shape, "shape", "VM.Standard.E4.Flex")

datasources.tf

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ data "oci_core_services" "all_oci_services" {
99
}
1010
}
1111

12-
data "oci_identity_availability_domains" "ad_list" {
12+
data "oci_identity_availability_domain" "ad" {
1313
compartment_id = var.tenancy_id
14-
}
1514

16-
data "template_file" "ad_names" {
17-
count = length(data.oci_identity_availability_domains.ad_list.availability_domains)
18-
template = lookup(data.oci_identity_availability_domains.ad_list.availability_domains[count.index], "name")
15+
ad_number = var.availability_domain
1916
}
2017

2118
data "oci_identity_tenancy" "tenancy" {
@@ -34,28 +31,6 @@ data "oci_core_vcn" "vcn" {
3431
vcn_id = var.vcn_id
3532
}
3633

37-
data "template_file" "oracle_template" {
38-
template = file("${path.module}/scripts/operator.template.sh")
39-
40-
vars = {
41-
ol = var.operating_system_version
42-
}
43-
44-
count = (var.create_operator == true) ? 1 : 0
45-
}
46-
47-
data "template_file" "oracle_cloud_init_file" {
48-
template = file("${path.module}/cloudinit/operator.template.yaml")
49-
50-
vars = {
51-
operator_sh_content = base64gzip(data.template_file.oracle_template[0].rendered)
52-
operator_upgrade = var.operator_upgrade
53-
timezone = var.timezone
54-
}
55-
56-
count = (var.create_operator == true) ? 1 : 0
57-
}
58-
5934
data "oci_core_images" "oracle_images" {
6035
compartment_id = var.compartment_id
6136
operating_system = "Oracle Linux"
@@ -67,21 +42,27 @@ data "oci_core_images" "oracle_images" {
6742
}
6843

6944
# cloud init for operator
70-
data "template_cloudinit_config" "operator" {
45+
data "cloudinit_config" "operator" {
7146
gzip = true
7247
base64_encode = true
7348

7449
part {
7550
filename = "operator.yaml"
7651
content_type = "text/cloud-config"
77-
content = data.template_file.oracle_cloud_init_file[0].rendered
52+
content = templatefile(
53+
local.operator_template, {
54+
operator_sh_content = local.operator_script_template,
55+
operator_timezone = var.operator_timezone,
56+
upgrade_operator = var.upgrade_operator,
57+
}
58+
)
7859
}
7960
count = var.create_operator == true ? 1 : 0
8061
}
8162

8263
# Gets a list of VNIC attachments on the operator instance
8364
data "oci_core_vnic_attachments" "operator_vnics_attachments" {
84-
availability_domain = element(local.ad_names, (var.availability_domain - 1))
65+
availability_domain = data.oci_identity_availability_domain.ad.name
8566
compartment_id = var.compartment_id
8667
depends_on = [oci_core_instance.operator]
8768
instance_id = oci_core_instance.operator[0].id

locals.tf

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,27 @@
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
8+
all_protocols = "all"
9+
10+
anywhere = "0.0.0.0/0"
11+
1312
operator_image_id = var.operator_image_id == "Oracle" ? data.oci_core_images.oracle_images[0].images.0.id : var.operator_image_id
14-
osn = lookup(data.oci_core_services.all_oci_services.services[0], "cidr_block")
15-
vcn_cidr = data.oci_core_vcn.vcn.cidr_block
13+
14+
operator_template = "${path.module}/cloudinit/operator.template.yaml"
15+
16+
operator_script_template = base64gzip(
17+
templatefile("${path.module}/scripts/operator.template.sh",
18+
{
19+
ol = var.operating_system_version
20+
}
21+
)
22+
)
23+
24+
osn = lookup(data.oci_core_services.all_oci_services.services[0], "cidr_block")
25+
26+
ssh_port = 22
27+
28+
tcp_protocol = 6
29+
30+
vcn_cidr = data.oci_core_vcn.vcn.cidr_block
1631
}

scripts/operator.template.sh

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

66
if [ ${ol} = 8 ]; then
7-
dnf makecache
87
dnf config-manager --enable ol8_developer && dnf -y install python3-oci-cli
98
else
109
yum -y -t update --security

terraform.tfvars.example

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ operator_shape = {
5151

5252
operator_state= "RUNNING"
5353

54-
operator_upgrade = false
54+
operator_timezone = "Australia/Sydney"
5555

5656
ssh_public_key = ""
5757

5858
ssh_public_key_path = ""
5959

60+
upgrade_operator = false
61+
6062
# notification
6163

6264
notification_enabled = false
@@ -70,5 +72,5 @@ notification_topic = "operator"
7072
tags = {
7173
department = "finance"
7274
environment = "dev"
73-
role = "bastion"
75+
role = "operator"
7476
}

variables.tf

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,18 @@ variable "vcn_id" {
8383

8484
# operator host parameters
8585

86-
variable "operating_system_version" {
87-
description = "The version of the Oracle Linux to use."
88-
default = "8"
89-
type = string
90-
}
91-
9286
variable "create_operator" {
93-
#! Deprecation notice: will be renamed to create_operator at next major release
9487
description = "whether to create the operator"
9588
default = false
9689
type = bool
9790
}
9891

92+
variable "operating_system_version" {
93+
description = "The version of the Oracle Linux to use."
94+
default = "8"
95+
type = string
96+
}
97+
9998
variable "operator_image_id" {
10099
description = "Provide a custom image id for the operator host or leave as Oracle."
101100
default = "Oracle"
@@ -122,12 +121,6 @@ variable "operator_state" {
122121
type = string
123122
}
124123

125-
variable "operator_upgrade" {
126-
description = "Whether to upgrade the operator host packages after provisioning. It's useful to set this to false during development/testing so the operator is provisioned faster."
127-
default = false
128-
type = bool
129-
}
130-
131124
variable "ssh_public_key" {
132125
description = "the content of the ssh public key used to access the operator. set this or the ssh_public_key_path"
133126
default = ""
@@ -140,12 +133,19 @@ variable "ssh_public_key_path" {
140133
type = string
141134
}
142135

143-
variable "timezone" {
136+
variable "operator_timezone" {
144137
description = "The preferred timezone for the operator host."
145138
default = "Australia/Sydney"
146139
type = string
147140
}
148141

142+
variable "upgrade_operator" {
143+
description = "Whether to upgrade the operator host packages after provisioning. It's useful to set this to false during development/testing so the operator is provisioned faster."
144+
default = false
145+
type = bool
146+
}
147+
148+
149149
# operator notification
150150

151151
variable "notification_enabled" {
@@ -174,11 +174,11 @@ variable "notification_topic" {
174174

175175
# tagging
176176
variable "tags" {
177-
description = "Freeform tags for bastion"
177+
description = "Freeform tags for operator"
178178
default = {
179179
department = "finance"
180180
environment = "dev"
181-
role = "bastion"
181+
role = "operator"
182182
}
183183
type = map(any)
184184
}

versions.tf

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ terraform {
66
oci = {
77
source = "hashicorp/oci"
88
}
9-
template = {
10-
source = "hashicorp/template"
11-
}
129
}
1310
required_version = ">= 1.0.0"
1411
}

0 commit comments

Comments
 (0)