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

Add Proximity Placement Group Resources + Support #4020

Merged
merged 21 commits into from
Sep 5, 2019
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
54 changes: 54 additions & 0 deletions azurerm/data_source_proximity_placement_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package azurerm

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmProximityPlacementGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmProximityPlacementGroupRead,
Schema: map[string]*schema.Schema{
"name": {
tobydoescode marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"location": azure.SchemaLocationForDataSource(),

"tags": tags.SchemaDataSource(),
},
}
}

func dataSourceArmProximityPlacementGroupRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).compute.ProximityPlacementGroupsClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
tobydoescode marked this conversation as resolved.
Show resolved Hide resolved
resourceGroup := d.Get("resource_group_name").(string)

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Proximity Placement Group %q (Resource Group %q) was not found", name, resourceGroup)
}

return fmt.Errorf("Error making Read request on Proximity Placement Group %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)

if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
return tags.FlattenAndSet(d, resp.Tags)
}
43 changes: 43 additions & 0 deletions azurerm/data_source_proximity_placement_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package azurerm

import (
"fmt"
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccProximityPlacementGroupDataSource_basic(t *testing.T) {
dataSourceName := "data.azurerm_proximity_placement_group.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccProximityPlacementGroupDataSource_basic(ri, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "location"),
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"),
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "2"),
),
},
},
})
}

func testAccProximityPlacementGroupDataSource_basic(rInt int, location string) string {
return fmt.Sprintf(`
%s

data "azurerm_proximity_placement_group" "test" {
resource_group_name = "${azurerm_resource_group.test.name}"
name = "${azurerm_proximity_placement_group.test.name}"
}
`, testAccProximityPlacementGroup_withTags(rInt, location))
}
1 change: 1 addition & 0 deletions azurerm/helpers/azure/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func SchemaLocationOptional() *schema.Schema {
}
}

// todo should we change this to SchemaLocationComputed
func SchemaLocationForDataSource() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Expand Down
57 changes: 31 additions & 26 deletions azurerm/internal/services/compute/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import (
)

type Client struct {
AvailabilitySetsClient *compute.AvailabilitySetsClient
DisksClient *compute.DisksClient
GalleriesClient *compute.GalleriesClient
GalleryImagesClient *compute.GalleryImagesClient
GalleryImageVersionsClient *compute.GalleryImageVersionsClient
ImagesClient *compute.ImagesClient
SnapshotsClient *compute.SnapshotsClient
UsageClient *compute.UsageClient
VMExtensionImageClient *compute.VirtualMachineExtensionImagesClient
VMExtensionClient *compute.VirtualMachineExtensionsClient
VMScaleSetClient *compute.VirtualMachineScaleSetsClient
VMClient *compute.VirtualMachinesClient
VMImageClient *compute.VirtualMachineImagesClient
AvailabilitySetsClient *compute.AvailabilitySetsClient
DisksClient *compute.DisksClient
GalleriesClient *compute.GalleriesClient
GalleryImagesClient *compute.GalleryImagesClient
GalleryImageVersionsClient *compute.GalleryImageVersionsClient
ProximityPlacementGroupsClient *compute.ProximityPlacementGroupsClient
ImagesClient *compute.ImagesClient
SnapshotsClient *compute.SnapshotsClient
UsageClient *compute.UsageClient
VMExtensionImageClient *compute.VirtualMachineExtensionImagesClient
VMExtensionClient *compute.VirtualMachineExtensionsClient
VMScaleSetClient *compute.VirtualMachineScaleSetsClient
VMClient *compute.VirtualMachinesClient
VMImageClient *compute.VirtualMachineImagesClient
}

func BuildClient(o *common.ClientOptions) *Client {
Expand All @@ -41,6 +42,9 @@ func BuildClient(o *common.ClientOptions) *Client {
ImagesClient := compute.NewImagesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ImagesClient.Client, o.ResourceManagerAuthorizer)

ProximityPlacementGroupsClient := compute.NewProximityPlacementGroupsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ProximityPlacementGroupsClient.Client, o.ResourceManagerAuthorizer)

SnapshotsClient := compute.NewSnapshotsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&SnapshotsClient.Client, o.ResourceManagerAuthorizer)

Expand All @@ -63,18 +67,19 @@ func BuildClient(o *common.ClientOptions) *Client {
o.ConfigureClient(&VMClient.Client, o.ResourceManagerAuthorizer)

return &Client{
AvailabilitySetsClient: &AvailabilitySetsClient,
DisksClient: &DisksClient,
GalleriesClient: &GalleriesClient,
GalleryImagesClient: &GalleryImagesClient,
GalleryImageVersionsClient: &GalleryImageVersionsClient,
ImagesClient: &ImagesClient,
SnapshotsClient: &SnapshotsClient,
UsageClient: &UsageClient,
VMExtensionImageClient: &VMExtensionImageClient,
VMExtensionClient: &VMExtensionClient,
VMScaleSetClient: &VMScaleSetClient,
VMClient: &VMClient,
VMImageClient: &VMImageClient,
AvailabilitySetsClient: &AvailabilitySetsClient,
DisksClient: &DisksClient,
GalleriesClient: &GalleriesClient,
GalleryImagesClient: &GalleryImagesClient,
GalleryImageVersionsClient: &GalleryImageVersionsClient,
ImagesClient: &ImagesClient,
ProximityPlacementGroupsClient: &ProximityPlacementGroupsClient,
SnapshotsClient: &SnapshotsClient,
UsageClient: &UsageClient,
VMExtensionImageClient: &VMExtensionImageClient,
VMExtensionClient: &VMExtensionClient,
VMScaleSetClient: &VMScaleSetClient,
VMClient: &VMClient,
VMImageClient: &VMImageClient,
}
}
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_notification_hub": dataSourceNotificationHub(),
"azurerm_platform_image": dataSourceArmPlatformImage(),
"azurerm_policy_definition": dataSourceArmPolicyDefinition(),
"azurerm_proximity_placement_group": dataSourceArmProximityPlacementGroup(),
"azurerm_public_ip": dataSourceArmPublicIP(),
"azurerm_public_ips": dataSourceArmPublicIPs(),
"azurerm_recovery_services_vault": dataSourceArmRecoveryServicesVault(),
Expand Down Expand Up @@ -331,6 +332,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_private_dns_zone": resourceArmPrivateDnsZone(),
"azurerm_private_dns_a_record": resourceArmPrivateDnsARecord(),
"azurerm_private_dns_cname_record": resourceArmPrivateDnsCNameRecord(),
"azurerm_proximity_placement_group": resourceArmProximityPlacementGroup(),
"azurerm_public_ip": resourceArmPublicIp(),
"azurerm_public_ip_prefix": resourceArmPublicIpPrefix(),
"azurerm_recovery_network_mapping": resourceArmRecoveryServicesNetworkMapping(),
Expand Down
23 changes: 23 additions & 0 deletions azurerm/resource_arm_availability_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
Expand Down Expand Up @@ -59,6 +60,18 @@ func resourceArmAvailabilitySet() *schema.Resource {
ForceNew: true,
},

"proximity_placement_group_id": {
tobydoescode marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Optional: true,
ForceNew: true,

// We have to ignore case due to incorrect capitalisation of resource group name in
// proximity placement group ID in the response we get from the API request
//
// todo can be removed when https://github.com/Azure/azure-sdk-for-go/issues/5699 is fixed
DiffSuppressFunc: suppress.CaseDifference,
},

"tags": tags.Schema(),
},
}
Expand Down Expand Up @@ -102,6 +115,12 @@ func resourceArmAvailabilitySetCreateUpdate(d *schema.ResourceData, meta interfa
Tags: tags.Expand(t),
}

if v, ok := d.GetOk("proximity_placement_group_id"); ok {
tobydoescode marked this conversation as resolved.
Show resolved Hide resolved
availSet.AvailabilitySetProperties.ProximityPlacementGroup = &compute.SubResource{
ID: utils.String(v.(string)),
}
}

if managed {
n := "Aligned"
availSet.Sku = &compute.Sku{
Expand Down Expand Up @@ -151,6 +170,10 @@ func resourceArmAvailabilitySetRead(d *schema.ResourceData, meta interface{}) er
if props := resp.AvailabilitySetProperties; props != nil {
d.Set("platform_update_domain_count", props.PlatformUpdateDomainCount)
d.Set("platform_fault_domain_count", props.PlatformFaultDomainCount)

if proximityPlacementGroup := props.ProximityPlacementGroup; proximityPlacementGroup != nil {
d.Set("proximity_placement_group_id", proximityPlacementGroup.ID)
}
}

return tags.FlattenAndSet(d, resp.Tags)
Expand Down
67 changes: 60 additions & 7 deletions azurerm/resource_arm_availability_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azurerm

import (
"fmt"
"net/http"
"testing"

"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -132,6 +133,32 @@ func TestAccAzureRMAvailabilitySet_withTags(t *testing.T) {
})
}

func TestAccAzureRMAvailabilitySet_withPPG(t *testing.T) {
resourceName := "azurerm_availability_set.test"
ri := tf.AccRandTimeInt()
config := testAccAzureRMAvailabilitySet_withPPG(ri, testLocation())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAvailabilitySetDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAvailabilitySetExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "proximity_placement_group_id"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMAvailabilitySet_withDomainCounts(t *testing.T) {
resourceName := "azurerm_availability_set.test"
ri := tf.AccRandTimeInt()
Expand Down Expand Up @@ -193,21 +220,24 @@ func testCheckAzureRMAvailabilitySetExists(resourceName string) resource.TestChe
return fmt.Errorf("Not found: %s", resourceName)
}

availSetName := rs.Primary.Attributes["name"]
// Name of the actual scale set
name := rs.Primary.Attributes["name"]

resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
if !hasResourceGroup {
return fmt.Errorf("Bad: no resource group found in state for availability set: %s", availSetName)
return fmt.Errorf("Bad: no resource group found in state for availability set: %s", name)
}

client := testAccProvider.Meta().(*ArmClient).compute.AvailabilitySetsClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext
resp, err := client.Get(ctx, resourceGroup, availSetName)

vmss, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: Availability Set %q (resource group: %q) does not exist", availSetName, resourceGroup)
}
return fmt.Errorf("Bad: Get on vmScaleSetClient: %+v", err)
}

return fmt.Errorf("Bad: Get on availSetClient: %+v", err)
if vmss.StatusCode == http.StatusNotFound {
return fmt.Errorf("Bad: VirtualMachineScaleSet %q (resource group: %q) does not exist", name, resourceGroup)
}

return nil
Expand Down Expand Up @@ -334,6 +364,29 @@ resource "azurerm_availability_set" "test" {
`, rInt, location, rInt)
}

func testAccAzureRMAvailabilitySet_withPPG(rInt int, location string) string {
return fmt.Sprintf(`
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}"
}

resource "azurerm_availability_set" "test" {
name = "acctestavset-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

proximity_placement_group_id = "${azurerm_proximity_placement_group.test.id}"
}
`, rInt, location, rInt, rInt)
}

func testAccAzureRMAvailabilitySet_withDomainCounts(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down
Loading