-
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
base: main
Are you sure you want to change the base?
Changes from 43 commits
54f8846
a43b8ac
b301550
8fac738
122c9e5
0fd4610
f548b68
ba89546
d453c4c
348010d
a3ddb62
5393d26
1afd1e3
25c0686
27bc9a2
cf97a21
d5042cf
518cc8c
8d7357d
872ac2b
fe253f2
c1ac16a
0216d12
4d987dd
0bd87d6
deddcef
a6491c8
d7e5965
dcbce81
db446f3
6399cf4
00f101d
f7c9d65
69ae786
145dbb7
dab2dfc
de2a40b
e42b25a
c98e038
057719b
f592025
3fe7c24
368ba04
6ab14c3
43f4b6a
754e957
2fef70f
abaeea1
c5cce23
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 |
|---|---|---|
| @@ -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 | ||
| } | ||
iamar7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
iamar7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <!-- 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 --> | ||
| 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 | ||
iamar7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| region = var.region | ||
| use_private_endpoint = var.use_private_endpoint | ||
| } | ||
| } | ||
| 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." | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| #!/usr/bin/env python3 | ||
| import http.client | ||
| import json | ||
| import os | ||
| import sys | ||
| import time | ||
| import urllib.parse | ||
|
|
||
|
|
||
| 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 http_get(url, headers=None, timeout=10): | ||
| headers = headers or {} | ||
| parsed = urllib.parse.urlparse(url) | ||
| metricsrouterconn = http.client.HTTPSConnection( | ||
| parsed.hostname, parsed.port, timeout=timeout | ||
| ) | ||
|
|
||
| try: | ||
| metricsrouterconn.request("GET", parsed.path, headers=headers) | ||
| response = metricsrouterconn.getresponse() | ||
| metrics_router_response = response.read().decode("utf-8") | ||
| return response.status, metrics_router_response | ||
|
|
||
| except Exception: | ||
| raise | ||
|
|
||
| finally: | ||
| metricsrouterconn.close() | ||
|
|
||
|
|
||
| def fetch_primary_metadata_region(base_url, iam_token): | ||
| url = f"{base_url}/api/v3/settings" | ||
| headers = {"Authorization": iam_token} | ||
|
|
||
| max_retries = 5 | ||
| retry_delay = 3 | ||
|
|
||
| for attempt in range(1, max_retries + 1): | ||
| try: | ||
| status, body = http_get(url, headers=headers, timeout=10) | ||
| data = json.loads(body) | ||
|
|
||
| except Exception as e: | ||
| data = {} | ||
| status = str(e) | ||
|
|
||
| if "primary_metadata_region" in data: | ||
| return data["primary_metadata_region"] | ||
|
|
||
| log_error( | ||
| f"Attempt {attempt} failed (status {status}), 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() |
| 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 | ||
| } | ||
| 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" | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.