-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
#22398
Merged
stephybun
merged 14 commits into
hashicorp:main
from
ms-zhenhua:role-assignment-marketplace2
Jul 24, 2023
+3,273
−2
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7269053
add code
ms-zhenhua 0eb384a
Merge branch 'main' of https://github.com/hashicorp/terraform-provide…
ms-zhenhua b3ba4c9
add code
ms-zhenhua 1c04118
format
ms-zhenhua 793c8ee
Merge branch 'main' of https://github.com/hashicorp/terraform-provide…
ms-zhenhua 50ac69f
fix build
ms-zhenhua 2ee9426
fix test
ms-zhenhua 7a2b1df
fix lint
ms-zhenhua 098fb9a
fix lint
ms-zhenhua a593c41
Merge branch 'main' of https://github.com/hashicorp/terraform-provide…
ms-zhenhua 6086609
resolve comments
ms-zhenhua fb80fff
rename `azurerm_role_assignment_marketplace` to `azurerm_marketplace_…
ms-zhenhua 74d5177
Merge branch 'main' of https://github.com/hashicorp/terraform-provide…
ms-zhenhua 726aaaf
fix gen check
ms-zhenhua File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
internal/services/authorization/parse/id_scopedroleassignment.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package parse | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2022-04-01/roleassignments" | ||
) | ||
|
||
type ScopedRoleAssignmentId struct { | ||
ScopedId roleassignments.ScopedRoleAssignmentId | ||
TenantId string | ||
} | ||
|
||
func NewScopedRoleAssignmentID(scope string, roleAssignmentName string, tenantId string) ScopedRoleAssignmentId { | ||
return ScopedRoleAssignmentId{ | ||
ScopedId: roleassignments.NewScopedRoleAssignmentID(scope, roleAssignmentName), | ||
TenantId: tenantId, | ||
} | ||
} | ||
|
||
func ScopedRoleAssignmentID(input string) (*ScopedRoleAssignmentId, error) { | ||
azureResourceId, tenantId := DestructRoleAssignmentId(input) | ||
scopedId, err := roleassignments.ParseScopedRoleAssignmentID(azureResourceId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &ScopedRoleAssignmentId{ScopedId: *scopedId, TenantId: tenantId}, nil | ||
} | ||
|
||
func ValidateScopedRoleAssignmentID(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 := ScopedRoleAssignmentID(v); err != nil { | ||
errors = append(errors, err) | ||
} | ||
|
||
return | ||
} | ||
|
||
func (id ScopedRoleAssignmentId) ID() string { | ||
return ConstructRoleAssignmentId(id.ScopedId.ID(), id.TenantId) | ||
} | ||
|
||
func (id ScopedRoleAssignmentId) String() string { | ||
components := []string{ | ||
id.ScopedId.String(), | ||
} | ||
|
||
if id.TenantId != "" { | ||
components = append(components, fmt.Sprintf("Tenant ID: %s", id.TenantId)) | ||
} | ||
|
||
return fmt.Sprintf("Scoped Role Assignment (%s)", strings.Join(components, "\n")) | ||
} |
124 changes: 124 additions & 0 deletions
124
internal/services/authorization/parse/id_scopedroleassignment_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package parse | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/go-azure-sdk/resource-manager/authorization/2022-04-01/roleassignments" | ||
) | ||
|
||
func TestScopedRoleAssignmentID(t *testing.T) { | ||
testData := []struct { | ||
Input string | ||
Error bool | ||
Expected *ScopedRoleAssignmentId | ||
}{ | ||
{ | ||
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", | ||
Expected: &ScopedRoleAssignmentId{ | ||
ScopedId: roleassignments.NewScopedRoleAssignmentID("/providers/Microsoft.Subscription", "23456781-2349-8764-5631-234567890121"), | ||
}, | ||
}, | ||
|
||
{ | ||
Input: "/providers/Microsoft.Marketplace/providers/Microsoft.Authorization/roleAssignments/23456781-2349-8764-5631-234567890121", | ||
Expected: &ScopedRoleAssignmentId{ | ||
ScopedId: roleassignments.NewScopedRoleAssignmentID("/providers/Microsoft.Marketplace", "23456781-2349-8764-5631-234567890121"), | ||
}, | ||
}, | ||
|
||
{ | ||
Input: "/providers/Microsoft.Marketplace/providers/Microsoft.Authorization/roleAssignments/23456781-2349-8764-5631-234567890121|12345678-1234-5678-1234-567890123456", | ||
Expected: &ScopedRoleAssignmentId{ | ||
ScopedId: roleassignments.NewScopedRoleAssignmentID("/providers/Microsoft.Marketplace", "23456781-2349-8764-5631-234567890121"), | ||
TenantId: "12345678-1234-5678-1234-567890123456", | ||
}, | ||
}, | ||
} | ||
|
||
for _, v := range testData { | ||
t.Logf("[DEBUG] Testing %q", v.Input) | ||
|
||
actual, err := ScopedRoleAssignmentID(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.ScopedId.RoleAssignmentName != v.Expected.ScopedId.RoleAssignmentName { | ||
t.Fatalf("Expected %q but got %q for Role Assignment Name", v.Expected.ScopedId.RoleAssignmentName, actual.ScopedId.RoleAssignmentName) | ||
} | ||
|
||
if actual.TenantId != v.Expected.TenantId { | ||
t.Fatalf("Expected %q but got %q for Tenant ID", v.Expected.TenantId, actual.TenantId) | ||
} | ||
} | ||
} | ||
|
||
func TestValidateScopedRoleAssignmentID(t *testing.T) { | ||
cases := []struct { | ||
Input string | ||
Valid bool | ||
}{ | ||
|
||
{ | ||
Input: "", | ||
Valid: false, | ||
}, | ||
|
||
{ | ||
Input: "/", | ||
Valid: false, | ||
}, | ||
|
||
{ | ||
Input: "/providers/Microsoft.Marketplace/providers/Microsoft.Authorization/roleAssignments/", | ||
Valid: false, | ||
}, | ||
|
||
{ | ||
Input: "/providers/Microsoft.Subscription/providers/Microsoft.Authorization/roleAssignments/23456781-2349-8764-5631-234567890121", | ||
Valid: true, | ||
}, | ||
|
||
{ | ||
Input: "/providers/Microsoft.Marketplace/providers/Microsoft.Authorization/roleAssignments/23456781-2349-8764-5631-234567890121", | ||
Valid: true, | ||
}, | ||
|
||
{ | ||
Input: "/providers/Microsoft.Marketplace/providers/Microsoft.Authorization/roleAssignments/23456781-2349-8764-5631-234567890121|12345678-1234-5678-1234-567890123456", | ||
Valid: true, | ||
}, | ||
} | ||
for _, tc := range cases { | ||
t.Logf("[DEBUG] Testing Value %s", tc.Input) | ||
_, errors := ValidateScopedRoleAssignmentID(tc.Input, "test") | ||
valid := len(errors) == 0 | ||
|
||
if tc.Valid != valid { | ||
t.Fatalf("Expected %t but got %t", tc.Valid, valid) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
internal/services/authorization/role_assignment_marketplace_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package authorization | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/authorization/parse" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
) | ||
|
||
const ( | ||
MarketplaceScope = "/providers/Microsoft.Marketplace" | ||
) | ||
|
||
var _ sdk.Resource = RoleAssignmentMarketplaceResource{} | ||
|
||
type RoleAssignmentMarketplaceResource struct { | ||
base roleAssignmentBaseResource | ||
} | ||
|
||
func (r RoleAssignmentMarketplaceResource) Arguments() map[string]*pluginsdk.Schema { | ||
return r.base.arguments() | ||
} | ||
|
||
func (r RoleAssignmentMarketplaceResource) Attributes() map[string]*pluginsdk.Schema { | ||
return r.base.attributes() | ||
} | ||
|
||
func (r RoleAssignmentMarketplaceResource) Create() sdk.ResourceFunc { | ||
return r.base.createFunc(r.ResourceType(), MarketplaceScope) | ||
} | ||
|
||
func (r RoleAssignmentMarketplaceResource) Delete() sdk.ResourceFunc { | ||
return r.base.deleteFunc() | ||
} | ||
|
||
func (r RoleAssignmentMarketplaceResource) Read() sdk.ResourceFunc { | ||
return r.base.readFunc(MarketplaceScope, true) | ||
} | ||
|
||
func (r RoleAssignmentMarketplaceResource) ResourceType() string { | ||
return "azurerm_marketplace_role_assignment" | ||
} | ||
|
||
func (r RoleAssignmentMarketplaceResource) ModelObject() interface{} { | ||
return nil | ||
} | ||
|
||
func (r RoleAssignmentMarketplaceResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||
return parse.ValidateScopedRoleAssignmentID | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After internal discussion, this is fine for now. But we will want to create a composite ID type in the
commonids
package in thego-azure-helpers
repo that can handle cases like these and will replace this in time. Just as an FYI.