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

switch maintenance sdk to use hashicorp/go-azure-sdk #17954

Merged
merged 11 commits into from
Aug 18, 2022
1 change: 0 additions & 1 deletion internal/services/compute/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ package compute
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=DataDisk -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Compute/virtualMachines/machine1/dataDisks/disk1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Snapshot -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Compute/snapshots/snapshot1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Plan -id=/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.MarketplaceOrdering/agreements/agreement1/offers/offer1/plans/hourly
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ProximityPlacementGroup -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Compute/proximityPlacementGroups/proximityPlacementGroup1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=HostGroup -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Compute/hostGroups/hostgroup1
16 changes: 9 additions & 7 deletions internal/services/maintenance/client/client.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/maintenance/mgmt/2021-05-01/maintenance"
"github.com/hashicorp/go-azure-sdk/resource-manager/maintenance/2021-05-01/configurationassignments"
"github.com/hashicorp/go-azure-sdk/resource-manager/maintenance/2021-05-01/maintenanceconfigurations"
"github.com/hashicorp/go-azure-sdk/resource-manager/maintenance/2021-05-01/publicmaintenanceconfigurations"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
ConfigurationsClient *maintenance.ConfigurationsClient
ConfigurationAssignmentsClient *maintenance.ConfigurationAssignmentsClient
PublicConfigurationsClient *maintenance.PublicMaintenanceConfigurationsClient
ConfigurationsClient *maintenanceconfigurations.MaintenanceConfigurationsClient
ConfigurationAssignmentsClient *configurationassignments.ConfigurationAssignmentsClient
PublicConfigurationsClient *publicmaintenanceconfigurations.PublicMaintenanceConfigurationsClient
}

func NewClient(o *common.ClientOptions) *Client {
configurationsClient := maintenance.NewConfigurationsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
configurationsClient := maintenanceconfigurations.NewMaintenanceConfigurationsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&configurationsClient.Client, o.ResourceManagerAuthorizer)

configurationAssignmentsClient := maintenance.NewConfigurationAssignmentsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
configurationAssignmentsClient := configurationassignments.NewConfigurationAssignmentsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&configurationAssignmentsClient.Client, o.ResourceManagerAuthorizer)

publicConfigurationsClient := maintenance.NewPublicMaintenanceConfigurationsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
publicConfigurationsClient := publicmaintenanceconfigurations.NewPublicMaintenanceConfigurationsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&publicConfigurationsClient.Client, o.ResourceManagerAuthorizer)

return &Client{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/maintenance/mgmt/2021-05-01/maintenance"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/dedicatedhosts"
"github.com/hashicorp/go-azure-sdk/resource-manager/maintenance/2021-05-01/configurationassignments"
"github.com/hashicorp/go-azure-sdk/resource-manager/maintenance/2021-05-01/maintenanceconfigurations"
"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/maintenance/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/maintenance/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/suppress"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand Down Expand Up @@ -41,10 +42,11 @@ func resourceArmMaintenanceAssignmentDedicatedHost() *pluginsdk.Resource {
"location": azure.SchemaLocation(),

"maintenance_configuration_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.MaintenanceConfigurationID,
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: maintenanceconfigurations.ValidateMaintenanceConfigurationID,
DiffSuppressFunc: suppress.CaseDifference, // TODO remove in 4.0
katbyte marked this conversation as resolved.
Show resolved Hide resolved
},

"dedicated_host_id": {
Expand All @@ -63,41 +65,48 @@ func resourceArmMaintenanceAssignmentDedicatedHostCreate(d *pluginsdk.ResourceDa
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

configurationId, err := maintenanceconfigurations.ParseMaintenanceConfigurationID(d.Get("maintenance_configuration_id").(string))
if err != nil {
return err
}

dedicatedHostIdRaw := d.Get("dedicated_host_id").(string)
dedicatedHostId, _ := dedicatedhosts.ParseHostID(dedicatedHostIdRaw)

existingList, err := getMaintenanceAssignmentDedicatedHost(ctx, client, *dedicatedHostId, dedicatedHostIdRaw)

if err != nil {
return err
}
if existingList != nil && len(*existingList) > 0 {
existing := (*existingList)[0]
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_maintenance_assignment_dedicated_host", *existing.ID)
if existing.Id != nil && *existing.Id != "" {
return tf.ImportAsExistsError("azurerm_maintenance_assignment_dedicated_host", configurationId.ID())
}
}

maintenanceConfigurationID := d.Get("maintenance_configuration_id").(string)
configurationId, _ := parse.MaintenanceConfigurationIDInsensitively(maintenanceConfigurationID)

// set assignment name to configuration name
assignmentName := configurationId.Name
configurationAssignment := maintenance.ConfigurationAssignment{
assignmentName := configurationId.ResourceName
configurationAssignment := configurationassignments.ConfigurationAssignment{
Name: utils.String(assignmentName),
Location: utils.String(location.Normalize(d.Get("location").(string))),
ConfigurationAssignmentProperties: &maintenance.ConfigurationAssignmentProperties{
MaintenanceConfigurationID: utils.String(maintenanceConfigurationID),
ResourceID: utils.String(dedicatedHostIdRaw),
Properties: &configurationassignments.ConfigurationAssignmentProperties{
MaintenanceConfigurationId: utils.String(configurationId.ID()),
ResourceId: utils.String(dedicatedHostId.ID()),
},
}

// It may take a few minutes after starting a VM for it to become available to assign to a configuration

id := configurationassignments.NewProviders2ConfigurationAssignmentID(dedicatedHostId.SubscriptionId, dedicatedHostId.ResourceGroupName, "Microsoft.Compute", "hostGroups", dedicatedHostId.HostGroupName, "hosts", dedicatedHostId.HostName, assignmentName)
err = pluginsdk.Retry(d.Timeout(pluginsdk.TimeoutCreate), func() *pluginsdk.RetryError {
if _, err := client.CreateOrUpdateParent(ctx, dedicatedHostId.ResourceGroupName, "Microsoft.Compute", "hostGroups", dedicatedHostId.HostGroupName, "hosts", dedicatedHostId.HostName, assignmentName, configurationAssignment); err != nil {

if _, err := client.CreateOrUpdateParent(ctx, id, configurationAssignment); err != nil {

if strings.Contains(err.Error(), "It may take a few minutes after starting a VM for it to become available to assign to a configuration") {
return pluginsdk.RetryableError(fmt.Errorf("expected VM is available to assign to a configuration but was in pending state, retrying"))
}
return pluginsdk.NonRetryableError(fmt.Errorf("issuing creating request for Maintenance Assignment (Dedicated Host ID %q): %+v", dedicatedHostIdRaw, err))
return pluginsdk.NonRetryableError(fmt.Errorf("issuing creating request for Maintenance Assignment (Dedicated Host ID %q): %+v", dedicatedHostId.ID(), err))
}

return nil
Expand All @@ -106,19 +115,8 @@ func resourceArmMaintenanceAssignmentDedicatedHostCreate(d *pluginsdk.ResourceDa
return err
}

resp, err := getMaintenanceAssignmentDedicatedHost(ctx, client, *dedicatedHostId, dedicatedHostIdRaw)
if err != nil {
return err
}
if resp == nil || len(*resp) == 0 {
return fmt.Errorf("could not find Maintenance assignment (virtual machine scale set ID: %q)", dedicatedHostIdRaw)
}
assignment := (*resp)[0]
if assignment.ID == nil || *assignment.ID == "" {
return fmt.Errorf("empty or nil ID of Maintenance Assignment (Dedicated Host ID %q)", dedicatedHostIdRaw)
}
d.SetId(id.ID())

d.SetId(*assignment.ID)
return resourceArmMaintenanceAssignmentDedicatedHostRead(d, meta)
}

Expand All @@ -141,14 +139,22 @@ func resourceArmMaintenanceAssignmentDedicatedHostRead(d *pluginsdk.ResourceData
return nil
}
assignment := (*resp)[0]
if assignment.ID == nil || *assignment.ID == "" {
if assignment.Id == nil || *assignment.Id == "" {
return fmt.Errorf("empty or nil ID of Maintenance Assignment (Dedicated Host ID: %q", id.DedicatedHostIdRaw)
}

d.Set("dedicated_host_id", id.DedicatedHostId.ID())

if props := assignment.ConfigurationAssignmentProperties; props != nil {
d.Set("maintenance_configuration_id", props.MaintenanceConfigurationID)
if props := assignment.Properties; props != nil {
maintenanceConfigurationId := ""
if props.MaintenanceConfigurationId != nil {
parsedId, err := maintenanceconfigurations.ParseMaintenanceConfigurationIDInsensitively(*props.MaintenanceConfigurationId)
if err != nil {
return fmt.Errorf("parsing %q: %+v", *props.MaintenanceConfigurationId, err)
}
maintenanceConfigurationId = parsedId.ID()
}
d.Set("maintenance_configuration_id", maintenanceConfigurationId)
}
return nil
}
Expand All @@ -163,20 +169,26 @@ func resourceArmMaintenanceAssignmentDedicatedHostDelete(d *pluginsdk.ResourceDa
return err
}

if _, err := client.DeleteParent(ctx, id.DedicatedHostId.ResourceGroupName, "Microsoft.Compute", "hostGroups", id.DedicatedHostId.HostGroupName, "hosts", id.DedicatedHostId.HostName, id.Name); err != nil {
providerConfigAssignmentId := configurationassignments.NewProviders2ConfigurationAssignmentID(id.DedicatedHostId.SubscriptionId, id.DedicatedHostId.ResourceGroupName, "Microsoft.Compute", "hostGroups", id.DedicatedHostId.HostGroupName, "hosts", id.DedicatedHostId.HostName, id.Name)

if _, err := client.DeleteParent(ctx, providerConfigAssignmentId); err != nil {
return fmt.Errorf("deleting Maintenance Assignment to resource %q: %+v", id.DedicatedHostIdRaw, err)
}

return nil
}

func getMaintenanceAssignmentDedicatedHost(ctx context.Context, client *maintenance.ConfigurationAssignmentsClient, id dedicatedhosts.HostId, dedicatedHostId string) (result *[]maintenance.ConfigurationAssignment, err error) {
resp, err := client.ListParent(ctx, id.ResourceGroupName, "Microsoft.Compute", "hostGroups", id.HostGroupName, "hosts", id.HostName)
func getMaintenanceAssignmentDedicatedHost(ctx context.Context, client *configurationassignments.ConfigurationAssignmentsClient, hostId dedicatedhosts.HostId, dedicatedHostId string) (result *[]configurationassignments.ConfigurationAssignment, err error) {

id := configurationassignments.NewResourceGroupProviderID(hostId.SubscriptionId, hostId.ResourceGroupName, "Microsoft.Compute", "hostGroups", hostId.HostGroupName, "hosts", hostId.HostName)

resp, err := client.ListParent(ctx, id)

if err != nil {
if !utils.ResponseWasNotFound(resp.Response) {
if !response.WasNotFound(resp.HttpResponse) {
err = fmt.Errorf("checking for presence of existing Maintenance assignment (Dedicated Host ID %q): %+v", dedicatedHostId, err)
return
return nil, err
}
}
return resp.Value, nil
return resp.Model.Value, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/maintenance/2021-05-01/configurationassignments"
"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"
Expand Down Expand Up @@ -51,12 +52,14 @@ func (MaintenanceAssignmentDedicatedHostResource) Exists(ctx context.Context, cl
return nil, err
}

resp, err := clients.Maintenance.ConfigurationAssignmentsClient.ListParent(ctx, id.DedicatedHostId.ResourceGroupName, "Microsoft.Compute", "hostGroups", id.DedicatedHostId.HostGroupName, "hosts", id.DedicatedHostId.HostName)
rgProviderId := configurationassignments.NewResourceGroupProviderID(id.DedicatedHostId.SubscriptionId, id.DedicatedHostId.ResourceGroupName, "Microsoft.Compute", "hostGroups", id.DedicatedHostId.HostGroupName, "hosts", id.DedicatedHostId.HostName)
resp, err := clients.Maintenance.ConfigurationAssignmentsClient.ListParent(ctx, rgProviderId)

if err != nil {
return nil, fmt.Errorf("retrieving Maintenance Assignment Dedicated Host (target resource id: %q): %v", id.DedicatedHostIdRaw, err)
}

return utils.Bool(resp.Value != nil && len(*resp.Value) != 0), nil
return utils.Bool(resp.Model != nil && resp.Model.Value != nil && len(*resp.Model.Value) != 0), nil
}

func (r MaintenanceAssignmentDedicatedHostResource) basic(data acceptance.TestData) string {
Expand Down Expand Up @@ -98,7 +101,7 @@ resource "azurerm_maintenance_configuration" "test" {
name = "acctest-MC%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
scope = "All"
scope = "Host"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens when All is specified?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We get the following error when All is specified:

Error: creating/updating Maintenance Configuration: (Name "acctest-MC220810035643036463" / Resource Group "acctestRG-maint-220810035643036463"): maintenance.ConfigurationsClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="" Message="Maintenance Scope provided is invalid"
with azurerm_maintenance_configuration.test,

Currently these tests are failing in main due to using All for scope:

TestAccMaintenanceAssignmentVirtualMachine_basic
TestAccMaintenanceConfiguration_basic
TestAccMaintenanceConfiguration_update
TestAccMaintenanceConfiguration_requiresImport
TestAccMaintenanceAssignmentDedicatedHost_requiresImport
TestAccMaintenanceAssignmentDedicatedHost_basic
TestAccMaintenanceAssignmentVirtualMachine_requiresImport

}

resource "azurerm_dedicated_host_group" "test" {
Expand Down
Loading