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_proximity_placement_group - support allowed_vm_sizes and zone #19675

Merged
merged 3 commits into from
Jan 3, 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
63 changes: 61 additions & 2 deletions internal/services/compute/proximity_placement_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"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/compute/2022-03-01/proximityplacementgroups"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-01/virtualmachines"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"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"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceProximityPlacementGroup() *pluginsdk.Resource {
Expand Down Expand Up @@ -47,6 +51,24 @@ func resourceProximityPlacementGroup() *pluginsdk.Resource {

"location": commonschema.Location(),

"allowed_vm_sizes": {
Type: pluginsdk.TypeSet,
Optional: true,
MinItems: 1,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringInSlice(virtualmachines.PossibleValuesForVirtualMachineSizeTypes(), false),
},
},

"zone": {
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"allowed_vm_sizes"},
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"tags": commonschema.Tags(),
},
}
Expand Down Expand Up @@ -74,8 +96,28 @@ func resourceProximityPlacementGroupCreateUpdate(d *pluginsdk.ResourceData, meta
}

payload := proximityplacementgroups.ProximityPlacementGroup{
Location: location.Normalize(d.Get("location").(string)),
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
Location: location.Normalize(d.Get("location").(string)),
Properties: &proximityplacementgroups.ProximityPlacementGroupProperties{},
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if v, ok := d.GetOk("allowed_vm_sizes"); ok {
if payload.Properties.Intent == nil {
payload.Properties.Intent = &proximityplacementgroups.ProximityPlacementGroupPropertiesIntent{}
}
payload.Properties.Intent.VMSizes = utils.ExpandStringSlice(v.(*pluginsdk.Set).List())
} else if !d.IsNewResource() {
// Need to explicitly set an empty slice when updating to empty vm sizes
if payload.Properties.Intent == nil {
payload.Properties.Intent = &proximityplacementgroups.ProximityPlacementGroupPropertiesIntent{}
}
vmSizes := make([]string, 0)
payload.Properties.Intent.VMSizes = &vmSizes
}

if v, ok := d.GetOk("zone"); ok {
zones := zones.Expand([]string{v.(string)})
payload.Zones = &zones
}

if _, err := client.CreateOrUpdate(ctx, id, payload); err != nil {
Expand Down Expand Up @@ -111,6 +153,23 @@ func resourceProximityPlacementGroupRead(d *pluginsdk.ResourceData, meta interfa
d.Set("resource_group_name", id.ResourceGroupName)

d.Set("location", location.Normalize(model.Location))

intentVmSizes := make([]string, 0)
if props := model.Properties; props != nil {
if intent := props.Intent; intent != nil {
if intent.VMSizes != nil {
intentVmSizes = *intent.VMSizes
}
}
}
d.Set("allowed_vm_sizes", intentVmSizes)

zone := ""
if v := zones.Flatten(model.Zones); len(v) != 0 {
zone = v[0]
}
d.Set("zone", zone)

if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return err
}
Expand Down
115 changes: 115 additions & 0 deletions internal/services/compute/proximity_placement_group_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,57 @@ func TestAccProximityPlacementGroup_disappears(t *testing.T) {
})
}

func TestAccProximityPlacementGroup_allowedVmSizes(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_proximity_placement_group", "test")
r := ProximityPlacementGroupResource{}

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

func TestAccProximityPlacementGroup_zone(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_proximity_placement_group", "test")
r := ProximityPlacementGroupResource{}

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

func TestAccProximityPlacementGroup_withTags(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_proximity_placement_group", "test")
r := ProximityPlacementGroupResource{}
Expand Down Expand Up @@ -144,6 +195,70 @@ resource "azurerm_proximity_placement_group" "import" {
`, r.basic(data))
}

func (ProximityPlacementGroupResource) basicWithAllowedVmSizes(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_proximity_placement_group" "test" {
name = "acctestPPG-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name

allowed_vm_sizes = ["Standard_F1"]
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (ProximityPlacementGroupResource) basicWithAllowedVmSizesUpdated(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_proximity_placement_group" "test" {
name = "acctestPPG-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name

allowed_vm_sizes = ["Standard_F1", "Standard_F2"]
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (ProximityPlacementGroupResource) zone(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_proximity_placement_group" "test" {
name = "acctestPPG-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name

allowed_vm_sizes = ["Standard_F2"]
zone = "1"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (ProximityPlacementGroupResource) withTags(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
Loading