Skip to content

Commit 5921780

Browse files
committed
automation_certificate: split CreateUpdate into separate Create and Update
1 parent e4792b6 commit 5921780

File tree

1 file changed

+57
-22
lines changed

1 file changed

+57
-22
lines changed

internal/services/automation/automation_certificate_resource.go

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"time"
1010

11+
"github.com/hashicorp/go-azure-helpers/lang/pointer"
1112
"github.com/hashicorp/go-azure-helpers/lang/response"
1213
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
1314
"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2024-10-23/certificate"
@@ -20,9 +21,9 @@ import (
2021

2122
func resourceAutomationCertificate() *pluginsdk.Resource {
2223
resource := &pluginsdk.Resource{
23-
Create: resourceAutomationCertificateCreateUpdate,
24+
Create: resourceAutomationCertificateCreate,
2425
Read: resourceAutomationCertificateRead,
25-
Update: resourceAutomationCertificateCreateUpdate,
26+
Update: resourceAutomationCertificateUpdate,
2627
Delete: resourceAutomationCertificateDelete,
2728

2829
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
@@ -83,53 +84,87 @@ func resourceAutomationCertificate() *pluginsdk.Resource {
8384
return resource
8485
}
8586

86-
func resourceAutomationCertificateCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
87+
func resourceAutomationCertificateCreate(d *pluginsdk.ResourceData, meta interface{}) error {
8788
client := meta.(*clients.Client).Automation.Certificate
8889
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
89-
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
90+
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
9091
defer cancel()
9192

9293
log.Printf("[INFO] preparing arguments for AzureRM Automation Certificate creation.")
9394

9495
id := certificate.NewCertificateID(subscriptionId, d.Get("resource_group_name").(string), d.Get("automation_account_name").(string), d.Get("name").(string))
95-
exportable := d.Get("exportable").(bool)
96-
97-
if d.IsNewResource() {
98-
existing, err := client.Get(ctx, id)
99-
if err != nil {
100-
if !response.WasNotFound(existing.HttpResponse) {
101-
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
102-
}
103-
}
10496

97+
existing, err := client.Get(ctx, id)
98+
if err != nil {
10599
if !response.WasNotFound(existing.HttpResponse) {
106-
return tf.ImportAsExistsError("azurerm_automation_certificate", id.ID())
100+
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
107101
}
108102
}
109103

110-
description := d.Get("description").(string)
104+
if !response.WasNotFound(existing.HttpResponse) {
105+
return tf.ImportAsExistsError("azurerm_automation_certificate", id.ID())
106+
}
111107

112108
parameters := certificate.CertificateCreateOrUpdateParameters{
113109
Name: id.CertificateName,
114110
Properties: certificate.CertificateCreateOrUpdateProperties{
115-
Description: &description,
116-
IsExportable: &exportable,
111+
Description: pointer.To(d.Get("description").(string)),
112+
Base64Value: d.Get("base64").(string),
113+
IsExportable: pointer.To(d.Get("exportable").(bool)),
117114
},
118115
}
119116

120-
if v, ok := d.GetOk("base64"); ok {
121-
parameters.Properties.Base64Value = v.(string)
122-
}
123-
124117
if _, err := client.CreateOrUpdate(ctx, id, parameters); err != nil {
125-
return fmt.Errorf("creating/updating %s: %+v", id, err)
118+
return fmt.Errorf("creating %s: %+v", id, err)
126119
}
127120

128121
d.SetId(id.ID())
129122

130123
return resourceAutomationCertificateRead(d, meta)
131124
}
132125

126+
func resourceAutomationCertificateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
127+
client := meta.(*clients.Client).Automation.Certificate
128+
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
129+
defer cancel()
130+
131+
id, err := certificate.ParseCertificateID(d.Id())
132+
if err != nil {
133+
return err
134+
}
135+
136+
existing, err := client.Get(ctx, *id)
137+
if err != nil {
138+
return fmt.Errorf("retrieving existing %s: %+v", *id, err)
139+
}
140+
141+
if existing.Model == nil || existing.Model.Properties == nil {
142+
return fmt.Errorf("retrieving existing %s: model or properties were nil", *id)
143+
}
144+
145+
parameters := certificate.CertificateCreateOrUpdateParameters{
146+
Name: id.CertificateName,
147+
Properties: certificate.CertificateCreateOrUpdateProperties{
148+
Description: existing.Model.Properties.Description,
149+
IsExportable: existing.Model.Properties.IsExportable,
150+
},
151+
}
152+
153+
if d.HasChange("description") {
154+
parameters.Properties.Description = pointer.To(d.Get("description").(string))
155+
}
156+
157+
if d.HasChange("exportable") {
158+
parameters.Properties.IsExportable = pointer.To(d.Get("exportable").(bool))
159+
}
160+
161+
if _, err := client.CreateOrUpdate(ctx, *id, parameters); err != nil {
162+
return fmt.Errorf("updating %s: %+v", *id, err)
163+
}
164+
165+
return resourceAutomationCertificateRead(d, meta)
166+
}
167+
133168
func resourceAutomationCertificateRead(d *pluginsdk.ResourceData, meta interface{}) error {
134169
client := meta.(*clients.Client).Automation.Certificate
135170
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)

0 commit comments

Comments
 (0)