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