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	}
104
105	return
106}
107
108// ListPreparer prepares the List request.
109func (client EntitiesClient) ListPreparer(ctx context.Context, selectParameter string, search string, filter string, view string, groupName string, cacheControl string) (*http.Request, error) {
110	const APIVersion = "2020-02-01"
111	queryParameters := map[string]interface{}{
112		"api-version": APIVersion,
113	}
114	if len(client.Skiptoken) > 0 {
115		queryParameters["$skiptoken"] = autorest.Encode("query", client.Skiptoken)
116	}
117	if client.Skip != nil {
118		queryParameters["$skip"] = autorest.Encode("query", *client.Skip)
119	}
120	if client.Top != nil {
121		queryParameters["$top"] = autorest.Encode("query", *client.Top)
122	}
123	if len(selectParameter) > 0 {
124		queryParameters["$select"] = autorest.Encode("query", selectParameter)
125	}
126	if len(string(search)) > 0 {
127		queryParameters["$search"] = autorest.Encode("query", search)
128	}
129	if len(filter) > 0 {
130		queryParameters["$filter"] = autorest.Encode("query", filter)
131	}
132	if len(string(view)) > 0 {
133		queryParameters["$view"] = autorest.Encode("query", view)
134	}
135	if len(groupName) > 0 {
136		queryParameters["groupName"] = autorest.Encode("query", groupName)
137	}
138
139	preparer := autorest.CreatePreparer(
140		autorest.AsPost(),
141		autorest.WithBaseURL(client.BaseURI),
142		autorest.WithPath("/providers/Microsoft.Management/getEntities"),
143		autorest.WithQueryParameters(queryParameters))
144	if len(cacheControl) > 0 {
145		preparer = autorest.DecoratePreparer(preparer,
146			autorest.WithHeader("Cache-Control", autorest.String(cacheControl)))
147	} else {
148		preparer = autorest.DecoratePreparer(preparer,
149			autorest.WithHeader("Cache-Control", autorest.String("no-cache")))
150	}
151	return preparer.Prepare((&http.Request{}).WithContext(ctx))
152}
153
154// ListSender sends the List request. The method will close the
155// http.Response Body if it receives an error.
156func (client EntitiesClient) ListSender(req *http.Request) (*http.Response, error) {
157	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
158}
159
160// ListResponder handles the response to the List request. The method always
161// closes the http.Response Body.
162func (client EntitiesClient) ListResponder(resp *http.Response) (result EntityListResult, err error) {
163	err = autorest.Respond(
164		resp,
165		client.ByInspecting(),
166		azure.WithErrorUnlessStatusCode(http.StatusOK),
167		autorest.ByUnmarshallingJSON(&result),
168		autorest.ByClosing())
169	result.Response = autorest.Response{Response: resp}
170	return
171}
172
173// listNextResults retrieves the next set of results, if any.
174func (client EntitiesClient) listNextResults(ctx context.Context, lastResults EntityListResult) (result EntityListResult, err error) {
175	req, err := lastResults.entityListResultPreparer(ctx)
176	if err != nil {
177		return result, autorest.NewErrorWithError(err, "managementgroups.EntitiesClient", "listNextResults", nil, "Failure preparing next results request")
178	}
179	if req == nil {
180		return
181	}
182	resp, err := client.ListSender(req)
183	if err != nil {
184		result.Response = autorest.Response{Response: resp}
185		return result, autorest.NewErrorWithError(err, "managementgroups.EntitiesClient", "listNextResults", resp, "Failure sending next results request")
186	}
187	result, err = client.ListResponder(resp)
188	if err != nil {
189		err = autorest.NewErrorWithError(err, "managementgroups.EntitiesClient", "listNextResults", resp, "Failure responding to next results request")
190	}
191	return
192}
193
194// ListComplete enumerates all values, automatically crossing page boundaries as required.
195func (client EntitiesClient) ListComplete(ctx context.Context, selectParameter string, search string, filter string, view string, groupName string, cacheControl string) (result EntityListResultIterator, err error) {
196	if tracing.IsEnabled() {
197		ctx = tracing.StartSpan(ctx, fqdn+"/EntitiesClient.List")
198		defer func() {
199			sc := -1
200			if result.Response().Response.Response != nil {
201				sc = result.page.Response().Response.Response.StatusCode
202			}
203			tracing.EndSpan(ctx, sc, err)
204		}()
205	}
206	result.page, err = client.List(ctx, selectParameter, search, filter, view, groupName, cacheControl)
207	return
208}
209