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

Add support for Privileged Identity Management Groups #248

Closed
wants to merge 22 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add Role Management Policies functions
  • Loading branch information
oWretch committed Jul 18, 2023
commit 2367a86a3c70010e803fba7ccb70d9423b75f012
101 changes: 101 additions & 0 deletions msgraph/role_management_policies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package msgraph

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/hashicorp/go-azure-sdk/sdk/odata"
)

type RoleManagementPolicyClient struct {
BaseClient Client
}

func NewRoleManagementPolicyClient() *RoleManagementPolicyClient {
return &RoleManagementPolicyClient{
BaseClient: NewClient(VersionBeta),
}
}

// List retrieves a list of Role Management Policies
func (c *RoleManagementPolicyClient) List(ctx context.Context, query odata.Query) (*[]UnifiedRoleManagementPolicy, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
OData: query,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: "/policies/roleManagementPolicies",
},
})
if err != nil {
return nil, status, fmt.Errorf("RoleManagementPolicyClient.BaseClient.Get(): %v", err)
}

defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}

var data struct {
UnifiedRoleManagementPolicy []UnifiedRoleManagementPolicy `json:"value"`
}
if err := json.Unmarshal(respBody, &data); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}

return &data.UnifiedRoleManagementPolicy, status, nil
}

// Get retrieves a UnifiedRoleManagementPolicy
func (c *RoleManagementPolicyClient) Get(ctx context.Context, id string, query odata.Query) (*UnifiedRoleManagementPolicy, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
OData: query,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/policies/roleManagementPolicies/%s", id),
},
})
if err != nil {
return nil, status, fmt.Errorf("RoleDefinitionsClient.BaseClient.Get(): %v", err)
}

defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}

var policy UnifiedRoleManagementPolicy
if err := json.Unmarshal(respBody, &policy); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}

return &policy, status, nil
}

// Update amends an existing UnifiedRoleManagementPolicy.
func (c *RoleManagementPolicyClient) Update(ctx context.Context, policy UnifiedRoleManagementPolicy) (int, error) {
var status int

body, err := json.Marshal(policy)
if err != nil {
return status, fmt.Errorf("json.Marshal(): %v", err)
}

_, status, _, err = c.BaseClient.Patch(ctx, PatchHttpRequestInput{
Body: body,
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusNoContent},
Uri: Uri{
Entity: fmt.Sprintf("/roleManagement/directory/roleDefinitions/%s", *policy.ID),
},
})
if err != nil {
return status, fmt.Errorf("RoleDefinitionsClient.BaseClient.Patch(): %v", err)
}

return status, nil
}