-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
azurerm_resource_provider_registration supports feature
- Loading branch information
Showing
15 changed files
with
1,664 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.