Skip to content

Commit 58cfc57

Browse files
committed
Add support for Flex instance shape
Add new output: instances_summary Fix outputs (Issue oracle-terraform-modules#31)
1 parent 0525055 commit 58cfc57

File tree

8 files changed

+145
-34
lines changed

8 files changed

+145
-34
lines changed

CHANGELOG.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ Given a version number MAJOR.MINOR.PATCH:
1212

1313
## [UNRELEASED]
1414

15-
Features:
15+
Added:
1616

17-
- add Flex instance shape support
17+
- support Flex instance shape
18+
- new output : instances_summary
19+
20+
Fixed:
21+
22+
- Outputs produces unnecessarily multidimensional objects (Issue #31)
1823

1924
Repo maintenance:
2025

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ module "instance" {
6060
| hostname\_label | The hostname for the VNIC's primary private IP. | `string` | `""` | no |
6161
| instance\_count | Number of instances to launch. | `number` | `1` | no |
6262
| instance\_display\_name | (Optional) (Updatable) A user-friendly name for the instance. Does not have to be unique, and it's changeable. | `string` | `""` | no |
63+
| instance\_flex\_memory\_in\_gbs | (Optional) (Updatable) The total amount of memory available to the instance, in gigabytes. | `number` | `null` | no |
64+
| instance\_flex\_ocpus | (Optional) (Updatable) The total number of OCPUs available to the instance. | `number` | `null` | no |
6365
| instance\_timeout | Timeout setting for creating instance. | `string` | `"25m"` | no |
6466
| ipxe\_script | (Optional) The iPXE script which to continue the boot process on the instance. | `string` | `null` | no |
6567
| preserve\_boot\_volume | Specifies whether to delete or preserve the boot volume when terminating an instance. | `bool` | `false` | no |
@@ -82,6 +84,7 @@ module "instance" {
8284
| instance\_id | ocid of created instances. |
8385
| instance\_password | Passwords to login to Windows instance. |
8486
| instance\_username | Usernames to login to Windows instance. |
87+
| instances\_summary | Private and Public IPs for each instance. |
8588
| private\_ip | Private IPs of created instances. |
8689
| public\_ip | Public IPs of created instances. |
8790

examples/instance_default/README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!-- TO BE EDITED -->
12
## Create Compute Instance
23
This example creates a compute instance and a set of block volumes.
34

@@ -30,8 +31,8 @@ block_storage_sizes_in_gbs = [60, 70]
3031

3132
Then apply the example using the following commands:
3233

33-
```
34-
$ terraform init
35-
$ terraform plan
36-
$ terraform apply
34+
```shell
35+
> terraform init
36+
> terraform plan
37+
> terraform apply
3738
```

examples/instance_default/instance_default.tf

+57-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ variable "block_storage_sizes_in_gbs" {
4545
}
4646

4747
variable "shape" {
48-
type = string
48+
type = string
49+
default = null
4950
}
5051

5152
variable "assign_public_ip" {
@@ -64,16 +65,67 @@ provider "oci" {
6465
region = var.region
6566
}
6667

67-
module "instance" {
68+
# * This module will create a Flex Compute Instance, using default values: 1 OCPU, 16 GB memory.
69+
module "instance_flex" {
6870
source = "../../"
69-
instance_count = var.instance_count
71+
instance_count = 1
72+
shape = "VM.Standard.E3.Flex"
7073
ad_number = 1
7174
compartment_ocid = var.compartment_ocid
72-
instance_display_name = var.instance_display_name
75+
instance_display_name = "instance_flex"
76+
source_ocid = var.source_ocid
77+
subnet_ocids = var.subnet_ocids
78+
ssh_authorized_keys = var.ssh_authorized_keys
79+
block_storage_sizes_in_gbs = var.block_storage_sizes_in_gbs
80+
assign_public_ip = var.assign_public_ip
81+
}
82+
83+
# * This module will create a Flex Compute Instance, using values provided by the module: 1 OCPU, 1 GB memory.
84+
module "instance_flex_custom" {
85+
source = "../../"
86+
instance_count = 2
87+
shape = "VM.Standard.E3.Flex"
88+
instance_flex_memory_in_gbs = 1
89+
instance_flex_ocpus = 1
90+
ad_number = 1
91+
compartment_ocid = var.compartment_ocid
92+
instance_display_name = "instance_flex_custom"
93+
source_ocid = var.source_ocid
94+
subnet_ocids = var.subnet_ocids
95+
ssh_authorized_keys = var.ssh_authorized_keys
96+
block_storage_sizes_in_gbs = var.block_storage_sizes_in_gbs
97+
assign_public_ip = var.assign_public_ip
98+
}
99+
100+
# * This module will create a shape-based Compute Instance. OCPU and memory values are defined by the shape.
101+
module "instance_nonflex" {
102+
source = "../../"
103+
instance_count = 1
104+
shape = "VM.Standard2.1"
105+
ad_number = 2
106+
compartment_ocid = var.compartment_ocid
107+
instance_display_name = "instance_nonflex"
73108
source_ocid = var.source_ocid
74109
subnet_ocids = var.subnet_ocids
75110
ssh_authorized_keys = var.ssh_authorized_keys
76111
block_storage_sizes_in_gbs = var.block_storage_sizes_in_gbs
77-
shape = var.shape
78112
assign_public_ip = var.assign_public_ip
79113
}
114+
115+
# * This module will create a shape-based Compute instance. OCPU and memory values are defined by the shape.
116+
# * `instance_flex_memory_in_gbs` and ìnstance_flex_ocpus` values are ignored.
117+
module "instance_nonflex_custom" {
118+
source = "../../"
119+
instance_count = 1
120+
shape = "VM.Standard2.1"
121+
instance_flex_memory_in_gbs = 8
122+
instance_flex_ocpus = 1
123+
ad_number = 2
124+
compartment_ocid = var.compartment_ocid
125+
instance_display_name = "instance_nonflex_custom"
126+
source_ocid = var.source_ocid
127+
subnet_ocids = var.subnet_ocids
128+
ssh_authorized_keys = var.ssh_authorized_keys
129+
block_storage_sizes_in_gbs = var.block_storage_sizes_in_gbs
130+
assign_public_ip = var.assign_public_ip
131+
}

examples/instance_default/outputs.tf

+11-17
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
// Copyright (c) 2018, 2021 Oracle and/or its affiliates.
22

3-
output "instance_id" {
3+
output "instance_flex" {
44
description = "ocid of created instances. "
5-
value = module.instance.instance_id
5+
value = module.instance_flex.instances_summary
66
}
77

8-
output "private_ip" {
9-
description = "Private IPs of created instances. "
10-
value = module.instance.private_ip
11-
}
12-
13-
output "public_ip" {
14-
description = "Public IPs of created instances. "
15-
value = module.instance.public_ip
8+
output "instance_flex_custom" {
9+
description = "ocid of created instances. "
10+
value = module.instance_flex_custom.instances_summary
1611
}
1712

18-
output "instance_username" {
19-
description = "Usernames to login to Windows instance. "
20-
value = module.instance.instance_username
13+
output "instance_nonflex" {
14+
description = "ocid of created instances. "
15+
value = module.instance_nonflex.instances_summary
2116
}
2217

23-
output "instance_password" {
24-
description = "Passwords to login to Windows instance. "
25-
sensitive = true
26-
value = module.instance.instance_password
18+
output "instance_nonflex_custom" {
19+
description = "ocid of created instances. "
20+
value = module.instance_nonflex_custom.instances_summary
2721
}

main.tf

+29
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ data "oci_core_subnet" "this" {
2929
subnet_id = element(var.subnet_ocids, count.index)
3030
}
3131

32+
############
33+
# Shapes
34+
############
35+
36+
// Create a data source for compute shapes.
37+
// Filter on AD1 to remove duplicates. This should give all the shapes supported on the region.
38+
// This will not check quota and limits for AD requested at resource creation
39+
data "oci_core_shapes" "ad1" {
40+
compartment_id = var.compartment_ocid
41+
availability_domain = local.ADs[0]
42+
}
43+
44+
locals {
45+
shapes_config = {
46+
// Iterate through data.oci_core_shapes.ad1.shapes and create a map { name = { memory_in_gbs = "xx"; ocpus = "xx" } }
47+
for i in data.oci_core_shapes.ad1.shapes : i.name => {
48+
"memory_in_gbs" = i.memory_in_gbs
49+
"ocpus" = i.ocpus
50+
}
51+
}
52+
shape_is_flex = length(regexall("^*.Flex", var.shape)) > 0
53+
}
54+
3255
############
3356
# Instance
3457
############
@@ -42,6 +65,12 @@ resource "oci_core_instance" "this" {
4265
ipxe_script = var.ipxe_script
4366
preserve_boot_volume = var.preserve_boot_volume
4467
shape = var.shape
68+
shape_config {
69+
// If shape name contains ".Flex" and instance_flex inputs are not null, use instance_flex inputs values for shape_config block
70+
// Else use values from data.oci_core_shapes.ad1 for var.shape
71+
memory_in_gbs = local.shape_is_flex == true && var.instance_flex_memory_in_gbs != null ? var.instance_flex_memory_in_gbs : local.shapes_config[var.shape]["memory_in_gbs"]
72+
ocpus = local.shape_is_flex == true && var.instance_flex_ocpus != null ? var.instance_flex_ocpus : local.shapes_config[var.shape]["ocpus"]
73+
}
4574

4675
create_vnic_details {
4776
assign_public_ip = var.assign_public_ip

outputs.tf

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
// Copyright (c) 2018, 2021, Oracle and/or its affiliates.
22

3+
locals {
4+
instances_details = [
5+
// display name, Primary VNIC Public/Private IP for each instance
6+
for i in oci_core_instance.this : <<EOT
7+
${~i.display_name~}
8+
Primary-PublicIP: %{if i.public_ip != ""}${i.public_ip~}%{else}N/A%{endif~}
9+
Primary-PrivateIP: ${i.private_ip~}
10+
EOT
11+
]
12+
}
13+
14+
output "instances_summary" {
15+
description = "Private and Public IPs for each instance."
16+
value = local.instances_details
17+
}
18+
319
output "instance_id" {
420
description = "ocid of created instances. "
5-
value = [oci_core_instance.this.*.id]
21+
value = oci_core_instance.this.*.id
622
}
723

824
output "private_ip" {
925
description = "Private IPs of created instances. "
10-
value = [oci_core_instance.this.*.private_ip]
26+
value = oci_core_instance.this.*.private_ip
1127
}
1228

1329
output "public_ip" {
1430
description = "Public IPs of created instances. "
15-
value = [oci_core_instance.this.*.public_ip]
31+
value = oci_core_instance.this.*.public_ip
1632
}
1733

1834
output "instance_username" {
1935
description = "Usernames to login to Windows instance. "
20-
value = [data.oci_core_instance_credentials.this.*.username]
36+
value = data.oci_core_instance_credentials.this.*.username
2137
}
2238

2339
output "instance_password" {
2440
description = "Passwords to login to Windows instance. "
2541
sensitive = true
26-
value = [data.oci_core_instance_credentials.this.*.password]
42+
value = data.oci_core_instance_credentials.this.*.password
2743
}
28-

variables.tf

+12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ variable "shape" {
5454
default = "VM.Standard2.1"
5555
}
5656

57+
variable "instance_flex_memory_in_gbs" {
58+
type = number
59+
description = "(Optional) (Updatable) The total amount of memory available to the instance, in gigabytes."
60+
default = null
61+
}
62+
63+
variable "instance_flex_ocpus" {
64+
type = number
65+
description = "(Optional) (Updatable) The total number of OCPUs available to the instance."
66+
default = null
67+
}
68+
5769
variable "assign_public_ip" {
5870
description = "Whether the VNIC should be assigned a public IP address."
5971
type = bool

0 commit comments

Comments
 (0)