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

r\linux/windows_virtual_machine: add support for capacity_reservation_group_id #17236

Merged
merged 1 commit into from
Jul 6, 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
48 changes: 48 additions & 0 deletions internal/services/compute/linux_virtual_machine_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,27 @@ func resourceLinuxVirtualMachine() *pluginsdk.Resource {
// TODO: raise a GH issue for the broken API
// availability_set_id: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/acctestRG-200122113424880096/providers/Microsoft.Compute/availabilitySets/ACCTESTAVSET-200122113424880096" => "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/acctestRG-200122113424880096/providers/Microsoft.Compute/availabilitySets/acctestavset-200122113424880096" (forces new resource)
ConflictsWith: []string{
"capacity_reservation_group_id",
"virtual_machine_scale_set_id",
"zone",
},
},

"boot_diagnostics": bootDiagnosticsSchema(),

"capacity_reservation_group_id": {
Type: pluginsdk.TypeString,
Optional: true,
// the Compute/VM API is broken and returns the Resource Group name in UPPERCASE
// tracked by https://github.com/Azure/azure-rest-api-specs/issues/19424
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: computeValidate.CapacityReservationGroupID,
ConflictsWith: []string{
"availability_set_id",
"proximity_placement_group_id",
},
},

"computer_name": {
Type: pluginsdk.TypeString,
Optional: true,
Expand Down Expand Up @@ -246,6 +260,9 @@ func resourceLinuxVirtualMachine() *pluginsdk.Resource {
ValidateFunc: computeValidate.ProximityPlacementGroupID,
// the Compute/VM API is broken and returns the Resource Group name in UPPERCASE :shrug:
DiffSuppressFunc: suppress.CaseDifference,
ConflictsWith: []string{
"capacity_reservation_group_id",
},
},

"secret": linuxSecretSchema(),
Expand Down Expand Up @@ -543,6 +560,14 @@ func resourceLinuxVirtualMachineCreate(d *pluginsdk.ResourceData, meta interface
}
}

if v, ok := d.GetOk("capacity_reservation_group_id"); ok {
params.CapacityReservation = &compute.CapacityReservationProfile{
CapacityReservationGroup: &compute.SubResource{
ID: utils.String(v.(string)),
},
}
}

if v, ok := d.GetOk("custom_data"); ok {
params.OsProfile.CustomData = utils.String(v.(string))
}
Expand Down Expand Up @@ -687,6 +712,12 @@ func resourceLinuxVirtualMachineRead(d *pluginsdk.ResourceData, meta interface{}
}
d.Set("availability_set_id", availabilitySetId)

capacityReservationGroupId := ""
if props.CapacityReservation != nil && props.CapacityReservation.CapacityReservationGroup != nil && props.CapacityReservation.CapacityReservationGroup.ID != nil {
capacityReservationGroupId = *props.CapacityReservation.CapacityReservationGroup.ID
}
d.Set("capacity_reservation_group_id", capacityReservationGroupId)

licenseType := ""
if props.LicenseType != nil {
licenseType = *props.LicenseType
Expand Down Expand Up @@ -944,6 +975,23 @@ func resourceLinuxVirtualMachineUpdate(d *pluginsdk.ResourceData, meta interface
}
}

if d.HasChange("capacity_reservation_group_id") {
shouldUpdate = true
shouldDeallocate = true

if v, ok := d.GetOk("capacity_reservation_group_id"); ok {
update.CapacityReservation = &compute.CapacityReservationProfile{
CapacityReservationGroup: &compute.SubResource{
ID: utils.String(v.(string)),
},
}
} else {
update.CapacityReservation = &compute.CapacityReservationProfile{
CapacityReservationGroup: &compute.SubResource{},
}
}
}

if d.HasChange("dedicated_host_id") {
shouldUpdate = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,57 @@ func TestAccLinuxVirtualMachine_scalingAvailabilitySet(t *testing.T) {
})
}

func TestAccLinuxVirtualMachine_scalingCapacityReservationGroup(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_linux_virtual_machine", "test")
r := LinuxVirtualMachineResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.scalingCapacityReservationGroup(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccLinuxVirtualMachine_scalingCapacityReservationGroupUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_linux_virtual_machine", "test")
r := LinuxVirtualMachineResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.scalingCapacityReservationGroupInitial(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.scalingCapacityReservationGroup(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.scalingCapacityReservationGroupUpdate(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.scalingCapacityReservationGroupRemoved(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccLinuxVirtualMachine_scalingDedicatedHost(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_linux_virtual_machine", "test")
r := LinuxVirtualMachineResource{}
Expand Down Expand Up @@ -325,6 +376,232 @@ resource "azurerm_linux_virtual_machine" "test" {
`, r.template(data), data.RandomInteger, data.RandomInteger)
}

func (r LinuxVirtualMachineResource) scalingCapacityReservationGroupInitial(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_capacity_reservation_group" "test" {
name = "acctest-ccrg-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
}

resource "azurerm_capacity_reservation" "test" {
name = "acctest-ccr-%[2]d"
capacity_reservation_group_id = azurerm_capacity_reservation_group.test.id
sku {
name = "Standard_F2"
capacity = 1
}
}

resource "azurerm_linux_virtual_machine" "test" {
name = "acctestVM-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
size = "Standard_F2"
admin_username = "adminuser"
network_interface_ids = [
azurerm_network_interface.test.id,
]

admin_ssh_key {
username = "adminuser"
public_key = local.first_public_key
}

os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
}
`, r.template(data), data.RandomInteger)
}

func (r LinuxVirtualMachineResource) scalingCapacityReservationGroup(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_capacity_reservation_group" "test" {
name = "acctest-ccrg-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
}

resource "azurerm_capacity_reservation" "test" {
name = "acctest-ccr-%[2]d"
capacity_reservation_group_id = azurerm_capacity_reservation_group.test.id
sku {
name = "Standard_F2"
capacity = 1
}
}

resource "azurerm_linux_virtual_machine" "test" {
name = "acctestVM-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
size = "Standard_F2"
admin_username = "adminuser"

capacity_reservation_group_id = azurerm_capacity_reservation_group.test.id

network_interface_ids = [
azurerm_network_interface.test.id,
]

admin_ssh_key {
username = "adminuser"
public_key = local.first_public_key
}

os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}

depends_on = [
azurerm_capacity_reservation.test,
]
}
`, r.template(data), data.RandomInteger)
}

func (r LinuxVirtualMachineResource) scalingCapacityReservationGroupUpdate(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_capacity_reservation_group" "test" {
name = "acctest-ccrg-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
}

resource "azurerm_capacity_reservation" "test" {
name = "acctest-ccr-%[2]d"
capacity_reservation_group_id = azurerm_capacity_reservation_group.test.id
sku {
name = "Standard_F2"
capacity = 1
}
}

resource "azurerm_capacity_reservation_group" "test2" {
name = "acctest-ccrg2-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
}

resource "azurerm_capacity_reservation" "test2" {
name = "acctest-ccr-%[2]d"
capacity_reservation_group_id = azurerm_capacity_reservation_group.test2.id
sku {
name = "Standard_F2"
capacity = 1
}
}

resource "azurerm_linux_virtual_machine" "test" {
name = "acctestVM-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
size = "Standard_F2"
admin_username = "adminuser"

capacity_reservation_group_id = azurerm_capacity_reservation_group.test2.id

network_interface_ids = [
azurerm_network_interface.test.id,
]

admin_ssh_key {
username = "adminuser"
public_key = local.first_public_key
}

os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}

depends_on = [
azurerm_capacity_reservation.test2,
]
}
`, r.template(data), data.RandomInteger)
}

func (r LinuxVirtualMachineResource) scalingCapacityReservationGroupRemoved(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_capacity_reservation_group" "test2" {
name = "acctest-ccrg2-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
}

resource "azurerm_capacity_reservation" "test2" {
name = "acctest-ccr-%[2]d"
capacity_reservation_group_id = azurerm_capacity_reservation_group.test2.id
sku {
name = "Standard_F2"
capacity = 1
}
}

resource "azurerm_linux_virtual_machine" "test" {
name = "acctestVM-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
size = "Standard_F2"
admin_username = "adminuser"

network_interface_ids = [
azurerm_network_interface.test.id,
]

admin_ssh_key {
username = "adminuser"
public_key = local.first_public_key
}

os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
}
`, r.template(data), data.RandomInteger)
}

func (r LinuxVirtualMachineResource) scalingDedicatedHostInitial(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
Expand Down
Loading