Skip to content

Commit 2b0729c

Browse files
committed
feat(cloud_stack): add cloud_provider_url, connections_api_url, and sm_url outputs
Add computed URL attributes to the grafana_cloud_stack resource and data source so users can wire them directly to the provider config without manual URL construction: - cloud_provider_url: derived from cluster_slug - connections_api_url: derived from cluster_slug - sm_url: read from GCOM API (RegionSyntheticMonitoringApiUrl) Update provider docs to show the Terraform-native approach and remove the manual curl/jq alternatives.
1 parent 3ee6e50 commit 2b0729c

File tree

7 files changed

+63
-102
lines changed

7 files changed

+63
-102
lines changed

docs/data-sources/cloud_stack.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ available at “https://<stack_slug>.grafana.net".
4040
- `alertmanager_status` (String) Status of the Alertmanager instance configured for this stack.
4141
- `alertmanager_url` (String) Base URL of the Alertmanager instance configured for this stack.
4242
- `alertmanager_user_id` (Number) User ID of the Alertmanager instance configured for this stack.
43+
- `cloud_provider_url` (String) Base URL of the Cloud Provider API for this stack's cluster. This can be used with the `cloud_provider_url` provider config option to manage Cloud Provider resources for this stack.
4344
- `cluster_name` (String) Name of the cluster where this stack resides.
4445
- `cluster_slug` (String) Slug of the cluster where this stack resides.
46+
- `connections_api_url` (String) Base URL of the Connections API for this stack's cluster. This can be used with the `connections_api_url` provider config option to manage Connections resources for this stack.
4547
- `delete_protection` (Boolean) Whether to enable delete protection for the stack, preventing accidental deletion.
4648
- `description` (String) Description of stack.
4749
- `fleet_management_name` (String) Name of the Fleet Management instance configured for this stack.
@@ -121,6 +123,7 @@ available at “https://<stack_slug>.grafana.net".
121123
- `prometheus_url` (String) Prometheus url for this instance.
122124
- `prometheus_user_id` (Number) Prometheus user ID. Used for e.g. remote_write.
123125
- `region_slug` (String) The region this stack is deployed to.
126+
- `sm_url` (String) Base URL of the Synthetic Monitoring API for this stack's region. This can be used with the `sm_url` provider config option. Note: Synthetic Monitoring requires activation either via the `grafana_synthetic_monitoring_installation` resource or manually in the Grafana Cloud UI before it can be used.
124127
- `status` (String) Status of the stack.
125128
- `traces_ip_allow_list_cname` (String) Comma-separated list of CNAMEs that can be whitelisted to access the Traces instance (Optional)
126129
- `traces_name` (String)

docs/index.md

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,18 @@ data "grafana_synthetic_monitoring_probes" "main" {
161161
}
162162
```
163163

164+
The Synthetic Monitoring API URL for a stack's region is available as an output of the `grafana_cloud_stack` resource or data source via the `sm_url` attribute. This can be used with the `sm_url` provider config option:
165+
166+
```hcl
167+
provider "grafana" {
168+
alias = "sm"
169+
sm_access_token = grafana_synthetic_monitoring_installation.sm_stack.sm_access_token
170+
sm_url = grafana_cloud_stack.my_stack.sm_url
171+
}
172+
```
173+
174+
~> **Note:** Synthetic Monitoring must be activated before it can be used. Use the `grafana_synthetic_monitoring_installation` resource or activate it manually in the Grafana Cloud UI.
175+
164176
### Managing Grafana OnCall
165177

166178
Note that you may need to set the `oncall_api_url` in the provider block
@@ -343,35 +355,12 @@ the in-screen instructions, of following [this guide](https://grafana.com/docs/g
343355

344356
#### Obtaining Cloud Provider API hostname
345357

346-
Having created the token, we can find the correct Cloud Provider API hostname by running the following script, that requires `curl` and [`jq`](https://jqlang.org/) installed:
347-
348-
```bash
349-
curl -sH "Authorization: Bearer <Access Token from previous step>" "https://grafana.com/api/instances" | \
350-
jq '[.items[]|{stackName: .slug, clusterName:.clusterSlug, cloudProviderAPIURL: "https://cloud-provider-api-\(.clusterSlug).grafana.net"}]'
351-
```
352-
353-
This script will return a list of all the Grafana Cloud stacks you own, with the Cloud Provider API hostname for each one. Choose the correct hostname for the stack you want to manage.
354-
For example, in the following response, the correct hostname for the `herokublogpost` stack is `https://cloud-provider-api-prod-us-central-0.grafana.net`.
355-
356-
```json
357-
[
358-
{
359-
"stackName": "herokublogpost",
360-
"clusterName": "prod-us-central-0",
361-
"cloudProviderAPIURL": "https://cloud-provider-api-prod-us-central-0.grafana.net"
362-
}
363-
]
364-
```
365-
366-
#### Configuring the Provider to use the Cloud Provider API
367-
368-
Once you have the token and Cloud Provider API hostanme, you can configure the provider as follows:
358+
The Cloud Provider API URL is available as an output of the `grafana_cloud_stack` resource or data source via the `cloud_provider_url` attribute:
369359

370360
```hcl
371361
provider "grafana" {
372-
// ...
373-
cloud_provider_url = <Cloud Provider API URL from previous step>
374-
cloud_provider_access_token = <Access Token from previous step>
362+
cloud_provider_url = grafana_cloud_stack.my_stack.cloud_provider_url
363+
cloud_provider_access_token = "<Access Token from previous step>"
375364
}
376365
```
377366

@@ -476,33 +465,11 @@ the in-screen instructions, of following [this guide](https://grafana.com/docs/g
476465

477466
#### Obtaining Connections API hostname
478467

479-
Having created the token, we can find the correct Connections API hostname by running the following script, that requires `curl` and [`jq`](https://jqlang.org/) installed:
480-
481-
```bash
482-
curl -sH "Authorization: Bearer <Access Token from previous step>" "https://grafana.com/api/instances" | \
483-
jq '[.items[]|{stackName: .slug, clusterName:.clusterSlug, connectionsAPIURL: "https://connections-api-\(.clusterSlug).grafana.net"}]'
484-
```
485-
486-
This script will return a list of all the Grafana Cloud stacks you own, with the Connections API hostname for each one. Choose the correct hostname for the stack you want to manage.
487-
For example, in the following response, the correct hostname for the `examplestackname` stack is `https://connections-api-prod-eu-west-0.grafana.net`.
488-
489-
```json
490-
[
491-
{
492-
"stackName": "examplestackname",
493-
"clusterName": "prod-eu-west-0",
494-
"connectionsAPIURL": "https://connections-api-prod-eu-west-0.grafana.net"
495-
}
496-
]
497-
```
498-
499-
#### Configuring the Provider to use the Connections API
500-
501-
Once you have the token and Connections API hostname, you can configure the provider as follows:
468+
The Connections API URL is available as an output of the `grafana_cloud_stack` resource or data source via the `connections_api_url` attribute:
502469

503470
```hcl
504471
provider "grafana" {
505-
connections_api_url = "<Connections API URL from previous step>"
472+
connections_api_url = grafana_cloud_stack.my_stack.connections_api_url
506473
connections_api_access_token = "<Access Token from previous step>"
507474
}
508475
```

docs/resources/cloud_stack.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ resource "grafana_cloud_stack" "test" {
5454
- `alertmanager_status` (String) Status of the Alertmanager instance configured for this stack.
5555
- `alertmanager_url` (String) Base URL of the Alertmanager instance configured for this stack.
5656
- `alertmanager_user_id` (Number) User ID of the Alertmanager instance configured for this stack.
57+
- `cloud_provider_url` (String) Base URL of the Cloud Provider API for this stack's cluster. This can be used with the `cloud_provider_url` provider config option to manage Cloud Provider resources for this stack.
5758
- `cluster_name` (String) Name of the cluster where this stack resides.
5859
- `cluster_slug` (String) Slug of the cluster where this stack resides.
60+
- `connections_api_url` (String) Base URL of the Connections API for this stack's cluster. This can be used with the `connections_api_url` provider config option to manage Connections resources for this stack.
5961
- `fleet_management_name` (String) Name of the Fleet Management instance configured for this stack.
6062
- `fleet_management_private_connectivity_info_availability_zone_ids` (List of String) Availability Zone IDs for Fleet Management when using AWS PrivateLink (only for AWS stacks)
6163
- `fleet_management_private_connectivity_info_availability_zones` (List of String) Availability Zones for Fleet Management when using AWS PrivateLink (only for AWS stacks)
@@ -130,6 +132,7 @@ resource "grafana_cloud_stack" "test" {
130132
- `prometheus_status` (String) Prometheus status for this instance.
131133
- `prometheus_url` (String) Prometheus url for this instance.
132134
- `prometheus_user_id` (Number) Prometheus user ID. Used for e.g. remote_write.
135+
- `sm_url` (String) Base URL of the Synthetic Monitoring API for this stack's region. This can be used with the `sm_url` provider config option. Note: Synthetic Monitoring requires activation either via the `grafana_synthetic_monitoring_installation` resource or manually in the Grafana Cloud UI before it can be used.
133136
- `status` (String) Status of the stack.
134137
- `traces_ip_allow_list_cname` (String) Comma-separated list of CNAMEs that can be whitelisted to access the Traces instance (Optional)
135138
- `traces_name` (String)

internal/resources/cloud/data_source_cloud_stack_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ func TestAccDataSourceStack_Basic(t *testing.T) {
3636
resource.TestCheckResourceAttrSet("data.grafana_cloud_stack.test", "prometheus_user_id"),
3737
resource.TestCheckResourceAttrSet("data.grafana_cloud_stack.test", "alertmanager_user_id"),
3838
resource.TestCheckResourceAttrSet("data.grafana_cloud_stack.test", "cluster_slug"),
39+
resource.TestCheckResourceAttrSet("data.grafana_cloud_stack.test", "cloud_provider_url"),
40+
resource.TestCheckResourceAttrSet("data.grafana_cloud_stack.test", "connections_api_url"),
41+
resource.TestCheckResourceAttrSet("data.grafana_cloud_stack.test", "sm_url"),
3942
resource.TestCheckResourceAttrSet("data.grafana_cloud_stack.test", "fleet_management_user_id"),
4043
resource.TestCheckResourceAttrSet("data.grafana_cloud_stack.test", "fleet_management_name"),
4144
resource.TestCheckResourceAttrSet("data.grafana_cloud_stack.test", "fleet_management_url"),

internal/resources/cloud/resource_cloud_stack.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ Required access policy scopes:
196196
"alertmanager_status": common.ComputedStringWithDescription("Status of the Alertmanager instance configured for this stack."),
197197
"alertmanager_ip_allow_list_cname": ipAllowListCNAMEDescription("the Alertmanager instances"),
198198

199+
// Synthetic Monitoring
200+
"sm_url": common.ComputedStringWithDescription("Base URL of the Synthetic Monitoring API for this stack's region. This can be used with the `sm_url` provider config option. Note: Synthetic Monitoring requires activation either via the `grafana_synthetic_monitoring_installation` resource or manually in the Grafana Cloud UI before it can be used."),
201+
199202
// OnCall
200203
"oncall_api_url": common.ComputedStringWithDescription("Base URL of the OnCall API instance configured for this stack."),
201204

@@ -258,9 +261,13 @@ Required access policy scopes:
258261
"fleet_management_private_connectivity_info_availability_zones": privateConnectivityArrayDescription("Availability Zones", "Fleet Management"),
259262
"fleet_management_private_connectivity_info_availability_zone_ids": privateConnectivityArrayDescription("Availability Zone IDs", "Fleet Management"),
260263

264+
// Cloud Provider
265+
"cloud_provider_url": common.ComputedStringWithDescription("Base URL of the Cloud Provider API for this stack's cluster. This can be used with the `cloud_provider_url` provider config option to manage Cloud Provider resources for this stack."),
266+
261267
// Connections
262-
"influx_url": common.ComputedStringWithDescription("Base URL of the InfluxDB instance configured for this stack. The username is the same as the metrics' (`prometheus_user_id` attribute of this resource). See https://grafana.com/docs/grafana-cloud/send-data/metrics/metrics-influxdb/push-from-telegraf/ for docs on how to use this."),
263-
"otlp_url": common.ComputedStringWithDescription("Base URL of the OTLP instance configured for this stack. The username is the stack's ID (`id` attribute of this resource). See https://grafana.com/docs/grafana-cloud/send-data/otlp/send-data-otlp/ for docs on how to use this."),
268+
"connections_api_url": common.ComputedStringWithDescription("Base URL of the Connections API for this stack's cluster. This can be used with the `connections_api_url` provider config option to manage Connections resources for this stack."),
269+
"influx_url": common.ComputedStringWithDescription("Base URL of the InfluxDB instance configured for this stack. The username is the same as the metrics' (`prometheus_user_id` attribute of this resource). See https://grafana.com/docs/grafana-cloud/send-data/metrics/metrics-influxdb/push-from-telegraf/ for docs on how to use this."),
270+
"otlp_url": common.ComputedStringWithDescription("Base URL of the OTLP instance configured for this stack. The username is the stack's ID (`id` attribute of this resource). See https://grafana.com/docs/grafana-cloud/send-data/otlp/send-data-otlp/ for docs on how to use this."),
264271
"otlp_private_connectivity_info_private_dns": privateConnectivityDescription("Private DNS", "OTLP"),
265272
"otlp_private_connectivity_info_service_name": privateConnectivityDescription("Service Name", "OTLP"),
266273
"otlp_private_connectivity_info_regions": privateConnectivityArrayDescription("Regions", "OTLP"),
@@ -565,6 +572,8 @@ func flattenStack(d *schema.ResourceData, stack *gcom.FormattedApiInstance, conn
565572
addIPAllowListIfPresent(d, "alertmanager", tenant)
566573
})
567574

575+
d.Set("sm_url", stack.RegionSyntheticMonitoringApiUrl)
576+
568577
if oncallURL := connections.OncallApiUrl; oncallURL.IsSet() {
569578
d.Set("oncall_api_url", oncallURL.Get())
570579
}
@@ -624,6 +633,9 @@ func flattenStack(d *schema.ResourceData, stack *gcom.FormattedApiInstance, conn
624633
d.Set("influx_url", influxURL.Get())
625634
}
626635

636+
d.Set("cloud_provider_url", fmt.Sprintf("https://cloud-provider-api-%s.grafana.net", stack.ClusterSlug))
637+
d.Set("connections_api_url", fmt.Sprintf("https://connections-api-%s.grafana.net", stack.ClusterSlug))
638+
627639
return nil
628640
}
629641

internal/resources/cloud/resource_cloud_stack_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ func TestResourceStack_Basic(t *testing.T) {
9494
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "otlp_private_connectivity_info_availability_zones"),
9595
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "otlp_private_connectivity_info_availability_zone_ids"),
9696
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "influx_url"),
97+
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "cloud_provider_url"),
98+
resource.TestMatchResourceAttr("grafana_cloud_stack.test", "cloud_provider_url", regexp.MustCompile(`^https://cloud-provider-api-.+\.grafana\.net$`)),
99+
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "connections_api_url"),
100+
resource.TestMatchResourceAttr("grafana_cloud_stack.test", "connections_api_url", regexp.MustCompile(`^https://connections-api-.+\.grafana\.net$`)),
97101
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "cluster_slug"),
98102
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "cluster_name"),
99103
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "pdc_api_private_connectivity_info_private_dns"),
@@ -107,6 +111,8 @@ func TestResourceStack_Basic(t *testing.T) {
107111
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "pdc_gateway_private_connectivity_info_availability_zones"),
108112
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "pdc_gateway_private_connectivity_info_availability_zone_ids"),
109113
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "oncall_api_url"),
114+
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "sm_url"),
115+
resource.TestMatchResourceAttr("grafana_cloud_stack.test", "sm_url", regexp.MustCompile(`^https://synthetic-monitoring-api.*\.grafana\.net$`)),
110116
resource.TestCheckResourceAttrSet("grafana_cloud_stack.test", "delete_protection"),
111117
)
112118

templates/index.md.tmpl

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ The changelog for this provider can be found here: <https://github.com/grafana/t
2626

2727
{{ tffile "examples/resources/grafana_synthetic_monitoring_installation/resource.tf" }}
2828

29+
The Synthetic Monitoring API URL for a stack's region is available as an output of the `grafana_cloud_stack` resource or data source via the `sm_url` attribute. This can be used with the `sm_url` provider config option:
30+
31+
```hcl
32+
provider "grafana" {
33+
alias = "sm"
34+
sm_access_token = grafana_synthetic_monitoring_installation.sm_stack.sm_access_token
35+
sm_url = grafana_cloud_stack.my_stack.sm_url
36+
}
37+
```
38+
39+
~> **Note:** Synthetic Monitoring must be activated before it can be used. Use the `grafana_synthetic_monitoring_installation` resource or activate it manually in the Grafana Cloud UI.
40+
2941
### Managing Grafana OnCall
3042

3143
Note that you may need to set the `oncall_api_url` in the provider block
@@ -103,35 +115,12 @@ the in-screen instructions, of following [this guide](https://grafana.com/docs/g
103115

104116
#### Obtaining Cloud Provider API hostname
105117

106-
Having created the token, we can find the correct Cloud Provider API hostname by running the following script, that requires `curl` and [`jq`](https://jqlang.org/) installed:
107-
108-
```bash
109-
curl -sH "Authorization: Bearer <Access Token from previous step>" "https://grafana.com/api/instances" | \
110-
jq '[.items[]|{stackName: .slug, clusterName:.clusterSlug, cloudProviderAPIURL: "https://cloud-provider-api-\(.clusterSlug).grafana.net"}]'
111-
```
112-
113-
This script will return a list of all the Grafana Cloud stacks you own, with the Cloud Provider API hostname for each one. Choose the correct hostname for the stack you want to manage.
114-
For example, in the following response, the correct hostname for the `herokublogpost` stack is `https://cloud-provider-api-prod-us-central-0.grafana.net`.
115-
116-
```json
117-
[
118-
{
119-
"stackName": "herokublogpost",
120-
"clusterName": "prod-us-central-0",
121-
"cloudProviderAPIURL": "https://cloud-provider-api-prod-us-central-0.grafana.net"
122-
}
123-
]
124-
```
125-
126-
#### Configuring the Provider to use the Cloud Provider API
127-
128-
Once you have the token and Cloud Provider API hostanme, you can configure the provider as follows:
118+
The Cloud Provider API URL is available as an output of the `grafana_cloud_stack` resource or data source via the `cloud_provider_url` attribute:
129119

130120
```hcl
131121
provider "grafana" {
132-
// ...
133-
cloud_provider_url = <Cloud Provider API URL from previous step>
134-
cloud_provider_access_token = <Access Token from previous step>
122+
cloud_provider_url = grafana_cloud_stack.my_stack.cloud_provider_url
123+
cloud_provider_access_token = "<Access Token from previous step>"
135124
}
136125
```
137126

@@ -161,33 +150,11 @@ the in-screen instructions, of following [this guide](https://grafana.com/docs/g
161150

162151
#### Obtaining Connections API hostname
163152

164-
Having created the token, we can find the correct Connections API hostname by running the following script, that requires `curl` and [`jq`](https://jqlang.org/) installed:
165-
166-
```bash
167-
curl -sH "Authorization: Bearer <Access Token from previous step>" "https://grafana.com/api/instances" | \
168-
jq '[.items[]|{stackName: .slug, clusterName:.clusterSlug, connectionsAPIURL: "https://connections-api-\(.clusterSlug).grafana.net"}]'
169-
```
170-
171-
This script will return a list of all the Grafana Cloud stacks you own, with the Connections API hostname for each one. Choose the correct hostname for the stack you want to manage.
172-
For example, in the following response, the correct hostname for the `examplestackname` stack is `https://connections-api-prod-eu-west-0.grafana.net`.
173-
174-
```json
175-
[
176-
{
177-
"stackName": "examplestackname",
178-
"clusterName": "prod-eu-west-0",
179-
"connectionsAPIURL": "https://connections-api-prod-eu-west-0.grafana.net"
180-
}
181-
]
182-
```
183-
184-
#### Configuring the Provider to use the Connections API
185-
186-
Once you have the token and Connections API hostname, you can configure the provider as follows:
153+
The Connections API URL is available as an output of the `grafana_cloud_stack` resource or data source via the `connections_api_url` attribute:
187154

188155
```hcl
189156
provider "grafana" {
190-
connections_api_url = "<Connections API URL from previous step>"
157+
connections_api_url = grafana_cloud_stack.my_stack.connections_api_url
191158
connections_api_access_token = "<Access Token from previous step>"
192159
}
193160
```

0 commit comments

Comments
 (0)