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

iothub - switch dps to go-azure-sdk #18299

Merged
merged 1 commit into from
Sep 8, 2022
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
11 changes: 6 additions & 5 deletions internal/services/iothub/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package client

import (
"github.com/Azure/azure-sdk-for-go/services/iothub/mgmt/2021-07-02/devices"
"github.com/Azure/azure-sdk-for-go/services/provisioningservices/mgmt/2021-10-15/iothub"
"github.com/hashicorp/go-azure-sdk/resource-manager/deviceprovisioningservices/2022-02-05/dpscertificate"
"github.com/hashicorp/go-azure-sdk/resource-manager/deviceprovisioningservices/2022-02-05/iotdpsresource"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
ResourceClient *devices.IotHubResourceClient
IotHubCertificateClient *devices.CertificatesClient
DPSResourceClient *iothub.IotDpsResourceClient
DPSCertificateClient *iothub.DpsCertificateClient
DPSResourceClient *iotdpsresource.IotDpsResourceClient
DPSCertificateClient *dpscertificate.DpsCertificateClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -20,10 +21,10 @@ func NewClient(o *common.ClientOptions) *Client {
IotHubCertificateClient := devices.NewCertificatesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&IotHubCertificateClient.Client, o.ResourceManagerAuthorizer)

DPSResourceClient := iothub.NewIotDpsResourceClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
DPSResourceClient := iotdpsresource.NewIotDpsResourceClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&DPSResourceClient.Client, o.ResourceManagerAuthorizer)

DPSCertificateClient := iothub.NewDpsCertificateClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
DPSCertificateClient := dpscertificate.NewDpsCertificateClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&DPSCertificateClient.Client, o.ResourceManagerAuthorizer)

return &Client{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func resourceIotHubConsumerGroup() *pluginsdk.Resource {

func resourceIotHubConsumerGroupCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).IoTHub.ResourceClient
subscriptionId := meta.(*clients.Client).IoTHub.DPSResourceClient.SubscriptionID
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()
log.Printf("[INFO] preparing arguments for AzureRM IoTHub Consumer Group creation.")
Expand Down
64 changes: 35 additions & 29 deletions internal/services/iothub/iothub_dps_certificate_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/provisioningservices/mgmt/2021-10-15/iothub"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/deviceprovisioningservices/2022-02-05/dpscertificate"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/iothub/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/iothub/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand All @@ -24,7 +24,7 @@ func resourceIotHubDPSCertificate() *pluginsdk.Resource {
Delete: resourceIotHubDPSCertificateDelete,

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.DpsCertificateID(id)
_, err := dpscertificate.ParseCertificateID(id)
return err
}),

Expand Down Expand Up @@ -71,34 +71,35 @@ func resourceIotHubDPSCertificate() *pluginsdk.Resource {

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

id := parse.NewDpsCertificateID(subscriptionId, d.Get("resource_group_name").(string), d.Get("iot_dps_name").(string), d.Get("name").(string))
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.CertificateName, id.ResourceGroup, id.ProvisioningServiceName, "")
existing, err := client.Get(ctx, id, dpscertificate.GetOperationOptions{IfMatch: utils.String("")})
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing IoT Device Provisioning Service Certificate %s: %+v", id.String(), err)
}
}

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

certificate := iothub.CertificateBodyDescription{
Certificate: utils.String(d.Get("certificate_content").(string)),
certificate := dpscertificate.CertificateResponse{
Properties: &dpscertificate.CertificateProperties{
Certificate: utils.String(d.Get("certificate_content").(string)),
},
}

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

if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.ProvisioningServiceName, id.CertificateName, certificate, ""); err != nil {
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)
}

Expand All @@ -112,30 +113,32 @@ func resourceIotHubDPSCertificateRead(d *pluginsdk.ResourceData, meta interface{
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.DpsCertificateID(d.Id())
id, err := dpscertificate.ParseCertificateID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.CertificateName, id.ResourceGroup, id.ProvisioningServiceName, "")
resp, err := client.Get(ctx, *id, dpscertificate.GetOperationOptions{IfMatch: utils.String("")})
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %s: %+v", id.String(), err)
}

d.Set("name", id.CertificateName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("resource_group_name", id.ResourceGroupName)
d.Set("iot_dps_name", id.ProvisioningServiceName)
// We are unable to set `certificate_content` since it is not returned from the API
if props := resp.Properties; props != nil {
isVerified := false
if props.IsVerified != nil {
isVerified = *props.IsVerified
if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
isVerified := false
if props.IsVerified != nil {
isVerified = *props.IsVerified
}
d.Set("is_verified", isVerified)
}
d.Set("is_verified", isVerified)
}

return nil
Expand All @@ -146,25 +149,28 @@ func resourceIotHubDPSCertificateDelete(d *pluginsdk.ResourceData, meta interfac
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.DpsCertificateID(d.Id())
id, err := dpscertificate.ParseCertificateID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.CertificateName, id.ResourceGroup, id.ProvisioningServiceName, "")
resp, err := client.Get(ctx, *id, dpscertificate.GetOperationOptions{IfMatch: utils.String("")})
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return nil
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}

if resp.Etag == nil {
return fmt.Errorf("deleting %s because Etag is nil", id)
if resp.Model != nil && resp.Model.Etag == nil {
return fmt.Errorf("deleting %s because Etag is nil", id)
}

// TODO address this delete call if https://github.com/Azure/azure-rest-api-specs/pull/6311 get's merged
if _, err := client.Delete(ctx, id.ResourceGroup, *resp.Etag, id.ProvisioningServiceName, id.CertificateName, "", nil, nil, iothub.CertificatePurposeServerAuthentication, nil, nil, nil, ""); err != nil {
deleteOptions := dpscertificate.DeleteOperationOptions{
IfMatch: resp.Model.Etag,
CertificateName: utils.String(id.CertificateName),
}
if _, err := client.Delete(ctx, *id, deleteOptions); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/deviceprovisioningservices/2022-02-05/dpscertificate"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/iothub/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -86,20 +86,17 @@ func TestAccIotHubDPSCertificate_isVerified(t *testing.T) {
}

func (t IotHubDPSCertificateResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.DpsCertificateID(state.ID)
id, err := dpscertificate.ParseCertificateID(state.ID)
if err != nil {
return nil, err
}
resourceGroup := id.ResourceGroup
iotDPSName := id.ProvisioningServiceName
name := id.CertificateName

resp, err := clients.IoTHub.DPSCertificateClient.Get(ctx, name, resourceGroup, iotDPSName, "")
resp, err := clients.IoTHub.DPSCertificateClient.Get(ctx, *id, dpscertificate.GetOperationOptions{IfMatch: utils.String("")})
if err != nil {
return nil, fmt.Errorf("reading IotHuB DPS Certificate (%s): %+v", id, err)
}

return utils.Bool(resp.ID != nil), nil
return utils.Bool(resp.Model != nil), nil
}

func (IotHubDPSCertificateResource) basic(data acceptance.TestData) string {
Expand Down
24 changes: 13 additions & 11 deletions internal/services/iothub/iothub_dps_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/iothub/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/iothub/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceIotHubDPS() *pluginsdk.Resource {
Expand Down Expand Up @@ -61,33 +61,35 @@ func dataSourceIotHubDPS() *pluginsdk.Resource {

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

id := parse.NewIotHubDpsID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
id := commonids.NewProvisioningServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, id.ProvisioningServiceName, id.ResourceGroup)
resp, err := client.Get(ctx, id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("Error: IoT Device Provisioning Service %s was not found", id)
}

return fmt.Errorf("retrieving IoT Device Provisioning Service %s: %+v", id, err)
}

d.Set("name", id.ProvisioningServiceName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("resource_group_name", id.ResourceGroupName)
d.SetId(id.ID())

d.Set("location", location.NormalizeNilable(resp.Location))
if model := resp.Model; model != nil {
d.Set("location", location.Normalize(model.Location))

if props := resp.Properties; props != nil {
props := model.Properties
d.Set("service_operations_host_name", props.ServiceOperationsHostName)
d.Set("device_provisioning_host_name", props.DeviceProvisioningHostName)
d.Set("id_scope", props.IDScope)
d.Set("id_scope", props.IdScope)
d.Set("allocation_policy", props.AllocationPolicy)
d.Set("tags", flattenTags(model.Tags))
}

return tags.FlattenAndSet(d, resp.Tags)
return nil
}
Loading