Skip to content

Commit 5dcf200

Browse files
authored
automation: split automation_connection_certificate CreateUpdate into separate Create and Update (#31932)
1 parent 2160ab7 commit 5dcf200

File tree

1 file changed

+62
-18
lines changed

1 file changed

+62
-18
lines changed

internal/services/automation/automation_connection_certificate_resource.go

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222

2323
func resourceAutomationConnectionCertificate() *pluginsdk.Resource {
2424
return &pluginsdk.Resource{
25-
Create: resourceAutomationConnectionCertificateCreateUpdate,
25+
Create: resourceAutomationConnectionCertificateCreate,
2626
Read: resourceAutomationConnectionCertificateRead,
27-
Update: resourceAutomationConnectionCertificateCreateUpdate,
27+
Update: resourceAutomationConnectionCertificateUpdate,
2828
Delete: resourceAutomationConnectionCertificateDelete,
2929

3030
Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error {
@@ -76,32 +76,25 @@ func resourceAutomationConnectionCertificate() *pluginsdk.Resource {
7676
}
7777
}
7878

79-
func resourceAutomationConnectionCertificateCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
79+
func resourceAutomationConnectionCertificateCreate(d *pluginsdk.ResourceData, meta interface{}) error {
8080
client := meta.(*clients.Client).Automation.Connection
8181
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
82-
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
82+
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
8383
defer cancel()
8484

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

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

89-
if d.IsNewResource() {
90-
existing, err := client.Get(ctx, id)
91-
if err != nil {
92-
if !response.WasNotFound(existing.HttpResponse) {
93-
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
94-
}
95-
}
96-
89+
existing, err := client.Get(ctx, id)
90+
if err != nil {
9791
if !response.WasNotFound(existing.HttpResponse) {
98-
return tf.ImportAsExistsError("azurerm_automation_connection_certificate", id.ID())
92+
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
9993
}
10094
}
10195

102-
fieldDefinitionValues := map[string]string{
103-
"AutomationCertificateName": d.Get("automation_certificate_name").(string),
104-
"SubscriptionID": d.Get("subscription_id").(string),
96+
if !response.WasNotFound(existing.HttpResponse) {
97+
return tf.ImportAsExistsError("azurerm_automation_connection_certificate", id.ID())
10598
}
10699

107100
parameters := connection.ConnectionCreateOrUpdateParameters{
@@ -111,19 +104,70 @@ func resourceAutomationConnectionCertificateCreateUpdate(d *pluginsdk.ResourceDa
111104
ConnectionType: connection.ConnectionTypeAssociationProperty{
112105
Name: pointer.To("Azure"),
113106
},
114-
FieldDefinitionValues: &fieldDefinitionValues,
107+
FieldDefinitionValues: &map[string]string{
108+
"AutomationCertificateName": d.Get("automation_certificate_name").(string),
109+
"SubscriptionID": d.Get("subscription_id").(string),
110+
},
115111
},
116112
}
117113

118114
if _, err := client.CreateOrUpdate(ctx, id, parameters); err != nil {
119-
return err
115+
return fmt.Errorf("creating %s: %+v", id, err)
120116
}
121117

122118
d.SetId(id.ID())
123119

124120
return resourceAutomationConnectionCertificateRead(d, meta)
125121
}
126122

123+
func resourceAutomationConnectionCertificateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
124+
client := meta.(*clients.Client).Automation.Connection
125+
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
126+
defer cancel()
127+
128+
id, err := connection.ParseConnectionID(d.Id())
129+
if err != nil {
130+
return err
131+
}
132+
133+
existing, err := client.Get(ctx, *id)
134+
if err != nil {
135+
return fmt.Errorf("retrieving existing %s: %+v", *id, err)
136+
}
137+
138+
// Start from the existing field definition values so that fields managed
139+
// via ignore_changes retain their server-side value.
140+
fieldDefinitionValues := make(map[string]string)
141+
if existing.Model != nil && existing.Model.Properties != nil && existing.Model.Properties.FieldDefinitionValues != nil {
142+
fieldDefinitionValues = *existing.Model.Properties.FieldDefinitionValues
143+
}
144+
145+
if d.HasChange("automation_certificate_name") {
146+
fieldDefinitionValues["AutomationCertificateName"] = d.Get("automation_certificate_name").(string)
147+
}
148+
149+
if d.HasChange("subscription_id") {
150+
fieldDefinitionValues["SubscriptionID"] = d.Get("subscription_id").(string)
151+
}
152+
153+
parameters := connection.ConnectionUpdateParameters{
154+
Name: &id.ConnectionName,
155+
Properties: &connection.ConnectionUpdateProperties{
156+
FieldDefinitionValues: &fieldDefinitionValues,
157+
},
158+
}
159+
160+
if d.HasChange("description") {
161+
parameters.Properties.Description = pointer.To(d.Get("description").(string))
162+
}
163+
164+
if _, err := client.Update(ctx, *id, parameters); err != nil {
165+
return fmt.Errorf("updating %s: %+v", *id, err)
166+
}
167+
168+
return resourceAutomationConnectionCertificateRead(d, meta)
169+
}
170+
127171
func resourceAutomationConnectionCertificateRead(d *pluginsdk.ResourceData, meta interface{}) error {
128172
client := meta.(*clients.Client).Automation.Connection
129173
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)

0 commit comments

Comments
 (0)