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

Refactor and add id formatters for compute RP #8240

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
13 changes: 12 additions & 1 deletion azurerm/internal/services/compute/parse/availability_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ type AvailabilitySetId struct {
Name string
}

func NewAvailabilitySetId(resourceGroup, name string) AvailabilitySetId {
return AvailabilitySetId{
ResourceGroup: resourceGroup,
Name: name,
}
}

func (id AvailabilitySetId) ID(subscriptionId string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/availabilitySets/%s", subscriptionId, id.ResourceGroup, id.Name)
}

func AvailabilitySetID(input string) (*AvailabilitySetId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Availability Set ID %q: %+v", input, err)
return nil, fmt.Errorf("unable to parse Availability Set ID %q: %+v", input, err)
}

set := AvailabilitySetId{
Expand Down
13 changes: 13 additions & 0 deletions azurerm/internal/services/compute/parse/availability_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@ package parse

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = AvailabilitySetId{}

func TestAvailabilitySetIDFormatter(t *testing.T) {
subscriptionId := "12345678-1234-5678-1234-123456789012"
actual := NewAvailabilitySetId("group1", "set1").ID(subscriptionId)
expected := "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Compute/availabilitySets/set1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestAvailabilitySetID(t *testing.T) {
testData := []struct {
Name string
Expand Down
15 changes: 14 additions & 1 deletion azurerm/internal/services/compute/parse/dedicated_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,23 @@ type DedicatedHostId struct {
Name string
}

func NewDedicatedHostId(id DedicatedHostGroupId, name string) DedicatedHostId {
return DedicatedHostId{
ResourceGroup: id.ResourceGroup,
HostGroup: id.Name,
Name: name,
}
}

func (id DedicatedHostId) ID(subscriptionId string) string {
base := NewDedicatedHostGroupId(id.ResourceGroup, id.HostGroup).ID(subscriptionId)
return fmt.Sprintf("%s/hosts/%s", base, id.Name)
}

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)
return nil, fmt.Errorf("unable to parse Dedicated Host ID %q: %+v", input, err)
}

host := DedicatedHostId{
Expand Down
13 changes: 12 additions & 1 deletion azurerm/internal/services/compute/parse/dedicated_host_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ type DedicatedHostGroupId struct {
Name string
}

func NewDedicatedHostGroupId(resourceGroup, name string) DedicatedHostGroupId {
return DedicatedHostGroupId{
ResourceGroup: resourceGroup,
Name: name,
}
}

func (id DedicatedHostGroupId) ID(subscriptionId string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/hostGroups/%s", subscriptionId, id.ResourceGroup, id.Name)
}

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)
return nil, fmt.Errorf("unable to parse Dedicated Host Group ID %q: %+v", input, err)
}

group := DedicatedHostGroupId{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@ package parse

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = DedicatedHostGroupId{}

func TestDedicatedHostGroupIDFormatter(t *testing.T) {
subscriptionId := "12345678-1234-5678-1234-123456789012"
actual := NewDedicatedHostGroupId("group1", "hostGroup1").ID(subscriptionId)
expected := "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Compute/hostGroups/hostGroup1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestDedicatedHostGroupID(t *testing.T) {
testData := []struct {
Name string
Expand Down
14 changes: 14 additions & 0 deletions azurerm/internal/services/compute/parse/dedicated_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@ package parse

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = DedicatedHostId{}

func TestDedicatedHostIDFormatter(t *testing.T) {
subscriptionId := "12345678-1234-5678-1234-123456789012"
hostGroupId := NewDedicatedHostGroupId("group1", "hostGroup1")
actual := NewDedicatedHostId(hostGroupId, "host1").ID(subscriptionId)
expected := "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Compute/hostGroups/hostGroup1/hosts/host1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestDedicatedHostID(t *testing.T) {
testData := []struct {
Name string
Expand Down
13 changes: 12 additions & 1 deletion azurerm/internal/services/compute/parse/disk_encryption_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ type DiskEncryptionSetId struct {
Name string
}

func NewDiskEncryptionSetId(resourceGroup, name string) DiskEncryptionSetId {
return DiskEncryptionSetId{
ResourceGroup: resourceGroup,
Name: name,
}
}

func (id DiskEncryptionSetId) ID(subscriptionId string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/diskEncryptionSets/%s", subscriptionId, id.ResourceGroup, id.Name)
}

func DiskEncryptionSetID(input string) (*DiskEncryptionSetId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Disk Encryption Set ID %q: %+v", input, err)
return nil, fmt.Errorf("unable to parse Disk Encryption Set ID %q: %+v", input, err)
}

encryptionSetId := DiskEncryptionSetId{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package parse

import "testing"
import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = DiskEncryptionSetId{}

func TestDiskEncryptionSetIDFormatter(t *testing.T) {
subscriptionId := "12345678-1234-5678-1234-123456789012"
actual := NewDiskEncryptionSetId("group1", "set1").ID(subscriptionId)
expected := "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Compute/diskEncryptionSets/set1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestDiskEncryptionSetID(t *testing.T) {
testData := []struct {
Expand Down
13 changes: 12 additions & 1 deletion azurerm/internal/services/compute/parse/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ type ImageId struct {
Name string
}

func NewImageId(resourceGroup, name string) ImageId {
return ImageId{
ResourceGroup: resourceGroup,
Name: name,
}
}

func (id ImageId) ID(subscriptionId string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/images/%s", subscriptionId, id.ResourceGroup, id.Name)
}

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

set := ImageId{
Expand Down
13 changes: 13 additions & 0 deletions azurerm/internal/services/compute/parse/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@ package parse

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = ImageId{}

func TestImageIDFormatter(t *testing.T) {
subscriptionId := "12345678-1234-5678-1234-123456789012"
actual := NewImageId("group1", "image1").ID(subscriptionId)
expected := "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Compute/images/image1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestImageID(t *testing.T) {
testData := []struct {
Name string
Expand Down
13 changes: 12 additions & 1 deletion azurerm/internal/services/compute/parse/managed_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ type ManagedDiskId struct {
Name string
}

func NewManagedDiskId(resourceGroup, name string) ManagedDiskId {
return ManagedDiskId{
ResourceGroup: resourceGroup,
Name: name,
}
}

func (id ManagedDiskId) ID(subscriptionId string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/disks/%s", subscriptionId, id.ResourceGroup, id.Name)
}

func ManagedDiskID(input string) (*ManagedDiskId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Managed Disk ID %q: %+v", input, err)
return nil, fmt.Errorf("unable to parse Managed Disk ID %q: %+v", input, err)
}

disk := ManagedDiskId{
Expand Down
13 changes: 13 additions & 0 deletions azurerm/internal/services/compute/parse/managed_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@ package parse

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = ManagedDiskId{}

func TestManagedDiskIDFormatter(t *testing.T) {
subscriptionId := "12345678-1234-5678-1234-123456789012"
actual := NewManagedDiskId("group1", "disk1").ID(subscriptionId)
expected := "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Compute/disks/disk1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestManagedDiskID(t *testing.T) {
testData := []struct {
Name string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ type ProximityPlacementGroupId struct {
Name string
}

func NewProximityPlacementGroupId(resourceGroup, name string) ProximityPlacementGroupId {
return ProximityPlacementGroupId{
ResourceGroup: resourceGroup,
Name: name,
}
}

func (id ProximityPlacementGroupId) ID(subscriptionId string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/proximityPlacementGroups/%s", subscriptionId, id.ResourceGroup, id.Name)
}

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

server := ProximityPlacementGroupId{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@ package parse

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = ProximityPlacementGroupId{}

func TestProximityPlacementGroupIDFormatter(t *testing.T) {
subscriptionId := "12345678-1234-5678-1234-123456789012"
actual := NewProximityPlacementGroupId("group1", "ppg1").ID(subscriptionId)
expected := "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Compute/proximityPlacementGroups/ppg1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestProximityPlacementGroupID(t *testing.T) {
testData := []struct {
Name string
Expand Down
23 changes: 18 additions & 5 deletions azurerm/internal/services/compute/parse/shared_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,40 @@ type SharedImageId struct {
Name string
}

func NewSharedImageId(id SharedImageGalleryId, name string) SharedImageId {
return SharedImageId{
ResourceGroup: id.ResourceGroup,
Gallery: id.Name,
Name: name,
}
}

func (id SharedImageId) ID(subscriptionId string) string {
base := NewSharedImageGalleryId(id.ResourceGroup, id.Gallery).ID(subscriptionId)
return fmt.Sprintf("%s/images/%s", base, id.Name)
}

func SharedImageID(input string) (*SharedImageId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Image ID %q: %+v", input, err)
return nil, fmt.Errorf("unable to parse Shared Image ID %q: %+v", input, err)
}

set := SharedImageId{
image := SharedImageId{
ResourceGroup: id.ResourceGroup,
}

if set.Gallery, err = id.PopSegment("galleries"); err != nil {
if image.Gallery, err = id.PopSegment("galleries"); err != nil {
return nil, err
}

if set.Name, err = id.PopSegment("images"); err != nil {
if image.Name, err = id.PopSegment("images"); err != nil {
return nil, err
}

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

return &set, nil
return &image, nil
}
Loading