Skip to content
Open
Show file tree
Hide file tree
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
Nov 11, 2025
a43b8ac
update DA
Nov 11, 2025
b301550
update main
Nov 11, 2025
8fac738
resolve comments
Nov 12, 2025
122c9e5
resolve pc and update description
Nov 12, 2025
0fd4610
update script
Nov 12, 2025
f548b68
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
Nov 12, 2025
ba89546
update script to handle custome endpoint
Nov 13, 2025
d453c4c
update readme
Nov 13, 2025
348010d
resolve comments
Nov 17, 2025
a3ddb62
Merge branch 'main' into 16324-pmr
iamar7 Nov 17, 2025
5393d26
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
Nov 17, 2025
1afd1e3
Merge branch '16324-pmr' of https://github.com/terraform-ibm-modules/…
Nov 17, 2025
25c0686
update readme
Nov 17, 2025
27bc9a2
update tests
Nov 17, 2025
cf97a21
update test
Nov 17, 2025
d5042cf
update test
Nov 17, 2025
518cc8c
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
Nov 17, 2025
8d7357d
SKIP UPGRADE TEST
Nov 18, 2025
872ac2b
resolve comments
Nov 19, 2025
fe253f2
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
Nov 19, 2025
c1ac16a
resolve comments
Nov 24, 2025
0216d12
update script and resolve pc
Nov 24, 2025
4d987dd
update script and add cda
Nov 26, 2025
0bd87d6
update cdev
Nov 26, 2025
deddcef
update script and tests
iamar7 Nov 27, 2025
a6491c8
update script
Nov 27, 2025
d7e5965
remove commented code
Nov 27, 2025
dcbce81
resolve pc
iamar7 Nov 27, 2025
db446f3
add debug logs
Nov 28, 2025
6399cf4
Merge branch '16324-pmr' of https://github.com/terraform-ibm-modules/…
Nov 28, 2025
00f101d
update script
Nov 28, 2025
f7c9d65
update script
Nov 28, 2025
69ae786
update script
Nov 28, 2025
145dbb7
update script
Nov 28, 2025
dab2dfc
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
Dec 1, 2025
de2a40b
remove debugger
Dec 1, 2025
e42b25a
run pc
iamar7 Dec 1, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ibm_catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
]
},
{
"key": "enable_primary_metadata_region"
"key": "use_private_endpoint"
},
{
"key": "metrics_routing_route_name"
Expand Down
62 changes: 62 additions & 0 deletions modules/get_primary_metadata_region/README.md
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 -->
15 changes: 15 additions & 0 deletions modules/get_primary_metadata_region/main.tf
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
}
}
8 changes: 8 additions & 0 deletions modules/get_primary_metadata_region/outputs.tf
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,78 @@
#!/usr/bin/env python3
import json
import os
import sys
import time

import requests
Copy link
Contributor

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.



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()
15 changes: 15 additions & 0 deletions modules/get_primary_metadata_region/variables.tf
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
}
14 changes: 14 additions & 0 deletions modules/get_primary_metadata_region/version.tf
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"
}
}
}
8 changes: 7 additions & 1 deletion solutions/fully-configurable/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,11 @@ module "metrics_routing" {
]

metrics_router_routes = length(var.metrics_router_routes) != 0 ? var.metrics_router_routes : local.default_metrics_router_route
metrics_router_settings = var.enable_primary_metadata_region ? { primary_metadata_region = var.region } : null
metrics_router_settings = length(module.primary_metadata_region.primary_metadata_region) != 0 ? null : { primary_metadata_region = var.region }
}

module "primary_metadata_region" {
source = "../../modules/get_primary_metadata_region"
region = var.region
use_private_endpoint = var.use_private_endpoint
}
12 changes: 6 additions & 6 deletions solutions/fully-configurable/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
default = false
}

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."
Expand All @@ -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
Expand Down
23 changes: 11 additions & 12 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.25.4

require (
github.com/stretchr/testify v1.11.1
github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper v1.60.21
github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper v1.60.31
)

require (
Expand All @@ -23,7 +23,6 @@ require (
github.com/ProtonMail/go-crypto v1.1.6 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/cloudflare/circl v1.6.1 // indirect
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
Expand All @@ -37,13 +36,13 @@ require (
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/analysis v0.23.0 // indirect
github.com/go-openapi/errors v0.22.3 // indirect
github.com/go-openapi/errors v0.22.4 // indirect
github.com/go-openapi/jsonpointer v0.21.1 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/loads v0.22.0 // indirect
github.com/go-openapi/runtime v0.28.0 // indirect
github.com/go-openapi/spec v0.21.0 // indirect
github.com/go-openapi/strfmt v0.24.0 // indirect
github.com/go-openapi/strfmt v0.25.0 // indirect
github.com/go-openapi/swag v0.23.1 // indirect
github.com/go-openapi/validate v0.24.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand All @@ -53,7 +52,7 @@ require (
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gruntwork-io/terratest v0.52.0 // indirect
github.com/gruntwork-io/terratest v0.53.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter/v2 v2.2.3 // indirect
Expand Down Expand Up @@ -85,19 +84,19 @@ require (
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/zclconf/go-cty v1.16.4 // indirect
go.mongodb.org/mongo-driver v1.17.4 // indirect
go.mongodb.org/mongo-driver v1.17.6 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/otel v1.35.0 // indirect
go.opentelemetry.io/otel/metric v1.35.0 // indirect
go.opentelemetry.io/otel/trace v1.35.0 // indirect
go.yaml.in/yaml/v2 v2.4.2 // indirect
golang.org/x/crypto v0.43.0 // indirect
golang.org/x/mod v0.28.0 // indirect
golang.org/x/net v0.45.0 // indirect
golang.org/x/crypto v0.44.0 // indirect
golang.org/x/mod v0.29.0 // indirect
golang.org/x/net v0.46.0 // indirect
golang.org/x/sync v0.18.0 // indirect
golang.org/x/sys v0.37.0 // indirect
golang.org/x/text v0.30.0 // indirect
golang.org/x/tools v0.37.0 // indirect
golang.org/x/sys v0.38.0 // indirect
golang.org/x/text v0.31.0 // indirect
golang.org/x/tools v0.38.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading