Skip to content

Commit

Permalink
azurerm_resource_provider_registration supports feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed Aug 10, 2021
1 parent e50328f commit 59a36f1
Show file tree
Hide file tree
Showing 15 changed files with 1,664 additions and 49 deletions.
6 changes: 6 additions & 0 deletions internal/services/resource/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package client
import (
providers "github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/resources/mgmt/resources"
"github.com/Azure/azure-sdk-for-go/services/preview/resources/mgmt/2019-06-01-preview/templatespecs"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2015-12-01/features"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-09-01/locks"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-06-01/resources"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
DeploymentsClient *resources.DeploymentsClient
FeaturesClient *features.Client
GroupsClient *resources.GroupsClient
LocksClient *locks.ManagementLocksClient
ProvidersClient *providers.ProvidersClient
Expand All @@ -22,6 +24,9 @@ func NewClient(o *common.ClientOptions) *Client {
deploymentsClient := resources.NewDeploymentsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&deploymentsClient.Client, o.ResourceManagerAuthorizer)

featuresClient := features.NewClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&featuresClient.Client, o.ResourceManagerAuthorizer)

groupsClient := resources.NewGroupsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&groupsClient.Client, o.ResourceManagerAuthorizer)

Expand All @@ -45,6 +50,7 @@ func NewClient(o *common.ClientOptions) *Client {
return &Client{
GroupsClient: &groupsClient,
DeploymentsClient: &deploymentsClient,
FeaturesClient: &featuresClient,
LocksClient: &locksClient,
ProvidersClient: &providersClient,
ResourceProvidersClient: &resourceProvidersClient,
Expand Down
66 changes: 66 additions & 0 deletions internal/services/resource/parse/feature.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package parse

import (
"fmt"
"strings"

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

type FeatureId struct {
SubscriptionId string
ProviderNamespace string
Name string
}

func NewFeatureID(subscriptionId, resourceProvider, name string) FeatureId {
return FeatureId{
SubscriptionId: subscriptionId,
ProviderNamespace: resourceProvider,
Name: name,
}
}

func (id FeatureId) String() string {
segments := []string{
fmt.Sprintf("Name %q", id.Name),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("Feature: (%s)", segmentsStr)
}

func (id FeatureId) ID() string {
fmtString := "/subscriptions/%s/providers/Microsoft.Features/providers/%s/features/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ProviderNamespace, id.Name)
}

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

resourceId := FeatureId{
SubscriptionId: id.SubscriptionID,
ProviderNamespace: id.SecondaryProvider,
}

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

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

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

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

return &resourceId, nil
}
98 changes: 98 additions & 0 deletions internal/services/resource/parse/feature_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package parse

import (
"testing"

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

var _ resourceid.Formatter = FeatureId{}

func TestFeatureIDFormatter(t *testing.T) {
actual := NewFeatureID("12345678-1234-9876-4563-123456789012", "Microsoft.Test", "Feature1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Features/providers/Microsoft.Test/features/Feature1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

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

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

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

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

{
// missing Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Features/providers/Microsoft.SomeAzureService/",
Error: true,
},

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Features/providers/Microsoft.SomeAzureService/features/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Features/providers/Microsoft.SomeAzureService/features/Feature1",
Expected: &FeatureId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ProviderNamespace: "Microsoft.SomeAzureService",
Name: "Feature1",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/PROVIDERS/MICROSOFT.FEATURES/PROVIDERS/MICROSOFT.SOMEAZURESERVICE/FEATURES/FEATURE1",
Error: true,
},
}

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

actual, err := FeatureID(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.ProviderNamespace != v.Expected.ProviderNamespace {
t.Fatalf("Expected %q but got %q for ProviderNamespace", v.Expected.ProviderNamespace, actual.ProviderNamespace)
}
if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}
}
}
Loading

0 comments on commit 59a36f1

Please sign in to comment.