Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Resource: azurerm_role_assignment_marketplace #22178

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
applicationinsights.Registration{},
appservice.Registration{},
arckubernetes.Registration{},
authorization.Registration{},
automanage.Registration{},
automation.Registration{},
batch.Registration{},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package parse

import (
"fmt"
"strings"
)

type RoleAssignmentMarketplaceId struct {
Name string
TenantId string
}

func NewRoleAssignmentMarketplaceID(name, tenantID string) RoleAssignmentMarketplaceId {
return RoleAssignmentMarketplaceId{
Name: name,
TenantId: tenantID,
}
}

func (id RoleAssignmentMarketplaceId) AzureResourceID() string {
return fmt.Sprintf("/providers/Microsoft.Marketplace/providers/Microsoft.Authorization/roleAssignments/%s", id.Name)
}

func (id RoleAssignmentMarketplaceId) ID() string {
return ConstructRoleAssignmentId(id.AzureResourceID(), id.TenantId)
}

func (id RoleAssignmentMarketplaceId) String() string {
components := []string{
fmt.Sprintf("Name: %q", id.Name),
fmt.Sprintf("TenantId: %q", id.TenantId),
}

return fmt.Sprintf("Role Assignment Marketplace (%s)", strings.Join(components, "\n"))
}

func ValidateRoleAssignmentMarketplaceID(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 := RoleAssignmentMarketplaceID(v); err != nil {
errors = append(errors, err)
}

return
}

func RoleAssignmentMarketplaceID(input string) (*RoleAssignmentMarketplaceId, error) {
if len(input) == 0 {
return nil, fmt.Errorf("Role Assignment Marketplace ID is empty string")
}

roleAssignmentId := RoleAssignmentMarketplaceId{}

parts := strings.Split(input, "|")
if len(parts) == 2 {
input = parts[0]
roleAssignmentId.TenantId = parts[1]
}

idParts := strings.Split(input, "/providers/Microsoft.Authorization/roleAssignments/")
if len(idParts) != 2 {
return nil, fmt.Errorf("could not parse Role Assignment Marketplace ID %q", input)
}

if idParts[0] != "/providers/Microsoft.Marketplace" {
return nil, fmt.Errorf("resource provider %s is invalid", idParts[0])
}

if idParts[1] == "" {
return nil, fmt.Errorf("ID was missing a value for the roleAssignments element")
}

roleAssignmentId.Name = idParts[1]

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

import "testing"

func TestRoleAssignmentMarketplaceID(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *RoleAssignmentMarketplaceId
}{
{
Input: "",
Error: true,
},

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

{
Input: "/providers/Microsoft.Marketplace/providers/Microsoft.Authorization/roleAssignments/",
Error: true,
},

{
Input: "/providers/Microsoft.Subscription/providers/Microsoft.Authorization/roleAssignments/23456781-2349-8764-5631-234567890121",
Error: true,
},

{
Input: "/providers/Microsoft.Marketplace/providers/Microsoft.Authorization/roleAssignments/23456781-2349-8764-5631-234567890121",
Expected: &RoleAssignmentMarketplaceId{
Name: "23456781-2349-8764-5631-234567890121",
},
},

{
Input: "/providers/Microsoft.Marketplace/providers/Microsoft.Authorization/roleAssignments/23456781-2349-8764-5631-234567890121|12345678-1234-5678-1234-567890123456",
ms-zhenhua marked this conversation as resolved.
Show resolved Hide resolved
Expected: &RoleAssignmentMarketplaceId{
Name: "23456781-2349-8764-5631-234567890121",
TenantId: "12345678-1234-5678-1234-567890123456",
},
},
}

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

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

t.Fatalf("expected a value but got an error: %+v", err)
}

if v.Error {
t.Fatal("Expect an error but didn't get one")
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Role Assignment Name", v.Expected.Name, actual.Name)
}
}
}
15 changes: 14 additions & 1 deletion internal/services/authorization/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (

type Registration struct{}

var _ sdk.UntypedServiceRegistrationWithAGitHubLabel = Registration{}
var (
_ sdk.TypedServiceRegistration = Registration{}
_ sdk.UntypedServiceRegistrationWithAGitHubLabel = Registration{}
)

func (r Registration) AssociatedGitHubLabel() string {
return "service/authorization"
Expand Down Expand Up @@ -40,3 +43,13 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource {
"azurerm_role_definition": resourceArmRoleDefinition(),
}
}

func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{}
}

func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
RoleAssignmentMarketplaceResource{},
}
}
Loading