Skip to content

Commit 2f55943

Browse files
authored
Keep Frontend O11y URL exceptions separate for faroEndpointUrl (#2392)
1 parent 4e87baa commit 2f55943

File tree

3 files changed

+31
-70
lines changed

3 files changed

+31
-70
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package frontendo11y
2+
3+
import "fmt"
4+
5+
// TODO: make faroEndpointUrl visible in gcom response
6+
var faroEndpointUrlsRegionExceptions = map[string]string{
7+
"au": "https://faro-api-prod-au-southeast-0.grafana.net/faro",
8+
"eu": "https://faro-api-prod-eu-west-0.grafana.net/faro",
9+
"prod-us-east-0": "https://faro-api-prod-us-east-2.grafana.net/faro",
10+
"us-azure": "https://faro-api-prod-us-central-7.grafana.net/faro",
11+
"us": "https://faro-api-prod-us-central-0.grafana.net/faro",
12+
}
13+
14+
// getFrontendO11yAPIURLForRegion gets the frontend o11y API URL given a region slug
15+
func getFrontendO11yAPIURLForRegion(regionSlug string) string {
16+
if url, ok := faroEndpointUrlsRegionExceptions[regionSlug]; ok {
17+
return url
18+
}
19+
20+
return fmt.Sprintf("https://faro-api-%s.grafana.net/faro", regionSlug)
21+
}

internal/resources/frontendo11y/data_source_frontend_o11y_app.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func (r *datasourceFrontendO11yApp) Schema(ctx context.Context, req datasource.S
8585
}
8686
}
8787

88+
// getStackRegion gets the region slug from the stack id
8889
func (r *datasourceFrontendO11yApp) getStackRegion(ctx context.Context, stackID string) (string, error) {
8990
stack, res, err := r.gcomClient.InstancesAPI.GetInstance(ctx, stackID).Execute()
9091
if err != nil {
@@ -101,22 +102,6 @@ func (r *datasourceFrontendO11yApp) getStackRegion(ctx context.Context, stackID
101102
return stack.RegionSlug, nil
102103
}
103104

104-
func (r *datasourceFrontendO11yApp) getFrontendO11yAPIURLForRegion(ctx context.Context, regionSlug string) (string, error) {
105-
resp, httpResp, err := r.gcomClient.StackRegionsAPI.GetStackRegions(ctx).Slug(regionSlug).Execute()
106-
if err != nil || httpResp.StatusCode >= 300 || resp == nil || len(resp.Items) == 0 {
107-
return "", fmt.Errorf("failed to get region information for region %q", regionSlug)
108-
}
109-
110-
region := resp.Items[0]
111-
if val, ok := region.FormattedApiStackRegionAnyOf.AdditionalProperties["faroEndpointUrl"]; ok {
112-
if strVal, ok := val.(string); ok {
113-
return strVal, nil
114-
}
115-
}
116-
117-
return "", fmt.Errorf("faroEndpointUrl not found for region %q", regionSlug)
118-
}
119-
120105
func (r *datasourceFrontendO11yApp) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
121106
var dataTF FrontendO11yAppTFModel
122107
diags := req.Config.Get(ctx, &dataTF)
@@ -125,17 +110,12 @@ func (r *datasourceFrontendO11yApp) Read(ctx context.Context, req datasource.Rea
125110
return
126111
}
127112

128-
stackRegion, err := r.getStackRegion(ctx, dataTF.StackID.String())
113+
stackRegionSlug, err := r.getStackRegion(ctx, dataTF.StackID.String())
129114
if err != nil {
130115
resp.Diagnostics.AddError("failed to get Grafana Cloud Stack information", err.Error())
131116
return
132117
}
133-
faroEndpointURL, err := r.getFrontendO11yAPIURLForRegion(ctx, stackRegion)
134-
if err != nil {
135-
resp.Diagnostics.AddError("failed to get Grafana Cloud Stack region information", err.Error())
136-
return
137-
}
138-
118+
faroEndpointURL := getFrontendO11yAPIURLForRegion(stackRegionSlug)
139119
appsClientModel, err := r.client.GetApps(ctx, faroEndpointURL, dataTF.StackID.ValueInt64())
140120
if err != nil {
141121
resp.Diagnostics.AddError("failed to get frontend o11y apps", err.Error())

internal/resources/frontendo11y/resource_frontend_o11y_app.go

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1313
"github.com/hashicorp/terraform-plugin-framework/resource"
1414
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
15+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1516

1617
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
1718
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1819
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
19-
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
2020
"github.com/hashicorp/terraform-plugin-framework/types"
2121

2222
"github.com/grafana/grafana-com-public-clients/go/gcom"
@@ -127,6 +127,7 @@ func (r *resourceFrontendO11yApp) Schema(ctx context.Context, req resource.Schem
127127
}
128128
}
129129

130+
// getRegionSlug gets the region slug from the stack id
130131
func (r *resourceFrontendO11yApp) getRegionSlug(ctx context.Context, stackID string) (string, error) {
131132
stack, res, err := r.gcomClient.InstancesAPI.GetInstance(ctx, stackID).Execute()
132133
if err != nil {
@@ -159,22 +160,6 @@ func (r *resourceFrontendO11yApp) getStack(ctx context.Context, stackID string)
159160
return stack, nil
160161
}
161162

162-
func (r *resourceFrontendO11yApp) getFrontendO11yAPIURLForRegion(ctx context.Context, regionSlug string) (string, error) {
163-
resp, httpResp, err := r.gcomClient.StackRegionsAPI.GetStackRegions(ctx).Slug(regionSlug).Execute()
164-
if err != nil || httpResp.StatusCode >= 300 || resp == nil || len(resp.Items) == 0 {
165-
return "", fmt.Errorf("failed to get region information for region %q", regionSlug)
166-
}
167-
168-
region := resp.Items[0]
169-
if val, ok := region.FormattedApiStackRegionAnyOf.AdditionalProperties["faroEndpointUrl"]; ok {
170-
if strVal, ok := val.(string); ok {
171-
return strVal, nil
172-
}
173-
}
174-
175-
return "", fmt.Errorf("faroEndpointUrl not found for region %q", regionSlug)
176-
}
177-
178163
func (r *resourceFrontendO11yApp) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
179164
var dataTF FrontendO11yAppTFModel
180165
diags := req.Plan.Get(ctx, &dataTF)
@@ -192,12 +177,7 @@ func (r *resourceFrontendO11yApp) Create(ctx context.Context, req resource.Creat
192177
return
193178
}
194179

195-
faroEndpointURL, err := r.getFrontendO11yAPIURLForRegion(ctx, stackRegionSlug)
196-
if err != nil {
197-
resp.Diagnostics.AddError("failed to get Grafana Cloud Stack region information", err.Error())
198-
return
199-
}
200-
180+
faroEndpointURL := getFrontendO11yAPIURLForRegion(stackRegionSlug)
201181
appClientModel, err := r.client.CreateApp(ctx, faroEndpointURL, dataTF.StackID.ValueInt64(), app)
202182
if err != nil {
203183
resp.Diagnostics.AddError("failed to get Grafana Cloud Stack information", err.Error())
@@ -235,12 +215,7 @@ func (r *resourceFrontendO11yApp) ImportState(ctx context.Context, req resource.
235215
return
236216
}
237217

238-
faroEndpointURL, err := r.getFrontendO11yAPIURLForRegion(ctx, stackRegionSlug)
239-
if err != nil {
240-
resp.Diagnostics.AddError("failed to get Grafana Cloud Stack region information", err.Error())
241-
return
242-
}
243-
218+
faroEndpointURL := getFrontendO11yAPIURLForRegion(stackRegionSlug)
244219
appClientModel, err := r.client.GetApp(ctx, faroEndpointURL, int64(stack.Id), i64AppID)
245220
if err != nil {
246221
resp.Diagnostics.AddError("failed to get frontend o11y app", err.Error())
@@ -266,12 +241,7 @@ func (r *resourceFrontendO11yApp) Read(ctx context.Context, req resource.ReadReq
266241
return
267242
}
268243

269-
faroEndpointURL, err := r.getFrontendO11yAPIURLForRegion(ctx, stackRegionSlug)
270-
if err != nil {
271-
resp.Diagnostics.AddError("failed to get Grafana Cloud Stack region information", err.Error())
272-
return
273-
}
274-
244+
faroEndpointURL := getFrontendO11yAPIURLForRegion(stackRegionSlug)
275245
appClientModel, err := r.client.GetApps(ctx, faroEndpointURL, dataTF.StackID.ValueInt64())
276246
if err != nil {
277247
resp.Diagnostics.AddError("failed to get frontend o11y app", err.Error())
@@ -305,12 +275,7 @@ func (r *resourceFrontendO11yApp) Update(ctx context.Context, req resource.Updat
305275
return
306276
}
307277

308-
faroEndpointURL, err := r.getFrontendO11yAPIURLForRegion(ctx, stackRegionSlug)
309-
if err != nil {
310-
resp.Diagnostics.AddError("failed to get Grafana Cloud Stack region information", err.Error())
311-
return
312-
}
313-
278+
faroEndpointURL := getFrontendO11yAPIURLForRegion(stackRegionSlug)
314279
appClientModel, err := r.client.UpdateApp(ctx, faroEndpointURL, dataTF.StackID.ValueInt64(), app.ID, app)
315280
if err != nil {
316281
resp.Diagnostics.AddError("failed to update frontend o11y app", err.Error())
@@ -337,12 +302,7 @@ func (r *resourceFrontendO11yApp) Delete(ctx context.Context, req resource.Delet
337302
return
338303
}
339304

340-
faroEndpointURL, err := r.getFrontendO11yAPIURLForRegion(ctx, stackRegionSlug)
341-
if err != nil {
342-
resp.Diagnostics.AddError("failed to get Grafana Cloud Stack region information", err.Error())
343-
return
344-
}
345-
305+
faroEndpointURL := getFrontendO11yAPIURLForRegion(stackRegionSlug)
346306
err = r.client.DeleteApp(ctx, faroEndpointURL, dataTF.StackID.ValueInt64(), dataTF.ID.ValueInt64())
347307
if err != nil {
348308
resp.Diagnostics.AddError("failed to delete frontend o11y app", err.Error())

0 commit comments

Comments
 (0)