From f4a2960be951dacaf78b893885a3627ad49aa5fc Mon Sep 17 00:00:00 2001 From: Sean Sundberg Date: Wed, 11 May 2022 23:25:43 -0500 Subject: [PATCH] Updates handling of login closes #30 Signed-off-by: Sean Sundberg --- main.tf | 25 +++++++++++-------------- scripts/bind-instance.sh | 18 +++++++++++++++++- scripts/ibmcloud-login.sh | 17 ----------------- scripts/unbind-instance.sh | 17 +++++++++++++++++ 4 files changed, 45 insertions(+), 32 deletions(-) delete mode 100755 scripts/ibmcloud-login.sh diff --git a/main.tf b/main.tf index 84f8964..151f48a 100644 --- a/main.tf +++ b/main.tf @@ -38,42 +38,39 @@ resource "ibm_resource_key" "logdna_instance_key" { } } -resource null_resource ibmcloud_login { - provisioner "local-exec" { - command = "${path.module}/scripts/ibmcloud-login.sh ${var.region} ${var.resource_group_name}" - - environment = { - BIN_DIR = module.setup_clis.bin_dir - APIKEY = var.ibmcloud_api_key - } - } -} - resource "null_resource" "logdna_bind" { count = local.bind ? 1 : 0 - depends_on = [null_resource.ibmcloud_login] triggers = { bin_dir = module.setup_clis.bin_dir cluster_id = var.cluster_id instance_id = var.logdna_id + region = var.region + resource_group = var.resource_group_name + ibmcloud_api_key = var.ibmcloud_api_key } provisioner "local-exec" { - command = "${path.module}/scripts/bind-instance.sh ${self.triggers.cluster_id} ${self.triggers.instance_id} ${ibm_resource_key.logdna_instance_key[0].name} ${var.private_endpoint}" + command = "${path.module}/scripts/bind-instance.sh '${self.triggers.cluster_id}' '${self.triggers.instance_id}' '${ibm_resource_key.logdna_instance_key[0].name}' '${var.private_endpoint}'" environment = { BIN_DIR = self.triggers.bin_dir SYNC = var.sync + REGION = self.triggers.region + RESOURCE_GROUP = self.triggers.resource_group + IBMCLOUD_API_KEY = self.triggers.ibmcloud_api_key } } provisioner "local-exec" { when = destroy - command = "${path.module}/scripts/unbind-instance.sh ${self.triggers.cluster_id} ${self.triggers.instance_id}" + command = "${path.module}/scripts/unbind-instance.sh '${self.triggers.cluster_id}' '${self.triggers.instance_id}'" environment = { BIN_DIR = self.triggers.bin_dir + REGION = self.triggers.region + RESOURCE_GROUP = self.triggers.resource_group + IBMCLOUD_API_KEY = self.triggers.ibmcloud_api_key } } } diff --git a/scripts/bind-instance.sh b/scripts/bind-instance.sh index fb41c6c..005a302 100755 --- a/scripts/bind-instance.sh +++ b/scripts/bind-instance.sh @@ -18,9 +18,25 @@ if [[ -n "${BIN_DIR}" ]]; then export PATH="${BIN_DIR}:${PATH}" fi +if [[ -z "${IBMCLOUD_API_KEY}" ]]; then + echo "IBMCLOUD_API_KEY must be provided as an environment variable" >&2 + exit 1 +fi + +if [[ -z "${REGION}" ]]; then + echo "REGION must be provided as an environment variable" >&2 + exit 1 +fi + +if [[ -z "${RESOURCE_GROUP}" ]]; then + echo "RESOURCE_GROUP must be provided as an environment variable" >&2 + exit 1 +fi + echo "Configuring LogDNA for ${CLUSTER_ID} cluster and ${INSTANCE_ID} LogDNA instance" -ibmcloud target +ibmcloud login -r "${REGION}" -g "${RESOURCE_GROUP}" + if ibmcloud ob logging config ls --cluster "${CLUSTER_ID}" | grep -q "Instance ID"; then EXISTING_INSTANCE_ID=$(ibmcloud ob logging config ls --cluster "${CLUSTER_ID}" | grep "Instance ID" | sed -E "s/Instance ID: +([^ ]+)/\1/g") if [[ "${EXISTING_INSTANCE_ID}" == "${INSTANCE_ID}" ]]; then diff --git a/scripts/ibmcloud-login.sh b/scripts/ibmcloud-login.sh deleted file mode 100755 index ffad49c..0000000 --- a/scripts/ibmcloud-login.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env bash - -REGION="$1" -RESOURCE_GROUP="$2" - -if [[ -z "${APIKEY}" ]]; then - echo "The APIKEY is required" - exit 1 -fi - -if [[ -n "${BIN_DIR}" ]]; then - export PATH="${BIN_DIR}:${PATH}" -fi - -ibmcloud config --check-version=false - -ibmcloud login -r "${REGION}" -g "${RESOURCE_GROUP}" --apikey "${APIKEY}" diff --git a/scripts/unbind-instance.sh b/scripts/unbind-instance.sh index 515c108..4de7c84 100755 --- a/scripts/unbind-instance.sh +++ b/scripts/unbind-instance.sh @@ -10,6 +10,23 @@ if [[ -n "${BIN_DIR}" ]]; then export PATH="${BIN_DIR}:${PATH}" fi +if [[ -z "${IBMCLOUD_API_KEY}" ]]; then + echo "IBMCLOUD_API_KEY must be provided as an environment variable" >&2 + exit 1 +fi + +if [[ -z "${REGION}" ]]; then + echo "REGION must be provided as an environment variable" >&2 + exit 1 +fi + +if [[ -z "${RESOURCE_GROUP}" ]]; then + echo "RESOURCE_GROUP must be provided as an environment variable" >&2 + exit 1 +fi + +ibmcloud login -r "${REGION}" -g "${RESOURCE_GROUP}" + ibmcloud ob logging config delete \ --cluster "${CLUSTER_ID}" \ --instance "${INSTANCE_ID}" \