Skip to content

Commit

Permalink
azurerm_maintenance_assignment_virtual_machine - maintenance config…
Browse files Browse the repository at this point in the history
…uration is now obtained by name rather than using the first in the list (#20766)
  • Loading branch information
neil-yechenwei authored Mar 7, 2023
1 parent 005544d commit e87d66f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,16 @@ func resourceArmMaintenanceAssignmentVirtualMachineCreate(d *pluginsdk.ResourceD
return err
}
if existingList != nil && len(*existingList) > 0 {
existing := (*existingList)[0]
if existing.Id != nil && *existing.Id != "" {
isExist := false
for _, existing := range *existingList {
// Due to https://github.com/Azure/azure-rest-api-specs/issues/22894, API always returns the lowercase for Maintenance Assignment. So here it has to ignore case sensitivity. Once this issue is fixed, here will be updated to respect case sensitivity
if existing.Name != nil && strings.EqualFold(*existing.Name, configurationId.MaintenanceConfigurationName) {
isExist = true
break
}
}

if isExist {
return tf.ImportAsExistsError("azurerm_maintenance_assignment_virtual_machine", configurationId.ID())
}
}
Expand Down Expand Up @@ -137,7 +145,16 @@ func resourceArmMaintenanceAssignmentVirtualMachineRead(d *pluginsdk.ResourceDat
d.SetId("")
return nil
}
assignment := (*resp)[0]

var assignment configurationassignments.ConfigurationAssignment
for _, v := range *resp {
// Due to https://github.com/Azure/azure-rest-api-specs/issues/22894, API always returns the lowercase for Maintenance Assignment. So here it has to ignore case sensitivity. Once this issue is fixed, here will be updated to respect case sensitivity
if v.Name != nil && strings.EqualFold(*v.Name, id.Name) {
assignment = v
break
}
}

if assignment.Id == nil || *assignment.Id == "" {
return fmt.Errorf("empty or nil ID of Maintenance Assignment (virtual machine ID id: %q", id.VirtualMachineIdRaw)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ func TestAccMaintenanceAssignmentVirtualMachine_requiresImport(t *testing.T) {
})
}

func TestAccMaintenanceAssignmentVirtualMachine_linkMultipleMaintenanceAssignmentsToOneVM(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_maintenance_assignment_virtual_machine", "test")
r := MaintenanceAssignmentVirtualMachineResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.linkMultipleMaintenanceAssignmentsToOneVM(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
// location not returned by list rest api
data.ImportStep("location"),
})
}

func (MaintenanceAssignmentVirtualMachineResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
maintenanceAssignmentVirtualMachineId, err := parse.MaintenanceAssignmentVirtualMachineID(state.ID)
if err != nil {
Expand Down Expand Up @@ -159,3 +175,28 @@ resource "azurerm_linux_virtual_machine" "test" {
}
`, data.RandomInteger, data.Locations.Primary)
}

func (r MaintenanceAssignmentVirtualMachineResource) linkMultipleMaintenanceAssignmentsToOneVM(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_maintenance_configuration" "test2" {
name = "acctest-MC2%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
scope = "SQLDB"
}
resource "azurerm_maintenance_assignment_virtual_machine" "test" {
location = azurerm_resource_group.test.location
maintenance_configuration_id = azurerm_maintenance_configuration.test.id
virtual_machine_id = azurerm_linux_virtual_machine.test.id
}
resource "azurerm_maintenance_assignment_virtual_machine" "test2" {
location = azurerm_resource_group.test.location
maintenance_configuration_id = azurerm_maintenance_configuration.test2.id
virtual_machine_id = azurerm_linux_virtual_machine.test.id
}
`, r.template(data), data.RandomInteger)
}

0 comments on commit e87d66f

Please sign in to comment.