Skip to content

Commit 9b1411e

Browse files
authored
azurerm_application_insights_web_test - split create & update (#31919)
1 parent a8d216e commit 9b1411e

File tree

1 file changed

+105
-32
lines changed

1 file changed

+105
-32
lines changed

internal/services/applicationinsights/application_insights_web_test_resource.go

Lines changed: 105 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ import (
3030

3131
func resourceApplicationInsightsWebTests() *pluginsdk.Resource {
3232
return &pluginsdk.Resource{
33-
Create: resourceApplicationInsightsWebTestsCreateUpdate,
33+
Create: resourceApplicationInsightsWebTestsCreate,
3434
Read: resourceApplicationInsightsWebTestsRead,
35-
Update: resourceApplicationInsightsWebTestsCreateUpdate,
35+
Update: resourceApplicationInsightsWebTestsUpdate,
3636
Delete: resourceApplicationInsightsWebTestsDelete,
3737

3838
Importer: pluginsdk.ImporterValidatingIdentity(&webtests.WebTestId{}),
@@ -141,9 +141,9 @@ func resourceApplicationInsightsWebTests() *pluginsdk.Resource {
141141
}
142142
}
143143

144-
func resourceApplicationInsightsWebTestsCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
144+
func resourceApplicationInsightsWebTestsCreate(d *pluginsdk.ResourceData, meta interface{}) error {
145145
client := meta.(*clients.Client).AppInsights.WebTestsClient
146-
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
146+
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
147147
defer cancel()
148148

149149
log.Printf("[INFO] preparing arguments for AzureRM Application Insights WebTest creation.")
@@ -155,56 +155,49 @@ func resourceApplicationInsightsWebTestsCreateUpdate(d *pluginsdk.ResourceData,
155155

156156
id := webtests.NewWebTestID(appInsightsId.SubscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
157157

158-
if d.IsNewResource() {
159-
existing, err := client.WebTestsGet(ctx, id)
160-
if err != nil {
161-
if !response.WasNotFound(existing.HttpResponse) {
162-
return fmt.Errorf("checking for presence of %s: %+v", id, err)
163-
}
164-
}
165-
158+
existing, err := client.WebTestsGet(ctx, id)
159+
if err != nil {
166160
if !response.WasNotFound(existing.HttpResponse) {
167-
return tf.ImportAsExistsError("azurerm_application_insights_web_test", id.ID())
161+
return fmt.Errorf("checking for presence of %s: %+v", id, err)
168162
}
169163
}
170164

171-
kind := d.Get("kind").(string)
172-
description := d.Get("description").(string)
173-
frequency := int32(d.Get("frequency").(int))
174-
timeout := int32(d.Get("timeout").(int))
175-
isEnabled := d.Get("enabled").(bool)
176-
retryEnabled := d.Get("retry_enabled").(bool)
177-
geoLocationsRaw := d.Get("geo_locations").([]interface{})
178-
geoLocations := expandApplicationInsightsWebTestGeoLocations(geoLocationsRaw)
179-
testConf := d.Get("configuration").(string)
165+
if !response.WasNotFound(existing.HttpResponse) {
166+
return tf.ImportAsExistsError("azurerm_application_insights_web_test", id.ID())
167+
}
180168

169+
// Azure uses a special "hidden-link" tag to associate a web test with its parent Application Insights
170+
// component. The tag key is "hidden-link:<resource_id>" where <resource_id> is the full ARM resource ID
171+
// of the Application Insights component, and the value is "Resource". This tag is injected into the
172+
// user-supplied tags map before sending the request. It is genreally undocumented but can be seen in
173+
// https://learn.microsoft.com/en-us/azure/azure-monitor/app/availability?tabs=standard
181174
t := d.Get("tags").(map[string]interface{})
182175
tagKey := fmt.Sprintf("hidden-link:%s", appInsightsId.ID())
183176
t[tagKey] = "Resource"
184177

185178
webTest := webtests.WebTest{
186179
Name: pointer.To(id.WebTestName),
187180
Location: location.Normalize(d.Get("location").(string)),
188-
Kind: pointer.To(webtests.WebTestKind(kind)),
181+
Kind: pointer.To(webtests.WebTestKind(d.Get("kind").(string))),
189182
Properties: &webtests.WebTestProperties{
190183
SyntheticMonitorId: id.WebTestName,
191184
Name: id.WebTestName,
192-
Description: &description,
193-
Enabled: &isEnabled,
194-
Frequency: pointer.To(int64(frequency)),
195-
Timeout: pointer.To(int64(timeout)),
196-
Kind: webtests.WebTestKind(kind),
197-
RetryEnabled: &retryEnabled,
198-
Locations: geoLocations,
185+
Description: pointer.To(d.Get("description").(string)),
186+
Enabled: pointer.To(d.Get("enabled").(bool)),
187+
Frequency: pointer.To(int64(d.Get("frequency").(int))),
188+
Timeout: pointer.To(int64(d.Get("timeout").(int))),
189+
Kind: webtests.WebTestKind(d.Get("kind").(string)),
190+
RetryEnabled: pointer.To(d.Get("retry_enabled").(bool)),
191+
Locations: expandApplicationInsightsWebTestGeoLocations(d.Get("geo_locations").([]interface{})),
199192
Configuration: &webtests.WebTestPropertiesConfiguration{
200-
WebTest: &testConf,
193+
WebTest: pointer.To(d.Get("configuration").(string)),
201194
},
202195
},
203196
Tags: tags.Expand(t),
204197
}
205198

206199
if _, err = client.WebTestsCreateOrUpdate(ctx, id, webTest); err != nil {
207-
return fmt.Errorf("creating/updating %s: %+v", id, err)
200+
return fmt.Errorf("creating %s: %+v", id, err)
208201
}
209202

210203
d.SetId(id.ID())
@@ -215,6 +208,86 @@ func resourceApplicationInsightsWebTestsCreateUpdate(d *pluginsdk.ResourceData,
215208
return resourceApplicationInsightsWebTestsRead(d, meta)
216209
}
217210

211+
func resourceApplicationInsightsWebTestsUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
212+
client := meta.(*clients.Client).AppInsights.WebTestsClient
213+
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
214+
defer cancel()
215+
216+
id, err := webtests.ParseWebTestID(d.Id())
217+
if err != nil {
218+
return err
219+
}
220+
221+
existing, err := client.WebTestsGet(ctx, *id)
222+
if err != nil {
223+
return fmt.Errorf("retrieving %s: %+v", *id, err)
224+
}
225+
226+
if existing.Model == nil {
227+
return fmt.Errorf("retrieving %s: `model` was nil", *id)
228+
}
229+
230+
if existing.Model.Properties == nil {
231+
return fmt.Errorf("retrieving %s: `properties` was nil", *id)
232+
}
233+
234+
webTest := *existing.Model
235+
props := webTest.Properties
236+
237+
if d.HasChange("description") {
238+
props.Description = pointer.To(d.Get("description").(string))
239+
}
240+
241+
if d.HasChange("enabled") {
242+
props.Enabled = pointer.To(d.Get("enabled").(bool))
243+
}
244+
245+
if d.HasChange("frequency") {
246+
props.Frequency = pointer.To(int64(d.Get("frequency").(int)))
247+
}
248+
249+
if d.HasChange("timeout") {
250+
props.Timeout = pointer.To(int64(d.Get("timeout").(int)))
251+
}
252+
253+
if d.HasChange("retry_enabled") {
254+
props.RetryEnabled = pointer.To(d.Get("retry_enabled").(bool))
255+
}
256+
257+
if d.HasChange("geo_locations") {
258+
props.Locations = expandApplicationInsightsWebTestGeoLocations(d.Get("geo_locations").([]interface{}))
259+
}
260+
261+
if d.HasChange("configuration") {
262+
props.Configuration = &webtests.WebTestPropertiesConfiguration{
263+
WebTest: pointer.To(d.Get("configuration").(string)),
264+
}
265+
}
266+
267+
// Azure uses a special "hidden-link" tag to associate a web test with its parent Application Insights
268+
// component. The tag key is "hidden-link:<resource_id>" where <resource_id> is the full ARM resource ID
269+
// of the Application Insights component, and the value is "Resource". This tag is injected into the
270+
// user-supplied tags map before sending the request. It is genreally undocumented but can be seen in
271+
// https://learn.microsoft.com/en-us/azure/azure-monitor/app/availability?tabs=standard
272+
if d.HasChange("tags") {
273+
appInsightsId, err := components.ParseComponentID(d.Get("application_insights_id").(string))
274+
if err != nil {
275+
return err
276+
}
277+
278+
t := d.Get("tags").(map[string]interface{})
279+
tagKey := fmt.Sprintf("hidden-link:%s", appInsightsId.ID())
280+
t[tagKey] = "Resource"
281+
webTest.Tags = tags.Expand(t)
282+
}
283+
284+
if _, err = client.WebTestsCreateOrUpdate(ctx, *id, webTest); err != nil {
285+
return fmt.Errorf("updating %s: %+v", *id, err)
286+
}
287+
288+
return resourceApplicationInsightsWebTestsRead(d, meta)
289+
}
290+
218291
func resourceApplicationInsightsWebTestsRead(d *pluginsdk.ResourceData, meta interface{}) error {
219292
client := meta.(*clients.Client).AppInsights.WebTestsClient
220293
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)

0 commit comments

Comments
 (0)