1// +build go1.13
2
3// Copyright (c) Microsoft Corporation. All rights reserved.
4// Licensed under the MIT License. See License.txt in the project root for license information.
5// Code generated by Microsoft (R) AutoRest Code Generator.
6// Changes may cause incorrect behavior and will be lost if the code is regenerated.
7
8package armauthorization
9
10import (
11	"context"
12	"errors"
13	"fmt"
14	"github.com/Azure/azure-sdk-for-go/sdk/armcore"
15	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
16	"net/http"
17	"net/url"
18	"strings"
19)
20
21// RoleManagementPoliciesClient contains the methods for the RoleManagementPolicies group.
22// Don't use this type directly, use NewRoleManagementPoliciesClient() instead.
23type RoleManagementPoliciesClient struct {
24	con *armcore.Connection
25}
26
27// NewRoleManagementPoliciesClient creates a new instance of RoleManagementPoliciesClient with the specified values.
28func NewRoleManagementPoliciesClient(con *armcore.Connection) *RoleManagementPoliciesClient {
29	return &RoleManagementPoliciesClient{con: con}
30}
31
32// Delete - Delete a role management policy
33// If the operation fails it returns the *CloudError error type.
34func (client *RoleManagementPoliciesClient) Delete(ctx context.Context, scope string, roleManagementPolicyName string, options *RoleManagementPoliciesDeleteOptions) (*http.Response, error) {
35	req, err := client.deleteCreateRequest(ctx, scope, roleManagementPolicyName, options)
36	if err != nil {
37		return nil, err
38	}
39	resp, err := client.con.Pipeline().Do(req)
40	if err != nil {
41		return nil, err
42	}
43	if !resp.HasStatusCode(http.StatusOK, http.StatusNoContent) {
44		return nil, client.deleteHandleError(resp)
45	}
46	return resp.Response, nil
47}
48
49// deleteCreateRequest creates the Delete request.
50func (client *RoleManagementPoliciesClient) deleteCreateRequest(ctx context.Context, scope string, roleManagementPolicyName string, options *RoleManagementPoliciesDeleteOptions) (*azcore.Request, error) {
51	urlPath := "/{scope}/providers/Microsoft.Authorization/roleManagementPolicies/{roleManagementPolicyName}"
52	if scope == "" {
53		return nil, errors.New("parameter scope cannot be empty")
54	}
55	urlPath = strings.ReplaceAll(urlPath, "{scope}", scope)
56	if roleManagementPolicyName == "" {
57		return nil, errors.New("parameter roleManagementPolicyName cannot be empty")
58	}
59	urlPath = strings.ReplaceAll(urlPath, "{roleManagementPolicyName}", url.PathEscape(roleManagementPolicyName))
60	req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath))
61	if err != nil {
62		return nil, err
63	}
64	req.Telemetry(telemetryInfo)
65	reqQP := req.URL.Query()
66	reqQP.Set("api-version", "2020-10-01-preview")
67	req.URL.RawQuery = reqQP.Encode()
68	req.Header.Set("Accept", "application/json")
69	return req, nil
70}
71
72// deleteHandleError handles the Delete error response.
73func (client *RoleManagementPoliciesClient) deleteHandleError(resp *azcore.Response) error {
74	body, err := resp.Payload()
75	if err != nil {
76		return azcore.NewResponseError(err, resp.Response)
77	}
78	errType := CloudError{raw: string(body)}
79	if err := resp.UnmarshalAsJSON(&errType); err != nil {
80		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
81	}
82	return azcore.NewResponseError(&errType, resp.Response)
83}
84
85// Get - Get the specified role management policy for a resource scope
86// If the operation fails it returns the *CloudError error type.
87func (client *RoleManagementPoliciesClient) Get(ctx context.Context, scope string, roleManagementPolicyName string, options *RoleManagementPoliciesGetOptions) (RoleManagementPolicyResponse, error) {
88	req, err := client.getCreateRequest(ctx, scope, roleManagementPolicyName, options)
89	if err != nil {
90		return RoleManagementPolicyResponse{}, err
91	}
92	resp, err := client.con.Pipeline().Do(req)
93	if err != nil {
94		return RoleManagementPolicyResponse{}, err
95	}
96	if !resp.HasStatusCode(http.StatusOK) {
97		return RoleManagementPolicyResponse{}, client.getHandleError(resp)
98	}
99	return client.getHandleResponse(resp)
100}
101
102// getCreateRequest creates the Get request.
103func (client *RoleManagementPoliciesClient) getCreateRequest(ctx context.Context, scope string, roleManagementPolicyName string, options *RoleManagementPoliciesGetOptions) (*azcore.Request, error) {
104	urlPath := "/{scope}/providers/Microsoft.Authorization/roleManagementPolicies/{roleManagementPolicyName}"
105	if scope == "" {
106		return nil, errors.New("parameter scope cannot be empty")
107	}
108	urlPath = strings.ReplaceAll(urlPath, "{scope}", scope)
109	if roleManagementPolicyName == "" {
110		return nil, errors.New("parameter roleManagementPolicyName cannot be empty")
111	}
112	urlPath = strings.ReplaceAll(urlPath, "{roleManagementPolicyName}", url.PathEscape(roleManagementPolicyName))
113	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
114	if err != nil {
115		return nil, err
116	}
117	req.Telemetry(telemetryInfo)
118	reqQP := req.URL.Query()
119	reqQP.Set("api-version", "2020-10-01-preview")
120	req.URL.RawQuery = reqQP.Encode()
121	req.Header.Set("Accept", "application/json")
122	return req, nil
123}
124
125// getHandleResponse handles the Get response.
126func (client *RoleManagementPoliciesClient) getHandleResponse(resp *azcore.Response) (RoleManagementPolicyResponse, error) {
127	var val *RoleManagementPolicy
128	if err := resp.UnmarshalAsJSON(&val); err != nil {
129		return RoleManagementPolicyResponse{}, err
130	}
131	return RoleManagementPolicyResponse{RawResponse: resp.Response, RoleManagementPolicy: val}, nil
132}
133
134// getHandleError handles the Get error response.
135func (client *RoleManagementPoliciesClient) getHandleError(resp *azcore.Response) error {
136	body, err := resp.Payload()
137	if err != nil {
138		return azcore.NewResponseError(err, resp.Response)
139	}
140	errType := CloudError{raw: string(body)}
141	if err := resp.UnmarshalAsJSON(&errType); err != nil {
142		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
143	}
144	return azcore.NewResponseError(&errType, resp.Response)
145}
146
147// ListForScope - Gets role management policies for a resource scope.
148// If the operation fails it returns the *CloudError error type.
149func (client *RoleManagementPoliciesClient) ListForScope(scope string, options *RoleManagementPoliciesListForScopeOptions) RoleManagementPolicyListResultPager {
150	return &roleManagementPolicyListResultPager{
151		pipeline: client.con.Pipeline(),
152		requester: func(ctx context.Context) (*azcore.Request, error) {
153			return client.listForScopeCreateRequest(ctx, scope, options)
154		},
155		responder: client.listForScopeHandleResponse,
156		errorer:   client.listForScopeHandleError,
157		advancer: func(ctx context.Context, resp RoleManagementPolicyListResultResponse) (*azcore.Request, error) {
158			return azcore.NewRequest(ctx, http.MethodGet, *resp.RoleManagementPolicyListResult.NextLink)
159		},
160		statusCodes: []int{http.StatusOK},
161	}
162}
163
164// listForScopeCreateRequest creates the ListForScope request.
165func (client *RoleManagementPoliciesClient) listForScopeCreateRequest(ctx context.Context, scope string, options *RoleManagementPoliciesListForScopeOptions) (*azcore.Request, error) {
166	urlPath := "/{scope}/providers/Microsoft.Authorization/roleManagementPolicies"
167	if scope == "" {
168		return nil, errors.New("parameter scope cannot be empty")
169	}
170	urlPath = strings.ReplaceAll(urlPath, "{scope}", scope)
171	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
172	if err != nil {
173		return nil, err
174	}
175	req.Telemetry(telemetryInfo)
176	reqQP := req.URL.Query()
177	reqQP.Set("api-version", "2020-10-01-preview")
178	req.URL.RawQuery = reqQP.Encode()
179	req.Header.Set("Accept", "application/json")
180	return req, nil
181}
182
183// listForScopeHandleResponse handles the ListForScope response.
184func (client *RoleManagementPoliciesClient) listForScopeHandleResponse(resp *azcore.Response) (RoleManagementPolicyListResultResponse, error) {
185	var val *RoleManagementPolicyListResult
186	if err := resp.UnmarshalAsJSON(&val); err != nil {
187		return RoleManagementPolicyListResultResponse{}, err
188	}
189	return RoleManagementPolicyListResultResponse{RawResponse: resp.Response, RoleManagementPolicyListResult: val}, nil
190}
191
192// listForScopeHandleError handles the ListForScope error response.
193func (client *RoleManagementPoliciesClient) listForScopeHandleError(resp *azcore.Response) error {
194	body, err := resp.Payload()
195	if err != nil {
196		return azcore.NewResponseError(err, resp.Response)
197	}
198	errType := CloudError{raw: string(body)}
199	if err := resp.UnmarshalAsJSON(&errType); err != nil {
200		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
201	}
202	return azcore.NewResponseError(&errType, resp.Response)
203}
204
205// Update - Update a role management policy
206// If the operation fails it returns the *CloudError error type.
207func (client *RoleManagementPoliciesClient) Update(ctx context.Context, scope string, roleManagementPolicyName string, parameters RoleManagementPolicy, options *RoleManagementPoliciesUpdateOptions) (RoleManagementPolicyResponse, error) {
208	req, err := client.updateCreateRequest(ctx, scope, roleManagementPolicyName, parameters, options)
209	if err != nil {
210		return RoleManagementPolicyResponse{}, err
211	}
212	resp, err := client.con.Pipeline().Do(req)
213	if err != nil {
214		return RoleManagementPolicyResponse{}, err
215	}
216	if !resp.HasStatusCode(http.StatusOK) {
217		return RoleManagementPolicyResponse{}, client.updateHandleError(resp)
218	}
219	return client.updateHandleResponse(resp)
220}
221
222// updateCreateRequest creates the Update request.
223func (client *RoleManagementPoliciesClient) updateCreateRequest(ctx context.Context, scope string, roleManagementPolicyName string, parameters RoleManagementPolicy, options *RoleManagementPoliciesUpdateOptions) (*azcore.Request, error) {
224	urlPath := "/{scope}/providers/Microsoft.Authorization/roleManagementPolicies/{roleManagementPolicyName}"
225	if scope == "" {
226		return nil, errors.New("parameter scope cannot be empty")
227	}
228	urlPath = strings.ReplaceAll(urlPath, "{scope}", scope)
229	if roleManagementPolicyName == "" {
230		return nil, errors.New("parameter roleManagementPolicyName cannot be empty")
231	}
232	urlPath = strings.ReplaceAll(urlPath, "{roleManagementPolicyName}", url.PathEscape(roleManagementPolicyName))
233	req, err := azcore.NewRequest(ctx, http.MethodPatch, azcore.JoinPaths(client.con.Endpoint(), urlPath))
234	if err != nil {
235		return nil, err
236	}
237	req.Telemetry(telemetryInfo)
238	reqQP := req.URL.Query()
239	reqQP.Set("api-version", "2020-10-01-preview")
240	req.URL.RawQuery = reqQP.Encode()
241	req.Header.Set("Accept", "application/json")
242	return req, req.MarshalAsJSON(parameters)
243}
244
245// updateHandleResponse handles the Update response.
246func (client *RoleManagementPoliciesClient) updateHandleResponse(resp *azcore.Response) (RoleManagementPolicyResponse, error) {
247	var val *RoleManagementPolicy
248	if err := resp.UnmarshalAsJSON(&val); err != nil {
249		return RoleManagementPolicyResponse{}, err
250	}
251	return RoleManagementPolicyResponse{RawResponse: resp.Response, RoleManagementPolicy: val}, nil
252}
253
254// updateHandleError handles the Update error response.
255func (client *RoleManagementPoliciesClient) updateHandleError(resp *azcore.Response) error {
256	body, err := resp.Payload()
257	if err != nil {
258		return azcore.NewResponseError(err, resp.Response)
259	}
260	errType := CloudError{raw: string(body)}
261	if err := resp.UnmarshalAsJSON(&errType); err != nil {
262		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
263	}
264	return azcore.NewResponseError(&errType, resp.Response)
265}
266