@@ -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
130131func (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-
178163func (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