Skip to content

Commit

Permalink
New resource azurerm_vmware_express_route_authorization (#11812)
Browse files Browse the repository at this point in the history
Fix:#9283

--- PASS: TestAccVmwareAuthorization_basic (15817.58s)
--- PASS: TestAccVmwareAuthorization_requiresImport (15908.18s)
  • Loading branch information
yupwei68 authored Jun 2, 2021
1 parent 71eefdc commit 134785b
Show file tree
Hide file tree
Showing 10 changed files with 644 additions and 6 deletions.
13 changes: 9 additions & 4 deletions azurerm/internal/services/vmware/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

type Client struct {
PrivateCloudClient *avs.PrivateCloudsClient
ClusterClient *avs.ClustersClient
PrivateCloudClient *avs.PrivateCloudsClient
ClusterClient *avs.ClustersClient
AuthorizationClient *avs.AuthorizationsClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -17,8 +18,12 @@ func NewClient(o *common.ClientOptions) *Client {
clusterClient := avs.NewClustersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&clusterClient.Client, o.ResourceManagerAuthorizer)

authorizationClient := avs.NewAuthorizationsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&authorizationClient.Client, o.ResourceManagerAuthorizer)

return &Client{
PrivateCloudClient: &privateCloudClient,
ClusterClient: &clusterClient,
PrivateCloudClient: &privateCloudClient,
ClusterClient: &clusterClient,
AuthorizationClient: &authorizationClient,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"fmt"
"strings"

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

type ExpressRouteAuthorizationId struct {
SubscriptionId string
ResourceGroup string
PrivateCloudName string
AuthorizationName string
}

func NewExpressRouteAuthorizationID(subscriptionId, resourceGroup, privateCloudName, authorizationName string) ExpressRouteAuthorizationId {
return ExpressRouteAuthorizationId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
PrivateCloudName: privateCloudName,
AuthorizationName: authorizationName,
}
}

func (id ExpressRouteAuthorizationId) String() string {
segments := []string{
fmt.Sprintf("Authorization Name %q", id.AuthorizationName),
fmt.Sprintf("Private Cloud Name %q", id.PrivateCloudName),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Express Route Authorization", segmentsStr)
}

func (id ExpressRouteAuthorizationId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.AVS/privateClouds/%s/authorizations/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.PrivateCloudName, id.AuthorizationName)
}

// ExpressRouteAuthorizationID parses a ExpressRouteAuthorization ID into an ExpressRouteAuthorizationId struct
func ExpressRouteAuthorizationID(input string) (*ExpressRouteAuthorizationId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := ExpressRouteAuthorizationId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
}

if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}

if resourceId.ResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

if resourceId.PrivateCloudName, err = id.PopSegment("privateClouds"); err != nil {
return nil, err
}
if resourceId.AuthorizationName, err = id.PopSegment("authorizations"); err != nil {
return nil, err
}

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

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

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"testing"

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

var _ resourceid.Formatter = ExpressRouteAuthorizationId{}

func TestExpressRouteAuthorizationIDFormatter(t *testing.T) {
actual := NewExpressRouteAuthorizationID("12345678-1234-9876-4563-123456789012", "group1", "privateCloud1", "authorization1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AVS/privateClouds/privateCloud1/authorizations/authorization1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestExpressRouteAuthorizationID(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *ExpressRouteAuthorizationId
}{

{
// empty
Input: "",
Error: true,
},

{
// missing SubscriptionId
Input: "/",
Error: true,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Error: true,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Error: true,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Error: true,
},

{
// missing PrivateCloudName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AVS/",
Error: true,
},

{
// missing value for PrivateCloudName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AVS/privateClouds/",
Error: true,
},

{
// missing AuthorizationName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AVS/privateClouds/privateCloud1/",
Error: true,
},

{
// missing value for AuthorizationName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AVS/privateClouds/privateCloud1/authorizations/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AVS/privateClouds/privateCloud1/authorizations/authorization1",
Expected: &ExpressRouteAuthorizationId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "group1",
PrivateCloudName: "privateCloud1",
AuthorizationName: "authorization1",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/GROUP1/PROVIDERS/MICROSOFT.AVS/PRIVATECLOUDS/PRIVATECLOUD1/AUTHORIZATIONS/AUTHORIZATION1",
Error: true,
},
}

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

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

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get one")
}

if actual.SubscriptionId != v.Expected.SubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId)
}
if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup)
}
if actual.PrivateCloudName != v.Expected.PrivateCloudName {
t.Fatalf("Expected %q but got %q for PrivateCloudName", v.Expected.PrivateCloudName, actual.PrivateCloudName)
}
if actual.AuthorizationName != v.Expected.AuthorizationName {
t.Fatalf("Expected %q but got %q for AuthorizationName", v.Expected.AuthorizationName, actual.AuthorizationName)
}
}
}
5 changes: 3 additions & 2 deletions azurerm/internal/services/vmware/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
// SupportedResources returns the supported Resources supported by this Service
func (r Registration) SupportedResources() map[string]*pluginsdk.Resource {
return map[string]*pluginsdk.Resource{
"azurerm_vmware_private_cloud": resourceVmwarePrivateCloud(),
"azurerm_vmware_cluster": resourceVmwareCluster(),
"azurerm_vmware_private_cloud": resourceVmwarePrivateCloud(),
"azurerm_vmware_cluster": resourceVmwareCluster(),
"azurerm_vmware_express_route_authorization": resourceVmwareExpressRouteAuthorization(),
}
}
1 change: 1 addition & 0 deletions azurerm/internal/services/vmware/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ package vmware

//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=PrivateCloud -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AVS/privateClouds/privateCloud1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Cluster -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AVS/privateClouds/privateCloud1/clusters/cluster1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ExpressRouteAuthorization -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AVS/privateClouds/privateCloud1/authorizations/authorization1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package validate

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/vmware/parse"
)

func ExpressRouteAuthorizationID(input interface{}, key string) (warnings []string, errors []error) {
v, ok := input.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected %q to be a string", key))
return
}

if _, err := parse.ExpressRouteAuthorizationID(v); err != nil {
errors = append(errors, err)
}

return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package validate

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import "testing"

func TestExpressRouteAuthorizationID(t *testing.T) {
cases := []struct {
Input string
Valid bool
}{

{
// empty
Input: "",
Valid: false,
},

{
// missing SubscriptionId
Input: "/",
Valid: false,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Valid: false,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Valid: false,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Valid: false,
},

{
// missing PrivateCloudName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AVS/",
Valid: false,
},

{
// missing value for PrivateCloudName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AVS/privateClouds/",
Valid: false,
},

{
// missing AuthorizationName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AVS/privateClouds/privateCloud1/",
Valid: false,
},

{
// missing value for AuthorizationName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AVS/privateClouds/privateCloud1/authorizations/",
Valid: false,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AVS/privateClouds/privateCloud1/authorizations/authorization1",
Valid: true,
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/GROUP1/PROVIDERS/MICROSOFT.AVS/PRIVATECLOUDS/PRIVATECLOUD1/AUTHORIZATIONS/AUTHORIZATION1",
Valid: false,
},
}
for _, tc := range cases {
t.Logf("[DEBUG] Testing Value %s", tc.Input)
_, errors := ExpressRouteAuthorizationID(tc.Input, "test")
valid := len(errors) == 0

if tc.Valid != valid {
t.Fatalf("Expected %t but got %t", tc.Valid, valid)
}
}
}
Loading

0 comments on commit 134785b

Please sign in to comment.