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

Role Assignment/Definition: parsing the Resource ID's #1887

Merged
merged 2 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 24 additions & 3 deletions azurerm/resource_arm_role_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,12 @@ func resourceArmRoleAssignmentDelete(d *schema.ResourceData, meta interface{}) e
client := meta.(*ArmClient).roleAssignmentsClient
ctx := meta.(*ArmClient).StopContext

scope := d.Get("scope").(string)
name := d.Get("name").(string)
id, err := parseRoleAssignmentId(d.Id())
if err != nil {
return err
}

resp, err := client.Delete(ctx, scope, name)
resp, err := client.Delete(ctx, id.scope, id.name)
if err != nil {
if !utils.ResponseWasNotFound(resp.Response) {
return err
Expand Down Expand Up @@ -194,3 +196,22 @@ func retryRoleAssignmentsClient(scope string, name string, properties authorizat

}
}

type roleAssignmentId struct {
scope string
name string
}

func parseRoleAssignmentId(input string) (*roleAssignmentId, error) {
segments := strings.Split(input, "/providers/Microsoft.Authorization/roleAssignments/")
if len(segments) != 2 {
return nil, fmt.Errorf("Expected Role Assignment ID to be in the format `{scope}/providers/Microsoft.Authorization/roleAssignments/{name}` but got %q", input)
}

// /{scope}/providers/Microsoft.Authorization/roleAssignments/{roleAssignmentName}
id := roleAssignmentId{
scope: strings.TrimPrefix(segments[0], "/"),
name: segments[1],
}
return &id, nil
}
34 changes: 27 additions & 7 deletions azurerm/resource_arm_role_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package azurerm
import (
"fmt"
"log"
"strings"

"github.com/Azure/azure-sdk-for-go/services/preview/authorization/mgmt/2018-01-01-preview/authorization"
"github.com/hashicorp/go-uuid"
Expand Down Expand Up @@ -163,16 +164,16 @@ func resourceArmRoleDefinitionDelete(d *schema.ResourceData, meta interface{}) e
client := meta.(*ArmClient).roleDefinitionsClient
ctx := meta.(*ArmClient).StopContext

roleDefinitionId := d.Get("role_definition_id").(string)
scope := d.Get("scope").(string)
id, err := parseRoleDefinitionId(d.Id())
if err != nil {
return err
}

resp, err := client.Delete(ctx, scope, roleDefinitionId)
resp, err := client.Delete(ctx, id.scope, id.roleDefinitionId)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return nil
if !utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error deleting Role Definition %q at Scope %q: %+v", id.roleDefinitionId, id.scope, err)
}

return fmt.Errorf("Error deleting Role Definition %q: %+v", roleDefinitionId, err)
}

return nil
Expand Down Expand Up @@ -254,3 +255,22 @@ func flattenRoleDefinitionAssignableScopes(input *[]string) []interface{} {

return scopes
}

type roleDefinitionId struct {
scope string
roleDefinitionId string
}

func parseRoleDefinitionId(input string) (*roleDefinitionId, error) {
segments := strings.Split(input, "/providers/Microsoft.Authorization/roleDefinitions/")
if len(segments) != 2 {
return nil, fmt.Errorf("Expected Role Definition ID to be in the format `{scope}/providers/Microsoft.Authorization/roleDefinitions/{name}` but got %q", input)
}

// /{scope}/providers/Microsoft.Authorization/roleDefinitions/{roleDefinitionId}
id := roleDefinitionId{
scope: strings.TrimPrefix(segments[0], "/"),
roleDefinitionId: segments[1],
}
return &id, nil
}