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// EntitiesClient 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 EntitiesClient struct {
21	BaseClient
22}
23
24// NewEntitiesClient creates an instance of the EntitiesClient client.
25func NewEntitiesClient(operationResultID string, skiptoken string) EntitiesClient {
26	return NewEntitiesClientWithBaseURI(DefaultBaseURI, operationResultID, skiptoken)
27}
28
29// NewEntitiesClientWithBaseURI creates an instance of the EntitiesClient client using a custom endpoint.  Use this
30// when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
31func NewEntitiesClientWithBaseURI(baseURI string, operationResultID string, skiptoken string) EntitiesClient {
32	return EntitiesClient{NewWithBaseURI(baseURI, operationResultID, skiptoken)}
33}
34
35// List list all entities (Management Groups, Subscriptions, etc.) for the authenticated user.
36// Parameters:
37// groupName - a filter which allows the call to be filtered for a specific group.
38// cacheControl - indicates that the request shouldn't utilize any caches.
39func (client EntitiesClient) List(ctx context.Context, groupName string, cacheControl string) (result EntityListResultPage, err error) {
40	if tracing.IsEnabled() {
41		ctx = tracing.StartSpan(ctx, fqdn+"/EntitiesClient.List")
42		defer func() {
43			sc := -1
44			if result.elr.Response.Response != nil {
45				sc = result.elr.Response.Response.StatusCode
46			}
47			tracing.EndSpan(ctx, sc, err)
48		}()
49	}
50	result.fn = client.listNextResults
51	req, err := client.ListPreparer(ctx, groupName, cacheControl)
52	if err != nil {
53		err = autorest.NewErrorWithError(err, "managementgroups.EntitiesClient", "List", nil, "Failure preparing request")
54		return
55	}
56
57	resp, err := client.ListSender(req)
58	if err != nil {
59		result.elr.Response = autorest.Response{Response: resp}
60		err = autorest.NewErrorWithError(err, "managementgroups.EntitiesClient", "List", resp, "Failure sending request")
61		return
62	}
63
64	result.elr, err = client.ListResponder(resp)
65	if err != nil {
66		err = autorest.NewErrorWithError(err, "managementgroups.EntitiesClient", "List", resp, "Failure responding to request")
67		return
68	}
69	if result.elr.hasNextLink() && result.elr.IsEmpty() {
70		err = result.NextWithContext(ctx)
71		return
72	}
73
74	return
75}
76
77// ListPreparer prepares the List request.
78func (client EntitiesClient) ListPreparer(ctx context.Context, groupName string, cacheControl string) (*http.Request, error) {
79	const APIVersion = "2018-01-01-preview"
80	queryParameters := map[string]interface{}{
81		"api-version": APIVersion,
82	}
83	if len(client.Skiptoken) > 0 {
84		queryParameters["$skiptoken"] = autorest.Encode("query", client.Skiptoken)
85	}
86	if len(groupName) > 0 {
87		queryParameters["groupName"] = autorest.Encode("query", groupName)
88	}
89
90	preparer := autorest.CreatePreparer(
91		autorest.AsPost(),
92		autorest.WithBaseURL(client.BaseURI),
93		autorest.WithPath("/providers/Microsoft.Management/getEntities"),
94		autorest.WithQueryParameters(queryParameters))
95	if len(cacheControl) > 0 {
96		preparer = autorest.DecoratePreparer(preparer,
97			autorest.WithHeader("Cache-Control", autorest.String(cacheControl)))
98	} else {
99		preparer = autorest.DecoratePreparer(preparer,
100			autorest.WithHeader("Cache-Control", autorest.String("no-cache")))
101	}
102	return preparer.Prepare((&http.Request{}).WithContext(ctx))
103}
104
105// ListSender sends the List request. The method will close the
106// http.Response Body if it receives an error.
107func (client EntitiesClient) ListSender(req *http.Request) (*http.Response, error) {
108	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
109}
110
111// ListResponder handles the response to the List request. The method always
112// closes the http.Response Body.
113func (client EntitiesClient) ListResponder(resp *http.Response) (result EntityListResult, 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// listNextResults retrieves the next set of results, if any.
124func (client EntitiesClient) listNextResults(ctx context.Context, lastResults EntityListResult) (result EntityListResult, err error) {
125	req, err := lastResults.entityListResultPreparer(ctx)
126	if err != nil {
127		return result, autorest.NewErrorWithError(err, "managementgroups.EntitiesClient", "listNextResults", nil, "Failure preparing next results request")
128	}
129	if req == nil {
130		return
131	}
132	resp, err := client.ListSender(req)
133	if err != nil {
134		result.Response = autorest.Response{Response: resp}
135		return result, autorest.NewErrorWithError(err, "managementgroups.EntitiesClient", "listNextResults", resp, "Failure sending next results request")
136	}
137	result, err = client.ListResponder(resp)
138	if err != nil {
139		err = autorest.NewErrorWithError(err, "managementgroups.EntitiesClient", "listNextResults", resp, "Failure responding to next results request")
140	}
141	return
142}
143
144// ListComplete enumerates all values, automatically crossing page boundaries as required.
145func (client EntitiesClient) ListComplete(ctx context.Context, groupName string, cacheControl string) (result EntityListResultIterator, err error) {
146	if tracing.IsEnabled() {
147		ctx = tracing.StartSpan(ctx, fqdn+"/EntitiesClient.List")
148		defer func() {
149			sc := -1
150			if result.Response().Response.Response != nil {
151				sc = result.page.Response().Response.Response.StatusCode
152			}
153			tracing.EndSpan(ctx, sc, err)
154		}()
155	}
156	result.page, err = client.List(ctx, groupName, cacheControl)
157	return
158}
159