Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurem_iothub_certificate, azurem_iothub_dps_certificate - fix certificate updates #21163

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 44 additions & 13 deletions internal/services/iothub/iothub_certificate_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (

func resourceIotHubCertificate() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceIotHubCertificateCreateUpdate,
Create: resourceIotHubCertificateCreate,
Read: resourceIotHubCertificateRead,
Update: resourceIotHubCertificateCreateUpdate,
Update: resourceIotHubCertificateUpdate,
Delete: resourceIotHubCertificateDelete,

SchemaVersion: 1,
Expand Down Expand Up @@ -74,27 +74,25 @@ func resourceIotHubCertificate() *pluginsdk.Resource {
}
}

func resourceIotHubCertificateCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
func resourceIotHubCertificateCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).IoTHub.IotHubCertificateClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewIotHubCertificateID(subscriptionId, d.Get("resource_group_name").(string), d.Get("iothub_name").(string), d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.IotHubName, id.CertificateName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

existing, err := client.Get(ctx, id.ResourceGroup, id.IotHubName, id.CertificateName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_iothub_certificate", id.ID())
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_iothub_certificate", id.ID())
}

certificate := devices.CertificateDescription{
Properties: &devices.CertificateProperties{
IsVerified: utils.Bool(d.Get("is_verified").(bool)),
Expand All @@ -103,7 +101,7 @@ func resourceIotHubCertificateCreateUpdate(d *pluginsdk.ResourceData, meta inter
}

if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.IotHubName, id.CertificateName, certificate, ""); err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
return fmt.Errorf("creating %s: %+v", id, err)
}

d.SetId(id.ID())
Expand Down Expand Up @@ -142,6 +140,39 @@ func resourceIotHubCertificateRead(d *pluginsdk.ResourceData, meta interface{})
return nil
}

func resourceIotHubCertificateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).IoTHub.IotHubCertificateClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewIotHubCertificateID(subscriptionId, d.Get("resource_group_name").(string), d.Get("iothub_name").(string), d.Get("name").(string))

existing, err := client.Get(ctx, id.ResourceGroup, id.IotHubName, id.CertificateName)
if err != nil {
return fmt.Errorf("reading %s: %v", id, err)
}

etag := ""
if existing.Etag != nil {
etag = *existing.Etag
}

if d.HasChange("is_verified") {
existing.Properties.IsVerified = utils.Bool(d.Get("is_verified").(bool))
}

if d.HasChange("certificate_content") {
existing.Properties.Certificate = utils.String(d.Get("certificate_content").(string))
}

if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.IotHubName, id.CertificateName, existing, etag); err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}

return resourceIotHubCertificateRead(d, meta)
}

func resourceIotHubCertificateDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).IoTHub.IotHubCertificateClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ resource "azurerm_iothub_certificate" "test" {
iothub_name = azurerm_iothub.test.name
is_verified = true

certificate_content = filebase64("testdata/application_gateway_test.cer")
certificate_content = filebase64("testdata/iothub_test.cer")
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}
59 changes: 45 additions & 14 deletions internal/services/iothub/iothub_dps_certificate_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (

func resourceIotHubDPSCertificate() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceIotHubDPSCertificateCreateUpdate,
Create: resourceIotHubDPSCertificateCreate,
Read: resourceIotHubDPSCertificateRead,
Update: resourceIotHubDPSCertificateCreateUpdate,
Update: resourceIotHubDPSCertificateUpdate,
Delete: resourceIotHubDPSCertificateDelete,

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
Expand Down Expand Up @@ -69,27 +69,25 @@ func resourceIotHubDPSCertificate() *pluginsdk.Resource {
}
}

func resourceIotHubDPSCertificateCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
func resourceIotHubDPSCertificateCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).IoTHub.DPSCertificateClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id := dpscertificate.NewCertificateID(subscriptionId, d.Get("resource_group_name").(string), d.Get("iot_dps_name").(string), d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.Get(ctx, id, dpscertificate.GetOperationOptions{IfMatch: utils.String("")})
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing IoT Device Provisioning Service Certificate %s: %+v", id.String(), err)
}
}

existing, err := client.Get(ctx, id, dpscertificate.GetOperationOptions{IfMatch: utils.String("")})
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_iothub_dps_certificate", id.ID())
return fmt.Errorf("checking for presence of existing IoT Device Provisioning Service Certificate %s: %+v", id.String(), err)
}
}

if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_iothub_dps_certificate", id.ID())
}

certificate := dpscertificate.CertificateResponse{
Properties: &dpscertificate.CertificateProperties{
Certificate: utils.String(d.Get("certificate_content").(string)),
Expand All @@ -100,7 +98,7 @@ func resourceIotHubDPSCertificateCreateUpdate(d *pluginsdk.ResourceData, meta in
}

if _, err := client.CreateOrUpdate(ctx, id, certificate, dpscertificate.CreateOrUpdateOperationOptions{IfMatch: utils.String("")}); err != nil {
return fmt.Errorf("creating/updating IoT Device Provisioning Service Certificate %s: %+v", id.String(), err)
return fmt.Errorf("creating %s: %+v", id, err)
}

d.SetId(id.ID())
Expand All @@ -124,7 +122,7 @@ func resourceIotHubDPSCertificateRead(d *pluginsdk.ResourceData, meta interface{
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %s: %+v", id.String(), err)
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.CertificateName)
Expand All @@ -144,6 +142,39 @@ func resourceIotHubDPSCertificateRead(d *pluginsdk.ResourceData, meta interface{
return nil
}

func resourceIotHubDPSCertificateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).IoTHub.DPSCertificateClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id := dpscertificate.NewCertificateID(subscriptionId, d.Get("resource_group_name").(string), d.Get("iot_dps_name").(string), d.Get("name").(string))

existing, err := client.Get(ctx, id, dpscertificate.GetOperationOptions{IfMatch: utils.String("")})
if err != nil {
return fmt.Errorf("reading %s: %+v", id, err)
}

etag := ""
if existing.Model != nil && existing.Model.Etag != nil {
etag = *existing.Model.Etag
}

if d.HasChange("is_verified") {
existing.Model.Properties.IsVerified = utils.Bool(d.Get("is_verified").(bool))
}

if d.HasChange("certificate_content") {
existing.Model.Properties.Certificate = utils.String(d.Get("certificate_content").(string))
}

if _, err := client.CreateOrUpdate(ctx, id, *existing.Model, dpscertificate.CreateOrUpdateOperationOptions{IfMatch: utils.String(etag)}); err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}

return resourceIotHubDPSCertificateRead(d, meta)
}

func resourceIotHubDPSCertificateDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).IoTHub.DPSCertificateClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ resource "azurerm_iothub_dps_certificate" "test" {
resource_group_name = azurerm_resource_group.test.name
iot_dps_name = azurerm_iothub_dps.test.name

certificate_content = filebase64("testdata/application_gateway_test.cer")
certificate_content = filebase64("testdata/iothub_test.cer")
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}
Expand Down
17 changes: 17 additions & 0 deletions internal/services/iothub/testdata/iothub_test.cer
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-----BEGIN CERTIFICATE-----
MIICsjCCAZoCCQCMdt7DvygPtDANBgkqhkiG9w0BAQsFADAbMRkwFwYDVQQDDBBh
cGkudGVycmFmb3JtLmlvMB4XDTE4MDcwNTEwMzMzMFoXDTI4MDcwMjEwMzMzMFow
GzEZMBcGA1UEAwwQYXBpLnRlcnJhZm9ybS5pbzCCASIwDQYJKoZIhvcNAQEBBQAD
ggEPADCCAQoCggEBAKQW332Ol28CsidAheD1aL9Ul8JWnKLdaVxKZ3ssl5CXjPDO
mM7IXk0SgbQnUC8lIlPFZiDGbQ1sB6OTMun6ZZ4ipLp80dtl0roCLtCnDQOBGzCN
ArCYAoXRurjkXEY7tpD0wwtU72+37h3HQ4g0VS6VItJCqJ9QADV+HO2ZWuZTez70
MhoL6OLfZP7HGYdJDKgfEVNF5XlbVzNAGkDIJFdhjNxyGGu5Nfsm1pfQhAyunkk7
JVamjUg5IojRdo63IS9wwzMOdeGSAbBcsJfYeCfVg2kupR8q0TmZ+x93RmmOlbSi
66kEYxRzZ9YCQeHJmn1YfJ92BpCUiy9A6Z1iaKUCAwEAATANBgkqhkiG9w0BAQsF
AAOCAQEAJ7JhlecP7J48wI2QHTMbAMkkWBv/iWq1/QIF4ugH3Zb5PorOv+NfhQ0L
lWiw/SzN8Ae95vUixAGYHMSa28oumM5K1OsqKEkVIo1AoBH8nBz+VcTpRD/mHXot
AHPAZt9j5LqeHX+enR6RbINAf3jn+YU3MdVe0MsADdFASVDfjmQP2R7o9aJb/QqO
g3bZBWsiBDEISfyaH2+pgUM7wtwEoFWmEMlgjLK1MRBs1cDZXqnHaCd/rs+NmWV9
naEu7x5fyQOk4HozkpweR+Jx1sBlTRsa49/qSHt/6ULKfO01/cTs4iF71ykXPbh3
Kj9cI2uo9aYtXkxkhKrGyUpA7FJqWw==
-----END CERTIFICATE-----