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