-
Notifications
You must be signed in to change notification settings - Fork 2
Add feature to check the account type #337
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,4 +110,5 @@ module "scc_wp" { | |
| }] | ||
| } | ||
| ] | ||
| ibmcloud_api_key = var.ibmcloud_api_key | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # Account Check | ||
|
|
||
| This module determines whether a given IBM Cloud account is part of an `Enterprise` or is a `Standalone (Normal)` account. | ||
| It uses the IBM Cloud Enterprise Management API and can be easily integrated into Terraform configurations via the external data source. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the usage and required permissions here |
||
|
|
||
| <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
| ### Requirements | ||
|
|
||
| | Name | Version | | ||
| |------|---------| | ||
| | <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.9.0 | | ||
| | <a name="requirement_external"></a> [external](#requirement\_external) | 2.3.5 | | ||
|
|
||
| ### Modules | ||
|
|
||
| No modules. | ||
|
|
||
| ### Resources | ||
|
|
||
| | Name | Type | | ||
| |------|------| | ||
| | [external_external.account_check](https://registry.terraform.io/providers/hashicorp/external/2.3.5/docs/data-sources/external) | data source | | ||
|
|
||
| ### Inputs | ||
|
|
||
| | Name | Description | Type | Default | Required | | ||
| |------|-------------|------|---------|:--------:| | ||
| | <a name="input_ibmcloud_api_key"></a> [ibmcloud\_api\_key](#input\_ibmcloud\_api\_key) | The IBM Cloud platform API key needed to deploy IAM enabled resources. | `string` | n/a | yes | | ||
| | <a name="input_target_account_id"></a> [target\_account\_id](#input\_target\_account\_id) | The ID of the target account to check for type. | `string` | n/a | yes | | ||
|
|
||
| ### Outputs | ||
|
|
||
| | Name | Description | | ||
| |------|-------------| | ||
| | <a name="output_account_type"></a> [account\_type](#output\_account\_type) | The determined type of the IBM Cloud account. | | ||
| <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| locals { | ||
| # Set account_type variable from the external data source's JSON output. | ||
| account_type = data.external.account_check.result.account_type | ||
| } | ||
|
|
||
| data "external" "account_check" { | ||
| program = ["/bin/bash", "${path.module}/../scripts/account-check.sh"] | ||
| query = { | ||
| account_id = var.target_account_id | ||
| api_key = var.ibmcloud_api_key | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| output "account_type" { | ||
| description = "The determined type of the IBM Cloud account." | ||
| value = local.account_type | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| variable "ibmcloud_api_key" { | ||
| description = "The IBM Cloud platform API key needed to deploy IAM enabled resources." | ||
| type = string | ||
| sensitive = true | ||
| } | ||
|
|
||
| variable "target_account_id" { | ||
| description = "The ID of the target account to check for type." | ||
| type = string | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| terraform { | ||
| required_version = ">= 1.9.0" | ||
| required_providers { | ||
| external = { | ||
| source = "hashicorp/external" | ||
| version = "2.3.5" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. modules should use a range. EG: |
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #!/bin/bash | ||
|
|
||
| read -r TF_INPUT | ||
| ACCOUNT_ID=$(echo "$TF_INPUT" | jq -r '.account_id') | ||
| IBM_CLOUD_API_KEY=$(echo "$TF_INPUT" | jq -r '.api_key') | ||
|
|
||
| # --- Obtain IAM Token --- | ||
| IAM_TOKEN=$(curl -s -X POST "https://iam.cloud.ibm.com/identity/token" \ | ||
| -H "Content-Type: application/x-www-form-urlencoded" \ | ||
| -d "grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey=${IBM_CLOUD_API_KEY}" \ | ||
| | jq -r '.access_token') | ||
|
|
||
| # --- Query Enterprise API --- | ||
| HTTP_CODE=$(curl -s -w "%{http_code}" -o /tmp/account.json \ | ||
| -X GET "https://enterprise.cloud.ibm.com/v1/accounts/${ACCOUNT_ID}" \ | ||
| -H "Authorization: Bearer ${IAM_TOKEN}") | ||
|
|
||
| # --- Determine Account Type --- | ||
| ACCOUNT_TYPE="NORMAL" | ||
| if [ "$HTTP_CODE" == "200" ] && grep -q '"enterprise_id"' /tmp/account.json; then | ||
| ACCOUNT_TYPE="ENTERPRISE" | ||
| fi | ||
|
|
||
| # --- Output for Terraform --- | ||
| echo "{\"account_type\": \"${ACCOUNT_TYPE}\"}" | ||
| exit 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This script needs to be updated so the endpoints can be overridden by environment variables, incase someone is using the to deploy to stage for example. See example |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,12 @@ | |
| # Input Variables | ||
| ############################################################################## | ||
|
|
||
| variable "ibmcloud_api_key" { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of added a new required input here, maybe it would be better to get the IAM token using a data lookup in the new submodule and pass the token value directly to the script? |
||
| description = "The IBM Cloud platform API key needed to deploy IAM enabled resources." | ||
| type = string | ||
| sensitive = true | ||
| } | ||
|
|
||
| variable "region" { | ||
| description = "IBM Cloud region where all resources will be deployed" | ||
| type = string | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The output of this has to be used in the
restapi_objectblock as a new parameter. This should also have a count around it as its only needed if CSPM is enabled