From b6a2019d2636f45b79a13e0fecc16e98fd7f5487 Mon Sep 17 00:00:00 2001 From: Roman Zhuzha Date: Tue, 9 Nov 2021 16:33:07 +0100 Subject: [PATCH 1/9] add simple example for oci_core_instance --- oci/oci_core_instance/simple/main.tf | 65 ++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 oci/oci_core_instance/simple/main.tf diff --git a/oci/oci_core_instance/simple/main.tf b/oci/oci_core_instance/simple/main.tf new file mode 100644 index 00000000..18e1c2f2 --- /dev/null +++ b/oci/oci_core_instance/simple/main.tf @@ -0,0 +1,65 @@ +# Summary: Creates the simplest VM instance + +# Documentation: https://www.terraform.io/docs/language/settings/index.html +terraform { + required_version = ">= 1.0.0" + required_providers { + oci = { + source = "hashicorp/oci" + version = "~> 4.51.0" + } + } +} + +# Oracle Cloud Infrastructure Provider +# Documentation: https://registry.terraform.io/providers/hashicorp/oci/latest/docs +provider "oci" { +} + +# The OCID of the compartment +# Documentation: https://registry.terraform.io/providers/hashicorp/oci/latest/docs/resources/core_instance#compartment_id +variable "compartment_id" { + type = string +} + +# Subnet to create the VNIC in +# Documentation: https://registry.terraform.io/providers/hashicorp/oci/latest/docs/resources/core_instance#subnet_id +variable "subnet_id" { + type = string +} + +# Oracle Cloud Infrastructure Images +# https://docs.oracle.com/en-us/iaas/images/ubuntu-2004/ +variable "image_id" { + type = string + default = "ocid1.image.oc1.eu-amsterdam-1.aaaaaaaawlrfkqdc4fm4tco6ifgmd4pcbjg232hyo6gscvu6xcgnufdznqtq" +} + +# Data source for a single Availability Domain +# Documentation: https://registry.terraform.io/providers/hashicorp/oci/latest/docs/data-sources/identity_availability_domain +data "oci_identity_availability_domain" "ad" { + compartment_id = var.compartment_id + ad_number = 1 +} + +# Instance resource +# Documentation: https://registry.terraform.io/providers/hashicorp/oci/latest/docs/resources/core_instance +resource "oci_core_instance" "changeme_oci_simple_instance" { + availability_domain = data.oci_identity_availability_domain.ad.name + compartment_id = data.oci_identity_availability_domain.ad.compartment_id + display_name = "changeme-oci-simple-instance" + shape = "VM.Standard.E2.1.Micro" + + create_vnic_details { + subnet_id = var.subnet_id + } + + source_details { + source_type = "image" + source_id = var.image_id + } + + freeform_tags = { + cs_terraform_examples = "oci_core_instance/simple" + } +} From 47bc20376bd72a9c9d4811f69c8a5f42194556d9 Mon Sep 17 00:00:00 2001 From: Roman Zhuzha Date: Wed, 10 Nov 2021 21:38:56 +0100 Subject: [PATCH 2/9] add required scripts for oci examples deploy --- bin/apply.sh | 2 +- bin/destroy.sh | 2 +- bin/get_oci_folders.sh | 3 ++ bin/run_oci_examples.sh | 44 +++++++++++++++++++++++++ bin/shared_oci.sh | 30 +++++++++++++++++ oci/oci_core_instance/simple/destroy.sh | 2 ++ oci/oci_core_instance/simple/run.sh | 2 ++ 7 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 bin/get_oci_folders.sh create mode 100755 bin/run_oci_examples.sh create mode 100644 bin/shared_oci.sh create mode 100755 oci/oci_core_instance/simple/destroy.sh create mode 100755 oci/oci_core_instance/simple/run.sh diff --git a/bin/apply.sh b/bin/apply.sh index d1185ffe..a4cd3a5e 100755 --- a/bin/apply.sh +++ b/bin/apply.sh @@ -1,6 +1,6 @@ #!/bin/bash -SUPPORTED_PROVIDERS="aws|azurerm|digitalocean|google|kubernetes|linode" +SUPPORTED_PROVIDERS="aws|azurerm|digitalocean|google|kubernetes|linode|oci" if [[ -n "$1" && ! "$1" =~ ${SUPPORTED_PROVIDERS} ]] then echo "usage: $0 [PROVIDER]" diff --git a/bin/destroy.sh b/bin/destroy.sh index a2e84a00..6430ce53 100755 --- a/bin/destroy.sh +++ b/bin/destroy.sh @@ -1,6 +1,6 @@ #!/bin/bash -SUPPORTED_PROVIDERS="aws|azurerm|digitalocean|google|kubernetes|linode" +SUPPORTED_PROVIDERS="aws|azurerm|digitalocean|google|kubernetes|linode|oci" if [[ -n "$1" && ! "$1" =~ ${SUPPORTED_PROVIDERS} ]] then echo "usage: $0 [PROVIDER]" diff --git a/bin/get_oci_folders.sh b/bin/get_oci_folders.sh new file mode 100644 index 00000000..806d6a6b --- /dev/null +++ b/bin/get_oci_folders.sh @@ -0,0 +1,3 @@ +#!/bin/bash +# shellcheck disable=SC2034 +OCI_FOLDERS="$(find oci | grep tf$ | xargs -n1 dirname | sed 's/^.\///')" diff --git a/bin/run_oci_examples.sh b/bin/run_oci_examples.sh new file mode 100755 index 00000000..43c7f6f0 --- /dev/null +++ b/bin/run_oci_examples.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +set -o errexit +set -o nounset + +cd "${0%/*}/.." + +echo "$OCI_PROFILE_FILE" > "$OCI_PROFILE_FILENAME" +echo "$OCI_KEY_FILE_FILE" > "$OCI_KEY_FILE_FILENAME" + +last_successful_commit=$(./bin/get_last_successful_commit.sh) +changed_folders=$(./bin/get_changed_folders.sh) +# shellcheck disable=SC1091 +source bin/get_oci_folders.sh +for folder in ${OCI_FOLDERS} +do + echo "================================================================================" + echo -n "$0 checking folder: ${folder} ... " + if [[ -a ${folder}/.skiptest ]] + then + echo -n "found .skiptest file, skipping " + continue + fi + if ! echo "$changed_folders" | tr ' ' '\n' | grep "$folder" + then + if [[ -a oci/.forcetest ]] + then + echo "oci/.forcetest file exists, forcing test run" + elif [[ -a ${folder}/.forcetest ]] + then + echo "${folder}/.forcetest file exists, forcing test run" + else + echo "Folder ${folder} has not changed since last successful test on main (${last_successful_commit})" + continue + fi + fi + pushd "${folder}" >/dev/null + echo -n "./run.sh" + # If the run fails, try and clean up + ./run.sh || ( ./destroy.sh && exit 1 ) + echo -n "./destroy.sh" + ./destroy.sh + popd >/dev/null +done diff --git a/bin/shared_oci.sh b/bin/shared_oci.sh new file mode 100644 index 00000000..b89d91a6 --- /dev/null +++ b/bin/shared_oci.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +set -o errexit +set -o pipefail + + +if [ -z "$OCI_CLI_CONFIG_FILE" ] +then + OCI_CLI_CONFIG_FILE="${HOME}/.oci/config" + echo "By default we look for oci config file in ${OCI_CLI_CONFIG_FILE}" + echo "To use a custom oci config path, run 'export OCI_CLI_CONFIG_FILE='" + if [ ! -f "$OCI_CLI_CONFIG_FILE" ] + then + echo "No config file was found" + echo "Make sure oci-cli is initialised, by running: 'oci setup config'" + echo "More info available here: https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/cliinstall.htm#configfile__setupdialog" + fi +fi +if [ -z "$TF_VAR_config_file_profile" ] +then + TF_VAR_config_file_profile="DEFAULT" + echo "By default we use profile: ${TF_VAR_config_file_profile}" + echo "To use a custom profile, run 'export TF_VAR_config_file_profile='" + echo "More info avaiable at: https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/terraformproviderconfiguration.htm#terraformproviderconfiguration_topic-SDK_and_CLI_Config_File" +fi + +set -o nounset + +export OCI_CLI_CONFIG_FILE +export TF_VAR_config_file_profile diff --git a/oci/oci_core_instance/simple/destroy.sh b/oci/oci_core_instance/simple/destroy.sh new file mode 100755 index 00000000..4d08f021 --- /dev/null +++ b/oci/oci_core_instance/simple/destroy.sh @@ -0,0 +1,2 @@ +#!/bin/bash +../../../bin/destroy.sh oci diff --git a/oci/oci_core_instance/simple/run.sh b/oci/oci_core_instance/simple/run.sh new file mode 100755 index 00000000..8d970e02 --- /dev/null +++ b/oci/oci_core_instance/simple/run.sh @@ -0,0 +1,2 @@ +#!/bin/bash +../../../bin/apply.sh oci From 3cca375428713ef422f9078b00aae248906411c5 Mon Sep 17 00:00:00 2001 From: Roman Zhuzha Date: Wed, 10 Nov 2021 21:41:05 +0100 Subject: [PATCH 3/9] add github workflow for oci provider --- .github/workflows/main.yml | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 08fd4bd6..4050a8df 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -154,6 +154,34 @@ jobs: DIGITALOCEAN_TOKEN: ${{ secrets.DIGITALOCEAN_TOKEN }} if: github.ref == 'refs/heads/main' || env.GITHUB_REF_SLUG == 'integration' # Only run on main/integration, as these take a long time run: ${{github.workspace}}/bin/run_digitalocean_examples.sh + ProvidersOCI: + concurrency: provider_test_oci + needs: [TFLint, TFValidate, Checks] + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v2 + with: + fetch-depth: 0 # Need whole history, or at least far enough back to get .test_log.log reference + - name: Setup Terraform + uses: hashicorp/setup-terraform@v1 + with: + terraform_version: ${{ env.terraform_version }} + terraform_wrapper: false + cli_config_credentials_token: ${{ secrets.TERRAFORM_CLOUD_LOGIN_TOKEN }} + - name: Inject slug/short variables + uses: rlespinasse/github-slug-action@v3.x + - name: Run OCI examples + env: + CI_USE_TF_CLOUD: ${{ secrets.CI_USE_TF_CLOUD }} + TERRAFORM_CLOUD_LOGIN_TOKEN: ${{ secrets.TERRAFORM_CLOUD_LOGIN_TOKEN }} + OCI_PROFILE_FILE: ${{ secrets.OCI_PROFILE_FILE }} + OCI_PROFILE_FILENAME: ${{ secrets.OCI_PROFILE_FILENAME }} + OCI_KEY_FILE_FILE: ${{ secrets.OCI_KEY_FILE_FILE }} + OCI_KEY_FILE_FILENAME: ${{ secrets.OCI_KEY_FILE_FILENAME }} + TF_VAR_config_file_profile: ${{ secrets.OCI_CLI_PROFILE }} + if: github.ref == 'refs/heads/main' || env.GITHUB_REF_SLUG == 'integration' # Only run on main/integration, as these take a long time + run: ${{github.workspace}}/bin/run_oci_examples.sh ProvidersAzure: concurrency: provider_test_azure needs: [TFLint, TFValidate, Checks] @@ -207,7 +235,7 @@ jobs: run: ${{github.workspace}}/bin/run_local_examples.sh RecordActionSuccess: runs-on: ubuntu-latest - needs: [ProvidersLocal, ProvidersAzure, ProvidersDigitalocean, ProvidersLinode, ProvidersGCP, ProvidersAWS] + needs: [ProvidersLocal, ProvidersAzure, ProvidersDigitalocean, ProvidersOCI, ProvidersLinode, ProvidersGCP, ProvidersAWS] if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/integration' # Only run on main/integration, as we run provider tests only there steps: - name: Check out repository code From 15d5e18afe4119c0bd6db9b13da60ef550c861ea Mon Sep 17 00:00:00 2001 From: Roman Zhuzha Date: Wed, 10 Nov 2021 21:43:45 +0100 Subject: [PATCH 4/9] add note about OCI_CLI_CONFIG_FILE --- bin/shared_oci.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/shared_oci.sh b/bin/shared_oci.sh index b89d91a6..f03c198b 100644 --- a/bin/shared_oci.sh +++ b/bin/shared_oci.sh @@ -15,6 +15,7 @@ then echo "Make sure oci-cli is initialised, by running: 'oci setup config'" echo "More info available here: https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/cliinstall.htm#configfile__setupdialog" fi + echo "NOTE: Terraform provider doesn't use this value. It is used only by oci-cli." fi if [ -z "$TF_VAR_config_file_profile" ] then From 7df10b7506aaa0a54e927965bb690fd2fda04390 Mon Sep 17 00:00:00 2001 From: Roman Zhuzha Date: Fri, 12 Nov 2021 19:08:42 +0100 Subject: [PATCH 5/9] fix format --- oci/oci_core_instance/simple/main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/oci/oci_core_instance/simple/main.tf b/oci/oci_core_instance/simple/main.tf index 18e1c2f2..0f925195 100644 --- a/oci/oci_core_instance/simple/main.tf +++ b/oci/oci_core_instance/simple/main.tf @@ -19,13 +19,13 @@ provider "oci" { # The OCID of the compartment # Documentation: https://registry.terraform.io/providers/hashicorp/oci/latest/docs/resources/core_instance#compartment_id variable "compartment_id" { - type = string + type = string } # Subnet to create the VNIC in # Documentation: https://registry.terraform.io/providers/hashicorp/oci/latest/docs/resources/core_instance#subnet_id variable "subnet_id" { - type = string + type = string } # Oracle Cloud Infrastructure Images From 93833b9d37876a4d49d65537868d44cae6f6eb24 Mon Sep 17 00:00:00 2001 From: Roman Zhuzha Date: Fri, 12 Nov 2021 20:01:12 +0100 Subject: [PATCH 6/9] add oci_core_instance to INDEX.md --- INDEX.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/INDEX.md b/INDEX.md index cef6bc6c..404a5e6e 100644 --- a/INDEX.md +++ b/INDEX.md @@ -3,7 +3,7 @@ | Feature | Links/Related Features | | ------------- |:-------------:| | `aws_db_cluster_snapshot` | [aws](aws/aws_db_cluster_snapshot)

[simple](aws/aws_db_cluster_snapshot/simple) | -| `aws_db_instance` | [aws](aws/aws_db_instance)

[simple](aws/aws_db_instance/simple)

[postgres](aws/aws_db_instance/postgres)

[restore_db_from_snapshot](aws/aws_db_instance/restore_db_from_snapshot) | +| `aws_db_instance` | [aws](aws/aws_db_instance)

[simple](aws/aws_db_instance/simple)

[postgres](aws/aws_db_instance/postgres)

[restore_db_from_snapshot](aws/aws_db_instance/restore_db_from_snapshot) | | `aws_dynamodb_table_item` | [aws](aws/aws_dynamodb_table_item)

[simple](aws/aws_dynamodb_table_item/simple) | | `aws_dynamodb_table` | [aws](aws/aws_dynamodb_table)

[simple](aws/aws_dynamodb_table/simple) | | `aws_ebs_volume` | [aws](aws/aws_ebs_volume)

[simple](aws/aws_ebs_volume/simple)

[volume_attachment](aws/aws_ebs_volume/volume_attachment)

[ebs_snapshot](aws/aws_ebs_volume/ebs_snapshot) | @@ -83,6 +83,7 @@ | `map` | [null_resource, for_each, local](local/null_resource/for_each) | | `module` | [modules](modules)

[local, module usage](variables/local_file/module)

[local, module example](modules/local_file/hello_module)

[local, module example consumer](modules/local_file/hello_consumer) | | `null_resource` | [simple, local](local/null_resource/simple) | +| `oci_core_instance` | [oci, oci_core_instance](oci/oci_core_instance/simple) | | `outputs` | [outputs](outputs)

[local](outputs/local_file/local_file)

[local, module](outputs/local_file/module) | | `random_id` | [simple](aws/aws_s3_bucket/simple) | | `remote-exec` | [aws, inline](aws/aws_instance/remote-exec/inline) | From 4f308716910f074688e6e1fe70166ea37949823b Mon Sep 17 00:00:00 2001 From: Roman Zhuzha Date: Fri, 12 Nov 2021 20:05:31 +0100 Subject: [PATCH 7/9] follow naming convention --- oci/oci_core_instance/simple/main.tf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/oci/oci_core_instance/simple/main.tf b/oci/oci_core_instance/simple/main.tf index 0f925195..f40d5b0b 100644 --- a/oci/oci_core_instance/simple/main.tf +++ b/oci/oci_core_instance/simple/main.tf @@ -37,7 +37,7 @@ variable "image_id" { # Data source for a single Availability Domain # Documentation: https://registry.terraform.io/providers/hashicorp/oci/latest/docs/data-sources/identity_availability_domain -data "oci_identity_availability_domain" "ad" { +data "oci_identity_availability_domain" "changeme_availability_domain" { compartment_id = var.compartment_id ad_number = 1 } @@ -45,8 +45,8 @@ data "oci_identity_availability_domain" "ad" { # Instance resource # Documentation: https://registry.terraform.io/providers/hashicorp/oci/latest/docs/resources/core_instance resource "oci_core_instance" "changeme_oci_simple_instance" { - availability_domain = data.oci_identity_availability_domain.ad.name - compartment_id = data.oci_identity_availability_domain.ad.compartment_id + availability_domain = data.oci_identity_availability_domain.changeme_availability_domain.name + compartment_id = data.oci_identity_availability_domain.changeme_availability_domain.compartment_id display_name = "changeme-oci-simple-instance" shape = "VM.Standard.E2.1.Micro" From 7b0e3f9f1c87923fb88e37b3845dbe99c1adb510 Mon Sep 17 00:00:00 2001 From: Roman Zhuzha Date: Fri, 12 Nov 2021 20:10:14 +0100 Subject: [PATCH 8/9] fix variable names --- oci/oci_core_instance/simple/main.tf | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/oci/oci_core_instance/simple/main.tf b/oci/oci_core_instance/simple/main.tf index f40d5b0b..4464b37e 100644 --- a/oci/oci_core_instance/simple/main.tf +++ b/oci/oci_core_instance/simple/main.tf @@ -18,19 +18,19 @@ provider "oci" { # The OCID of the compartment # Documentation: https://registry.terraform.io/providers/hashicorp/oci/latest/docs/resources/core_instance#compartment_id -variable "compartment_id" { +variable "changeme_compartment_id" { type = string } # Subnet to create the VNIC in # Documentation: https://registry.terraform.io/providers/hashicorp/oci/latest/docs/resources/core_instance#subnet_id -variable "subnet_id" { +variable "changeme_subnet_id" { type = string } # Oracle Cloud Infrastructure Images # https://docs.oracle.com/en-us/iaas/images/ubuntu-2004/ -variable "image_id" { +variable "changeme_image_id" { type = string default = "ocid1.image.oc1.eu-amsterdam-1.aaaaaaaawlrfkqdc4fm4tco6ifgmd4pcbjg232hyo6gscvu6xcgnufdznqtq" } @@ -38,7 +38,7 @@ variable "image_id" { # Data source for a single Availability Domain # Documentation: https://registry.terraform.io/providers/hashicorp/oci/latest/docs/data-sources/identity_availability_domain data "oci_identity_availability_domain" "changeme_availability_domain" { - compartment_id = var.compartment_id + compartment_id = var.changeme_compartment_id ad_number = 1 } @@ -51,12 +51,12 @@ resource "oci_core_instance" "changeme_oci_simple_instance" { shape = "VM.Standard.E2.1.Micro" create_vnic_details { - subnet_id = var.subnet_id + subnet_id = var.changeme_subnet_id } source_details { source_type = "image" - source_id = var.image_id + source_id = var.changeme_image_id } freeform_tags = { From 557f5efce0306bde16ac375a23a483d2ca1d8269 Mon Sep 17 00:00:00 2001 From: Roman Zhuzha Date: Fri, 12 Nov 2021 20:56:52 +0100 Subject: [PATCH 9/9] hardcode already existing required resources --- oci/oci_core_instance/simple/main.tf | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/oci/oci_core_instance/simple/main.tf b/oci/oci_core_instance/simple/main.tf index 4464b37e..38531043 100644 --- a/oci/oci_core_instance/simple/main.tf +++ b/oci/oci_core_instance/simple/main.tf @@ -20,18 +20,23 @@ provider "oci" { # Documentation: https://registry.terraform.io/providers/hashicorp/oci/latest/docs/resources/core_instance#compartment_id variable "changeme_compartment_id" { type = string + + default = "ocid1.compartment.oc1..aaaaaaaavrhfdoos3wg4qnl52iy22pxyiseyjp4yn67x4mukc34dc6uruliq" } # Subnet to create the VNIC in # Documentation: https://registry.terraform.io/providers/hashicorp/oci/latest/docs/resources/core_instance#subnet_id variable "changeme_subnet_id" { type = string + + default = "ocid1.subnet.oc1.eu-amsterdam-1.aaaaaaaawd2edyewykhmjgfqrzxhlpk4q7hvyz5xv6p4tuxbhknp2jwo2xda" } # Oracle Cloud Infrastructure Images # https://docs.oracle.com/en-us/iaas/images/ubuntu-2004/ variable "changeme_image_id" { - type = string + type = string + default = "ocid1.image.oc1.eu-amsterdam-1.aaaaaaaawlrfkqdc4fm4tco6ifgmd4pcbjg232hyo6gscvu6xcgnufdznqtq" }