diff --git a/internal/services/maps/maps_account_data_source.go b/internal/services/maps/maps_account_data_source.go index 47d8c8533993..24ada6d84b65 100644 --- a/internal/services/maps/maps_account_data_source.go +++ b/internal/services/maps/maps_account_data_source.go @@ -77,8 +77,8 @@ func dataSourceMapsAccountRead(d *pluginsdk.ResourceData, meta interface{}) erro d.SetId(id.ID()) - d.Set("name", id.Name) - d.Set("resource_group_name", id.ResourceGroup) + d.Set("name", id.AccountName) + d.Set("resource_group_name", id.ResourceGroupName) if model := resp.Model; model != nil { d.Set("sku_name", model.Sku.Name) diff --git a/internal/services/maps/maps_account_resource.go b/internal/services/maps/maps_account_resource.go index 4800c5dd0d49..02e68e99b77d 100644 --- a/internal/services/maps/maps_account_resource.go +++ b/internal/services/maps/maps_account_resource.go @@ -141,8 +141,8 @@ func resourceMapsAccountRead(d *pluginsdk.ResourceData, meta interface{}) error return fmt.Errorf("retrieving %s: %+v", *id, err) } - d.Set("name", id.Name) - d.Set("resource_group_name", id.ResourceGroup) + d.Set("name", id.AccountName) + d.Set("resource_group_name", id.ResourceGroupName) if model := resp.Model; model != nil { d.Set("sku_name", model.Sku.Name) diff --git a/internal/services/maps/sdk/2021-02-01/accounts/constants.go b/internal/services/maps/sdk/2021-02-01/accounts/constants.go index 7278294c92cf..8a78feb2a4ad 100644 --- a/internal/services/maps/sdk/2021-02-01/accounts/constants.go +++ b/internal/services/maps/sdk/2021-02-01/accounts/constants.go @@ -1,5 +1,7 @@ package accounts +import "strings" + type CreatedByType string const ( @@ -9,6 +11,31 @@ const ( CreatedByTypeUser CreatedByType = "User" ) +func PossibleValuesForCreatedByType() []string { + return []string{ + string(CreatedByTypeApplication), + string(CreatedByTypeKey), + string(CreatedByTypeManagedIdentity), + string(CreatedByTypeUser), + } +} + +func parseCreatedByType(input string) (*CreatedByType, error) { + vals := map[string]CreatedByType{ + "application": CreatedByTypeApplication, + "key": CreatedByTypeKey, + "managedidentity": CreatedByTypeManagedIdentity, + "user": CreatedByTypeUser, + } + if v, ok := vals[strings.ToLower(input)]; ok { + return &v, nil + } + + // otherwise presume it's an undefined value and best-effort it + out := CreatedByType(input) + return &out, nil +} + type KeyType string const ( @@ -16,6 +43,27 @@ const ( KeyTypeSecondary KeyType = "secondary" ) +func PossibleValuesForKeyType() []string { + return []string{ + string(KeyTypePrimary), + string(KeyTypeSecondary), + } +} + +func parseKeyType(input string) (*KeyType, error) { + vals := map[string]KeyType{ + "primary": KeyTypePrimary, + "secondary": KeyTypeSecondary, + } + if v, ok := vals[strings.ToLower(input)]; ok { + return &v, nil + } + + // otherwise presume it's an undefined value and best-effort it + out := KeyType(input) + return &out, nil +} + type Kind string const ( @@ -23,6 +71,27 @@ const ( KindGenTwo Kind = "Gen2" ) +func PossibleValuesForKind() []string { + return []string{ + string(KindGenOne), + string(KindGenTwo), + } +} + +func parseKind(input string) (*Kind, error) { + vals := map[string]Kind{ + "gen1": KindGenOne, + "gen2": KindGenTwo, + } + if v, ok := vals[strings.ToLower(input)]; ok { + return &v, nil + } + + // otherwise presume it's an undefined value and best-effort it + out := Kind(input) + return &out, nil +} + type Name string const ( @@ -30,3 +99,26 @@ const ( NameSOne Name = "S1" NameSZero Name = "S0" ) + +func PossibleValuesForName() []string { + return []string{ + string(NameGTwo), + string(NameSOne), + string(NameSZero), + } +} + +func parseName(input string) (*Name, error) { + vals := map[string]Name{ + "g2": NameGTwo, + "s1": NameSOne, + "s0": NameSZero, + } + if v, ok := vals[strings.ToLower(input)]; ok { + return &v, nil + } + + // otherwise presume it's an undefined value and best-effort it + out := Name(input) + return &out, nil +} diff --git a/internal/services/maps/sdk/2021-02-01/accounts/id_account.go b/internal/services/maps/sdk/2021-02-01/accounts/id_account.go index 04fc5ea9e004..dcee34c86f3b 100644 --- a/internal/services/maps/sdk/2021-02-01/accounts/id_account.go +++ b/internal/services/maps/sdk/2021-02-01/accounts/id_account.go @@ -7,102 +7,118 @@ import ( "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" ) +var _ resourceids.ResourceId = AccountId{} + +// AccountId is a struct representing the Resource ID for a Account type AccountId struct { - SubscriptionId string - ResourceGroup string - Name string + SubscriptionId string + ResourceGroupName string + AccountName string } -func NewAccountID(subscriptionId, resourceGroup, name string) AccountId { +// NewAccountID returns a new AccountId struct +func NewAccountID(subscriptionId string, resourceGroupName string, accountName string) AccountId { return AccountId{ - SubscriptionId: subscriptionId, - ResourceGroup: resourceGroup, - Name: name, + SubscriptionId: subscriptionId, + ResourceGroupName: resourceGroupName, + AccountName: accountName, } } -func (id AccountId) String() string { - segments := []string{ - fmt.Sprintf("Name %q", id.Name), - fmt.Sprintf("Resource Group %q", id.ResourceGroup), - } - segmentsStr := strings.Join(segments, " / ") - return fmt.Sprintf("%s: (%s)", "Account", segmentsStr) -} - -func (id AccountId) ID() string { - fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Maps/accounts/%s" - return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.Name) -} - -// ParseAccountID parses a Account ID into an AccountId struct +// ParseAccountID parses 'input' into a AccountId func ParseAccountID(input string) (*AccountId, error) { - id, err := resourceids.ParseAzureResourceID(input) + parser := resourceids.NewParserFromResourceIdType(AccountId{}) + parsed, err := parser.Parse(input, false) if err != nil { - return nil, err - } - - resourceId := AccountId{ - SubscriptionId: id.SubscriptionID, - ResourceGroup: id.ResourceGroup, + return nil, fmt.Errorf("parsing %q: %+v", input, err) } - if resourceId.SubscriptionId == "" { - return nil, fmt.Errorf("ID was missing the 'subscriptions' element") - } + var ok bool + id := AccountId{} - if resourceId.ResourceGroup == "" { - return nil, fmt.Errorf("ID was missing the 'resourceGroups' element") + if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok { + return nil, fmt.Errorf("the segment 'subscriptionId' was not found in the resource id %q", input) } - if resourceId.Name, err = id.PopSegment("accounts"); err != nil { - return nil, err + if id.ResourceGroupName, ok = parsed.Parsed["resourceGroupName"]; !ok { + return nil, fmt.Errorf("the segment 'resourceGroupName' was not found in the resource id %q", input) } - if err := id.ValidateNoEmptySegments(input); err != nil { - return nil, err + if id.AccountName, ok = parsed.Parsed["accountName"]; !ok { + return nil, fmt.Errorf("the segment 'accountName' was not found in the resource id %q", input) } - return &resourceId, nil + return &id, nil } -// ParseAccountIDInsensitively parses an Account ID into an AccountId struct, insensitively -// This should only be used to parse an ID for rewriting to a consistent casing, -// the ParseAccountID method should be used instead for validation etc. +// ParseAccountIDInsensitively parses 'input' case-insensitively into a AccountId +// note: this method should only be used for API response data and not user input func ParseAccountIDInsensitively(input string) (*AccountId, error) { - id, err := resourceids.ParseAzureResourceID(input) + parser := resourceids.NewParserFromResourceIdType(AccountId{}) + parsed, err := parser.Parse(input, true) if err != nil { - return nil, err + return nil, fmt.Errorf("parsing %q: %+v", input, err) } - resourceId := AccountId{ - SubscriptionId: id.SubscriptionID, - ResourceGroup: id.ResourceGroup, + var ok bool + id := AccountId{} + + if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok { + return nil, fmt.Errorf("the segment 'subscriptionId' was not found in the resource id %q", input) } - if resourceId.SubscriptionId == "" { - return nil, fmt.Errorf("ID was missing the 'subscriptions' element") + if id.ResourceGroupName, ok = parsed.Parsed["resourceGroupName"]; !ok { + return nil, fmt.Errorf("the segment 'resourceGroupName' was not found in the resource id %q", input) } - if resourceId.ResourceGroup == "" { - return nil, fmt.Errorf("ID was missing the 'resourceGroups' element") + if id.AccountName, ok = parsed.Parsed["accountName"]; !ok { + return nil, fmt.Errorf("the segment 'accountName' was not found in the resource id %q", input) } - // find the correct casing for the 'accounts' segment - accountsKey := "accounts" - for key := range id.Path { - if strings.EqualFold(key, accountsKey) { - accountsKey = key - break - } + return &id, nil +} + +// ValidateAccountID checks that 'input' can be parsed as a Account ID +func ValidateAccountID(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 resourceId.Name, err = id.PopSegment(accountsKey); err != nil { - return nil, err + + if _, err := ParseAccountID(v); err != nil { + errors = append(errors, err) } - if err := id.ValidateNoEmptySegments(input); err != nil { - return nil, err + return +} + +// ID returns the formatted Account ID +func (id AccountId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Maps/accounts/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroupName, id.AccountName) +} + +// Segments returns a slice of Resource ID Segments which comprise this Account ID +func (id AccountId) Segments() []resourceids.Segment { + return []resourceids.Segment{ + resourceids.StaticSegment("subscriptions", "subscriptions", "subscriptions"), + resourceids.SubscriptionIdSegment("subscriptionId", "12345678-1234-9876-4563-123456789012"), + resourceids.StaticSegment("resourceGroups", "resourceGroups", "resourceGroups"), + resourceids.ResourceGroupSegment("resourceGroupName", "example-resource-group"), + resourceids.StaticSegment("providers", "providers", "providers"), + resourceids.ResourceProviderSegment("microsoftMaps", "Microsoft.Maps", "Microsoft.Maps"), + resourceids.StaticSegment("accounts", "accounts", "accounts"), + resourceids.UserSpecifiedSegment("accountName", "accountValue"), } +} - return &resourceId, nil +// String returns a human-readable description of this Account ID +func (id AccountId) String() string { + components := []string{ + fmt.Sprintf("Subscription: %q", id.SubscriptionId), + fmt.Sprintf("Resource Group Name: %q", id.ResourceGroupName), + fmt.Sprintf("Account Name: %q", id.AccountName), + } + return fmt.Sprintf("Account (%s)", strings.Join(components, "\n")) } diff --git a/internal/services/maps/sdk/2021-02-01/accounts/id_account_test.go b/internal/services/maps/sdk/2021-02-01/accounts/id_account_test.go index 4fb11da8a97a..e5c49cbc5db5 100644 --- a/internal/services/maps/sdk/2021-02-01/accounts/id_account_test.go +++ b/internal/services/maps/sdk/2021-02-01/accounts/id_account_test.go @@ -6,13 +6,29 @@ import ( "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" ) -var _ resourceids.Id = AccountId{} +var _ resourceids.ResourceId = AccountId{} -func TestAccountIDFormatter(t *testing.T) { - actual := NewAccountID("{subscriptionId}", "{resourceGroupName}", "{accountName}").ID() - expected := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Maps/accounts/{accountName}" +func TestNewAccountID(t *testing.T) { + id := NewAccountID("12345678-1234-9876-4563-123456789012", "example-resource-group", "accountValue") + + if id.SubscriptionId != "12345678-1234-9876-4563-123456789012" { + t.Fatalf("Expected %q but got %q for Segment 'SubscriptionId'", id.SubscriptionId, "12345678-1234-9876-4563-123456789012") + } + + if id.ResourceGroupName != "example-resource-group" { + t.Fatalf("Expected %q but got %q for Segment 'ResourceGroupName'", id.ResourceGroupName, "example-resource-group") + } + + if id.AccountName != "accountValue" { + t.Fatalf("Expected %q but got %q for Segment 'AccountName'", id.AccountName, "accountValue") + } +} + +func TestFormatAccountID(t *testing.T) { + actual := NewAccountID("12345678-1234-9876-4563-123456789012", "example-resource-group", "accountValue").ID() + expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue" if actual != expected { - t.Fatalf("Expected %q but got %q", expected, actual) + t.Fatalf("Expected the Formatted ID to be %q but got %q", actual, expected) } } @@ -22,66 +38,61 @@ func TestParseAccountID(t *testing.T) { Error bool Expected *AccountId }{ - { - // empty + // Incomplete URI Input: "", Error: true, }, - { - // missing SubscriptionId - Input: "/", + // Incomplete URI + Input: "/subscriptions", Error: true, }, - { - // missing value for SubscriptionId - Input: "/subscriptions/", + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012", Error: true, }, - { - // missing ResourceGroup - Input: "/subscriptions/{subscriptionId}/", + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups", Error: true, }, - { - // missing value for ResourceGroup - Input: "/subscriptions/{subscriptionId}/resourceGroups/", + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group", Error: true, }, - { - // missing Name - Input: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Maps/", + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers", Error: true, }, - { - // missing value for Name - Input: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Maps/accounts/", + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps", Error: true, }, - { - // valid - Input: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Maps/accounts/{accountName}", + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts", + Error: true, + }, + { + // Valid URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue", Expected: &AccountId{ - SubscriptionId: "{subscriptionId}", - ResourceGroup: "{resourceGroupName}", - Name: "{accountName}", + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroupName: "example-resource-group", + AccountName: "accountValue", }, }, - { - // upper-cased - Input: "/SUBSCRIPTIONS/{SUBSCRIPTIONID}/RESOURCEGROUPS/{RESOURCEGROUPNAME}/PROVIDERS/MICROSOFT.MAPS/ACCOUNTS/{ACCOUNTNAME}", + // Invalid (Valid Uri with Extra segment) + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue/extra", Error: true, }, } - for _, v := range testData { t.Logf("[DEBUG] Testing %q", v.Input) @@ -91,7 +102,7 @@ func TestParseAccountID(t *testing.T) { continue } - t.Fatalf("Expect a value but got an error: %s", err) + t.Fatalf("Expect a value but got an error: %+v", err) } if v.Error { t.Fatal("Expect an error but didn't get one") @@ -100,12 +111,15 @@ func TestParseAccountID(t *testing.T) { 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.ResourceGroupName != v.Expected.ResourceGroupName { + t.Fatalf("Expected %q but got %q for ResourceGroupName", v.Expected.ResourceGroupName, actual.ResourceGroupName) } - if actual.Name != v.Expected.Name { - t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) + + if actual.AccountName != v.Expected.AccountName { + t.Fatalf("Expected %q but got %q for AccountName", v.Expected.AccountName, actual.AccountName) } + } } @@ -115,90 +129,110 @@ func TestParseAccountIDInsensitively(t *testing.T) { Error bool Expected *AccountId }{ - { - // empty + // Incomplete URI Input: "", Error: true, }, - { - // missing SubscriptionId - Input: "/", + // Incomplete URI + Input: "/subscriptions", Error: true, }, - { - // missing value for SubscriptionId - Input: "/subscriptions/", + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs", Error: true, }, - { - // missing ResourceGroup - Input: "/subscriptions/{subscriptionId}/", + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012", Error: true, }, - { - // missing value for ResourceGroup - Input: "/subscriptions/{subscriptionId}/resourceGroups/", + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012", Error: true, }, - { - // missing Name - Input: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Maps/", + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups", Error: true, }, - { - // missing value for Name - Input: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Maps/accounts/", + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS", Error: true, }, - { - // valid - Input: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Maps/accounts/{accountName}", - Expected: &AccountId{ - SubscriptionId: "{subscriptionId}", - ResourceGroup: "{resourceGroupName}", - Name: "{accountName}", - }, + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group", + Error: true, }, - { - // lower-cased segment names - Input: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Maps/accounts/{accountName}", - Expected: &AccountId{ - SubscriptionId: "{subscriptionId}", - ResourceGroup: "{resourceGroupName}", - Name: "{accountName}", - }, + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs/mIcRoSoFt.mApS", + Error: true, }, - { - // upper-cased segment names - Input: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Maps/ACCOUNTS/{accountName}", + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs/mIcRoSoFt.mApS/aCcOuNtS", + Error: true, + }, + { + // Valid URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue", Expected: &AccountId{ - SubscriptionId: "{subscriptionId}", - ResourceGroup: "{resourceGroupName}", - Name: "{accountName}", + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroupName: "example-resource-group", + AccountName: "accountValue", }, }, - { - // mixed-cased segment names - Input: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Maps/AcCoUnTs/{accountName}", + // Invalid (Valid Uri with Extra segment) + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue/extra", + Error: true, + }, + { + // Valid URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs/mIcRoSoFt.mApS/aCcOuNtS/aCcOuNtVaLuE", Expected: &AccountId{ - SubscriptionId: "{subscriptionId}", - ResourceGroup: "{resourceGroupName}", - Name: "{accountName}", + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroupName: "eXaMpLe-rEsOuRcE-GrOuP", + AccountName: "aCcOuNtVaLuE", }, }, + { + // Invalid (Valid Uri with Extra segment - mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs/mIcRoSoFt.mApS/aCcOuNtS/aCcOuNtVaLuE/extra", + Error: true, + }, } - for _, v := range testData { t.Logf("[DEBUG] Testing %q", v.Input) @@ -208,7 +242,7 @@ func TestParseAccountIDInsensitively(t *testing.T) { continue } - t.Fatalf("Expect a value but got an error: %s", err) + t.Fatalf("Expect a value but got an error: %+v", err) } if v.Error { t.Fatal("Expect an error but didn't get one") @@ -217,11 +251,14 @@ func TestParseAccountIDInsensitively(t *testing.T) { 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.ResourceGroupName != v.Expected.ResourceGroupName { + t.Fatalf("Expected %q but got %q for ResourceGroupName", v.Expected.ResourceGroupName, actual.ResourceGroupName) } - if actual.Name != v.Expected.Name { - t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) + + if actual.AccountName != v.Expected.AccountName { + t.Fatalf("Expected %q but got %q for AccountName", v.Expected.AccountName, actual.AccountName) } + } } diff --git a/internal/services/maps/sdk/2021-02-01/accounts/id_resourcegroup.go b/internal/services/maps/sdk/2021-02-01/accounts/id_resourcegroup.go index 4cc75abff624..701ad7475d26 100644 --- a/internal/services/maps/sdk/2021-02-01/accounts/id_resourcegroup.go +++ b/internal/services/maps/sdk/2021-02-01/accounts/id_resourcegroup.go @@ -7,83 +7,103 @@ import ( "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" ) +var _ resourceids.ResourceId = ResourceGroupId{} + +// ResourceGroupId is a struct representing the Resource ID for a Resource Group type ResourceGroupId struct { - SubscriptionId string - ResourceGroup string + SubscriptionId string + ResourceGroupName string } -func NewResourceGroupID(subscriptionId, resourceGroup string) ResourceGroupId { +// NewResourceGroupID returns a new ResourceGroupId struct +func NewResourceGroupID(subscriptionId string, resourceGroupName string) ResourceGroupId { return ResourceGroupId{ - SubscriptionId: subscriptionId, - ResourceGroup: resourceGroup, + SubscriptionId: subscriptionId, + ResourceGroupName: resourceGroupName, } } -func (id ResourceGroupId) String() string { - segments := []string{ - fmt.Sprintf("Resource Group %q", id.ResourceGroup), - } - segmentsStr := strings.Join(segments, " / ") - return fmt.Sprintf("%s: (%s)", "Resource Group", segmentsStr) -} - -func (id ResourceGroupId) ID() string { - fmtString := "/subscriptions/%s/resourceGroups/%s" - return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup) -} - -// ParseResourceGroupID parses a ResourceGroup ID into an ResourceGroupId struct +// ParseResourceGroupID parses 'input' into a ResourceGroupId func ParseResourceGroupID(input string) (*ResourceGroupId, error) { - id, err := resourceids.ParseAzureResourceID(input) + parser := resourceids.NewParserFromResourceIdType(ResourceGroupId{}) + parsed, err := parser.Parse(input, false) if err != nil { - return nil, err - } - - resourceId := ResourceGroupId{ - SubscriptionId: id.SubscriptionID, - ResourceGroup: id.ResourceGroup, + return nil, fmt.Errorf("parsing %q: %+v", input, err) } - if resourceId.SubscriptionId == "" { - return nil, fmt.Errorf("ID was missing the 'subscriptions' element") - } + var ok bool + id := ResourceGroupId{} - if resourceId.ResourceGroup == "" { - return nil, fmt.Errorf("ID was missing the 'resourceGroups' element") + if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok { + return nil, fmt.Errorf("the segment 'subscriptionId' was not found in the resource id %q", input) } - if err := id.ValidateNoEmptySegments(input); err != nil { - return nil, err + if id.ResourceGroupName, ok = parsed.Parsed["resourceGroupName"]; !ok { + return nil, fmt.Errorf("the segment 'resourceGroupName' was not found in the resource id %q", input) } - return &resourceId, nil + return &id, nil } -// ParseResourceGroupIDInsensitively parses an ResourceGroup ID into an ResourceGroupId struct, insensitively -// This should only be used to parse an ID for rewriting to a consistent casing, -// the ParseResourceGroupID method should be used instead for validation etc. +// ParseResourceGroupIDInsensitively parses 'input' case-insensitively into a ResourceGroupId +// note: this method should only be used for API response data and not user input func ParseResourceGroupIDInsensitively(input string) (*ResourceGroupId, error) { - id, err := resourceids.ParseAzureResourceID(input) + parser := resourceids.NewParserFromResourceIdType(ResourceGroupId{}) + parsed, err := parser.Parse(input, true) if err != nil { - return nil, err + return nil, fmt.Errorf("parsing %q: %+v", input, err) + } + + var ok bool + id := ResourceGroupId{} + + if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok { + return nil, fmt.Errorf("the segment 'subscriptionId' was not found in the resource id %q", input) } - resourceId := ResourceGroupId{ - SubscriptionId: id.SubscriptionID, - ResourceGroup: id.ResourceGroup, + if id.ResourceGroupName, ok = parsed.Parsed["resourceGroupName"]; !ok { + return nil, fmt.Errorf("the segment 'resourceGroupName' was not found in the resource id %q", input) } - if resourceId.SubscriptionId == "" { - return nil, fmt.Errorf("ID was missing the 'subscriptions' element") + return &id, nil +} + +// ValidateResourceGroupID checks that 'input' can be parsed as a Resource Group ID +func ValidateResourceGroupID(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 resourceId.ResourceGroup == "" { - return nil, fmt.Errorf("ID was missing the 'resourceGroups' element") + if _, err := ParseResourceGroupID(v); err != nil { + errors = append(errors, err) } - if err := id.ValidateNoEmptySegments(input); err != nil { - return nil, err + return +} + +// ID returns the formatted Resource Group ID +func (id ResourceGroupId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroupName) +} + +// Segments returns a slice of Resource ID Segments which comprise this Resource Group ID +func (id ResourceGroupId) Segments() []resourceids.Segment { + return []resourceids.Segment{ + resourceids.StaticSegment("subscriptions", "subscriptions", "subscriptions"), + resourceids.SubscriptionIdSegment("subscriptionId", "12345678-1234-9876-4563-123456789012"), + resourceids.StaticSegment("resourceGroups", "resourceGroups", "resourceGroups"), + resourceids.ResourceGroupSegment("resourceGroupName", "example-resource-group"), } +} - return &resourceId, nil +// String returns a human-readable description of this Resource Group ID +func (id ResourceGroupId) String() string { + components := []string{ + fmt.Sprintf("Subscription: %q", id.SubscriptionId), + fmt.Sprintf("Resource Group Name: %q", id.ResourceGroupName), + } + return fmt.Sprintf("Resource Group (%s)", strings.Join(components, "\n")) } diff --git a/internal/services/maps/sdk/2021-02-01/accounts/id_resourcegroup_test.go b/internal/services/maps/sdk/2021-02-01/accounts/id_resourcegroup_test.go index 75bd41bc29e2..c4a6f235ea46 100644 --- a/internal/services/maps/sdk/2021-02-01/accounts/id_resourcegroup_test.go +++ b/internal/services/maps/sdk/2021-02-01/accounts/id_resourcegroup_test.go @@ -6,13 +6,25 @@ import ( "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" ) -var _ resourceids.Id = ResourceGroupId{} +var _ resourceids.ResourceId = ResourceGroupId{} -func TestResourceGroupIDFormatter(t *testing.T) { - actual := NewResourceGroupID("{subscriptionId}", "{resourceGroupName}").ID() - expected := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}" +func TestNewResourceGroupID(t *testing.T) { + id := NewResourceGroupID("12345678-1234-9876-4563-123456789012", "example-resource-group") + + if id.SubscriptionId != "12345678-1234-9876-4563-123456789012" { + t.Fatalf("Expected %q but got %q for Segment 'SubscriptionId'", id.SubscriptionId, "12345678-1234-9876-4563-123456789012") + } + + if id.ResourceGroupName != "example-resource-group" { + t.Fatalf("Expected %q but got %q for Segment 'ResourceGroupName'", id.ResourceGroupName, "example-resource-group") + } +} + +func TestFormatResourceGroupID(t *testing.T) { + actual := NewResourceGroupID("12345678-1234-9876-4563-123456789012", "example-resource-group").ID() + expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group" if actual != expected { - t.Fatalf("Expected %q but got %q", expected, actual) + t.Fatalf("Expected the Formatted ID to be %q but got %q", actual, expected) } } @@ -22,53 +34,40 @@ func TestParseResourceGroupID(t *testing.T) { Error bool Expected *ResourceGroupId }{ - { - // empty + // Incomplete URI Input: "", Error: true, }, - - { - // missing SubscriptionId - Input: "/", - Error: true, - }, - { - // missing value for SubscriptionId - Input: "/subscriptions/", + // Incomplete URI + Input: "/subscriptions", Error: true, }, - { - // missing ResourceGroup - Input: "/subscriptions/{subscriptionId}/", + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012", Error: true, }, - { - // missing value for ResourceGroup - Input: "/subscriptions/{subscriptionId}/resourceGroups/", + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups", Error: true, }, - { - // valid - Input: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}", + // Valid URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group", Expected: &ResourceGroupId{ - SubscriptionId: "{subscriptionId}", - ResourceGroup: "{resourceGroupName}", + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroupName: "example-resource-group", }, }, - { - // upper-cased - Input: "/SUBSCRIPTIONS/{SUBSCRIPTIONID}/RESOURCEGROUPS/{RESOURCEGROUPNAME}", + // Invalid (Valid Uri with Extra segment) + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/extra", Error: true, }, } - for _, v := range testData { t.Logf("[DEBUG] Testing %q", v.Input) @@ -78,7 +77,7 @@ func TestParseResourceGroupID(t *testing.T) { continue } - t.Fatalf("Expect a value but got an error: %s", err) + t.Fatalf("Expect a value but got an error: %+v", err) } if v.Error { t.Fatal("Expect an error but didn't get one") @@ -87,9 +86,11 @@ func TestParseResourceGroupID(t *testing.T) { 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.ResourceGroupName != v.Expected.ResourceGroupName { + t.Fatalf("Expected %q but got %q for ResourceGroupName", v.Expected.ResourceGroupName, actual.ResourceGroupName) } + } } @@ -99,74 +100,68 @@ func TestParseResourceGroupIDInsensitively(t *testing.T) { Error bool Expected *ResourceGroupId }{ - { - // empty + // Incomplete URI Input: "", Error: true, }, - { - // missing SubscriptionId - Input: "/", + // Incomplete URI + Input: "/subscriptions", Error: true, }, - { - // missing value for SubscriptionId - Input: "/subscriptions/", + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs", Error: true, }, - { - // missing ResourceGroup - Input: "/subscriptions/{subscriptionId}/", + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012", Error: true, }, - { - // missing value for ResourceGroup - Input: "/subscriptions/{subscriptionId}/resourceGroups/", + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012", Error: true, }, - { - // valid - Input: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}", - Expected: &ResourceGroupId{ - SubscriptionId: "{subscriptionId}", - ResourceGroup: "{resourceGroupName}", - }, + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups", + Error: true, }, - { - // lower-cased segment names - Input: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}", - Expected: &ResourceGroupId{ - SubscriptionId: "{subscriptionId}", - ResourceGroup: "{resourceGroupName}", - }, + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS", + Error: true, }, - { - // upper-cased segment names - Input: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}", + // Valid URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group", Expected: &ResourceGroupId{ - SubscriptionId: "{subscriptionId}", - ResourceGroup: "{resourceGroupName}", + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroupName: "example-resource-group", }, }, - { - // mixed-cased segment names - Input: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}", + // Invalid (Valid Uri with Extra segment) + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/extra", + Error: true, + }, + { + // Valid URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP", Expected: &ResourceGroupId{ - SubscriptionId: "{subscriptionId}", - ResourceGroup: "{resourceGroupName}", + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroupName: "eXaMpLe-rEsOuRcE-GrOuP", }, }, + { + // Invalid (Valid Uri with Extra segment - mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/extra", + Error: true, + }, } - for _, v := range testData { t.Logf("[DEBUG] Testing %q", v.Input) @@ -176,7 +171,7 @@ func TestParseResourceGroupIDInsensitively(t *testing.T) { continue } - t.Fatalf("Expect a value but got an error: %s", err) + t.Fatalf("Expect a value but got an error: %+v", err) } if v.Error { t.Fatal("Expect an error but didn't get one") @@ -185,8 +180,10 @@ func TestParseResourceGroupIDInsensitively(t *testing.T) { 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.ResourceGroupName != v.Expected.ResourceGroupName { + t.Fatalf("Expected %q but got %q for ResourceGroupName", v.Expected.ResourceGroupName, actual.ResourceGroupName) } + } } diff --git a/internal/services/maps/sdk/2021-02-01/accounts/id_subscription.go b/internal/services/maps/sdk/2021-02-01/accounts/id_subscription.go index dc099d4eeeca..f95107ae71d3 100644 --- a/internal/services/maps/sdk/2021-02-01/accounts/id_subscription.go +++ b/internal/services/maps/sdk/2021-02-01/accounts/id_subscription.go @@ -7,69 +7,90 @@ import ( "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" ) +var _ resourceids.ResourceId = SubscriptionId{} + +// SubscriptionId is a struct representing the Resource ID for a Subscription type SubscriptionId struct { SubscriptionId string } +// NewSubscriptionID returns a new SubscriptionId struct func NewSubscriptionID(subscriptionId string) SubscriptionId { return SubscriptionId{ SubscriptionId: subscriptionId, } } -func (id SubscriptionId) String() string { - segments := []string{} - segmentsStr := strings.Join(segments, " / ") - return fmt.Sprintf("%s: (%s)", "Subscription", segmentsStr) -} - -func (id SubscriptionId) ID() string { - fmtString := "/subscriptions/%s" - return fmt.Sprintf(fmtString, id.SubscriptionId) -} - -// ParseSubscriptionID parses a Subscription ID into an SubscriptionId struct +// ParseSubscriptionID parses 'input' into a SubscriptionId func ParseSubscriptionID(input string) (*SubscriptionId, error) { - id, err := resourceids.ParseAzureResourceID(input) + parser := resourceids.NewParserFromResourceIdType(SubscriptionId{}) + parsed, err := parser.Parse(input, false) if err != nil { - return nil, err + return nil, fmt.Errorf("parsing %q: %+v", input, err) } - resourceId := SubscriptionId{ - SubscriptionId: id.SubscriptionID, - } + var ok bool + id := SubscriptionId{} - if resourceId.SubscriptionId == "" { - return nil, fmt.Errorf("ID was missing the 'subscriptions' element") + if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok { + return nil, fmt.Errorf("the segment 'subscriptionId' was not found in the resource id %q", input) } - if err := id.ValidateNoEmptySegments(input); err != nil { - return nil, err - } - - return &resourceId, nil + return &id, nil } -// ParseSubscriptionIDInsensitively parses an Subscription ID into an SubscriptionId struct, insensitively -// This should only be used to parse an ID for rewriting to a consistent casing, -// the ParseSubscriptionID method should be used instead for validation etc. +// ParseSubscriptionIDInsensitively parses 'input' case-insensitively into a SubscriptionId +// note: this method should only be used for API response data and not user input func ParseSubscriptionIDInsensitively(input string) (*SubscriptionId, error) { - id, err := resourceids.ParseAzureResourceID(input) + parser := resourceids.NewParserFromResourceIdType(SubscriptionId{}) + parsed, err := parser.Parse(input, true) if err != nil { - return nil, err + return nil, fmt.Errorf("parsing %q: %+v", input, err) } - resourceId := SubscriptionId{ - SubscriptionId: id.SubscriptionID, + var ok bool + id := SubscriptionId{} + + if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok { + return nil, fmt.Errorf("the segment 'subscriptionId' was not found in the resource id %q", input) } - if resourceId.SubscriptionId == "" { - return nil, fmt.Errorf("ID was missing the 'subscriptions' element") + return &id, nil +} + +// ValidateSubscriptionID checks that 'input' can be parsed as a Subscription ID +func ValidateSubscriptionID(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 := id.ValidateNoEmptySegments(input); err != nil { - return nil, err + if _, err := ParseSubscriptionID(v); err != nil { + errors = append(errors, err) } - return &resourceId, nil + return +} + +// ID returns the formatted Subscription ID +func (id SubscriptionId) ID() string { + fmtString := "/subscriptions/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId) +} + +// Segments returns a slice of Resource ID Segments which comprise this Subscription ID +func (id SubscriptionId) Segments() []resourceids.Segment { + return []resourceids.Segment{ + resourceids.StaticSegment("subscriptions", "subscriptions", "subscriptions"), + resourceids.SubscriptionIdSegment("subscriptionId", "12345678-1234-9876-4563-123456789012"), + } +} + +// String returns a human-readable description of this Subscription ID +func (id SubscriptionId) String() string { + components := []string{ + fmt.Sprintf("Subscription: %q", id.SubscriptionId), + } + return fmt.Sprintf("Subscription (%s)", strings.Join(components, "\n")) } diff --git a/internal/services/maps/sdk/2021-02-01/accounts/id_subscription_test.go b/internal/services/maps/sdk/2021-02-01/accounts/id_subscription_test.go index 4eb8eb1ac22a..7f03b066d161 100644 --- a/internal/services/maps/sdk/2021-02-01/accounts/id_subscription_test.go +++ b/internal/services/maps/sdk/2021-02-01/accounts/id_subscription_test.go @@ -6,13 +6,21 @@ import ( "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" ) -var _ resourceids.Id = SubscriptionId{} +var _ resourceids.ResourceId = SubscriptionId{} -func TestSubscriptionIDFormatter(t *testing.T) { - actual := NewSubscriptionID("{subscriptionId}").ID() - expected := "/subscriptions/{subscriptionId}" +func TestNewSubscriptionID(t *testing.T) { + id := NewSubscriptionID("12345678-1234-9876-4563-123456789012") + + if id.SubscriptionId != "12345678-1234-9876-4563-123456789012" { + t.Fatalf("Expected %q but got %q for Segment 'SubscriptionId'", id.SubscriptionId, "12345678-1234-9876-4563-123456789012") + } +} + +func TestFormatSubscriptionID(t *testing.T) { + actual := NewSubscriptionID("12345678-1234-9876-4563-123456789012").ID() + expected := "/subscriptions/12345678-1234-9876-4563-123456789012" if actual != expected { - t.Fatalf("Expected %q but got %q", expected, actual) + t.Fatalf("Expected the Formatted ID to be %q but got %q", actual, expected) } } @@ -22,40 +30,29 @@ func TestParseSubscriptionID(t *testing.T) { Error bool Expected *SubscriptionId }{ - { - // empty + // Incomplete URI Input: "", Error: true, }, - { - // missing SubscriptionId - Input: "/", + // Incomplete URI + Input: "/subscriptions", Error: true, }, - { - // missing value for SubscriptionId - Input: "/subscriptions/", - Error: true, - }, - - { - // valid - Input: "/subscriptions/{subscriptionId}", + // Valid URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012", Expected: &SubscriptionId{ - SubscriptionId: "{subscriptionId}", + SubscriptionId: "12345678-1234-9876-4563-123456789012", }, }, - { - // upper-cased - Input: "/SUBSCRIPTIONS/{SUBSCRIPTIONID}", + // Invalid (Valid Uri with Extra segment) + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/extra", Error: true, }, } - for _, v := range testData { t.Logf("[DEBUG] Testing %q", v.Input) @@ -65,7 +62,7 @@ func TestParseSubscriptionID(t *testing.T) { continue } - t.Fatalf("Expect a value but got an error: %s", err) + t.Fatalf("Expect a value but got an error: %+v", err) } if v.Error { t.Fatal("Expect an error but didn't get one") @@ -74,6 +71,7 @@ func TestParseSubscriptionID(t *testing.T) { if actual.SubscriptionId != v.Expected.SubscriptionId { t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId) } + } } @@ -83,58 +81,46 @@ func TestParseSubscriptionIDInsensitively(t *testing.T) { Error bool Expected *SubscriptionId }{ - { - // empty + // Incomplete URI Input: "", Error: true, }, - { - // missing SubscriptionId - Input: "/", + // Incomplete URI + Input: "/subscriptions", Error: true, }, - { - // missing value for SubscriptionId - Input: "/subscriptions/", + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs", Error: true, }, - { - // valid - Input: "/subscriptions/{subscriptionId}", + // Valid URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012", Expected: &SubscriptionId{ - SubscriptionId: "{subscriptionId}", + SubscriptionId: "12345678-1234-9876-4563-123456789012", }, }, - { - // lower-cased segment names - Input: "/subscriptions/{subscriptionId}", - Expected: &SubscriptionId{ - SubscriptionId: "{subscriptionId}", - }, + // Invalid (Valid Uri with Extra segment) + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/extra", + Error: true, }, - { - // upper-cased segment names - Input: "/subscriptions/{subscriptionId}", + // Valid URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012", Expected: &SubscriptionId{ - SubscriptionId: "{subscriptionId}", + SubscriptionId: "12345678-1234-9876-4563-123456789012", }, }, - { - // mixed-cased segment names - Input: "/subscriptions/{subscriptionId}", - Expected: &SubscriptionId{ - SubscriptionId: "{subscriptionId}", - }, + // Invalid (Valid Uri with Extra segment - mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/extra", + Error: true, }, } - for _, v := range testData { t.Logf("[DEBUG] Testing %q", v.Input) @@ -144,7 +130,7 @@ func TestParseSubscriptionIDInsensitively(t *testing.T) { continue } - t.Fatalf("Expect a value but got an error: %s", err) + t.Fatalf("Expect a value but got an error: %+v", err) } if v.Error { t.Fatal("Expect an error but didn't get one") @@ -153,5 +139,6 @@ func TestParseSubscriptionIDInsensitively(t *testing.T) { if actual.SubscriptionId != v.Expected.SubscriptionId { t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId) } + } } diff --git a/internal/services/maps/sdk/2021-02-01/accounts/model_systemdata.go b/internal/services/maps/sdk/2021-02-01/accounts/model_systemdata.go index e096bd0b8bfa..8bc1758a073f 100644 --- a/internal/services/maps/sdk/2021-02-01/accounts/model_systemdata.go +++ b/internal/services/maps/sdk/2021-02-01/accounts/model_systemdata.go @@ -1,11 +1,5 @@ package accounts -import ( - "time" - - "github.com/hashicorp/go-azure-helpers/lang/dates" -) - type SystemData struct { CreatedAt *string `json:"createdAt,omitempty"` CreatedBy *string `json:"createdBy,omitempty"` @@ -14,21 +8,3 @@ type SystemData struct { LastModifiedBy *string `json:"lastModifiedBy,omitempty"` LastModifiedByType *CreatedByType `json:"lastModifiedByType,omitempty"` } - -func (o SystemData) GetCreatedAtAsTime() (*time.Time, error) { - return dates.ParseAsFormat(o.CreatedAt, "2006-01-02T15:04:05Z07:00") -} - -func (o SystemData) SetCreatedAtAsTime(input time.Time) { - formatted := input.Format("2006-01-02T15:04:05Z07:00") - o.CreatedAt = &formatted -} - -func (o SystemData) GetLastModifiedAtAsTime() (*time.Time, error) { - return dates.ParseAsFormat(o.LastModifiedAt, "2006-01-02T15:04:05Z07:00") -} - -func (o SystemData) SetLastModifiedAtAsTime(input time.Time) { - formatted := input.Format("2006-01-02T15:04:05Z07:00") - o.LastModifiedAt = &formatted -} diff --git a/internal/services/maps/sdk/2021-02-01/accounts/predicates.go b/internal/services/maps/sdk/2021-02-01/accounts/predicates.go index 11d6b453c585..05de8044727f 100644 --- a/internal/services/maps/sdk/2021-02-01/accounts/predicates.go +++ b/internal/services/maps/sdk/2021-02-01/accounts/predicates.go @@ -8,6 +8,7 @@ type MapsAccountPredicate struct { } func (p MapsAccountPredicate) Matches(input MapsAccount) bool { + if p.Id != nil && (input.Id == nil && *p.Id != *input.Id) { return false } diff --git a/internal/services/maps/sdk/2021-02-01/creators/client.go b/internal/services/maps/sdk/2021-02-01/creators/client.go new file mode 100644 index 000000000000..f01bc96dccfb --- /dev/null +++ b/internal/services/maps/sdk/2021-02-01/creators/client.go @@ -0,0 +1,15 @@ +package creators + +import "github.com/Azure/go-autorest/autorest" + +type CreatorsClient struct { + Client autorest.Client + baseUri string +} + +func NewCreatorsClientWithBaseURI(endpoint string) CreatorsClient { + return CreatorsClient{ + Client: autorest.NewClientWithUserAgent(userAgent()), + baseUri: endpoint, + } +} diff --git a/internal/services/maps/sdk/2021-02-01/creators/id_account.go b/internal/services/maps/sdk/2021-02-01/creators/id_account.go new file mode 100644 index 000000000000..eaf98848f564 --- /dev/null +++ b/internal/services/maps/sdk/2021-02-01/creators/id_account.go @@ -0,0 +1,124 @@ +package creators + +import ( + "fmt" + "strings" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" +) + +var _ resourceids.ResourceId = AccountId{} + +// AccountId is a struct representing the Resource ID for a Account +type AccountId struct { + SubscriptionId string + ResourceGroupName string + AccountName string +} + +// NewAccountID returns a new AccountId struct +func NewAccountID(subscriptionId string, resourceGroupName string, accountName string) AccountId { + return AccountId{ + SubscriptionId: subscriptionId, + ResourceGroupName: resourceGroupName, + AccountName: accountName, + } +} + +// ParseAccountID parses 'input' into a AccountId +func ParseAccountID(input string) (*AccountId, error) { + parser := resourceids.NewParserFromResourceIdType(AccountId{}) + parsed, err := parser.Parse(input, false) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) + } + + var ok bool + id := AccountId{} + + if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok { + return nil, fmt.Errorf("the segment 'subscriptionId' was not found in the resource id %q", input) + } + + if id.ResourceGroupName, ok = parsed.Parsed["resourceGroupName"]; !ok { + return nil, fmt.Errorf("the segment 'resourceGroupName' was not found in the resource id %q", input) + } + + if id.AccountName, ok = parsed.Parsed["accountName"]; !ok { + return nil, fmt.Errorf("the segment 'accountName' was not found in the resource id %q", input) + } + + return &id, nil +} + +// ParseAccountIDInsensitively parses 'input' case-insensitively into a AccountId +// note: this method should only be used for API response data and not user input +func ParseAccountIDInsensitively(input string) (*AccountId, error) { + parser := resourceids.NewParserFromResourceIdType(AccountId{}) + parsed, err := parser.Parse(input, true) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) + } + + var ok bool + id := AccountId{} + + if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok { + return nil, fmt.Errorf("the segment 'subscriptionId' was not found in the resource id %q", input) + } + + if id.ResourceGroupName, ok = parsed.Parsed["resourceGroupName"]; !ok { + return nil, fmt.Errorf("the segment 'resourceGroupName' was not found in the resource id %q", input) + } + + if id.AccountName, ok = parsed.Parsed["accountName"]; !ok { + return nil, fmt.Errorf("the segment 'accountName' was not found in the resource id %q", input) + } + + return &id, nil +} + +// ValidateAccountID checks that 'input' can be parsed as a Account ID +func ValidateAccountID(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 := ParseAccountID(v); err != nil { + errors = append(errors, err) + } + + return +} + +// ID returns the formatted Account ID +func (id AccountId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Maps/accounts/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroupName, id.AccountName) +} + +// Segments returns a slice of Resource ID Segments which comprise this Account ID +func (id AccountId) Segments() []resourceids.Segment { + return []resourceids.Segment{ + resourceids.StaticSegment("subscriptions", "subscriptions", "subscriptions"), + resourceids.SubscriptionIdSegment("subscriptionId", "12345678-1234-9876-4563-123456789012"), + resourceids.StaticSegment("resourceGroups", "resourceGroups", "resourceGroups"), + resourceids.ResourceGroupSegment("resourceGroupName", "example-resource-group"), + resourceids.StaticSegment("providers", "providers", "providers"), + resourceids.ResourceProviderSegment("microsoftMaps", "Microsoft.Maps", "Microsoft.Maps"), + resourceids.StaticSegment("accounts", "accounts", "accounts"), + resourceids.UserSpecifiedSegment("accountName", "accountValue"), + } +} + +// String returns a human-readable description of this Account ID +func (id AccountId) String() string { + components := []string{ + fmt.Sprintf("Subscription: %q", id.SubscriptionId), + fmt.Sprintf("Resource Group Name: %q", id.ResourceGroupName), + fmt.Sprintf("Account Name: %q", id.AccountName), + } + return fmt.Sprintf("Account (%s)", strings.Join(components, "\n")) +} diff --git a/internal/services/maps/sdk/2021-02-01/creators/id_account_test.go b/internal/services/maps/sdk/2021-02-01/creators/id_account_test.go new file mode 100644 index 000000000000..5422e848448a --- /dev/null +++ b/internal/services/maps/sdk/2021-02-01/creators/id_account_test.go @@ -0,0 +1,264 @@ +package creators + +import ( + "testing" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" +) + +var _ resourceids.ResourceId = AccountId{} + +func TestNewAccountID(t *testing.T) { + id := NewAccountID("12345678-1234-9876-4563-123456789012", "example-resource-group", "accountValue") + + if id.SubscriptionId != "12345678-1234-9876-4563-123456789012" { + t.Fatalf("Expected %q but got %q for Segment 'SubscriptionId'", id.SubscriptionId, "12345678-1234-9876-4563-123456789012") + } + + if id.ResourceGroupName != "example-resource-group" { + t.Fatalf("Expected %q but got %q for Segment 'ResourceGroupName'", id.ResourceGroupName, "example-resource-group") + } + + if id.AccountName != "accountValue" { + t.Fatalf("Expected %q but got %q for Segment 'AccountName'", id.AccountName, "accountValue") + } +} + +func TestFormatAccountID(t *testing.T) { + actual := NewAccountID("12345678-1234-9876-4563-123456789012", "example-resource-group", "accountValue").ID() + expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue" + if actual != expected { + t.Fatalf("Expected the Formatted ID to be %q but got %q", actual, expected) + } +} + +func TestParseAccountID(t *testing.T) { + testData := []struct { + Input string + Error bool + Expected *AccountId + }{ + { + // Incomplete URI + Input: "", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts", + Error: true, + }, + { + // Valid URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue", + Expected: &AccountId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroupName: "example-resource-group", + AccountName: "accountValue", + }, + }, + { + // Invalid (Valid Uri with Extra segment) + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue/extra", + Error: true, + }, + } + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Input) + + actual, err := ParseAccountID(v.Input) + if err != nil { + if v.Error { + continue + } + + t.Fatalf("Expect a value but got an error: %+v", 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.ResourceGroupName != v.Expected.ResourceGroupName { + t.Fatalf("Expected %q but got %q for ResourceGroupName", v.Expected.ResourceGroupName, actual.ResourceGroupName) + } + + if actual.AccountName != v.Expected.AccountName { + t.Fatalf("Expected %q but got %q for AccountName", v.Expected.AccountName, actual.AccountName) + } + + } +} + +func TestParseAccountIDInsensitively(t *testing.T) { + testData := []struct { + Input string + Error bool + Expected *AccountId + }{ + { + // Incomplete URI + Input: "", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs/mIcRoSoFt.mApS", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs/mIcRoSoFt.mApS/aCcOuNtS", + Error: true, + }, + { + // Valid URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue", + Expected: &AccountId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroupName: "example-resource-group", + AccountName: "accountValue", + }, + }, + { + // Invalid (Valid Uri with Extra segment) + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue/extra", + Error: true, + }, + { + // Valid URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs/mIcRoSoFt.mApS/aCcOuNtS/aCcOuNtVaLuE", + Expected: &AccountId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroupName: "eXaMpLe-rEsOuRcE-GrOuP", + AccountName: "aCcOuNtVaLuE", + }, + }, + { + // Invalid (Valid Uri with Extra segment - mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs/mIcRoSoFt.mApS/aCcOuNtS/aCcOuNtVaLuE/extra", + Error: true, + }, + } + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Input) + + actual, err := ParseAccountIDInsensitively(v.Input) + if err != nil { + if v.Error { + continue + } + + t.Fatalf("Expect a value but got an error: %+v", 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.ResourceGroupName != v.Expected.ResourceGroupName { + t.Fatalf("Expected %q but got %q for ResourceGroupName", v.Expected.ResourceGroupName, actual.ResourceGroupName) + } + + if actual.AccountName != v.Expected.AccountName { + t.Fatalf("Expected %q but got %q for AccountName", v.Expected.AccountName, actual.AccountName) + } + + } +} diff --git a/internal/services/maps/sdk/2021-02-01/creators/id_creator.go b/internal/services/maps/sdk/2021-02-01/creators/id_creator.go new file mode 100644 index 000000000000..dda2e7a230f1 --- /dev/null +++ b/internal/services/maps/sdk/2021-02-01/creators/id_creator.go @@ -0,0 +1,137 @@ +package creators + +import ( + "fmt" + "strings" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" +) + +var _ resourceids.ResourceId = CreatorId{} + +// CreatorId is a struct representing the Resource ID for a Creator +type CreatorId struct { + SubscriptionId string + ResourceGroupName string + AccountName string + CreatorName string +} + +// NewCreatorID returns a new CreatorId struct +func NewCreatorID(subscriptionId string, resourceGroupName string, accountName string, creatorName string) CreatorId { + return CreatorId{ + SubscriptionId: subscriptionId, + ResourceGroupName: resourceGroupName, + AccountName: accountName, + CreatorName: creatorName, + } +} + +// ParseCreatorID parses 'input' into a CreatorId +func ParseCreatorID(input string) (*CreatorId, error) { + parser := resourceids.NewParserFromResourceIdType(CreatorId{}) + parsed, err := parser.Parse(input, false) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) + } + + var ok bool + id := CreatorId{} + + if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok { + return nil, fmt.Errorf("the segment 'subscriptionId' was not found in the resource id %q", input) + } + + if id.ResourceGroupName, ok = parsed.Parsed["resourceGroupName"]; !ok { + return nil, fmt.Errorf("the segment 'resourceGroupName' was not found in the resource id %q", input) + } + + if id.AccountName, ok = parsed.Parsed["accountName"]; !ok { + return nil, fmt.Errorf("the segment 'accountName' was not found in the resource id %q", input) + } + + if id.CreatorName, ok = parsed.Parsed["creatorName"]; !ok { + return nil, fmt.Errorf("the segment 'creatorName' was not found in the resource id %q", input) + } + + return &id, nil +} + +// ParseCreatorIDInsensitively parses 'input' case-insensitively into a CreatorId +// note: this method should only be used for API response data and not user input +func ParseCreatorIDInsensitively(input string) (*CreatorId, error) { + parser := resourceids.NewParserFromResourceIdType(CreatorId{}) + parsed, err := parser.Parse(input, true) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) + } + + var ok bool + id := CreatorId{} + + if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok { + return nil, fmt.Errorf("the segment 'subscriptionId' was not found in the resource id %q", input) + } + + if id.ResourceGroupName, ok = parsed.Parsed["resourceGroupName"]; !ok { + return nil, fmt.Errorf("the segment 'resourceGroupName' was not found in the resource id %q", input) + } + + if id.AccountName, ok = parsed.Parsed["accountName"]; !ok { + return nil, fmt.Errorf("the segment 'accountName' was not found in the resource id %q", input) + } + + if id.CreatorName, ok = parsed.Parsed["creatorName"]; !ok { + return nil, fmt.Errorf("the segment 'creatorName' was not found in the resource id %q", input) + } + + return &id, nil +} + +// ValidateCreatorID checks that 'input' can be parsed as a Creator ID +func ValidateCreatorID(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 := ParseCreatorID(v); err != nil { + errors = append(errors, err) + } + + return +} + +// ID returns the formatted Creator ID +func (id CreatorId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Maps/accounts/%s/creators/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroupName, id.AccountName, id.CreatorName) +} + +// Segments returns a slice of Resource ID Segments which comprise this Creator ID +func (id CreatorId) Segments() []resourceids.Segment { + return []resourceids.Segment{ + resourceids.StaticSegment("subscriptions", "subscriptions", "subscriptions"), + resourceids.SubscriptionIdSegment("subscriptionId", "12345678-1234-9876-4563-123456789012"), + resourceids.StaticSegment("resourceGroups", "resourceGroups", "resourceGroups"), + resourceids.ResourceGroupSegment("resourceGroupName", "example-resource-group"), + resourceids.StaticSegment("providers", "providers", "providers"), + resourceids.ResourceProviderSegment("microsoftMaps", "Microsoft.Maps", "Microsoft.Maps"), + resourceids.StaticSegment("accounts", "accounts", "accounts"), + resourceids.UserSpecifiedSegment("accountName", "accountValue"), + resourceids.StaticSegment("creators", "creators", "creators"), + resourceids.UserSpecifiedSegment("creatorName", "creatorValue"), + } +} + +// String returns a human-readable description of this Creator ID +func (id CreatorId) String() string { + components := []string{ + fmt.Sprintf("Subscription: %q", id.SubscriptionId), + fmt.Sprintf("Resource Group Name: %q", id.ResourceGroupName), + fmt.Sprintf("Account Name: %q", id.AccountName), + fmt.Sprintf("Creator Name: %q", id.CreatorName), + } + return fmt.Sprintf("Creator (%s)", strings.Join(components, "\n")) +} diff --git a/internal/services/maps/sdk/2021-02-01/creators/id_creator_test.go b/internal/services/maps/sdk/2021-02-01/creators/id_creator_test.go new file mode 100644 index 000000000000..85b3f826e2c1 --- /dev/null +++ b/internal/services/maps/sdk/2021-02-01/creators/id_creator_test.go @@ -0,0 +1,309 @@ +package creators + +import ( + "testing" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" +) + +var _ resourceids.ResourceId = CreatorId{} + +func TestNewCreatorID(t *testing.T) { + id := NewCreatorID("12345678-1234-9876-4563-123456789012", "example-resource-group", "accountValue", "creatorValue") + + if id.SubscriptionId != "12345678-1234-9876-4563-123456789012" { + t.Fatalf("Expected %q but got %q for Segment 'SubscriptionId'", id.SubscriptionId, "12345678-1234-9876-4563-123456789012") + } + + if id.ResourceGroupName != "example-resource-group" { + t.Fatalf("Expected %q but got %q for Segment 'ResourceGroupName'", id.ResourceGroupName, "example-resource-group") + } + + if id.AccountName != "accountValue" { + t.Fatalf("Expected %q but got %q for Segment 'AccountName'", id.AccountName, "accountValue") + } + + if id.CreatorName != "creatorValue" { + t.Fatalf("Expected %q but got %q for Segment 'CreatorName'", id.CreatorName, "creatorValue") + } +} + +func TestFormatCreatorID(t *testing.T) { + actual := NewCreatorID("12345678-1234-9876-4563-123456789012", "example-resource-group", "accountValue", "creatorValue").ID() + expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue/creators/creatorValue" + if actual != expected { + t.Fatalf("Expected the Formatted ID to be %q but got %q", actual, expected) + } +} + +func TestParseCreatorID(t *testing.T) { + testData := []struct { + Input string + Error bool + Expected *CreatorId + }{ + { + // Incomplete URI + Input: "", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue/creators", + Error: true, + }, + { + // Valid URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue/creators/creatorValue", + Expected: &CreatorId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroupName: "example-resource-group", + AccountName: "accountValue", + CreatorName: "creatorValue", + }, + }, + { + // Invalid (Valid Uri with Extra segment) + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue/creators/creatorValue/extra", + Error: true, + }, + } + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Input) + + actual, err := ParseCreatorID(v.Input) + if err != nil { + if v.Error { + continue + } + + t.Fatalf("Expect a value but got an error: %+v", 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.ResourceGroupName != v.Expected.ResourceGroupName { + t.Fatalf("Expected %q but got %q for ResourceGroupName", v.Expected.ResourceGroupName, actual.ResourceGroupName) + } + + if actual.AccountName != v.Expected.AccountName { + t.Fatalf("Expected %q but got %q for AccountName", v.Expected.AccountName, actual.AccountName) + } + + if actual.CreatorName != v.Expected.CreatorName { + t.Fatalf("Expected %q but got %q for CreatorName", v.Expected.CreatorName, actual.CreatorName) + } + + } +} + +func TestParseCreatorIDInsensitively(t *testing.T) { + testData := []struct { + Input string + Error bool + Expected *CreatorId + }{ + { + // Incomplete URI + Input: "", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs/mIcRoSoFt.mApS", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs/mIcRoSoFt.mApS/aCcOuNtS", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs/mIcRoSoFt.mApS/aCcOuNtS/aCcOuNtVaLuE", + Error: true, + }, + { + // Incomplete URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue/creators", + Error: true, + }, + { + // Incomplete URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs/mIcRoSoFt.mApS/aCcOuNtS/aCcOuNtVaLuE/cReAtOrS", + Error: true, + }, + { + // Valid URI + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue/creators/creatorValue", + Expected: &CreatorId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroupName: "example-resource-group", + AccountName: "accountValue", + CreatorName: "creatorValue", + }, + }, + { + // Invalid (Valid Uri with Extra segment) + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group/providers/Microsoft.Maps/accounts/accountValue/creators/creatorValue/extra", + Error: true, + }, + { + // Valid URI (mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs/mIcRoSoFt.mApS/aCcOuNtS/aCcOuNtVaLuE/cReAtOrS/cReAtOrVaLuE", + Expected: &CreatorId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroupName: "eXaMpLe-rEsOuRcE-GrOuP", + AccountName: "aCcOuNtVaLuE", + CreatorName: "cReAtOrVaLuE", + }, + }, + { + // Invalid (Valid Uri with Extra segment - mIxEd CaSe since this is insensitive) + Input: "/sUbScRiPtIoNs/12345678-1234-9876-4563-123456789012/rEsOuRcEgRoUpS/eXaMpLe-rEsOuRcE-GrOuP/pRoViDeRs/mIcRoSoFt.mApS/aCcOuNtS/aCcOuNtVaLuE/cReAtOrS/cReAtOrVaLuE/extra", + Error: true, + }, + } + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Input) + + actual, err := ParseCreatorIDInsensitively(v.Input) + if err != nil { + if v.Error { + continue + } + + t.Fatalf("Expect a value but got an error: %+v", 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.ResourceGroupName != v.Expected.ResourceGroupName { + t.Fatalf("Expected %q but got %q for ResourceGroupName", v.Expected.ResourceGroupName, actual.ResourceGroupName) + } + + if actual.AccountName != v.Expected.AccountName { + t.Fatalf("Expected %q but got %q for AccountName", v.Expected.AccountName, actual.AccountName) + } + + if actual.CreatorName != v.Expected.CreatorName { + t.Fatalf("Expected %q but got %q for CreatorName", v.Expected.CreatorName, actual.CreatorName) + } + + } +} diff --git a/internal/services/maps/sdk/2021-02-01/creators/method_createorupdate_autorest.go b/internal/services/maps/sdk/2021-02-01/creators/method_createorupdate_autorest.go new file mode 100644 index 000000000000..acd0ca7ba9b2 --- /dev/null +++ b/internal/services/maps/sdk/2021-02-01/creators/method_createorupdate_autorest.go @@ -0,0 +1,65 @@ +package creators + +import ( + "context" + "net/http" + + "github.com/Azure/go-autorest/autorest" + "github.com/Azure/go-autorest/autorest/azure" +) + +type CreateOrUpdateResponse struct { + HttpResponse *http.Response + Model *Creator +} + +// CreateOrUpdate ... +func (c CreatorsClient) CreateOrUpdate(ctx context.Context, id CreatorId, input Creator) (result CreateOrUpdateResponse, err error) { + req, err := c.preparerForCreateOrUpdate(ctx, id, input) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "CreateOrUpdate", nil, "Failure preparing request") + return + } + + result.HttpResponse, err = c.Client.Send(req, azure.DoRetryWithRegistration(c.Client)) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "CreateOrUpdate", result.HttpResponse, "Failure sending request") + return + } + + result, err = c.responderForCreateOrUpdate(result.HttpResponse) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "CreateOrUpdate", result.HttpResponse, "Failure responding to request") + return + } + + return +} + +// preparerForCreateOrUpdate prepares the CreateOrUpdate request. +func (c CreatorsClient) preparerForCreateOrUpdate(ctx context.Context, id CreatorId, input Creator) (*http.Request, error) { + queryParameters := map[string]interface{}{ + "api-version": defaultApiVersion, + } + + preparer := autorest.CreatePreparer( + autorest.AsContentType("application/json; charset=utf-8"), + autorest.AsPut(), + autorest.WithBaseURL(c.baseUri), + autorest.WithPath(id.ID()), + autorest.WithJSON(input), + autorest.WithQueryParameters(queryParameters)) + return preparer.Prepare((&http.Request{}).WithContext(ctx)) +} + +// responderForCreateOrUpdate handles the response to the CreateOrUpdate request. The method always +// closes the http.Response Body. +func (c CreatorsClient) responderForCreateOrUpdate(resp *http.Response) (result CreateOrUpdateResponse, err error) { + err = autorest.Respond( + resp, + azure.WithErrorUnlessStatusCode(http.StatusCreated, http.StatusOK), + autorest.ByUnmarshallingJSON(&result.Model), + autorest.ByClosing()) + result.HttpResponse = resp + return +} diff --git a/internal/services/maps/sdk/2021-02-01/creators/method_delete_autorest.go b/internal/services/maps/sdk/2021-02-01/creators/method_delete_autorest.go new file mode 100644 index 000000000000..808983262e61 --- /dev/null +++ b/internal/services/maps/sdk/2021-02-01/creators/method_delete_autorest.go @@ -0,0 +1,61 @@ +package creators + +import ( + "context" + "net/http" + + "github.com/Azure/go-autorest/autorest" + "github.com/Azure/go-autorest/autorest/azure" +) + +type DeleteResponse struct { + HttpResponse *http.Response +} + +// Delete ... +func (c CreatorsClient) Delete(ctx context.Context, id CreatorId) (result DeleteResponse, err error) { + req, err := c.preparerForDelete(ctx, id) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "Delete", nil, "Failure preparing request") + return + } + + result.HttpResponse, err = c.Client.Send(req, azure.DoRetryWithRegistration(c.Client)) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "Delete", result.HttpResponse, "Failure sending request") + return + } + + result, err = c.responderForDelete(result.HttpResponse) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "Delete", result.HttpResponse, "Failure responding to request") + return + } + + return +} + +// preparerForDelete prepares the Delete request. +func (c CreatorsClient) preparerForDelete(ctx context.Context, id CreatorId) (*http.Request, error) { + queryParameters := map[string]interface{}{ + "api-version": defaultApiVersion, + } + + preparer := autorest.CreatePreparer( + autorest.AsDelete(), + autorest.WithBaseURL(c.baseUri), + autorest.WithPath(id.ID()), + autorest.WithQueryParameters(queryParameters)) + return preparer.Prepare((&http.Request{}).WithContext(ctx)) +} + +// responderForDelete handles the response to the Delete request. The method always +// closes the http.Response Body. +func (c CreatorsClient) responderForDelete(resp *http.Response) (result DeleteResponse, err error) { + err = autorest.Respond( + resp, + azure.WithErrorUnlessStatusCode(http.StatusNoContent, http.StatusOK), + autorest.ByClosing()) + result.HttpResponse = resp + return +} diff --git a/internal/services/maps/sdk/2021-02-01/creators/method_get_autorest.go b/internal/services/maps/sdk/2021-02-01/creators/method_get_autorest.go new file mode 100644 index 000000000000..cf25918e10f9 --- /dev/null +++ b/internal/services/maps/sdk/2021-02-01/creators/method_get_autorest.go @@ -0,0 +1,64 @@ +package creators + +import ( + "context" + "net/http" + + "github.com/Azure/go-autorest/autorest" + "github.com/Azure/go-autorest/autorest/azure" +) + +type GetResponse struct { + HttpResponse *http.Response + Model *Creator +} + +// Get ... +func (c CreatorsClient) Get(ctx context.Context, id CreatorId) (result GetResponse, err error) { + req, err := c.preparerForGet(ctx, id) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "Get", nil, "Failure preparing request") + return + } + + result.HttpResponse, err = c.Client.Send(req, azure.DoRetryWithRegistration(c.Client)) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "Get", result.HttpResponse, "Failure sending request") + return + } + + result, err = c.responderForGet(result.HttpResponse) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "Get", result.HttpResponse, "Failure responding to request") + return + } + + return +} + +// preparerForGet prepares the Get request. +func (c CreatorsClient) preparerForGet(ctx context.Context, id CreatorId) (*http.Request, error) { + queryParameters := map[string]interface{}{ + "api-version": defaultApiVersion, + } + + preparer := autorest.CreatePreparer( + autorest.AsContentType("application/json; charset=utf-8"), + autorest.AsGet(), + autorest.WithBaseURL(c.baseUri), + autorest.WithPath(id.ID()), + autorest.WithQueryParameters(queryParameters)) + return preparer.Prepare((&http.Request{}).WithContext(ctx)) +} + +// responderForGet handles the response to the Get request. The method always +// closes the http.Response Body. +func (c CreatorsClient) responderForGet(resp *http.Response) (result GetResponse, err error) { + err = autorest.Respond( + resp, + azure.WithErrorUnlessStatusCode(http.StatusOK), + autorest.ByUnmarshallingJSON(&result.Model), + autorest.ByClosing()) + result.HttpResponse = resp + return +} diff --git a/internal/services/maps/sdk/2021-02-01/creators/method_listbyaccount_autorest.go b/internal/services/maps/sdk/2021-02-01/creators/method_listbyaccount_autorest.go new file mode 100644 index 000000000000..0cf1e097cafa --- /dev/null +++ b/internal/services/maps/sdk/2021-02-01/creators/method_listbyaccount_autorest.go @@ -0,0 +1,183 @@ +package creators + +import ( + "context" + "fmt" + "net/http" + "net/url" + + "github.com/Azure/go-autorest/autorest" + "github.com/Azure/go-autorest/autorest/azure" +) + +type ListByAccountResponse struct { + HttpResponse *http.Response + Model *[]Creator + + nextLink *string + nextPageFunc func(ctx context.Context, nextLink string) (ListByAccountResponse, error) +} + +type ListByAccountCompleteResult struct { + Items []Creator +} + +func (r ListByAccountResponse) HasMore() bool { + return r.nextLink != nil +} + +func (r ListByAccountResponse) LoadMore(ctx context.Context) (resp ListByAccountResponse, err error) { + if !r.HasMore() { + err = fmt.Errorf("no more pages returned") + return + } + return r.nextPageFunc(ctx, *r.nextLink) +} + +// ListByAccount ... +func (c CreatorsClient) ListByAccount(ctx context.Context, id AccountId) (resp ListByAccountResponse, err error) { + req, err := c.preparerForListByAccount(ctx, id) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "ListByAccount", nil, "Failure preparing request") + return + } + + resp.HttpResponse, err = c.Client.Send(req, azure.DoRetryWithRegistration(c.Client)) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "ListByAccount", resp.HttpResponse, "Failure sending request") + return + } + + resp, err = c.responderForListByAccount(resp.HttpResponse) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "ListByAccount", resp.HttpResponse, "Failure responding to request") + return + } + return +} + +// ListByAccountComplete retrieves all of the results into a single object +func (c CreatorsClient) ListByAccountComplete(ctx context.Context, id AccountId) (ListByAccountCompleteResult, error) { + return c.ListByAccountCompleteMatchingPredicate(ctx, id, CreatorPredicate{}) +} + +// ListByAccountCompleteMatchingPredicate retrieves all of the results and then applied the predicate +func (c CreatorsClient) ListByAccountCompleteMatchingPredicate(ctx context.Context, id AccountId, predicate CreatorPredicate) (resp ListByAccountCompleteResult, err error) { + items := make([]Creator, 0) + + page, err := c.ListByAccount(ctx, id) + if err != nil { + err = fmt.Errorf("loading the initial page: %+v", err) + return + } + if page.Model != nil { + for _, v := range *page.Model { + if predicate.Matches(v) { + items = append(items, v) + } + } + } + + for page.HasMore() { + page, err = page.LoadMore(ctx) + if err != nil { + err = fmt.Errorf("loading the next page: %+v", err) + return + } + + if page.Model != nil { + for _, v := range *page.Model { + if predicate.Matches(v) { + items = append(items, v) + } + } + } + } + + out := ListByAccountCompleteResult{ + Items: items, + } + return out, nil +} + +// preparerForListByAccount prepares the ListByAccount request. +func (c CreatorsClient) preparerForListByAccount(ctx context.Context, id AccountId) (*http.Request, error) { + queryParameters := map[string]interface{}{ + "api-version": defaultApiVersion, + } + + preparer := autorest.CreatePreparer( + autorest.AsContentType("application/json; charset=utf-8"), + autorest.AsGet(), + autorest.WithBaseURL(c.baseUri), + autorest.WithPath(fmt.Sprintf("%s/creators", id.ID())), + autorest.WithQueryParameters(queryParameters)) + return preparer.Prepare((&http.Request{}).WithContext(ctx)) +} + +// preparerForListByAccountWithNextLink prepares the ListByAccount request with the given nextLink token. +func (c CreatorsClient) preparerForListByAccountWithNextLink(ctx context.Context, nextLink string) (*http.Request, error) { + uri, err := url.Parse(nextLink) + if err != nil { + return nil, fmt.Errorf("parsing nextLink %q: %+v", nextLink, err) + } + queryParameters := map[string]interface{}{} + for k, v := range uri.Query() { + if len(v) == 0 { + continue + } + val := v[0] + val = autorest.Encode("query", val) + queryParameters[k] = val + } + + preparer := autorest.CreatePreparer( + autorest.AsContentType("application/json; charset=utf-8"), + autorest.AsGet(), + autorest.WithBaseURL(c.baseUri), + autorest.WithPath(uri.Path), + autorest.WithQueryParameters(queryParameters)) + return preparer.Prepare((&http.Request{}).WithContext(ctx)) +} + +// responderForListByAccount handles the response to the ListByAccount request. The method always +// closes the http.Response Body. +func (c CreatorsClient) responderForListByAccount(resp *http.Response) (result ListByAccountResponse, err error) { + type page struct { + Values []Creator `json:"value"` + NextLink *string `json:"nextLink"` + } + var respObj page + err = autorest.Respond( + resp, + azure.WithErrorUnlessStatusCode(http.StatusOK), + autorest.ByUnmarshallingJSON(&respObj), + autorest.ByClosing()) + result.HttpResponse = resp + result.Model = &respObj.Values + result.nextLink = respObj.NextLink + if respObj.NextLink != nil { + result.nextPageFunc = func(ctx context.Context, nextLink string) (result ListByAccountResponse, err error) { + req, err := c.preparerForListByAccountWithNextLink(ctx, nextLink) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "ListByAccount", nil, "Failure preparing request") + return + } + + result.HttpResponse, err = c.Client.Send(req, azure.DoRetryWithRegistration(c.Client)) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "ListByAccount", result.HttpResponse, "Failure sending request") + return + } + + result, err = c.responderForListByAccount(result.HttpResponse) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "ListByAccount", result.HttpResponse, "Failure responding to request") + return + } + + return + } + } + return +} diff --git a/internal/services/maps/sdk/2021-02-01/creators/method_update_autorest.go b/internal/services/maps/sdk/2021-02-01/creators/method_update_autorest.go new file mode 100644 index 000000000000..131d64d6ec47 --- /dev/null +++ b/internal/services/maps/sdk/2021-02-01/creators/method_update_autorest.go @@ -0,0 +1,65 @@ +package creators + +import ( + "context" + "net/http" + + "github.com/Azure/go-autorest/autorest" + "github.com/Azure/go-autorest/autorest/azure" +) + +type UpdateResponse struct { + HttpResponse *http.Response + Model *Creator +} + +// Update ... +func (c CreatorsClient) Update(ctx context.Context, id CreatorId, input CreatorUpdateParameters) (result UpdateResponse, err error) { + req, err := c.preparerForUpdate(ctx, id, input) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "Update", nil, "Failure preparing request") + return + } + + result.HttpResponse, err = c.Client.Send(req, azure.DoRetryWithRegistration(c.Client)) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "Update", result.HttpResponse, "Failure sending request") + return + } + + result, err = c.responderForUpdate(result.HttpResponse) + if err != nil { + err = autorest.NewErrorWithError(err, "creators.CreatorsClient", "Update", result.HttpResponse, "Failure responding to request") + return + } + + return +} + +// preparerForUpdate prepares the Update request. +func (c CreatorsClient) preparerForUpdate(ctx context.Context, id CreatorId, input CreatorUpdateParameters) (*http.Request, error) { + queryParameters := map[string]interface{}{ + "api-version": defaultApiVersion, + } + + preparer := autorest.CreatePreparer( + autorest.AsContentType("application/json; charset=utf-8"), + autorest.AsPatch(), + autorest.WithBaseURL(c.baseUri), + autorest.WithPath(id.ID()), + autorest.WithJSON(input), + autorest.WithQueryParameters(queryParameters)) + return preparer.Prepare((&http.Request{}).WithContext(ctx)) +} + +// responderForUpdate handles the response to the Update request. The method always +// closes the http.Response Body. +func (c CreatorsClient) responderForUpdate(resp *http.Response) (result UpdateResponse, err error) { + err = autorest.Respond( + resp, + azure.WithErrorUnlessStatusCode(http.StatusOK), + autorest.ByUnmarshallingJSON(&result.Model), + autorest.ByClosing()) + result.HttpResponse = resp + return +} diff --git a/internal/services/maps/sdk/2021-02-01/creators/model_creator.go b/internal/services/maps/sdk/2021-02-01/creators/model_creator.go new file mode 100644 index 000000000000..c21ffc6c4023 --- /dev/null +++ b/internal/services/maps/sdk/2021-02-01/creators/model_creator.go @@ -0,0 +1,10 @@ +package creators + +type Creator struct { + Id *string `json:"id,omitempty"` + Location string `json:"location"` + Name *string `json:"name,omitempty"` + Properties CreatorProperties `json:"properties"` + Tags *map[string]string `json:"tags,omitempty"` + Type *string `json:"type,omitempty"` +} diff --git a/internal/services/maps/sdk/2021-02-01/creators/model_creatorproperties.go b/internal/services/maps/sdk/2021-02-01/creators/model_creatorproperties.go new file mode 100644 index 000000000000..29911060ce26 --- /dev/null +++ b/internal/services/maps/sdk/2021-02-01/creators/model_creatorproperties.go @@ -0,0 +1,6 @@ +package creators + +type CreatorProperties struct { + ProvisioningState *string `json:"provisioningState,omitempty"` + StorageUnits int64 `json:"storageUnits"` +} diff --git a/internal/services/maps/sdk/2021-02-01/creators/model_creatorupdateparameters.go b/internal/services/maps/sdk/2021-02-01/creators/model_creatorupdateparameters.go new file mode 100644 index 000000000000..9642bcb0d329 --- /dev/null +++ b/internal/services/maps/sdk/2021-02-01/creators/model_creatorupdateparameters.go @@ -0,0 +1,6 @@ +package creators + +type CreatorUpdateParameters struct { + Properties *CreatorProperties `json:"properties,omitempty"` + Tags *map[string]string `json:"tags,omitempty"` +} diff --git a/internal/services/maps/sdk/2021-02-01/creators/predicates.go b/internal/services/maps/sdk/2021-02-01/creators/predicates.go new file mode 100644 index 000000000000..5094dec93f85 --- /dev/null +++ b/internal/services/maps/sdk/2021-02-01/creators/predicates.go @@ -0,0 +1,29 @@ +package creators + +type CreatorPredicate struct { + Id *string + Location *string + Name *string + Type *string +} + +func (p CreatorPredicate) Matches(input Creator) bool { + + if p.Id != nil && (input.Id == nil && *p.Id != *input.Id) { + return false + } + + if p.Location != nil && *p.Location != input.Location { + return false + } + + if p.Name != nil && (input.Name == nil && *p.Name != *input.Name) { + return false + } + + if p.Type != nil && (input.Type == nil && *p.Type != *input.Type) { + return false + } + + return true +} diff --git a/internal/services/maps/sdk/2021-02-01/creators/version.go b/internal/services/maps/sdk/2021-02-01/creators/version.go new file mode 100644 index 000000000000..a938be04aeb5 --- /dev/null +++ b/internal/services/maps/sdk/2021-02-01/creators/version.go @@ -0,0 +1,9 @@ +package creators + +import "fmt" + +const defaultApiVersion = "2021-02-01" + +func userAgent() string { + return fmt.Sprintf("pandora/creators/%s", defaultApiVersion) +}