Skip to content

Commit d5b3d87

Browse files
SM Installation: Determine API URL from stack's region slug (#1233)
* SM Installation: Determine API URL from region slug Most APIs (other than some exceptions) have determinate API URLs based on the stack's region slug For the exceptions which are the older regions, we can keep a map * Linting * Lint again
1 parent b34da1e commit d5b3d87

File tree

3 files changed

+53
-51
lines changed

3 files changed

+53
-51
lines changed

internal/resources/cloud/resource_cloud_stack_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestResourceStack_Basic(t *testing.T) {
5858
// but it shouldn't allow to apply an already existing stack on a new resource
5959
{
6060
Config: testAccStackConfigBasic(resourceName, resourceName) +
61-
testAccStackConfigBasicWithCustomResourceName(resourceName, resourceName, "test2"), // new stack with same name/slug
61+
testAccStackConfigBasicWithCustomResourceName(resourceName, resourceName, "eu", "test2"), // new stack with same name/slug
6262
ExpectError: regexp.MustCompile(fmt.Sprintf(".*a stack with the name '%s' already exists.*", resourceName)),
6363
},
6464
// Test that the stack is correctly recreated if it's tainted and reapplied
@@ -170,17 +170,17 @@ func testAccStackCheckDestroy(a *gapi.Stack) resource.TestCheckFunc {
170170
}
171171

172172
func testAccStackConfigBasic(name string, slug string) string {
173-
return testAccStackConfigBasicWithCustomResourceName(name, slug, "test")
173+
return testAccStackConfigBasicWithCustomResourceName(name, slug, "eu", "test")
174174
}
175175

176-
func testAccStackConfigBasicWithCustomResourceName(name string, slug string, resourceName string) string {
176+
func testAccStackConfigBasicWithCustomResourceName(name, slug, region, resourceName string) string {
177177
return fmt.Sprintf(`
178178
resource "grafana_cloud_stack" "%s" {
179179
name = "%s"
180180
slug = "%s"
181-
region_slug = "eu"
181+
region_slug = "%s"
182182
}
183-
`, resourceName, name, slug)
183+
`, resourceName, name, slug, region)
184184
}
185185

186186
func testAccStackConfigUpdate(name string, slug string, description string) string {

internal/resources/cloud/resource_synthetic_monitoring_installation.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,12 @@ import (
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1515
)
1616

17-
// TODO: Automate finding the correct API URL based on the stack region
18-
var smAPIURLs = map[string]string{
19-
"us": "https://synthetic-monitoring-api.grafana.net",
20-
"us-azure": "https://synthetic-monitoring-api-us-central2.grafana.net",
21-
"eu": "https://synthetic-monitoring-api-eu-west.grafana.net",
22-
"au": "https://synthetic-monitoring-api-au-southeast.grafana.net",
23-
"prod-ap-southeast-0": "https://synthetic-monitoring-api-ap-southeast-0.grafana.net",
24-
"prod-gb-south-0": "https://synthetic-monitoring-api-gb-south.grafana.net",
25-
"prod-eu-west-2": "https://synthetic-monitoring-api-eu-west-2.grafana.net",
26-
"prod-eu-west-3": "https://synthetic-monitoring-api-eu-west-3.grafana.net",
27-
"prod-ap-south-0": "https://synthetic-monitoring-api-ap-south-0.grafana.net",
28-
"prod-sa-east-0": "https://synthetic-monitoring-api-sa-east-0.grafana.net",
29-
"prod-us-east-0": "https://synthetic-monitoring-api-us-east-0.grafana.net",
17+
var smAPIURLsExceptions = map[string]string{
18+
"au": "https://synthetic-monitoring-api-au-southeast.grafana.net",
19+
"eu": "https://synthetic-monitoring-api-eu-west.grafana.net",
20+
"prod-gb-south-0": "https://synthetic-monitoring-api-gb-south.grafana.net",
21+
"us": "https://synthetic-monitoring-api.grafana.net",
22+
"us-azure": "https://synthetic-monitoring-api-us-central2.grafana.net",
3023
}
3124

3225
func ResourceInstallation() *schema.Resource {
@@ -90,12 +83,14 @@ func ResourceInstallationCreate(ctx context.Context, d *schema.ResourceData, met
9083
return diag.FromErr(err)
9184
}
9285

86+
// TODO: Get this URL programatically
87+
// Perhaps it should be exposed to users through /api/stack-regions?
9388
apiURL := d.Get("stack_sm_api_url").(string)
9489
if apiURL == "" {
95-
apiURL = smAPIURLs[stack.RegionSlug]
90+
apiURL = smAPIURLsExceptions[stack.RegionSlug]
9691
}
9792
if apiURL == "" {
98-
return diag.Errorf("could not find a valid SM API URL for stack region %s", stack.RegionSlug)
93+
apiURL = fmt.Sprintf("https://synthetic-monitoring-api-%s.grafana.net", strings.TrimPrefix(stack.RegionSlug, "prod-"))
9994
}
10095

10196
smClient := SMAPI.NewClient(apiURL, "", nil)

internal/resources/cloud/resource_synthetic_monitoring_installation_test.go

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,49 @@ import (
1212
func TestAccSyntheticMonitoringInstallation(t *testing.T) {
1313
testutils.CheckCloudAPITestsEnabled(t)
1414

15-
var stack gapi.Stack
16-
stackPrefix := "tfsminstalltest"
17-
testAccDeleteExistingStacks(t, stackPrefix)
18-
stackSlug := GetRandomStackName(stackPrefix)
19-
20-
apiKeyPrefix := "testsminstall-"
21-
testAccDeleteExistingCloudAPIKeys(t, apiKeyPrefix)
22-
apiKeyName := apiKeyPrefix + acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
23-
24-
resource.ParallelTest(t, resource.TestCase{
25-
ProviderFactories: testutils.ProviderFactories,
26-
CheckDestroy: testAccStackCheckDestroy(&stack),
27-
Steps: []resource.TestStep{
28-
{
29-
Config: testAccSyntheticMonitoringInstallation(stackSlug, apiKeyName),
30-
Check: resource.ComposeTestCheckFunc(
31-
testAccStackCheckExists("grafana_cloud_stack.test", &stack),
32-
resource.TestCheckResourceAttrSet("grafana_synthetic_monitoring_installation.test", "sm_access_token"),
33-
resource.TestCheckResourceAttrSet("grafana_synthetic_monitoring_installation.test", "stack_sm_api_url"),
34-
),
35-
},
36-
// Test deletion
37-
{
38-
Config: testAccSyntheticMonitoringInstallation(stackSlug, apiKeyName),
39-
},
40-
},
41-
})
15+
for region, expectedURL := range map[string]string{
16+
"prod-ca-east-0": "https://synthetic-monitoring-api-ca-east-0.grafana.net",
17+
"eu": "https://synthetic-monitoring-api-eu-west.grafana.net",
18+
} {
19+
t.Run(region, func(t *testing.T) {
20+
var stack gapi.Stack
21+
stackPrefix := "tfsminstalltest"
22+
testAccDeleteExistingStacks(t, stackPrefix)
23+
stackSlug := GetRandomStackName(stackPrefix)
24+
25+
apiKeyPrefix := "testsminstall-"
26+
testAccDeleteExistingCloudAPIKeys(t, apiKeyPrefix)
27+
apiKeyName := apiKeyPrefix + acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
28+
29+
resource.ParallelTest(t, resource.TestCase{
30+
ProviderFactories: testutils.ProviderFactories,
31+
CheckDestroy: testAccStackCheckDestroy(&stack),
32+
Steps: []resource.TestStep{
33+
{
34+
Config: testAccSyntheticMonitoringInstallation(stackSlug, apiKeyName, region),
35+
Check: resource.ComposeTestCheckFunc(
36+
testAccStackCheckExists("grafana_cloud_stack.test", &stack),
37+
resource.TestCheckResourceAttrSet("grafana_synthetic_monitoring_installation.test", "sm_access_token"),
38+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_installation.test", "stack_sm_api_url", expectedURL),
39+
),
40+
},
41+
// Test deletion
42+
{
43+
Config: testAccSyntheticMonitoringInstallation_Base(stackSlug, apiKeyName, region),
44+
},
45+
},
46+
})
47+
})
48+
}
4249
}
4350

44-
func testAccSyntheticMonitoringInstallation_Base(stackSlug, apiKeyName string) string {
45-
return testAccStackConfigBasic(stackSlug, stackSlug) +
51+
func testAccSyntheticMonitoringInstallation_Base(stackSlug, apiKeyName, region string) string {
52+
return testAccStackConfigBasicWithCustomResourceName(stackSlug, stackSlug, region, "test") +
4653
testAccCloudAPIKeyConfig(apiKeyName, "MetricsPublisher")
4754
}
4855

49-
func testAccSyntheticMonitoringInstallation(stackSlug, apiKeyName string) string {
50-
return testAccSyntheticMonitoringInstallation_Base(stackSlug, apiKeyName) +
56+
func testAccSyntheticMonitoringInstallation(stackSlug, apiKeyName, region string) string {
57+
return testAccSyntheticMonitoringInstallation_Base(stackSlug, apiKeyName, region) +
5158
`
5259
resource "grafana_synthetic_monitoring_installation" "test" {
5360
stack_id = grafana_cloud_stack.test.id

0 commit comments

Comments
 (0)