1package managementgroups
2
3// Copyright (c) Microsoft and contributors.  All rights reserved.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13//
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17// Code generated by Microsoft (R) AutoRest Code Generator.
18// Changes may cause incorrect behavior and will be lost if the code is regenerated.
19
20import (
21	"context"
22	"github.com/Azure/go-autorest/autorest"
23	"github.com/Azure/go-autorest/autorest/azure"
24	"github.com/Azure/go-autorest/tracing"
25	"net/http"
26)
27
28// HierarchySettingsClient is the the Azure Management Groups API enables consolidation of multiple
29// subscriptions/resources into an organizational hierarchy and centrally
30// manage access control, policies, alerting and reporting for those resources.
31type HierarchySettingsClient struct {
32	BaseClient
33}
34
35// NewHierarchySettingsClient creates an instance of the HierarchySettingsClient client.
36func NewHierarchySettingsClient(operationResultID string, skip *int32, top *int32, skiptoken string) HierarchySettingsClient {
37	return NewHierarchySettingsClientWithBaseURI(DefaultBaseURI, operationResultID, skip, top, skiptoken)
38}
39
40// NewHierarchySettingsClientWithBaseURI creates an instance of the HierarchySettingsClient client using a custom
41// endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure
42// stack).
43func NewHierarchySettingsClientWithBaseURI(baseURI string, operationResultID string, skip *int32, top *int32, skiptoken string) HierarchySettingsClient {
44	return HierarchySettingsClient{NewWithBaseURI(baseURI, operationResultID, skip, top, skiptoken)}
45}
46
47// CreateOrUpdate creates or updates the hierarchy settings defined at the Management Group level.
48// Parameters:
49// groupID - management Group ID.
50// createTenantSettingsRequest - tenant level settings request parameter.
51func (client HierarchySettingsClient) CreateOrUpdate(ctx context.Context, groupID string, createTenantSettingsRequest CreateOrUpdateSettingsRequest) (result HierarchySettings, err error) {
52	if tracing.IsEnabled() {
53		ctx = tracing.StartSpan(ctx, fqdn+"/HierarchySettingsClient.CreateOrUpdate")
54		defer func() {
55			sc := -1
56			if result.Response.Response != nil {
57				sc = result.Response.Response.StatusCode
58			}
59			tracing.EndSpan(ctx, sc, err)
60		}()
61	}
62	req, err := client.CreateOrUpdatePreparer(ctx, groupID, createTenantSettingsRequest)
63	if err != nil {
64		err = autorest.NewErrorWithError(err, "managementgroups.HierarchySettingsClient", "CreateOrUpdate", nil, "Failure preparing request")
65		return
66	}
67
68	resp, err := client.CreateOrUpdateSender(req)
69	if err != nil {
70		result.Response = autorest.Response{Response: resp}
71		err = autorest.NewErrorWithError(err, "managementgroups.HierarchySettingsClient", "CreateOrUpdate", resp, "Failure sending request")
72		return
73	}
74
75	result, err = client.CreateOrUpdateResponder(resp)
76	if err != nil {
77		err = autorest.NewErrorWithError(err, "managementgroups.HierarchySettingsClient", "CreateOrUpdate", resp, "Failure responding to request")
78		return
79	}
80
81	return
82}
83
84// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
85func (client HierarchySettingsClient) CreateOrUpdatePreparer(ctx context.Context, groupID string, createTenantSettingsRequest CreateOrUpdateSettingsRequest) (*http.Request, error) {
86	pathParameters := map[string]interface{}{
87		"groupId": autorest.Encode("path", groupID),
88	}
89
90	const APIVersion = "2020-02-01"
91	queryParameters := map[string]interface{}{
92		"api-version": APIVersion,
93	}
94
95	preparer := autorest.CreatePreparer(
96		autorest.AsContentType("application/json; charset=utf-8"),
97		autorest.AsPut(),
98		autorest.WithBaseURL(client.BaseURI),
99		autorest.WithPathParameters("/providers/Microsoft.Management/managementGroups/{groupId}/settings/default", pathParameters),
100		autorest.WithJSON(createTenantSettingsRequest),
101		autorest.WithQueryParameters(queryParameters))
102	return preparer.Prepare((&http.Request{}).WithContext(ctx))
103}
104
105// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
106// http.Response Body if it receives an error.
107func (client HierarchySettingsClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
108	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
109}
110
111// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
112// closes the http.Response Body.
113func (client HierarchySettingsClient) CreateOrUpdateResponder(resp *http.Response) (result HierarchySettings, err error) {
114	err = autorest.Respond(
115		resp,
116		azure.WithErrorUnlessStatusCode(http.StatusOK),
117		autorest.ByUnmarshallingJSON(&result),
118		autorest.ByClosing())
119	result.Response = autorest.Response{Response: resp}
120	return
121}
122
123// Delete deletes the hierarchy settings defined at the Management Group level.
124// Parameters:
125// groupID - management Group ID.
126func (client HierarchySettingsClient) Delete(ctx context.Context, groupID string) (result autorest.Response, err error) {
127	if tracing.IsEnabled() {
128		ctx = tracing.StartSpan(ctx, fqdn+"/HierarchySettingsClient.Delete")
129		defer func() {
130			sc := -1
131			if result.Response != nil {
132				sc = result.Response.StatusCode
133			}
134			tracing.EndSpan(ctx, sc, err)
135		}()
136	}
137	req, err := client.DeletePreparer(ctx, groupID)
138	if err != nil {
139		err = autorest.NewErrorWithError(err, "managementgroups.HierarchySettingsClient", "Delete", nil, "Failure preparing request")
140		return
141	}
142
143	resp, err := client.DeleteSender(req)
144	if err != nil {
145		result.Response = resp
146		err = autorest.NewErrorWithError(err, "managementgroups.HierarchySettingsClient", "Delete", resp, "Failure sending request")
147		return
148	}
149
150	result, err = client.DeleteResponder(resp)
151	if err != nil {
152		err = autorest.NewErrorWithError(err, "managementgroups.HierarchySettingsClient", "Delete", resp, "Failure responding to request")
153		return
154	}
155
156	return
157}
158
159// DeletePreparer prepares the Delete request.
160func (client HierarchySettingsClient) DeletePreparer(ctx context.Context, groupID string) (*http.Request, error) {
161	pathParameters := map[string]interface{}{
162		"groupId": autorest.Encode("path", groupID),
163	}
164
165	const APIVersion = "2020-02-01"
166	queryParameters := map[string]interface{}{
167		"api-version": APIVersion,
168	}
169
170	preparer := autorest.CreatePreparer(
171		autorest.AsDelete(),
172		autorest.WithBaseURL(client.BaseURI),
173		autorest.WithPathParameters("/providers/Microsoft.Management/managementGroups/{groupId}/settings/default", pathParameters),
174		autorest.WithQueryParameters(queryParameters))
175	return preparer.Prepare((&http.Request{}).WithContext(ctx))
176}
177
178// DeleteSender sends the Delete request. The method will close the
179// http.Response Body if it receives an error.
180func (client HierarchySettingsClient) DeleteSender(req *http.Request) (*http.Response, error) {
181	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
182}
183
184// DeleteResponder handles the response to the Delete request. The method always
185// closes the http.Response Body.
186func (client HierarchySettingsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
187	err = autorest.Respond(
188		resp,
189		azure.WithErrorUnlessStatusCode(http.StatusOK),
190		autorest.ByClosing())
191	result.Response = resp
192	return
193}
194
195// Get gets the hierarchy settings defined at the Management Group level. Settings can only be set on the root
196// Management Group of the hierarchy.
197// Parameters:
198// groupID - management Group ID.
199func (client HierarchySettingsClient) Get(ctx context.Context, groupID string) (result HierarchySettings, err error) {
200	if tracing.IsEnabled() {
201		ctx = tracing.StartSpan(ctx, fqdn+"/HierarchySettingsClient.Get")
202		defer func() {
203			sc := -1
204			if result.Response.Response != nil {
205				sc = result.Response.Response.StatusCode
206			}
207			tracing.EndSpan(ctx, sc, err)
208		}()
209	}
210	req, err := client.GetPreparer(ctx, groupID)
211	if err != nil {
212		err = autorest.NewErrorWithError(err, "managementgroups.HierarchySettingsClient", "Get", nil, "Failure preparing request")
213		return
214	}
215
216	resp, err := client.GetSender(req)
217	if err != nil {
218		result.Response = autorest.Response{Response: resp}
219		err = autorest.NewErrorWithError(err, "managementgroups.HierarchySettingsClient", "Get", resp, "Failure sending request")
220		return
221	}
222
223	result, err = client.GetResponder(resp)
224	if err != nil {
225		err = autorest.NewErrorWithError(err, "managementgroups.HierarchySettingsClient", "Get", resp, "Failure responding to request")
226		return
227	}
228
229	return
230}
231
232// GetPreparer prepares the Get request.
233func (client HierarchySettingsClient) GetPreparer(ctx context.Context, groupID string) (*http.Request, error) {
234	pathParameters := map[string]interface{}{
235		"groupId": autorest.Encode("path", groupID),
236	}
237
238	const APIVersion = "2020-02-01"
239	queryParameters := map[string]interface{}{
240		"api-version": APIVersion,
241	}
242
243	preparer := autorest.CreatePreparer(
244		autorest.AsGet(),
245		autorest.WithBaseURL(client.BaseURI),
246		autorest.WithPathParameters("/providers/Microsoft.Management/managementGroups/{groupId}/settings/default", pathParameters),
247		autorest.WithQueryParameters(queryParameters))
248	return preparer.Prepare((&http.Request{}).WithContext(ctx))
249}
250
251// GetSender sends the Get request. The method will close the
252// http.Response Body if it receives an error.
253func (client HierarchySettingsClient) GetSender(req *http.Request) (*http.Response, error) {
254	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
255}
256
257// GetResponder handles the response to the Get request. The method always
258// closes the http.Response Body.
259func (client HierarchySettingsClient) GetResponder(resp *http.Response) (result HierarchySettings, err error) {
260	err = autorest.Respond(
261		resp,
262		azure.WithErrorUnlessStatusCode(http.StatusOK),
263		autorest.ByUnmarshallingJSON(&result),
264		autorest.ByClosing())
265	result.Response = autorest.Response{Response: resp}
266	return
267}
268
269// List gets all the hierarchy settings defined at the Management Group level. Settings can only be set on the root
270// Management Group of the hierarchy.
271// Parameters:
272// groupID - management Group ID.
273func (client HierarchySettingsClient) List(ctx context.Context, groupID string) (result HierarchySettingsList, err error) {
274	if tracing.IsEnabled() {
275		ctx = tracing.StartSpan(ctx, fqdn+"/HierarchySettingsClient.List")
276		defer func() {
277			sc := -1
278			if result.Response.Response != nil {
279				sc = result.Response.Response.StatusCode
280			}
281			tracing.EndSpan(ctx, sc, err)
282		}()
283	}
284	req, err := client.ListPreparer(ctx, groupID)
285	if err != nil {
286		err = autorest.NewErrorWithError(err, "managementgroups.HierarchySettingsClient", "List", nil, "Failure preparing request")
287		return
288	}
289
290	resp, err := client.ListSender(req)
291	if err != nil {
292		result.Response = autorest.Response{Response: resp}
293		err = autorest.NewErrorWithError(err, "managementgroups.HierarchySettingsClient", "List", resp, "Failure sending request")
294		return
295	}
296
297	result, err = client.ListResponder(resp)
298	if err != nil {
299		err = autorest.NewErrorWithError(err, "managementgroups.HierarchySettingsClient", "List", resp, "Failure responding to request")
300		return
301	}
302
303	return
304}
305
306// ListPreparer prepares the List request.
307func (client HierarchySettingsClient) ListPreparer(ctx context.Context, groupID string) (*http.Request, error) {
308	pathParameters := map[string]interface{}{
309		"groupId": autorest.Encode("path", groupID),
310	}
311
312	const APIVersion = "2020-02-01"
313	queryParameters := map[string]interface{}{
314		"api-version": APIVersion,
315	}
316
317	preparer := autorest.CreatePreparer(
318		autorest.AsGet(),
319		autorest.WithBaseURL(client.BaseURI),
320		autorest.WithPathParameters("/providers/Microsoft.Management/managementGroups/{groupId}/settings", pathParameters),
321		autorest.WithQueryParameters(queryParameters))
322	return preparer.Prepare((&http.Request{}).WithContext(ctx))
323}
324
325// ListSender sends the List request. The method will close the
326// http.Response Body if it receives an error.
327func (client HierarchySettingsClient) ListSender(req *http.Request) (*http.Response, error) {
328	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
329}
330
331// ListResponder handles the response to the List request. The method always
332// closes the http.Response Body.
333func (client HierarchySettingsClient) ListResponder(resp *http.Response) (result HierarchySettingsList, err error) {
334	err = autorest.Respond(
335		resp,
336		azure.WithErrorUnlessStatusCode(http.StatusOK),
337		autorest.ByUnmarshallingJSON(&result),
338		autorest.ByClosing())
339	result.Response = autorest.Response{Response: resp}
340	return
341}
342
343// Update updates the hierarchy settings defined at the Management Group level.
344// Parameters:
345// groupID - management Group ID.
346// createTenantSettingsRequest - tenant level settings request parameter.
347func (client HierarchySettingsClient) Update(ctx context.Context, groupID string, createTenantSettingsRequest CreateOrUpdateSettingsRequest) (result HierarchySettings, err error) {
348	if tracing.IsEnabled() {
349		ctx = tracing.StartSpan(ctx, fqdn+"/HierarchySettingsClient.Update")
350		defer func() {
351			sc := -1
352			if result.Response.Response != nil {
353				sc = result.Response.Response.StatusCode
354			}
355			tracing.EndSpan(ctx, sc, err)
356		}()
357	}
358	req, err := client.UpdatePreparer(ctx, groupID, createTenantSettingsRequest)
359	if err != nil {
360		err = autorest.NewErrorWithError(err, "managementgroups.HierarchySettingsClient", "Update", nil, "Failure preparing request")
361		return
362	}
363
364	resp, err := client.UpdateSender(req)
365	if err != nil {
366		result.Response = autorest.Response{Response: resp}
367		err = autorest.NewErrorWithError(err, "managementgroups.HierarchySettingsClient", "Update", resp, "Failure sending request")
368		return
369	}
370
371	result, err = client.UpdateResponder(resp)
372	if err != nil {
373		err = autorest.NewErrorWithError(err, "managementgroups.HierarchySettingsClient", "Update", resp, "Failure responding to request")
374		return
375	}
376
377	return
378}
379
380// UpdatePreparer prepares the Update request.
381func (client HierarchySettingsClient) UpdatePreparer(ctx context.Context, groupID string, createTenantSettingsRequest CreateOrUpdateSettingsRequest) (*http.Request, error) {
382	pathParameters := map[string]interface{}{
383		"groupId": autorest.Encode("path", groupID),
384	}
385
386	const APIVersion = "2020-02-01"
387	queryParameters := map[string]interface{}{
388		"api-version": APIVersion,
389	}
390
391	preparer := autorest.CreatePreparer(
392		autorest.AsContentType("application/json; charset=utf-8"),
393		autorest.AsPatch(),
394		autorest.WithBaseURL(client.BaseURI),
395		autorest.WithPathParameters("/providers/Microsoft.Management/managementGroups/{groupId}/settings/default", pathParameters),
396		autorest.WithJSON(createTenantSettingsRequest),
397		autorest.WithQueryParameters(queryParameters))
398	return preparer.Prepare((&http.Request{}).WithContext(ctx))
399}
400
401// UpdateSender sends the Update request. The method will close the
402// http.Response Body if it receives an error.
403func (client HierarchySettingsClient) UpdateSender(req *http.Request) (*http.Response, error) {
404	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
405}
406
407// UpdateResponder handles the response to the Update request. The method always
408// closes the http.Response Body.
409func (client HierarchySettingsClient) UpdateResponder(resp *http.Response) (result HierarchySettings, err error) {
410	err = autorest.Respond(
411		resp,
412		azure.WithErrorUnlessStatusCode(http.StatusOK),
413		autorest.ByUnmarshallingJSON(&result),
414		autorest.ByClosing())
415	result.Response = autorest.Response{Response: resp}
416	return
417}
418