-
Notifications
You must be signed in to change notification settings - Fork 62
/
role_definitions.go
153 lines (129 loc) · 4.36 KB
/
role_definitions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package msgraph
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
)
// RoleDefinitionsClient performs operations on RoleDefinitions.
type RoleDefinitionsClient struct {
BaseClient Client
}
// NewRoleDefinitionsClient returns a new RoleDefinitionsClient
func NewRoleDefinitionsClient() *RoleDefinitionsClient {
return &RoleDefinitionsClient{
BaseClient: NewClient(Version10),
}
}
// List returns a list of RoleDefinitions
func (c *RoleDefinitionsClient) List(ctx context.Context, query odata.Query) (*[]UnifiedRoleDefinition, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
OData: query,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: "/roleManagement/directory/roleDefinitions",
},
})
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 data struct {
RoleDefinitions []UnifiedRoleDefinition `json:"value"`
}
if err := json.Unmarshal(respBody, &data); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &data.RoleDefinitions, status, nil
}
// Get retrieves a UnifiedRoleDefinition
func (c *RoleDefinitionsClient) Get(ctx context.Context, id string, query odata.Query) (*UnifiedRoleDefinition, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
OData: query,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/roleManagement/directory/roleDefinitions/%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 dirRole UnifiedRoleDefinition
if err := json.Unmarshal(respBody, &dirRole); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &dirRole, status, nil
}
// Create creates a new UnifiedRoleDefinition.
func (c *RoleDefinitionsClient) Create(ctx context.Context, roleDefinition UnifiedRoleDefinition) (*UnifiedRoleDefinition, int, error) {
var status int
body, err := json.Marshal(roleDefinition)
if err != nil {
return nil, status, fmt.Errorf("json.Marshal(): %v", err)
}
resp, status, _, err := c.BaseClient.Post(ctx, PostHttpRequestInput{
Body: body,
ValidStatusCodes: []int{http.StatusCreated},
Uri: Uri{
Entity: "/roleManagement/directory/roleDefinitions",
},
})
if err != nil {
return nil, status, fmt.Errorf("RoleDefinitionsClient.BaseClient.Post(): %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 newRoleDefinition UnifiedRoleDefinition
if err := json.Unmarshal(respBody, &newRoleDefinition); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &newRoleDefinition, status, nil
}
// Update amends an existing UnifiedRoleDefinition.
func (c *RoleDefinitionsClient) Update(ctx context.Context, roleDefinition UnifiedRoleDefinition) (int, error) {
var status int
body, err := json.Marshal(roleDefinition)
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", *roleDefinition.ID()),
},
})
if err != nil {
return status, fmt.Errorf("RoleDefinitionsClient.BaseClient.Patch(): %v", err)
}
return status, nil
}
// Delete removes a UnifiedRoleDefinition.
func (c *RoleDefinitionsClient) Delete(ctx context.Context, id string) (int, error) {
_, status, _, err := c.BaseClient.Delete(ctx, DeleteHttpRequestInput{
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusNoContent},
Uri: Uri{
Entity: fmt.Sprintf("/roleManagement/directory/roleDefinitions/%s", id),
},
})
if err != nil {
return status, fmt.Errorf("RoleDefinitions.BaseClient.Get(): %v", err)
}
return status, nil
}