Skip to content

Commit cdcb027

Browse files
authored
automation_connection: split CreateUpdate into separate Create and Update functions (#31934)
1 parent dd4a8c8 commit cdcb027

1 file changed

Lines changed: 45 additions & 14 deletions

File tree

internal/services/automation/automation_connection_resource.go

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import (
2424

2525
func resourceAutomationConnection() *pluginsdk.Resource {
2626
return &pluginsdk.Resource{
27-
Create: resourceAutomationConnectionCreateUpdate,
27+
Create: resourceAutomationConnectionCreate,
2828
Read: resourceAutomationConnectionRead,
29-
Update: resourceAutomationConnectionCreateUpdate,
29+
Update: resourceAutomationConnectionUpdate,
3030
Delete: resourceAutomationConnectionDelete,
3131

3232
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
@@ -81,30 +81,28 @@ func resourceAutomationConnection() *pluginsdk.Resource {
8181
}
8282
}
8383

84-
func resourceAutomationConnectionCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
84+
func resourceAutomationConnectionCreate(d *pluginsdk.ResourceData, meta interface{}) error {
8585
client := meta.(*clients.Client).Automation.Connection
8686
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
8787
connectionTypeClient := meta.(*clients.Client).Automation.ConnectionType
88-
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
88+
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
8989
defer cancel()
9090

9191
log.Printf("[INFO] preparing arguments for AzureRM Automation Connection creation.")
9292

9393
id := connection.NewConnectionID(subscriptionId, d.Get("resource_group_name").(string), d.Get("automation_account_name").(string), d.Get("name").(string))
9494

95-
if d.IsNewResource() {
96-
existing, err := client.Get(ctx, id)
97-
if err != nil {
98-
if !response.WasNotFound(existing.HttpResponse) {
99-
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
100-
}
101-
}
102-
95+
existing, err := client.Get(ctx, id)
96+
if err != nil {
10397
if !response.WasNotFound(existing.HttpResponse) {
104-
return tf.ImportAsExistsError("azurerm_automation_connection", id.ID())
98+
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
10599
}
106100
}
107101

102+
if !response.WasNotFound(existing.HttpResponse) {
103+
return tf.ImportAsExistsError("azurerm_automation_connection", id.ID())
104+
}
105+
108106
connectionTypeName := d.Get("type").(string)
109107
values := expandStringInterfaceMap(d.Get("values").(map[string]interface{}))
110108

@@ -138,14 +136,47 @@ func resourceAutomationConnectionCreateUpdate(d *pluginsdk.ResourceData, meta in
138136
}
139137

140138
if _, err := client.CreateOrUpdate(ctx, id, parameters); err != nil {
141-
return err
139+
return fmt.Errorf("creating %s: %+v", id, err)
142140
}
143141

144142
d.SetId(id.ID())
145143

146144
return resourceAutomationConnectionRead(d, meta)
147145
}
148146

147+
func resourceAutomationConnectionUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
148+
client := meta.(*clients.Client).Automation.Connection
149+
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
150+
defer cancel()
151+
152+
log.Printf("[INFO] preparing arguments for AzureRM Automation Connection update.")
153+
154+
id, err := connection.ParseConnectionID(d.Id())
155+
if err != nil {
156+
return err
157+
}
158+
159+
parameters := connection.ConnectionUpdateParameters{
160+
Name: pointer.To(id.ConnectionName),
161+
Properties: &connection.ConnectionUpdateProperties{},
162+
}
163+
164+
if d.HasChange("description") {
165+
parameters.Properties.Description = pointer.To(d.Get("description").(string))
166+
}
167+
168+
if d.HasChange("values") {
169+
values := expandStringInterfaceMap(d.Get("values").(map[string]interface{}))
170+
parameters.Properties.FieldDefinitionValues = &values
171+
}
172+
173+
if _, err := client.Update(ctx, *id, parameters); err != nil {
174+
return fmt.Errorf("updating %s: %+v", *id, err)
175+
}
176+
177+
return resourceAutomationConnectionRead(d, meta)
178+
}
179+
149180
func resourceAutomationConnectionRead(d *pluginsdk.ResourceData, meta interface{}) error {
150181
client := meta.(*clients.Client).Automation.Connection
151182
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)

0 commit comments

Comments
 (0)