-
Notifications
You must be signed in to change notification settings - Fork 62
/
delegated_permission_grants_client.go
174 lines (147 loc) · 5.27 KB
/
delegated_permission_grants_client.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package msgraph
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
)
// DelegatedPermissionGrantsClient performs operations on DelegatedPermissionGrants.
type DelegatedPermissionGrantsClient struct {
BaseClient Client
}
// NewDelegatedPermissionGrantsClient returns a new DelegatedPermissionGrantsClient
func NewDelegatedPermissionGrantsClient() *DelegatedPermissionGrantsClient {
return &DelegatedPermissionGrantsClient{
BaseClient: NewClient(Version10),
}
}
// List returns a list of delegated permission grants
func (c *DelegatedPermissionGrantsClient) List(ctx context.Context, query odata.Query) (*[]DelegatedPermissionGrant, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
OData: query,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: "/oauth2PermissionGrants",
},
})
if err != nil {
return nil, status, fmt.Errorf("DelegatedPermissionGrantsClient.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 {
DelegatedPermissionGrants []DelegatedPermissionGrant `json:"value"`
}
if err := json.Unmarshal(respBody, &data); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &data.DelegatedPermissionGrants, status, nil
}
// Create creates a new delegated permission grant
func (c *DelegatedPermissionGrantsClient) Create(ctx context.Context, delegatedPermissionGrant DelegatedPermissionGrant) (*DelegatedPermissionGrant, int, error) {
var status int
if delegatedPermissionGrant.ClientId == nil {
return nil, status, errors.New("DelegatedPermissionGrantsClient.Create(): ClientId was nil for delegatedPermissionGrant")
}
body, err := json.Marshal(delegatedPermissionGrant)
if err != nil {
return nil, status, fmt.Errorf("json.Marshal(): %v", err)
}
consistencyFunc := func(resp *http.Response, o *odata.OData) bool {
if resp != nil && o != nil && o.Error != nil {
if resp.StatusCode == http.StatusNotFound {
return true
} else if resp.StatusCode == http.StatusBadRequest {
return o.Error.Match(odata.ErrorResourceDoesNotExist)
}
}
return false
}
resp, status, _, err := c.BaseClient.Post(ctx, PostHttpRequestInput{
Body: body,
ConsistencyFailureFunc: consistencyFunc,
ValidStatusCodes: []int{http.StatusCreated},
Uri: Uri{
Entity: "/oauth2PermissionGrants",
},
})
if err != nil {
return nil, status, fmt.Errorf("DelegatedPermissionGrantsClient.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 newDelegatedPermissionGrant DelegatedPermissionGrant
if err := json.Unmarshal(respBody, &newDelegatedPermissionGrant); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &newDelegatedPermissionGrant, status, nil
}
// Get returns a delegated permission grant
func (c *DelegatedPermissionGrantsClient) Get(ctx context.Context, id string, query odata.Query) (*DelegatedPermissionGrant, int, error) {
resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
OData: query,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/oauth2PermissionGrants/%s", id),
},
})
if err != nil {
return nil, status, fmt.Errorf("DelegatedPermissionGrantsClient.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 DelegatedPermissionGrant
if err := json.Unmarshal(respBody, &data); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}
return &data, status, nil
}
// Update amends an existing delegated permission grant
func (c *DelegatedPermissionGrantsClient) Update(ctx context.Context, delegatedPermissionGrant DelegatedPermissionGrant) (int, error) {
var status int
if delegatedPermissionGrant.Id == nil {
return status, errors.New("cannot update delegated permission grant with nil ID")
}
body, err := json.Marshal(delegatedPermissionGrant)
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("/oauth2PermissionGrants/%s", *delegatedPermissionGrant.Id),
},
})
if err != nil {
return status, fmt.Errorf("DelegatedPermissionGrantsClient.BaseClient.Patch(): %v", err)
}
return status, nil
}
// Delete removes a delegated permission grant
func (c *DelegatedPermissionGrantsClient) 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("/oauth2PermissionGrants/%s", id),
},
})
if err != nil {
return status, fmt.Errorf("DelegatedPermissionGrantsClient.BaseClient.Delete(): %v", err)
}
return status, nil
}