Skip to content

Commit 06d0a84

Browse files
committed
automation: split resourceAutomationWebhookCreateUpdate into Create and Update
Split the shared resourceAutomationWebhookCreateUpdate function into separate resourceAutomationWebhookCreate and resourceAutomationWebhookUpdate functions. Create uses client.CreateOrUpdate (PUT) with existence check and URI generation logic. Update uses client.Update (PATCH) with HasChanges guards for the updatable fields (enabled, run_on_worker_group, parameters). The URI is sensitive and not returned by the API, so it is preserved from state.
1 parent c9008a9 commit 06d0a84

1 file changed

Lines changed: 84 additions & 42 deletions

File tree

internal/services/automation/automation_webhook_resource.go

Lines changed: 84 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import (
2525

2626
func resourceAutomationWebhook() *pluginsdk.Resource {
2727
return &pluginsdk.Resource{
28-
Create: resourceAutomationWebhookCreateUpdate,
28+
Create: resourceAutomationWebhookCreate,
2929
Read: resourceAutomationWebhookRead,
30-
Update: resourceAutomationWebhookCreateUpdate,
30+
Update: resourceAutomationWebhookUpdate,
3131
Delete: resourceAutomationWebhookDelete,
3232

3333
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
@@ -108,65 +108,52 @@ func resourceAutomationWebhook() *pluginsdk.Resource {
108108
}
109109
}
110110

111-
func resourceAutomationWebhookCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
111+
func resourceAutomationWebhookCreate(d *pluginsdk.ResourceData, meta interface{}) error {
112112
client := meta.(*clients.Client).Automation.WebhookClient
113113
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
114-
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
114+
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
115115
defer cancel()
116116

117117
id := webhook.NewWebHookID(subscriptionId, d.Get("resource_group_name").(string), d.Get("automation_account_name").(string), d.Get("name").(string))
118-
expiryTime := d.Get("expiry_time").(string)
119-
enabled := d.Get("enabled").(bool)
120-
runbookName := d.Get("runbook_name").(string)
121-
runOn := d.Get("run_on_worker_group").(string)
122-
webhookParameters := expandStringInterfaceMap(d.Get("parameters").(map[string]interface{}))
123-
124-
if d.IsNewResource() {
125-
resp, err := client.Get(ctx, id)
126-
if err != nil {
127-
if !response.WasNotFound(resp.HttpResponse) {
128-
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
129-
}
130-
}
131118

132-
if resp.Model != nil && resp.Model.Id != nil && *resp.Model.Id != "" {
133-
return tf.ImportAsExistsError("azurerm_automation_webhook", *resp.Model.Id)
119+
resp, err := client.Get(ctx, id)
120+
if err != nil {
121+
if !response.WasNotFound(resp.HttpResponse) {
122+
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
134123
}
135124
}
136125

126+
if resp.Model != nil && resp.Model.Id != nil && *resp.Model.Id != "" {
127+
return tf.ImportAsExistsError("azurerm_automation_webhook", *resp.Model.Id)
128+
}
129+
137130
parameters := webhook.WebhookCreateOrUpdateParameters{
138131
Name: id.WebHookName,
139132
Properties: webhook.WebhookCreateOrUpdateProperties{
140-
IsEnabled: pointer.To(enabled),
141-
ExpiryTime: &expiryTime,
142-
Parameters: &webhookParameters,
133+
IsEnabled: pointer.To(d.Get("enabled").(bool)),
134+
ExpiryTime: pointer.To(d.Get("expiry_time").(string)),
135+
Parameters: pointer.To(expandStringInterfaceMap(d.Get("parameters").(map[string]interface{}))),
143136
Runbook: &webhook.RunbookAssociationProperty{
144-
Name: pointer.To(runbookName),
137+
Name: pointer.To(d.Get("runbook_name").(string)),
145138
},
146-
RunOn: pointer.To(runOn),
139+
RunOn: pointer.To(d.Get("run_on_worker_group").(string)),
147140
},
148141
}
149142

150143
uri := ""
151-
if d.IsNewResource() {
152-
if v := d.Get("uri"); v != nil && v.(string) != "" {
153-
uri = v.(string)
154-
parameters.Properties.Uri = &uri
155-
} else {
156-
automationAccountId := webhook.NewAutomationAccountID(subscriptionId, id.ResourceGroupName, id.AutomationAccountName)
157-
resp, err := client.GenerateUri(ctx, automationAccountId)
158-
if err != nil {
159-
return fmt.Errorf("unable to generate URI for %s: %+v", id, err)
160-
}
161-
162-
parameters.Properties.Uri = resp.Model
163-
if resp.Model != nil {
164-
uri = *resp.Model
165-
}
166-
}
144+
if v := d.Get("uri"); v != nil && v.(string) != "" {
145+
uri = v.(string)
146+
parameters.Properties.Uri = &uri
167147
} else {
168-
if d.Get("uri") != nil {
169-
parameters.Properties.Uri = pointer.To(d.Get("uri").(string))
148+
automationAccountId := webhook.NewAutomationAccountID(subscriptionId, id.ResourceGroupName, id.AutomationAccountName)
149+
resp, err := client.GenerateUri(ctx, automationAccountId)
150+
if err != nil {
151+
return fmt.Errorf("unable to generate URI for %s: %+v", id, err)
152+
}
153+
154+
parameters.Properties.Uri = resp.Model
155+
if resp.Model != nil {
156+
uri = *resp.Model
170157
}
171158
}
172159

@@ -182,6 +169,61 @@ func resourceAutomationWebhookCreateUpdate(d *pluginsdk.ResourceData, meta inter
182169
return resourceAutomationWebhookRead(d, meta)
183170
}
184171

172+
func resourceAutomationWebhookUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
173+
client := meta.(*clients.Client).Automation.WebhookClient
174+
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
175+
defer cancel()
176+
177+
id, err := webhook.ParseWebHookID(d.Id())
178+
if err != nil {
179+
return err
180+
}
181+
182+
resp, err := client.Get(ctx, *id)
183+
if err != nil {
184+
return fmt.Errorf("retrieving %s: %+v", *id, err)
185+
}
186+
187+
if resp.Model == nil || resp.Model.Properties == nil {
188+
return fmt.Errorf("retrieving %s: model or properties was nil", *id)
189+
}
190+
191+
existing := resp.Model.Properties
192+
193+
parameters := webhook.WebhookUpdateParameters{
194+
Name: pointer.To(id.WebHookName),
195+
Properties: &webhook.WebhookUpdateProperties{
196+
IsEnabled: existing.IsEnabled,
197+
RunOn: existing.RunOn,
198+
Parameters: existing.Parameters,
199+
},
200+
}
201+
202+
if d.HasChange("enabled") {
203+
parameters.Properties.IsEnabled = pointer.To(d.Get("enabled").(bool))
204+
}
205+
206+
if d.HasChange("run_on_worker_group") {
207+
parameters.Properties.RunOn = pointer.To(d.Get("run_on_worker_group").(string))
208+
}
209+
210+
// NOTE: parameters is a map that is replaced as a whole
211+
if d.HasChange("parameters") {
212+
parameters.Properties.Parameters = pointer.To(expandStringInterfaceMap(d.Get("parameters").(map[string]interface{})))
213+
}
214+
215+
if _, err := client.Update(ctx, *id, parameters); err != nil {
216+
return fmt.Errorf("updating %s: %+v", *id, err)
217+
}
218+
219+
// URI is not present in the response from Azure, preserve from state
220+
if v := d.Get("uri"); v != nil && v.(string) != "" {
221+
d.Set("uri", v.(string))
222+
}
223+
224+
return resourceAutomationWebhookRead(d, meta)
225+
}
226+
185227
func resourceAutomationWebhookRead(d *pluginsdk.ResourceData, meta interface{}) error {
186228
client := meta.(*clients.Client).Automation.WebhookClient
187229
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)

0 commit comments

Comments
 (0)