generated from terraform-ibm-modules/terraform-ibm-module-template
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add module to fetch primary_metadata_region #127
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
Open
iamar7
wants to merge
38
commits into
main
Choose a base branch
from
16324-pmr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+251
−51
Open
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
54f8846
add get primary metadata region
a43b8ac
update DA
b301550
update main
8fac738
resolve comments
122c9e5
resolve pc and update description
0fd4610
update script
f548b68
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
ba89546
update script to handle custome endpoint
d453c4c
update readme
348010d
resolve comments
a3ddb62
Merge branch 'main' into 16324-pmr
iamar7 5393d26
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
1afd1e3
Merge branch '16324-pmr' of https://github.com/terraform-ibm-modules/…
25c0686
update readme
27bc9a2
update tests
cf97a21
update test
d5042cf
update test
518cc8c
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
8d7357d
SKIP UPGRADE TEST
872ac2b
resolve comments
fe253f2
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
c1ac16a
resolve comments
0216d12
update script and resolve pc
4d987dd
update script and add cda
0bd87d6
update cdev
deddcef
update script and tests
iamar7 a6491c8
update script
d7e5965
remove commented code
dcbce81
resolve pc
iamar7 db446f3
add debug logs
6399cf4
Merge branch '16324-pmr' of https://github.com/terraform-ibm-modules/…
00f101d
update script
f7c9d65
update script
69ae786
update script
145dbb7
update script
dab2dfc
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
de2a40b
remove debugger
e42b25a
run pc
iamar7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule common-dev-assets
updated
16 files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # Primary Metadata Region | ||
|
|
||
| This module retrieves the `primary_metadata_region` value from the IBM Cloud Metrics Routing Account Settings. | ||
|
|
||
| ### Customizing default cloud service endpoints | ||
|
|
||
| The user must export the endpoint as an environment variable in order to use custom cloud service endpoints with this module. [Learn more](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/guides/custom-service-endpoints#getting-started-with-custom-service-endpoints). | ||
|
|
||
| **Important** The only supported method for customizing cloud service endpoints is to export the environment variables endpoint; be sure to export the value for `IBMCLOUD_METRICS_ROUTING_API_ENDPOINT`. For example, | ||
|
|
||
| ``` | ||
| export IBMCLOUD_METRICS_ROUTING_API_ENDPOINT="<endpoint_url>" | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```hcl | ||
|
|
||
| provider "ibm" { | ||
| ibmcloud_api_key = "XXXXXXXXXXXXXXXXXXXXXXXX" # pragma: allowlist secret | ||
| } | ||
|
|
||
| module "metrics_router" { | ||
| source = "terraform-ibm-modules/cloud-monitoring/ibm//modules/get_primary_metadata_region" | ||
| region = "us-south" | ||
| use_private_endpoint = false | ||
| } | ||
| ``` | ||
|
|
||
| <!-- 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, <3.0.0 | | ||
| | <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | >= 1.69.2, < 2.0.0 | | ||
|
|
||
| ### Modules | ||
|
|
||
| No modules. | ||
|
|
||
| ### Resources | ||
|
|
||
| | Name | Type | | ||
| |------|------| | ||
| | [external_external.get_primary_metadata_region](https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/external) | data source | | ||
| | [ibm_iam_auth_token.token](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/iam_auth_token) | data source | | ||
|
|
||
| ### Inputs | ||
|
|
||
| | Name | Description | Type | Default | Required | | ||
| |------|-------------|------|---------|:--------:| | ||
| | <a name="input_region"></a> [region](#input\_region) | The IBM Cloud Metrics Routing region. | `string` | `"us-south"` | no | | ||
| | <a name="input_use_private_endpoint"></a> [use\_private\_endpoint](#input\_use\_private\_endpoint) | Set to true to use the private endpoints instead of public endpoints for IBM Cloud Metrics Routing service. When true, the script queries the private Metrics Routing endpoint for the given region. [Learn more](https://cloud.ibm.com/docs/metrics-router?topic=metrics-router-endpoints) | `bool` | `false` | no | | ||
|
|
||
| ### Outputs | ||
|
|
||
| | Name | Description | | ||
| |------|-------------| | ||
| | <a name="output_primary_metadata_region"></a> [primary\_metadata\_region](#output\_primary\_metadata\_region) | The current primary metadata region set for IBM Cloud Metrics Routing. | | ||
| <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| ######################################################################## | ||
| # Metrics Routing Primary Metadata Region | ||
| ######################################################################### | ||
|
|
||
| data "ibm_iam_auth_token" "token" {} | ||
|
|
||
| data "external" "get_primary_metadata_region" { | ||
| program = ["python3", "${path.module}/scripts/primary_metadata_region.py"] | ||
|
|
||
| query = { | ||
| iam_access_token = data.ibm_iam_auth_token.token.iam_access_token | ||
| region = var.region | ||
| use_private_endpoint = var.use_private_endpoint | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| ######################################################################## | ||
| # Output | ||
| ######################################################################### | ||
|
|
||
| output "primary_metadata_region" { | ||
| value = data.external.get_primary_metadata_region.result.primary_metadata_region | ||
| description = "The current primary metadata region set for IBM Cloud Metrics Routing." | ||
| } |
78 changes: 78 additions & 0 deletions
78
modules/get_primary_metadata_region/scripts/primary_metadata_region.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #!/usr/bin/env python3 | ||
| import json | ||
| import os | ||
| import sys | ||
| import time | ||
|
|
||
| import requests | ||
|
|
||
|
|
||
| def load_input(): | ||
| try: | ||
| raw = sys.stdin.read() | ||
| data = json.loads(raw) | ||
| return data | ||
| except Exception as e: | ||
| log_error(f"Failed to parse JSON input: {e}") | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| def log_error(message): | ||
| print(message, file=sys.stderr) | ||
|
|
||
|
|
||
| def resolve_metrics_router_endpoint(region, use_private): | ||
| metrics_endpoint = os.getenv("IBMCLOUD_METRICS_ROUTING_API_ENDPOINT") | ||
|
|
||
| if not metrics_endpoint: | ||
| metrics_endpoint = "metrics-router.cloud.ibm.com" | ||
| metrics_endpoint = metrics_endpoint.replace("https://", "") | ||
|
|
||
| if metrics_endpoint == "metrics-router.cloud.ibm.com": | ||
| if use_private: | ||
| return f"https://private.{region}.{metrics_endpoint}" | ||
| else: | ||
| return f"https://{region}.{metrics_endpoint}" | ||
|
|
||
| return f"https://{metrics_endpoint}" | ||
|
|
||
|
|
||
| def fetch_primary_metadata_region(base_url, iam_token): | ||
| url = f"{base_url}/api/v3/settings" | ||
| headers = {"Authorization": f"{iam_token}"} | ||
|
|
||
| max_retries = 5 | ||
| retry_delay = 3 | ||
|
|
||
| for attempt in range(1, max_retries + 1): | ||
| try: | ||
| resp = requests.get(url, headers=headers, timeout=10) | ||
| data = resp.json() | ||
| except Exception: | ||
| data = {} | ||
|
|
||
| if "primary_metadata_region" in data: | ||
| return data["primary_metadata_region"] | ||
|
|
||
| log_error(f"Attempt {attempt} failed, retrying in {retry_delay}s...") | ||
| time.sleep(retry_delay) | ||
|
|
||
| log_error("`primary_metadata_region` could not be fetched after 5 attempts.") | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| def main(): | ||
| input_data = load_input() | ||
|
|
||
| region = input_data["region"] | ||
| iam_token = input_data["iam_access_token"] | ||
| use_private_endpoint = json.loads(input_data["use_private_endpoint"]) | ||
|
|
||
| base_url = resolve_metrics_router_endpoint(region, use_private_endpoint) | ||
| primary_region = fetch_primary_metadata_region(base_url, iam_token) | ||
|
|
||
| print(json.dumps({"primary_metadata_region": primary_region})) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| ############################################################################## | ||
| # Input Variables | ||
| ############################################################################## | ||
|
|
||
| variable "region" { | ||
| description = "The IBM Cloud Metrics Routing region." | ||
| type = string | ||
| default = "us-south" | ||
| } | ||
|
|
||
| variable "use_private_endpoint" { | ||
| type = bool | ||
| description = "Set to true to use the private endpoints instead of public endpoints for IBM Cloud Metrics Routing service. When true, the script queries the private Metrics Routing endpoint for the given region. [Learn more](https://cloud.ibm.com/docs/metrics-router?topic=metrics-router-endpoints)" | ||
| default = false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| terraform { | ||
| required_version = ">= 1.9.0" | ||
| required_providers { | ||
| # Use "greater than or equal to" range in modules | ||
| ibm = { | ||
| source = "ibm-cloud/ibm" | ||
| version = ">= 1.69.2, < 2.0.0" | ||
| } | ||
| external = { | ||
| source = "hashicorp/external" | ||
| version = ">= 2.3.5, <3.0.0" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We cannot use requests - this is an external pip package that requires installation. I did mention this on the deep dive. We can use the python built in http library: https://docs.python.org/3/library/http.client.html
Check out our internal sdnlb module which recently was updated to use this library too.