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

hardwaresecuritymodules: refactoring to use hashicorp/go-azure-sdk #17470

Merged
merged 4 commits into from
Jul 3, 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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ require (
github.com/gofrs/uuid v4.0.0+incompatible
github.com/google/go-cmp v0.5.8
github.com/google/uuid v1.1.2
github.com/hashicorp/go-azure-helpers v0.34.0
github.com/hashicorp/go-azure-sdk v0.20220628.1190740
github.com/hashicorp/go-azure-helpers v0.35.0
github.com/hashicorp/go-azure-sdk v0.20220701.1073833
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-uuid v1.0.2
github.com/hashicorp/go-version v1.4.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-azure-helpers v0.12.0/go.mod h1:Zc3v4DNeX6PDdy7NljlYpnrdac1++qNW0I4U+ofGwpg=
github.com/hashicorp/go-azure-helpers v0.34.0 h1:Lvk3IgwFT51WVu1IK5rpUMd+rKnRylg9vMTt6Qx6xn4=
github.com/hashicorp/go-azure-helpers v0.34.0/go.mod h1:gcutZ/Hf/O7YN9M3UIvyZ9l0Rxv7Yrc9x5sSfM9cuSw=
github.com/hashicorp/go-azure-sdk v0.20220628.1190740 h1:jI/MGHxnXhx8hFyI+gIHIvmDntQxVphjHMynZP7GTJ8=
github.com/hashicorp/go-azure-sdk v0.20220628.1190740/go.mod h1:jPd83zRme0J5C+rUQaU6bzrWg/AS5WN1TiilFzRoKU0=
github.com/hashicorp/go-azure-helpers v0.35.0 h1:/Jpm37dzTmSHobt9SuC8bK6/jSoWw5FdxB9WIqQFXbI=
github.com/hashicorp/go-azure-helpers v0.35.0/go.mod h1:gcutZ/Hf/O7YN9M3UIvyZ9l0Rxv7Yrc9x5sSfM9cuSw=
github.com/hashicorp/go-azure-sdk v0.20220701.1073833 h1:cwutAQhHojIpJR0a7gyf4884A4KX3Un1lj9SUZDRdxE=
github.com/hashicorp/go-azure-sdk v0.20220701.1073833/go.mod h1:yjQPw8DCOtQR8E8+FNaTxF6yz+tyQGkDNiVAGCNoLOo=
github.com/hashicorp/go-checkpoint v0.5.0 h1:MFYpPZCnQqQTE18jFwSII6eUQrD/oxMFp3mlgcqk5mU=
github.com/hashicorp/go-checkpoint v0.5.0/go.mod h1:7nfLNL10NsxqO4iWuW6tWW0HjZuDrwkBuEQsVcpCOgg=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
Expand Down
6 changes: 3 additions & 3 deletions internal/services/hsm/client/client.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/preview/hardwaresecuritymodules/mgmt/2018-10-31-preview/hardwaresecuritymodules"
"github.com/hashicorp/go-azure-sdk/resource-manager/hardwaresecuritymodules/2021-11-30/dedicatedhsms"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
DedicatedHsmClient *hardwaresecuritymodules.DedicatedHsmClient
DedicatedHsmClient *dedicatedhsms.DedicatedHsmsClient
}

func NewClient(o *common.ClientOptions) *Client {
dedicatedHsmClient := hardwaresecuritymodules.NewDedicatedHsmClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
dedicatedHsmClient := dedicatedhsms.NewDedicatedHsmsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&dedicatedHsmClient.Client, o.ResourceManagerAuthorizer)

return &Client{
Expand Down
142 changes: 66 additions & 76 deletions internal/services/hsm/dedicated_hardware_security_module_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ import (
"log"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/zones"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/Azure/azure-sdk-for-go/services/preview/hardwaresecuritymodules/mgmt/2018-10-31-preview/hardwaresecuritymodules"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-helpers/resourcemanager/zones"
"github.com/hashicorp/go-azure-sdk/resource-manager/hardwaresecuritymodules/2021-11-30/dedicatedhsms"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
azValidate "github.com/hashicorp/terraform-provider-azurerm/helpers/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/hsm/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/hsm/validate"
networkValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/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/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand All @@ -40,7 +37,7 @@ func resourceDedicatedHardwareSecurityModule() *pluginsdk.Resource {
},

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.DedicatedHardwareSecurityModuleID(id)
_, err := dedicatedhsms.ParseDedicatedHSMID(id)
return err
}),

Expand All @@ -61,7 +58,7 @@ func resourceDedicatedHardwareSecurityModule() *pluginsdk.Resource {
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(hardwaresecuritymodules.SafeNetLunaNetworkHSMA790),
string(dedicatedhsms.SkuNameSafeNetLunaNetworkHSMASevenNineZero),
}, false),
},

Expand Down Expand Up @@ -103,7 +100,7 @@ func resourceDedicatedHardwareSecurityModule() *pluginsdk.Resource {

"zones": commonschema.ZonesMultipleOptionalForceNew(),

"tags": tags.Schema(),
"tags": commonschema.Tags(),
},
}
}
Expand All @@ -114,48 +111,44 @@ func resourceDedicatedHardwareSecurityModuleCreate(d *pluginsdk.ResourceData, me
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewDedicatedHardwareSecurityModuleID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
existing, err := client.Get(ctx, id.ResourceGroup, id.DedicatedHSMName)
id := dedicatedhsms.NewDedicatedHSMID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
existing, err := client.DedicatedHsmGet(ctx, id)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_dedicated_hardware_security_module", id.ID())
}

parameters := hardwaresecuritymodules.DedicatedHsm{
Location: utils.String(location.Normalize(d.Get("location").(string))),
DedicatedHsmProperties: &hardwaresecuritymodules.DedicatedHsmProperties{
skuName := dedicatedhsms.SkuName(d.Get("sku_name").(string))
parameters := dedicatedhsms.DedicatedHsm{
Location: location.Normalize(d.Get("location").(string)),
Properties: dedicatedhsms.DedicatedHsmProperties{
NetworkProfile: expandDedicatedHsmNetworkProfile(d.Get("network_profile").([]interface{})),
},
Sku: &hardwaresecuritymodules.Sku{
Name: hardwaresecuritymodules.Name(d.Get("sku_name").(string)),
Sku: dedicatedhsms.Sku{
Name: &skuName,
},
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if v, ok := d.GetOk("stamp_id"); ok {
parameters.DedicatedHsmProperties.StampID = utils.String(v.(string))
parameters.Properties.StampId = utils.String(v.(string))
}

if v, ok := d.GetOk("zones"); ok {
zones := zones.Expand(v.(*schema.Set).List())
zones := zones.Expand(v.(*pluginsdk.Set).List())
if len(zones) > 0 {
parameters.Zones = &zones
}
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.DedicatedHSMName, parameters)
if err != nil {
if err := client.DedicatedHsmCreateOrUpdateThenPoll(ctx, id, parameters); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation of %s: %+v", id, err)
}

d.SetId(id.ID())
return resourceDedicatedHardwareSecurityModuleRead(d, meta)
}
Expand All @@ -165,63 +158,67 @@ func resourceDedicatedHardwareSecurityModuleRead(d *pluginsdk.ResourceData, meta
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.DedicatedHardwareSecurityModuleID(d.Id())
id, err := dedicatedhsms.ParseDedicatedHSMID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.DedicatedHSMName)
resp, err := client.DedicatedHsmGet(ctx, *id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Dedicated Hardware Security Module %q does not exist - removing from state", d.Id())
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[INFO] %s was not found - removing from state", *id)
d.SetId("")
return nil
}

return fmt.Errorf("retrieving Dedicate Hardware Security Module %q (Resource Group %q): %+v", id.DedicatedHSMName, id.ResourceGroup, err)
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.DedicatedHSMName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("location", location.NormalizeNilable(resp.Location))
d.Set("zones", zones.Flatten(resp.Zones))
d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroupName)

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

props := model.Properties

if props := resp.DedicatedHsmProperties; props != nil {
if err := d.Set("network_profile", flattenDedicatedHsmNetworkProfile(props.NetworkProfile)); err != nil {
return fmt.Errorf("setting network_profile: %+v", err)
}
d.Set("stamp_id", props.StampID)
}
d.Set("stamp_id", props.StampId)

skuName := ""
if model.Sku.Name != nil {
skuName = string(*model.Sku.Name)
}
d.Set("sku_name", skuName)

if sku := resp.Sku; sku != nil {
d.Set("sku_name", sku.Name)
if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return err
}
}

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

func resourceDedicatedHardwareSecurityModuleUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).HSM.DedicatedHsmClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.DedicatedHardwareSecurityModuleID(d.Id())
id, err := dedicatedhsms.ParseDedicatedHSMID(d.Id())
if err != nil {
return err
}

parameters := hardwaresecuritymodules.DedicatedHsmPatchParameters{}
parameters := dedicatedhsms.DedicatedHsmPatchParameters{}
if d.HasChange("tags") {
parameters.Tags = tags.Expand(d.Get("tags").(map[string]interface{}))
}

future, err := client.Update(ctx, id.ResourceGroup, id.DedicatedHSMName, parameters)
if err != nil {
return fmt.Errorf("updating Dedicate Hardware Security Module %q (Resource Group %q): %+v", id.DedicatedHSMName, id.ResourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting on updating future for Dedicate Hardware Security Module %q (Resource Group %q): %+v", id.DedicatedHSMName, id.ResourceGroup, err)
if err := client.DedicatedHsmUpdateThenPoll(ctx, *id, parameters); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}

return resourceDedicatedHardwareSecurityModuleRead(d, meta)
Expand All @@ -232,64 +229,57 @@ func resourceDedicatedHardwareSecurityModuleDelete(d *pluginsdk.ResourceData, me
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.DedicatedHardwareSecurityModuleID(d.Id())
id, err := dedicatedhsms.ParseDedicatedHSMID(d.Id())
if err != nil {
return err
}

future, err := client.Delete(ctx, id.ResourceGroup, id.DedicatedHSMName)
if err != nil {
return fmt.Errorf("deleting Dedicated Hardware Security Module %q (Resource Group %q): %+v", id.DedicatedHSMName, id.ResourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting on deleting future for Dedicated Hardware Security Module %q (Resource Group %q): %+v", id.DedicatedHSMName, id.ResourceGroup, err)
if err := client.DedicatedHsmDeleteThenPoll(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}

return nil
}

func expandDedicatedHsmNetworkProfile(input []interface{}) *hardwaresecuritymodules.NetworkProfile {
func expandDedicatedHsmNetworkProfile(input []interface{}) *dedicatedhsms.NetworkProfile {
if len(input) == 0 {
return nil
}

v := input[0].(map[string]interface{})

result := hardwaresecuritymodules.NetworkProfile{
Subnet: &hardwaresecuritymodules.APIEntityReference{
ID: utils.String(v["subnet_id"].(string)),
result := dedicatedhsms.NetworkProfile{
Subnet: &dedicatedhsms.ApiEntityReference{
Id: utils.String(v["subnet_id"].(string)),
},
NetworkInterfaces: expandDedicatedHsmNetworkInterfacePrivateIPAddresses(v["network_interface_private_ip_addresses"].(*pluginsdk.Set).List()),
}

return &result
}

func expandDedicatedHsmNetworkInterfacePrivateIPAddresses(input []interface{}) *[]hardwaresecuritymodules.NetworkInterface {
results := make([]hardwaresecuritymodules.NetworkInterface, 0)
func expandDedicatedHsmNetworkInterfacePrivateIPAddresses(input []interface{}) *[]dedicatedhsms.NetworkInterface {
results := make([]dedicatedhsms.NetworkInterface, 0)

for _, item := range input {
if item != nil {
result := hardwaresecuritymodules.NetworkInterface{
PrivateIPAddress: utils.String(item.(string)),
}

results = append(results, result)
results = append(results, dedicatedhsms.NetworkInterface{
PrivateIpAddress: utils.String(item.(string)),
})
}
}

return &results
}

func flattenDedicatedHsmNetworkProfile(input *hardwaresecuritymodules.NetworkProfile) []interface{} {
func flattenDedicatedHsmNetworkProfile(input *dedicatedhsms.NetworkProfile) []interface{} {
if input == nil {
return make([]interface{}, 0)
}

var subnetId string
if input.Subnet != nil && input.Subnet.ID != nil {
subnetId = *input.Subnet.ID
if input.Subnet != nil && input.Subnet.Id != nil {
subnetId = *input.Subnet.Id
}

return []interface{}{
Expand All @@ -300,15 +290,15 @@ func flattenDedicatedHsmNetworkProfile(input *hardwaresecuritymodules.NetworkPro
}
}

func flattenDedicatedHsmNetworkInterfacePrivateIPAddresses(input *[]hardwaresecuritymodules.NetworkInterface) []interface{} {
func flattenDedicatedHsmNetworkInterfacePrivateIPAddresses(input *[]dedicatedhsms.NetworkInterface) []interface{} {
results := make([]interface{}, 0)
if input == nil {
return results
}

for _, item := range *input {
if item.PrivateIPAddress != nil {
results = append(results, *item.PrivateIPAddress)
if item.PrivateIpAddress != nil {
results = append(results, *item.PrivateIpAddress)
}
}

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/hardwaresecuritymodules/2021-11-30/dedicatedhsms"
"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/hsm/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -102,17 +102,17 @@ resource "azurerm_dedicated_hardware_security_module" "import" {
}

func (DedicatedHardwareSecurityModuleResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.DedicatedHardwareSecurityModuleID(state.ID)
id, err := dedicatedhsms.ParseDedicatedHSMID(state.ID)
if err != nil {
return nil, err
}

resp, err := clients.HSM.DedicatedHsmClient.Get(ctx, id.ResourceGroup, id.DedicatedHSMName)
resp, err := clients.HSM.DedicatedHsmClient.DedicatedHsmGet(ctx, *id)
if err != nil {
return nil, fmt.Errorf("retrieving Dedicated HardwareSecurityModule %q (resource group: %q): %+v", id.DedicatedHSMName, id.ResourceGroup, err)
return nil, fmt.Errorf("retrieving %s: %+v", *id, err)
}

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

func (DedicatedHardwareSecurityModuleResource) template(data acceptance.TestData) string {
Expand Down
Loading