Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Flex instance shape support #49

Merged
merged 2 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.txt text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=4
*.md text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=4
*.tf text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=4
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0 # Get the latest from: https://github.com/pre-commit/pre-commit-hooks/releases
hooks:
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
- repo: https://github.com/gruntwork-io/pre-commit
rev: v0.1.12 # Get the latest from: https://github.com/gruntwork-io/pre-commit/releases
hooks:
- id: terraform-validate
# - id: terraform-fmt #* this version of tf-fmt detect problem and fails. You have to run tf fmt yourself
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.45.0 # Get the latest from: https://github.com/antonbabenko/pre-commit-terraform/releases
hooks:
- id: terraform_docs
- id: terraform_fmt #* this version of tf-fmt actually runs fmt and fix files
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ Given a version number MAJOR.MINOR.PATCH:

## [UNRELEASED]

Added:

- support Flex instance shape
- new output : instances_summary

Fixed:

- Outputs produces unnecessarily multidimensional objects (Issue #31)

Repo maintenance:

- add .gitattributes for consistent line ending and tab
- add pre-commit configuration file

## 2.0.4 - 2021-02-13

### Changed
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ module "instance" {
| hostname\_label | The hostname for the VNIC's primary private IP. | `string` | `""` | no |
| instance\_count | Number of instances to launch. | `number` | `1` | no |
| instance\_display\_name | (Optional) (Updatable) A user-friendly name for the instance. Does not have to be unique, and it's changeable. | `string` | `""` | no |
| instance\_flex\_memory\_in\_gbs | (Optional) (Updatable) The total amount of memory available to the instance, in gigabytes. | `number` | `null` | no |
| instance\_flex\_ocpus | (Optional) (Updatable) The total number of OCPUs available to the instance. | `number` | `null` | no |
| instance\_timeout | Timeout setting for creating instance. | `string` | `"25m"` | no |
| ipxe\_script | (Optional) The iPXE script which to continue the boot process on the instance. | `string` | `null` | no |
| preserve\_boot\_volume | Specifies whether to delete or preserve the boot volume when terminating an instance. | `bool` | `false` | no |
Expand All @@ -82,6 +84,7 @@ module "instance" {
| instance\_id | ocid of created instances. |
| instance\_password | Passwords to login to Windows instance. |
| instance\_username | Usernames to login to Windows instance. |
| instances\_summary | Private and Public IPs for each instance. |
| private\_ip | Private IPs of created instances. |
| public\_ip | Public IPs of created instances. |

Expand Down
9 changes: 5 additions & 4 deletions examples/instance_default/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!-- TO BE EDITED -->
## Create Compute Instance
This example creates a compute instance and a set of block volumes.

Expand Down Expand Up @@ -30,8 +31,8 @@ block_storage_sizes_in_gbs = [60, 70]

Then apply the example using the following commands:

```
$ terraform init
$ terraform plan
$ terraform apply
```shell
> terraform init
> terraform plan
> terraform apply
```
62 changes: 57 additions & 5 deletions examples/instance_default/instance_default.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ variable "block_storage_sizes_in_gbs" {
}

variable "shape" {
type = string
type = string
default = null
}

variable "assign_public_ip" {
Expand All @@ -64,16 +65,67 @@ provider "oci" {
region = var.region
}

module "instance" {
# * This module will create a Flex Compute Instance, using default values: 1 OCPU, 16 GB memory.
module "instance_flex" {
source = "../../"
instance_count = var.instance_count
instance_count = 1
shape = "VM.Standard.E3.Flex"
ad_number = 1
compartment_ocid = var.compartment_ocid
instance_display_name = var.instance_display_name
instance_display_name = "instance_flex"
source_ocid = var.source_ocid
subnet_ocids = var.subnet_ocids
ssh_authorized_keys = var.ssh_authorized_keys
block_storage_sizes_in_gbs = var.block_storage_sizes_in_gbs
assign_public_ip = var.assign_public_ip
}

# * This module will create a Flex Compute Instance, using values provided by the module: 1 OCPU, 1 GB memory.
module "instance_flex_custom" {
source = "../../"
instance_count = 2
shape = "VM.Standard.E3.Flex"
instance_flex_memory_in_gbs = 1
instance_flex_ocpus = 1
ad_number = 1
compartment_ocid = var.compartment_ocid
instance_display_name = "instance_flex_custom"
source_ocid = var.source_ocid
subnet_ocids = var.subnet_ocids
ssh_authorized_keys = var.ssh_authorized_keys
block_storage_sizes_in_gbs = var.block_storage_sizes_in_gbs
assign_public_ip = var.assign_public_ip
}

# * This module will create a shape-based Compute Instance. OCPU and memory values are defined by the shape.
module "instance_nonflex" {
source = "../../"
instance_count = 1
shape = "VM.Standard2.1"
ad_number = 2
compartment_ocid = var.compartment_ocid
instance_display_name = "instance_nonflex"
source_ocid = var.source_ocid
subnet_ocids = var.subnet_ocids
ssh_authorized_keys = var.ssh_authorized_keys
block_storage_sizes_in_gbs = var.block_storage_sizes_in_gbs
shape = var.shape
assign_public_ip = var.assign_public_ip
}

# * This module will create a shape-based Compute instance. OCPU and memory values are defined by the shape.
# * `instance_flex_memory_in_gbs` and ìnstance_flex_ocpus` values are ignored.
module "instance_nonflex_custom" {
source = "../../"
instance_count = 1
shape = "VM.Standard2.1"
instance_flex_memory_in_gbs = 8
instance_flex_ocpus = 1
ad_number = 2
compartment_ocid = var.compartment_ocid
instance_display_name = "instance_nonflex_custom"
source_ocid = var.source_ocid
subnet_ocids = var.subnet_ocids
ssh_authorized_keys = var.ssh_authorized_keys
block_storage_sizes_in_gbs = var.block_storage_sizes_in_gbs
assign_public_ip = var.assign_public_ip
}
28 changes: 11 additions & 17 deletions examples/instance_default/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
// Copyright (c) 2018, 2021 Oracle and/or its affiliates.

output "instance_id" {
output "instance_flex" {
description = "ocid of created instances. "
value = module.instance.instance_id
value = module.instance_flex.instances_summary
}

output "private_ip" {
description = "Private IPs of created instances. "
value = module.instance.private_ip
}

output "public_ip" {
description = "Public IPs of created instances. "
value = module.instance.public_ip
output "instance_flex_custom" {
description = "ocid of created instances. "
value = module.instance_flex_custom.instances_summary
}

output "instance_username" {
description = "Usernames to login to Windows instance. "
value = module.instance.instance_username
output "instance_nonflex" {
description = "ocid of created instances. "
value = module.instance_nonflex.instances_summary
}

output "instance_password" {
description = "Passwords to login to Windows instance. "
sensitive = true
value = module.instance.instance_password
output "instance_nonflex_custom" {
description = "ocid of created instances. "
value = module.instance_nonflex_custom.instances_summary
}
29 changes: 29 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ data "oci_core_subnet" "this" {
subnet_id = element(var.subnet_ocids, count.index)
}

############
# Shapes
############

// Create a data source for compute shapes.
// Filter on AD1 to remove duplicates. This should give all the shapes supported on the region.
// This will not check quota and limits for AD requested at resource creation
data "oci_core_shapes" "ad1" {
compartment_id = var.compartment_ocid
availability_domain = local.ADs[0]
}

locals {
shapes_config = {
// Iterate through data.oci_core_shapes.ad1.shapes and create a map { name = { memory_in_gbs = "xx"; ocpus = "xx" } }
for i in data.oci_core_shapes.ad1.shapes : i.name => {
"memory_in_gbs" = i.memory_in_gbs
"ocpus" = i.ocpus
}
}
shape_is_flex = length(regexall("^*.Flex", var.shape)) > 0
}

############
# Instance
############
Expand All @@ -42,6 +65,12 @@ resource "oci_core_instance" "this" {
ipxe_script = var.ipxe_script
preserve_boot_volume = var.preserve_boot_volume
shape = var.shape
shape_config {
// If shape name contains ".Flex" and instance_flex inputs are not null, use instance_flex inputs values for shape_config block
// Else use values from data.oci_core_shapes.ad1 for var.shape
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"]
ocpus = local.shape_is_flex == true && var.instance_flex_ocpus != null ? var.instance_flex_ocpus : local.shapes_config[var.shape]["ocpus"]
}

create_vnic_details {
assign_public_ip = var.assign_public_ip
Expand Down
27 changes: 21 additions & 6 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
// Copyright (c) 2018, 2021, Oracle and/or its affiliates.

locals {
instances_details = [
// display name, Primary VNIC Public/Private IP for each instance
for i in oci_core_instance.this : <<EOT
${~i.display_name~}
Primary-PublicIP: %{if i.public_ip != ""}${i.public_ip~}%{else}N/A%{endif~}
Primary-PrivateIP: ${i.private_ip~}
EOT
]
}

output "instances_summary" {
description = "Private and Public IPs for each instance."
value = local.instances_details
}

output "instance_id" {
description = "ocid of created instances. "
value = [oci_core_instance.this.*.id]
value = oci_core_instance.this.*.id
}

output "private_ip" {
description = "Private IPs of created instances. "
value = [oci_core_instance.this.*.private_ip]
value = oci_core_instance.this.*.private_ip
}

output "public_ip" {
description = "Public IPs of created instances. "
value = [oci_core_instance.this.*.public_ip]
value = oci_core_instance.this.*.public_ip
}

output "instance_username" {
description = "Usernames to login to Windows instance. "
value = [data.oci_core_instance_credentials.this.*.username]
value = data.oci_core_instance_credentials.this.*.username
}

output "instance_password" {
description = "Passwords to login to Windows instance. "
sensitive = true
value = [data.oci_core_instance_credentials.this.*.password]
value = data.oci_core_instance_credentials.this.*.password
}

12 changes: 12 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ variable "shape" {
default = "VM.Standard2.1"
}

variable "instance_flex_memory_in_gbs" {
type = number
description = "(Optional) (Updatable) The total amount of memory available to the instance, in gigabytes."
default = null
}

variable "instance_flex_ocpus" {
type = number
description = "(Optional) (Updatable) The total number of OCPUs available to the instance."
default = null
}

variable "assign_public_ip" {
description = "Whether the VNIC should be assigned a public IP address."
type = bool
Expand Down