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 Data Source/Resource: azurerm_dedicated_host #5513

Merged
merged 10 commits into from
Jan 24, 2020
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
5 changes: 5 additions & 0 deletions azurerm/internal/services/compute/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

type Client struct {
AvailabilitySetsClient *compute.AvailabilitySetsClient
DedicatedHostsClient *compute.DedicatedHostsClient
DedicatedHostGroupsClient *compute.DedicatedHostGroupsClient
DisksClient *compute.DisksClient
DiskEncryptionSetsClient *compute.DiskEncryptionSetsClient
Expand All @@ -32,6 +33,9 @@ func NewClient(o *common.ClientOptions) *Client {
availabilitySetsClient := compute.NewAvailabilitySetsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&availabilitySetsClient.Client, o.ResourceManagerAuthorizer)

dedicatedHostsClient := compute.NewDedicatedHostsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&dedicatedHostsClient.Client, o.ResourceManagerAuthorizer)

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

Expand Down Expand Up @@ -88,6 +92,7 @@ func NewClient(o *common.ClientOptions) *Client {

return &Client{
AvailabilitySetsClient: &availabilitySetsClient,
DedicatedHostsClient: &dedicatedHostsClient,
DedicatedHostGroupsClient: &dedicatedHostGroupsClient,
DisksClient: &disksClient,
DiskEncryptionSetsClient: &diskEncryptionSetsClient,
Expand Down
72 changes: 72 additions & 0 deletions azurerm/internal/services/compute/data_source_dedicated_host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package compute

import (
"fmt"
"time"

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

"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/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmDedicatedHost() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmDedicatedHostRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateDedicatedHostName(),
},

"dedicated_host_group_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateDedicatedHostGroupName(),
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"location": azure.SchemaLocationForDataSource(),

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

func dataSourceArmDedicatedHostRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.DedicatedHostsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Get(ctx, resourceGroupName, hostGroupName, name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Dedicated Host %q (Host Group Name %q / Resource Group %q) was not found", name, hostGroupName, resourceGroupName)
}
return fmt.Errorf("Error reading Dedicated Host %q (Host Group Name %q / Resource Group %q): %+v", name, hostGroupName, 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))
}
d.Set("dedicated_host_group_name", hostGroupName)

return tags.FlattenAndSet(d, resp.Tags)
}
38 changes: 38 additions & 0 deletions azurerm/internal/services/compute/parse/dedicated_host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package parse

import (
"fmt"

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

type DedicatedHostId struct {
ResourceGroup string
HostGroup string
Name string
}

func DedicatedHostID(input string) (*DedicatedHostId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Dedicated Host ID %q: %+v", input, err)
}

server := DedicatedHostId{
ResourceGroup: id.ResourceGroup,
}

if server.HostGroup, err = id.PopSegment("hostGroups"); err != nil {
return nil, err
}

if server.Name, err = id.PopSegment("hosts"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &server, nil
}
33 changes: 33 additions & 0 deletions azurerm/internal/services/compute/parse/dedicated_host_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package parse

import (
"fmt"

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

type DedicatedHostGroupId struct {
ResourceGroup string
Name string
}

func DedicatedHostGroupID(input string) (*DedicatedHostGroupId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Dedicated Host Group ID %q: %+v", input, err)
}

server := DedicatedHostGroupId{
ResourceGroup: id.ResourceGroup,
}

if server.Name, err = id.PopSegment("hostGroups"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &server, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package parse

import (
"testing"
)

func TestDedicatedHostGroupID(t *testing.T) {
testData := []struct {
Name string
Input string
Error bool
Expect *DedicatedHostGroupId
}{
{
Name: "Empty",
Input: "",
Error: true,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Error: true,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Error: true,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Error: true,
},
{
Name: "Missing Host Group Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Compute/hostGroups/",
Error: true,
},
{
Name: "Host Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Compute/hostGroups/group1",
Error: false,
Expect: &DedicatedHostGroupId{
ResourceGroup: "resGroup1",
Name: "group1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Compute/HostGroups/group1",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := DedicatedHostGroupID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expect.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expect.Name, actual.Name)
}

if actual.ResourceGroup != v.Expect.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expect.ResourceGroup, actual.ResourceGroup)
}
}
}
90 changes: 90 additions & 0 deletions azurerm/internal/services/compute/parse/dedicated_host_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package parse

import (
"testing"
)

func TestDedicatedHostID(t *testing.T) {
testData := []struct {
Name string
Input string
Error bool
Expect *DedicatedHostId
}{
{
Name: "Empty",
Input: "",
Error: true,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Error: true,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Error: true,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Error: true,
},
{
Name: "Missing Host Group Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Compute/hostGroups/",
Error: true,
},
{
Name: "Host Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Compute/hostGroups/group1/",
Error: true,
},
{
Name: "Missing Host Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Compute/hostGroups/group1/hosts/",
Error: true,
},
{
Name: "Host ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Compute/hostGroups/group1/hosts/host1",
Error: false,
Expect: &DedicatedHostId{
ResourceGroup: "resGroup1",
HostGroup: "group1",
Name: "host1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Compute/hostGroups/group1/Hosts/host1",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := DedicatedHostID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expect.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expect.Name, actual.Name)
}

if actual.HostGroup != v.Expect.HostGroup {
t.Fatalf("Expected %q but got %q for HostGroup", v.Expect.HostGroup, actual.HostGroup)
}

if actual.ResourceGroup != v.Expect.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expect.ResourceGroup, actual.ResourceGroup)
}
}
}
2 changes: 2 additions & 0 deletions azurerm/internal/services/compute/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func (r Registration) Name() string {
func (r Registration) SupportedDataSources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_availability_set": dataSourceArmAvailabilitySet(),
"azurerm_dedicated_host": dataSourceArmDedicatedHost(),
"azurerm_dedicated_host_group": dataSourceArmDedicatedHostGroup(),
"azurerm_disk_encryption_set": dataSourceArmDiskEncryptionSet(),
"azurerm_managed_disk": dataSourceArmManagedDisk(),
Expand All @@ -34,6 +35,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
func (r Registration) SupportedResources() map[string]*schema.Resource {
resources := map[string]*schema.Resource{
"azurerm_availability_set": resourceArmAvailabilitySet(),
"azurerm_dedicated_host": resourceArmDedicatedHost(),
"azurerm_dedicated_host_group": resourceArmDedicatedHostGroup(),
"azurerm_disk_encryption_set": resourceArmDiskEncryptionSet(),
"azurerm_image": resourceArmImage(),
Expand Down
Loading