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

azurerm_maintenance_assignment_virtual_machine - maintenance configuration is now obtained by name rather than using the first in the list #20766

Merged
merged 6 commits into from
Mar 7, 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
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)
}