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// EntitiesClient is the the Azure Management Groups API enables consolidation of multiple subscriptions/resources into
29// an organizational hierarchy and centrally manage access control, policies, alerting and reporting for those
30// resources.
31type EntitiesClient struct {
32	BaseClient
33}
34
35// NewEntitiesClient creates an instance of the EntitiesClient client.
36func NewEntitiesClient() EntitiesClient {
37	return NewEntitiesClientWithBaseURI(DefaultBaseURI)
38}
39
40// NewEntitiesClientWithBaseURI creates an instance of the EntitiesClient client using a custom endpoint.  Use this
41// when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
42func NewEntitiesClientWithBaseURI(baseURI string) EntitiesClient {
43	return EntitiesClient{NewWithBaseURI(baseURI)}
44}
45
46// List list all entities (Management Groups, Subscriptions, etc.) for the authenticated user.
47// Parameters:
48// skiptoken - page continuation token is only used if a previous operation returned a partial result. If a
49// previous response contains a nextLink element, the value of the nextLink element will include a token
50// parameter that specifies a starting point to use for subsequent calls.
51// skip - number of entities to skip over when retrieving results. Passing this in will override $skipToken.
52// top - number of elements to return when retrieving results. Passing this in will override $skipToken.
53// selectParameter - this parameter specifies the fields to include in the response. Can include any
54// combination of Name,DisplayName,Type,ParentDisplayNameChain,ParentChain, e.g.
55// '$select=Name,DisplayName,Type,ParentDisplayNameChain,ParentNameChain'. When specified the $select parameter
56// can override select in $skipToken.
57// search - the $search parameter is used in conjunction with the $filter parameter to return three different
58// outputs depending on the parameter passed in. With $search=AllowedParents the API will return the entity
59// info of all groups that the requested entity will be able to reparent to as determined by the user's
60// permissions.With $search=AllowedChildren the API will return the entity info of all entities that can be
61// added as children of the requested entity.With $search=ParentAndFirstLevelChildren the API will return the
62// parent and  first level of children that the user has either direct access to or indirect access via one of
63// their descendants.With $search=ParentOnly the API will return only the group if the user has access to at
64// least one of the descendants of the group.With $search=ChildrenOnly the API will return only the first level
65// of children of the group entity info specified in $filter.  The user must have direct access to the children
66// entities or one of it's descendants for it to show up in the results.
67// filter - the filter parameter allows you to filter on the the name or display name fields. You can check for
68// equality on the name field (e.g. name eq '{entityName}')  and you can check for substrings on either the
69// name or display name fields(e.g. contains(name, '{substringToSearch}'), contains(displayName,
70// '{substringToSearch')). Note that the '{entityName}' and '{substringToSearch}' fields are checked case
71// insensitively.
72// view - the view parameter allows clients to filter the type of data that is returned by the getEntities
73// call.
74// groupName - a filter which allows the get entities call to focus on a particular group (i.e. "$filter=name
75// eq 'groupName'")
76// cacheControl - indicates that the request shouldn't utilize any caches.
77func (client EntitiesClient) List(ctx context.Context, skiptoken string, skip *int32, top *int32, selectParameter string, search string, filter string, view string, groupName string, cacheControl string) (result EntityListResultPage, err error) {
78	if tracing.IsEnabled() {
79		ctx = tracing.StartSpan(ctx, fqdn+"/EntitiesClient.List")
80		defer func() {
81			sc := -1
82			if result.elr.Response.Response != nil {
83				sc = result.elr.Response.Response.StatusCode
84			}
85			tracing.EndSpan(ctx, sc, err)
86		}()
87	}
88	result.fn = client.listNextResults
89	req, err := client.ListPreparer(ctx, skiptoken, skip, top, selectParameter, search, filter, view, groupName, cacheControl)
90	if err != nil {
91		err = autorest.NewErrorWithError(err, "managementgroups.EntitiesClient", "List", nil, "Failure preparing request")
92		return
93	}
94
95	resp, err := client.ListSender(req)
96	if err != nil {
97		result.elr.Response = autorest.Response{Response: resp}
98		err = autorest.NewErrorWithError(err, "managementgroups.EntitiesClient", "List", resp, "Failure sending request")
99		return
100	}
101
102	result.elr, err = client.ListResponder(resp)
103	if err != nil {
104		err = autorest.NewErrorWithError(err, "managementgroups.EntitiesClient", "List", resp, "Failure responding to request")
105	}
106
107	return
108}
109
110// ListPreparer prepares the List request.
111func (client EntitiesClient) ListPreparer(ctx context.Context, skiptoken string, skip *int32, top *int32, selectParameter string, search string, filter string, view string, groupName string, cacheControl string) (*http.Request, error) {
112	const APIVersion = "2019-11-01"
113	queryParameters := map[string]interface{}{
114		"api-version": APIVersion,
115	}
116	if len(skiptoken) > 0 {
117		queryParameters["$skiptoken"] = autorest.Encode("query", skiptoken)
118	}
119	if skip != nil {
120		queryParameters["$skip"] = autorest.Encode("query", *skip)
121	}
122	if top != nil {
123		queryParameters["$top"] = autorest.Encode("query", *top)
124	}
125	if len(selectParameter) > 0 {
126		queryParameters["$select"] = autorest.Encode("query", selectParameter)
127	}
128	if len(string(search)) > 0 {
129		queryParameters["$search"] = autorest.Encode("query", search)
130	}
131	if len(filter) > 0 {
132		queryParameters["$filter"] = autorest.Encode("query", filter)
133	}
134	if len(string(view)) > 0 {
135		queryParameters["$view"] = autorest.Encode("query", view)
136	}
137	if len(groupName) > 0 {
138		queryParameters["groupName"] = autorest.Encode("query", groupName)
139	}
140
141	preparer := autorest.CreatePreparer(
142		autorest.AsPost(),
143		autorest.WithBaseURL(client.BaseURI),
144		autorest.WithPath("/providers/Microsoft.Management/getEntities"),
145		autorest.WithQueryParameters(queryParameters))
146	if len(cacheControl) > 0 {
147		preparer = autorest.DecoratePreparer(preparer,
148			autorest.WithHeader("Cache-Control", autorest.String(cacheControl)))
149	} else {
150		preparer = autorest.DecoratePreparer(preparer,
151			autorest.WithHeader("Cache-Control", autorest.String("no-cache")))
152	}
153	return preparer.Prepare((&http.Request{}).WithContext(ctx))
154}
155
156// ListSender sends the List request. The method will close the
157// http.Response Body if it receives an error.
158func (client EntitiesClient) ListSender(req *http.Request) (*http.Response, error) {
159	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
160}
161
162// ListResponder handles the response to the List request. The method always
163// closes the http.Response Body.
164func (client EntitiesClient) ListResponder(resp *http.Response) (result EntityListResult, err error) {
165	err = autorest.Respond(
166		resp,
167		client.ByInspecting(),
168		azure.WithErrorUnlessStatusCode(http.StatusOK),
169		autorest.ByUnmarshallingJSON(&result),
170		autorest.ByClosing())
171	result.Response = autorest.Response{Response: resp}
172	return
173}
174
175// listNextResults retrieves the next set of results, if any.
176func (client EntitiesClient) listNextResults(ctx context.Context, lastResults EntityListResult) (result EntityListResult, err error) {
177	req, err := lastResults.entityListResultPreparer(ctx)
178	if err != nil {
179		return result, autorest.NewErrorWithError(err, "managementgroups.EntitiesClient", "listNextResults", nil, "Failure preparing next results request")
180	}
181	if req == nil {
182		return
183	}
184	resp, err := client.ListSender(req)
185	if err != nil {
186		result.Response = autorest.Response{Response: resp}
187		return result, autorest.NewErrorWithError(err, "managementgroups.EntitiesClient", "listNextResults", resp, "Failure sending next results request")
188	}
189	result, err = client.ListResponder(resp)
190	if err != nil {
191		err = autorest.NewErrorWithError(err, "managementgroups.EntitiesClient", "listNextResults", resp, "Failure responding to next results request")
192	}
193	return
194}
195
196// ListComplete enumerates all values, automatically crossing page boundaries as required.
197func (client EntitiesClient) ListComplete(ctx context.Context, skiptoken string, skip *int32, top *int32, selectParameter string, search string, filter string, view string, groupName string, cacheControl string) (result EntityListResultIterator, err error) {
198	if tracing.IsEnabled() {
199		ctx = tracing.StartSpan(ctx, fqdn+"/EntitiesClient.List")
200		defer func() {
201			sc := -1
202			if result.Response().Response.Response != nil {
203				sc = result.page.Response().Response.Response.StatusCode
204			}
205			tracing.EndSpan(ctx, sc, err)
206		}()
207	}
208	result.page, err = client.List(ctx, skiptoken, skip, top, selectParameter, search, filter, view, groupName, cacheControl)
209	return
210}
211