Skip to content

Commit 4e4ccf0

Browse files
author
akocbek
committed
address PR comments
1 parent 8b56512 commit 4e4ccf0

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

solutions/project/main.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,21 @@ module "project" {
2727
# Code Engine Build
2828
##############################################################################
2929
locals {
30+
registry_region_result = data.external.container_registry_region.result
31+
registry = lookup(local.registry_region_result, "registry", null)
32+
container_registry = local.registry != null ? "private.${local.registry}" : null
33+
registry_region_error = lookup(local.registry_region_result, "error", null)
3034

31-
container_registry = "private.${data.external.container_registry_region.result["registry"]}"
35+
# This will cause Terraform to fail if "error" is present in the external script output executed as a part of container_registry_region
36+
# tflint-ignore: terraform_unused_declarations
37+
fail_if_registry_region_error = local.registry_region_error != null ? tobool("Registry region script failed: ${local.registry_region_error}") : null
3238

3339
# if no build defines a container image reference (output_image), a new container registry namespace must be created using container_registry_namespace.
3440
any_missing_output_image = anytrue([
3541
for build in values(var.builds) :
3642
!contains(keys(build), "output_image") || build.output_image == null
3743
])
38-
image_container = local.any_missing_output_image ? "${local.container_registry}/${resource.ibm_cr_namespace.my_namespace[0].name}" : ""
44+
image_container = local.any_missing_output_image && local.container_registry != null ? "${local.container_registry}/${resource.ibm_cr_namespace.my_namespace[0].name}" : ""
3945

4046
# if output_image not exists then a new created container image reference
4147
updated_builds = {

solutions/project/scripts/build-run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if [[ -z "${BUILDS}" ]]; then
2626
exit 1
2727
fi
2828

29-
ibmcloud login -r "${REGION}" -g "${RESOURCE_GROUP_ID}" --apikey "${IBMCLOUD_API_KEY}"
29+
ibmcloud login -r "${REGION}" -g "${RESOURCE_GROUP_ID}" --quiet
3030

3131
# selecet the right code engine project
3232
ibmcloud ce project select -n "${CE_PROJECT_NAME}"
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
#!/bin/bash
2-
set -e
2+
set -euo pipefail
33

44
INPUT=$(cat)
55
REGION=$(echo "$INPUT" | jq -r '.REGION')
66
RESOURCE_GROUP_ID=$(echo "$INPUT" | jq -r '.RESOURCE_GROUP_ID')
77
IBMCLOUD_API_KEY=$(echo "$INPUT" | jq -r '.IBMCLOUD_API_KEY')
8+
export IBMCLOUD_API_KEY
89

910
if [[ -z "${IBMCLOUD_API_KEY}" || "${IBMCLOUD_API_KEY}" == "null" ]]; then
10-
echo "IBMCLOUD_API_KEY is required" >&2
11-
exit 1
11+
echo '{"error": "IBMCLOUD_API_KEY is required"}'
12+
exit 0
1213
fi
1314

1415
if [[ -z "${RESOURCE_GROUP_ID}" || "${RESOURCE_GROUP_ID}" == "null" ]]; then
15-
echo "RESOURCE_GROUP_ID is required" >&2
16-
exit 1
16+
echo '{"error": "RESOURCE_GROUP_ID is required"}'
17+
exit 0
1718
fi
1819

1920
if [[ -z "${REGION}" || "${REGION}" == "null" ]]; then
20-
echo "REGION is required" >&2
21-
exit 1
21+
echo '{"error": "REGION is required"}'
22+
exit 0
2223
fi
2324

24-
# Login to IBM Cloud quietly
25-
if ! ibmcloud login -r "${REGION}" -g "${RESOURCE_GROUP_ID}" --apikey "${IBMCLOUD_API_KEY}" --quiet > /dev/null 2>&1; then
26-
exit 1
25+
if ! ibmcloud login -r "${REGION}" -g "${RESOURCE_GROUP_ID}" --quiet > /dev/null 2>&1; then
26+
printf '{"error": "Failed to login using: ibmcloud login -r %s -g %s"}' "$REGION" "$RESOURCE_GROUP_ID"
27+
exit 0
2728
fi
2829

2930
# extract registry value from text "You are targeting region 'us-south', the registry is 'us.icr.io'."
30-
registry=$(ibmcloud cr region 2>/dev/null | grep registry | sed -E "s/.*registry is '([^']+)'.*/\1/") || exit 1
31+
registry=$(ibmcloud cr region 2>/dev/null | grep registry | sed -E "s/.*registry is '([^']+)'.*/\1/")
32+
33+
# Validate registry value
34+
if [[ -z "$registry" ]]; then
35+
echo '{"error": "Failed to parse registry region from ibmcloud cr region"}'
36+
exit 0
37+
fi
3138

32-
# Output valid JSON for Terraform external data source
3339
echo "{\"registry\": \"${registry}\"}"

0 commit comments

Comments
 (0)