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
49
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.
+273
−51
Open
Changes from all commits
Commits
Show all changes
49 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 c98e038
resolved comments
057719b
Merge branch '16324-pmr' of https://github.com/terraform-ibm-modules/…
f592025
updated get function
3fe7c24
remove twice logging
368ba04
resolve pc
iamar7 6ab14c3
resolve comments
43f4b6a
Merge branch '16324-pmr' of https://github.com/terraform-ibm-modules/…
754e957
update IAM permission
2fef70f
test IAM permission
abaeea1
resolve comments and remove testing code
c5cce23
resolve pc
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,68 @@ | ||
| # 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" | ||
| use_private_endpoint = false | ||
| } | ||
| ``` | ||
|
|
||
iamar7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ### Required IAM access policies | ||
|
|
||
| You need the following permissions to run this module. | ||
|
|
||
| * Service | ||
| * **Metric Routing** | ||
| * `Viewer` platform access | ||
|
|
||
| <!-- 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_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,14 @@ | ||
| ######################################################################## | ||
| # 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 = sensitive(data.ibm_iam_auth_token.token.iam_access_token) | ||
| 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." | ||
| } |
101 changes: 101 additions & 0 deletions
101
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,101 @@ | ||
| #!/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(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.us-south.{metrics_endpoint}" | ||
| else: | ||
| return f"https://us-south.{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() | ||
|
|
||
| iam_token = input_data["iam_access_token"] | ||
| use_private_endpoint = json.loads(input_data["use_private_endpoint"]) | ||
|
|
||
| base_url = resolve_metrics_router_endpoint(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,9 @@ | ||
| ############################################################################## | ||
| # Input Variables | ||
| ############################################################################## | ||
|
|
||
| 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)" | ||
|
Contributor
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. there is no given region |
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,6 +120,12 @@ variable "enable_platform_metrics" { | |
| # Metrics Routing | ||
| ######################################################################################################################## | ||
|
|
||
| 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)" | ||
|
Contributor
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. there is no given region anymore |
||
| default = true | ||
| } | ||
|
|
||
| variable "metrics_routing_target_name" { | ||
| type = string | ||
| description = "The name of the IBM Cloud Metrics Routing target where metrics are collected. If the prefix variable is passed, the name of the target is prefixed to the value in the `<prefix>-value` format." | ||
|
|
@@ -138,12 +144,6 @@ variable "enable_metrics_routing_to_cloud_monitoring" { | |
| default = true | ||
| } | ||
|
|
||
| variable "enable_primary_metadata_region" { | ||
| type = bool | ||
| description = "When set to `true`, sets `primary_metadata_region` to `region`, storing Metrics Router metadata in that region. When `false`, no region is set and the default global region is used. For new accounts, creating targets and routes will fail until primary_metadata_region is set, so it is recommended to default enable_primary_metadata_region to true. [Learn more](https://cloud.ibm.com/docs/metrics-router?topic=metrics-router-getting-started#configuring_account_settings)." | ||
| default = true | ||
| } | ||
|
|
||
| variable "metrics_router_routes" { | ||
| type = list(object({ | ||
| name = string | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.