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

New resource: dedicated_host_group (compute) #5048

Closed
wants to merge 2 commits into from
Closed
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
101 changes: 101 additions & 0 deletions azurerm/data_source_dedicated_host_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package azurerm

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"

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

func dataSourceArmDedicatedHostGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmDedicatedHostGroupRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},

"location": azure.SchemaLocationForDataSource(),

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"hosts": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"platform_fault_domain_count": {
Type: schema.TypeInt,
Computed: true,
},

"type": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't; think this makes sense for terraform?

Type: schema.TypeString,
Computed: true,
},

"zones": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

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

func dataSourceArmDedicatedHostGroupRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Compute.DedicatedHostGroupsClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroupName := d.Get("resource_group_name").(string)

resp, err := client.Get(ctx, resourceGroupName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Dedicated Host Group %q (Resource Group %q) was not found", name, resourceGroupName)
}
return fmt.Errorf("Error reading Dedicated Host Group %q (Resource Group %q): %+v", name, resourceGroupName, err)
}

d.SetId(*resp.ID)

d.Set("name", name)
d.Set("resource_group_name", resourceGroupName)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if dedicatedHostGroupProperties := resp.DedicatedHostGroupProperties; dedicatedHostGroupProperties != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor we typically use a shorter name here

Suggested change
if dedicatedHostGroupProperties := resp.DedicatedHostGroupProperties; dedicatedHostGroupProperties != nil {
if props := resp.DedicatedHostGroupProperties; dedicatedHostGroupProperties != nil {

if err := d.Set("hosts", flattenArmDedicatedHostGroupSubResourceReadOnly(dedicatedHostGroupProperties.Hosts)); err != nil {
return fmt.Errorf("Error setting `hosts`: %+v", err)
}
d.Set("platform_fault_domain_count", int(*dedicatedHostGroupProperties.PlatformFaultDomainCount))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need to deref and convert for top level properties

Suggested change
d.Set("platform_fault_domain_count", int(*dedicatedHostGroupProperties.PlatformFaultDomainCount))
d.Set("platform_fault_domain_count", dedicatedHostGroupProperties.PlatformFaultDomainCount)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that? I assume a schema.TypeInt corresponds to int, not int32.

}
d.Set("type", resp.Type)
if resp.Zones != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the flatten function does this check?

d.Set("zones", utils.FlattenStringSlice(resp.Zones))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we check for an error here?

}

return nil
}
46 changes: 46 additions & 0 deletions azurerm/data_source_dedicated_host_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"

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

func TestAccDataSourceAzureRMDedicatedHostGroup_basic(t *testing.T) {
dataSourceName := "data.azurerm_dedicated_host_group.test"
ri := tf.AccRandTimeInt()
location := testLocation()
rName := acctest.RandStringFromCharSet(4, acctest.CharSetAlpha)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceDedicatedHostGroup_basic(ri, location, rName),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDedicatedHostGroupExists(dataSourceName),
resource.TestCheckResourceAttr(dataSourceName, "zones.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "zones.0", "1"),
resource.TestCheckResourceAttr(dataSourceName, "platform_fault_domain_count", "2"),
),
},
},
})
}

func testAccDataSourceDedicatedHostGroup_basic(rInt int, location string, rName string) string {
config := testAccAzureRMDedicatedHostGroup_complete(rInt, location, rName)
return fmt.Sprintf(`
%s

data "azurerm_dedicated_host_group" "test" {
resource_group_name = "${azurerm_dedicated_host_group.test.resource_group_name}"
name = "${azurerm_dedicated_host_group.test.name}"
}
`, config)
}
5 changes: 5 additions & 0 deletions azurerm/internal/clients/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

type ComputeClient struct {
AvailabilitySetsClient *compute.AvailabilitySetsClient
DedicatedHostGroupsClient *compute.DedicatedHostGroupsClient
DisksClient *compute.DisksClient
GalleriesClient *compute.GalleriesClient
GalleryImagesClient *compute.GalleryImagesClient
Expand All @@ -30,6 +31,9 @@ func NewComputeClient(o *common.ClientOptions) *ComputeClient {
availabilitySetsClient := compute.NewAvailabilitySetsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&availabilitySetsClient.Client, o.ResourceManagerAuthorizer)

dedicatedHostGroupsClient := compute.NewDedicatedHostGroupsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&dedicatedHostGroupsClient.Client, o.ResourceManagerAuthorizer)

disksClient := compute.NewDisksClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&disksClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -80,6 +84,7 @@ func NewComputeClient(o *common.ClientOptions) *ComputeClient {

return &ComputeClient{
AvailabilitySetsClient: &availabilitySetsClient,
DedicatedHostGroupsClient: &dedicatedHostGroupsClient,
DisksClient: &disksClient,
GalleriesClient: &galleriesClient,
GalleryImagesClient: &galleryImagesClient,
Expand Down
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_cosmosdb_account": dataSourceArmCosmosDbAccount(),
"azurerm_data_factory": dataSourceArmDataFactory(),
"azurerm_data_lake_store": dataSourceArmDataLakeStoreAccount(),
"azurerm_dedicated_host_group": dataSourceArmDedicatedHostGroup(),
"azurerm_dev_test_lab": dataSourceArmDevTestLab(),
"azurerm_dev_test_virtual_network": dataSourceArmDevTestVirtualNetwork(),
"azurerm_dns_zone": dataSourceArmDnsZone(),
Expand Down Expand Up @@ -255,6 +256,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_data_lake_store": resourceArmDataLakeStore(),
"azurerm_databricks_workspace": resourceArmDatabricksWorkspace(),
"azurerm_ddos_protection_plan": resourceArmDDoSProtectionPlan(),
"azurerm_dedicated_host_group": resourceArmDedicatedHostGroup(),
"azurerm_dev_test_lab": resourceArmDevTestLab(),
"azurerm_dev_test_schedule": resourceArmDevTestLabSchedules(),
"azurerm_dev_test_linux_virtual_machine": resourceArmDevTestLinuxVirtualMachine(),
Expand Down
Loading