1package containerservice
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/autorest/validation"
14	"github.com/Azure/go-autorest/tracing"
15	"net/http"
16)
17
18// AgentPoolsClient is the the Container Service Client.
19type AgentPoolsClient struct {
20	BaseClient
21}
22
23// NewAgentPoolsClient creates an instance of the AgentPoolsClient client.
24func NewAgentPoolsClient(subscriptionID string) AgentPoolsClient {
25	return NewAgentPoolsClientWithBaseURI(DefaultBaseURI, subscriptionID)
26}
27
28// NewAgentPoolsClientWithBaseURI creates an instance of the AgentPoolsClient client using a custom endpoint.  Use this
29// when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
30func NewAgentPoolsClientWithBaseURI(baseURI string, subscriptionID string) AgentPoolsClient {
31	return AgentPoolsClient{NewWithBaseURI(baseURI, subscriptionID)}
32}
33
34// CreateOrUpdate creates or updates an agent pool in the specified managed cluster.
35// Parameters:
36// resourceGroupName - the name of the resource group.
37// managedClusterName - the name of the managed cluster resource.
38// agentPoolName - the name of the agent pool.
39// parameters - parameters supplied to the Create or Update an agent pool operation.
40func (client AgentPoolsClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, managedClusterName string, agentPoolName string, parameters AgentPool) (result AgentPoolsCreateOrUpdateFuture, err error) {
41	if tracing.IsEnabled() {
42		ctx = tracing.StartSpan(ctx, fqdn+"/AgentPoolsClient.CreateOrUpdate")
43		defer func() {
44			sc := -1
45			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
46				sc = result.FutureAPI.Response().StatusCode
47			}
48			tracing.EndSpan(ctx, sc, err)
49		}()
50	}
51	if err := validation.Validate([]validation.Validation{
52		{TargetValue: resourceGroupName,
53			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil}}},
54		{TargetValue: parameters,
55			Constraints: []validation.Constraint{{Target: "parameters.ManagedClusterAgentPoolProfileProperties", Name: validation.Null, Rule: false,
56				Chain: []validation.Constraint{{Target: "parameters.ManagedClusterAgentPoolProfileProperties.Count", Name: validation.Null, Rule: true,
57					Chain: []validation.Constraint{{Target: "parameters.ManagedClusterAgentPoolProfileProperties.Count", Name: validation.InclusiveMaximum, Rule: int64(100), Chain: nil},
58						{Target: "parameters.ManagedClusterAgentPoolProfileProperties.Count", Name: validation.InclusiveMinimum, Rule: int64(1), Chain: nil},
59					}},
60				}}}}}); err != nil {
61		return result, validation.NewError("containerservice.AgentPoolsClient", "CreateOrUpdate", err.Error())
62	}
63
64	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, managedClusterName, agentPoolName, parameters)
65	if err != nil {
66		err = autorest.NewErrorWithError(err, "containerservice.AgentPoolsClient", "CreateOrUpdate", nil, "Failure preparing request")
67		return
68	}
69
70	result, err = client.CreateOrUpdateSender(req)
71	if err != nil {
72		err = autorest.NewErrorWithError(err, "containerservice.AgentPoolsClient", "CreateOrUpdate", nil, "Failure sending request")
73		return
74	}
75
76	return
77}
78
79// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
80func (client AgentPoolsClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, managedClusterName string, agentPoolName string, parameters AgentPool) (*http.Request, error) {
81	pathParameters := map[string]interface{}{
82		"agentPoolName":      autorest.Encode("path", agentPoolName),
83		"managedClusterName": autorest.Encode("path", managedClusterName),
84		"resourceGroupName":  autorest.Encode("path", resourceGroupName),
85		"subscriptionId":     autorest.Encode("path", client.SubscriptionID),
86	}
87
88	const APIVersion = "2019-02-01"
89	queryParameters := map[string]interface{}{
90		"api-version": APIVersion,
91	}
92
93	preparer := autorest.CreatePreparer(
94		autorest.AsContentType("application/json; charset=utf-8"),
95		autorest.AsPut(),
96		autorest.WithBaseURL(client.BaseURI),
97		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ContainerService/managedClusters/{managedClusterName}/agentPools/{agentPoolName}", pathParameters),
98		autorest.WithJSON(parameters),
99		autorest.WithQueryParameters(queryParameters))
100	return preparer.Prepare((&http.Request{}).WithContext(ctx))
101}
102
103// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
104// http.Response Body if it receives an error.
105func (client AgentPoolsClient) CreateOrUpdateSender(req *http.Request) (future AgentPoolsCreateOrUpdateFuture, err error) {
106	var resp *http.Response
107	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
108	if err != nil {
109		return
110	}
111	var azf azure.Future
112	azf, err = azure.NewFutureFromResponse(resp)
113	future.FutureAPI = &azf
114	future.Result = future.result
115	return
116}
117
118// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
119// closes the http.Response Body.
120func (client AgentPoolsClient) CreateOrUpdateResponder(resp *http.Response) (result AgentPool, err error) {
121	err = autorest.Respond(
122		resp,
123		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
124		autorest.ByUnmarshallingJSON(&result),
125		autorest.ByClosing())
126	result.Response = autorest.Response{Response: resp}
127	return
128}
129
130// Delete deletes the agent pool in the specified managed cluster.
131// Parameters:
132// resourceGroupName - the name of the resource group.
133// managedClusterName - the name of the managed cluster resource.
134// agentPoolName - the name of the agent pool.
135func (client AgentPoolsClient) Delete(ctx context.Context, resourceGroupName string, managedClusterName string, agentPoolName string) (result AgentPoolsDeleteFuture, err error) {
136	if tracing.IsEnabled() {
137		ctx = tracing.StartSpan(ctx, fqdn+"/AgentPoolsClient.Delete")
138		defer func() {
139			sc := -1
140			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
141				sc = result.FutureAPI.Response().StatusCode
142			}
143			tracing.EndSpan(ctx, sc, err)
144		}()
145	}
146	if err := validation.Validate([]validation.Validation{
147		{TargetValue: resourceGroupName,
148			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
149		return result, validation.NewError("containerservice.AgentPoolsClient", "Delete", err.Error())
150	}
151
152	req, err := client.DeletePreparer(ctx, resourceGroupName, managedClusterName, agentPoolName)
153	if err != nil {
154		err = autorest.NewErrorWithError(err, "containerservice.AgentPoolsClient", "Delete", nil, "Failure preparing request")
155		return
156	}
157
158	result, err = client.DeleteSender(req)
159	if err != nil {
160		err = autorest.NewErrorWithError(err, "containerservice.AgentPoolsClient", "Delete", nil, "Failure sending request")
161		return
162	}
163
164	return
165}
166
167// DeletePreparer prepares the Delete request.
168func (client AgentPoolsClient) DeletePreparer(ctx context.Context, resourceGroupName string, managedClusterName string, agentPoolName string) (*http.Request, error) {
169	pathParameters := map[string]interface{}{
170		"agentPoolName":      autorest.Encode("path", agentPoolName),
171		"managedClusterName": autorest.Encode("path", managedClusterName),
172		"resourceGroupName":  autorest.Encode("path", resourceGroupName),
173		"subscriptionId":     autorest.Encode("path", client.SubscriptionID),
174	}
175
176	const APIVersion = "2019-02-01"
177	queryParameters := map[string]interface{}{
178		"api-version": APIVersion,
179	}
180
181	preparer := autorest.CreatePreparer(
182		autorest.AsDelete(),
183		autorest.WithBaseURL(client.BaseURI),
184		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ContainerService/managedClusters/{managedClusterName}/agentPools/{agentPoolName}", pathParameters),
185		autorest.WithQueryParameters(queryParameters))
186	return preparer.Prepare((&http.Request{}).WithContext(ctx))
187}
188
189// DeleteSender sends the Delete request. The method will close the
190// http.Response Body if it receives an error.
191func (client AgentPoolsClient) DeleteSender(req *http.Request) (future AgentPoolsDeleteFuture, err error) {
192	var resp *http.Response
193	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
194	if err != nil {
195		return
196	}
197	var azf azure.Future
198	azf, err = azure.NewFutureFromResponse(resp)
199	future.FutureAPI = &azf
200	future.Result = future.result
201	return
202}
203
204// DeleteResponder handles the response to the Delete request. The method always
205// closes the http.Response Body.
206func (client AgentPoolsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
207	err = autorest.Respond(
208		resp,
209		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
210		autorest.ByClosing())
211	result.Response = resp
212	return
213}
214
215// Get gets the details of the agent pool by managed cluster and resource group.
216// Parameters:
217// resourceGroupName - the name of the resource group.
218// managedClusterName - the name of the managed cluster resource.
219// agentPoolName - the name of the agent pool.
220func (client AgentPoolsClient) Get(ctx context.Context, resourceGroupName string, managedClusterName string, agentPoolName string) (result AgentPool, err error) {
221	if tracing.IsEnabled() {
222		ctx = tracing.StartSpan(ctx, fqdn+"/AgentPoolsClient.Get")
223		defer func() {
224			sc := -1
225			if result.Response.Response != nil {
226				sc = result.Response.Response.StatusCode
227			}
228			tracing.EndSpan(ctx, sc, err)
229		}()
230	}
231	if err := validation.Validate([]validation.Validation{
232		{TargetValue: resourceGroupName,
233			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
234		return result, validation.NewError("containerservice.AgentPoolsClient", "Get", err.Error())
235	}
236
237	req, err := client.GetPreparer(ctx, resourceGroupName, managedClusterName, agentPoolName)
238	if err != nil {
239		err = autorest.NewErrorWithError(err, "containerservice.AgentPoolsClient", "Get", nil, "Failure preparing request")
240		return
241	}
242
243	resp, err := client.GetSender(req)
244	if err != nil {
245		result.Response = autorest.Response{Response: resp}
246		err = autorest.NewErrorWithError(err, "containerservice.AgentPoolsClient", "Get", resp, "Failure sending request")
247		return
248	}
249
250	result, err = client.GetResponder(resp)
251	if err != nil {
252		err = autorest.NewErrorWithError(err, "containerservice.AgentPoolsClient", "Get", resp, "Failure responding to request")
253		return
254	}
255
256	return
257}
258
259// GetPreparer prepares the Get request.
260func (client AgentPoolsClient) GetPreparer(ctx context.Context, resourceGroupName string, managedClusterName string, agentPoolName string) (*http.Request, error) {
261	pathParameters := map[string]interface{}{
262		"agentPoolName":      autorest.Encode("path", agentPoolName),
263		"managedClusterName": autorest.Encode("path", managedClusterName),
264		"resourceGroupName":  autorest.Encode("path", resourceGroupName),
265		"subscriptionId":     autorest.Encode("path", client.SubscriptionID),
266	}
267
268	const APIVersion = "2019-02-01"
269	queryParameters := map[string]interface{}{
270		"api-version": APIVersion,
271	}
272
273	preparer := autorest.CreatePreparer(
274		autorest.AsGet(),
275		autorest.WithBaseURL(client.BaseURI),
276		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ContainerService/managedClusters/{managedClusterName}/agentPools/{agentPoolName}", pathParameters),
277		autorest.WithQueryParameters(queryParameters))
278	return preparer.Prepare((&http.Request{}).WithContext(ctx))
279}
280
281// GetSender sends the Get request. The method will close the
282// http.Response Body if it receives an error.
283func (client AgentPoolsClient) GetSender(req *http.Request) (*http.Response, error) {
284	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
285}
286
287// GetResponder handles the response to the Get request. The method always
288// closes the http.Response Body.
289func (client AgentPoolsClient) GetResponder(resp *http.Response) (result AgentPool, err error) {
290	err = autorest.Respond(
291		resp,
292		azure.WithErrorUnlessStatusCode(http.StatusOK),
293		autorest.ByUnmarshallingJSON(&result),
294		autorest.ByClosing())
295	result.Response = autorest.Response{Response: resp}
296	return
297}
298
299// List gets a list of agent pools in the specified managed cluster. The operation returns properties of each agent
300// pool.
301// Parameters:
302// resourceGroupName - the name of the resource group.
303// managedClusterName - the name of the managed cluster resource.
304func (client AgentPoolsClient) List(ctx context.Context, resourceGroupName string, managedClusterName string) (result AgentPoolListResultPage, err error) {
305	if tracing.IsEnabled() {
306		ctx = tracing.StartSpan(ctx, fqdn+"/AgentPoolsClient.List")
307		defer func() {
308			sc := -1
309			if result.aplr.Response.Response != nil {
310				sc = result.aplr.Response.Response.StatusCode
311			}
312			tracing.EndSpan(ctx, sc, err)
313		}()
314	}
315	if err := validation.Validate([]validation.Validation{
316		{TargetValue: resourceGroupName,
317			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
318		return result, validation.NewError("containerservice.AgentPoolsClient", "List", err.Error())
319	}
320
321	result.fn = client.listNextResults
322	req, err := client.ListPreparer(ctx, resourceGroupName, managedClusterName)
323	if err != nil {
324		err = autorest.NewErrorWithError(err, "containerservice.AgentPoolsClient", "List", nil, "Failure preparing request")
325		return
326	}
327
328	resp, err := client.ListSender(req)
329	if err != nil {
330		result.aplr.Response = autorest.Response{Response: resp}
331		err = autorest.NewErrorWithError(err, "containerservice.AgentPoolsClient", "List", resp, "Failure sending request")
332		return
333	}
334
335	result.aplr, err = client.ListResponder(resp)
336	if err != nil {
337		err = autorest.NewErrorWithError(err, "containerservice.AgentPoolsClient", "List", resp, "Failure responding to request")
338		return
339	}
340	if result.aplr.hasNextLink() && result.aplr.IsEmpty() {
341		err = result.NextWithContext(ctx)
342		return
343	}
344
345	return
346}
347
348// ListPreparer prepares the List request.
349func (client AgentPoolsClient) ListPreparer(ctx context.Context, resourceGroupName string, managedClusterName string) (*http.Request, error) {
350	pathParameters := map[string]interface{}{
351		"managedClusterName": autorest.Encode("path", managedClusterName),
352		"resourceGroupName":  autorest.Encode("path", resourceGroupName),
353		"subscriptionId":     autorest.Encode("path", client.SubscriptionID),
354	}
355
356	const APIVersion = "2019-02-01"
357	queryParameters := map[string]interface{}{
358		"api-version": APIVersion,
359	}
360
361	preparer := autorest.CreatePreparer(
362		autorest.AsGet(),
363		autorest.WithBaseURL(client.BaseURI),
364		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ContainerService/managedClusters/{managedClusterName}/agentPools", pathParameters),
365		autorest.WithQueryParameters(queryParameters))
366	return preparer.Prepare((&http.Request{}).WithContext(ctx))
367}
368
369// ListSender sends the List request. The method will close the
370// http.Response Body if it receives an error.
371func (client AgentPoolsClient) ListSender(req *http.Request) (*http.Response, error) {
372	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
373}
374
375// ListResponder handles the response to the List request. The method always
376// closes the http.Response Body.
377func (client AgentPoolsClient) ListResponder(resp *http.Response) (result AgentPoolListResult, err error) {
378	err = autorest.Respond(
379		resp,
380		azure.WithErrorUnlessStatusCode(http.StatusOK),
381		autorest.ByUnmarshallingJSON(&result),
382		autorest.ByClosing())
383	result.Response = autorest.Response{Response: resp}
384	return
385}
386
387// listNextResults retrieves the next set of results, if any.
388func (client AgentPoolsClient) listNextResults(ctx context.Context, lastResults AgentPoolListResult) (result AgentPoolListResult, err error) {
389	req, err := lastResults.agentPoolListResultPreparer(ctx)
390	if err != nil {
391		return result, autorest.NewErrorWithError(err, "containerservice.AgentPoolsClient", "listNextResults", nil, "Failure preparing next results request")
392	}
393	if req == nil {
394		return
395	}
396	resp, err := client.ListSender(req)
397	if err != nil {
398		result.Response = autorest.Response{Response: resp}
399		return result, autorest.NewErrorWithError(err, "containerservice.AgentPoolsClient", "listNextResults", resp, "Failure sending next results request")
400	}
401	result, err = client.ListResponder(resp)
402	if err != nil {
403		err = autorest.NewErrorWithError(err, "containerservice.AgentPoolsClient", "listNextResults", resp, "Failure responding to next results request")
404	}
405	return
406}
407
408// ListComplete enumerates all values, automatically crossing page boundaries as required.
409func (client AgentPoolsClient) ListComplete(ctx context.Context, resourceGroupName string, managedClusterName string) (result AgentPoolListResultIterator, err error) {
410	if tracing.IsEnabled() {
411		ctx = tracing.StartSpan(ctx, fqdn+"/AgentPoolsClient.List")
412		defer func() {
413			sc := -1
414			if result.Response().Response.Response != nil {
415				sc = result.page.Response().Response.Response.StatusCode
416			}
417			tracing.EndSpan(ctx, sc, err)
418		}()
419	}
420	result.page, err = client.List(ctx, resourceGroupName, managedClusterName)
421	return
422}
423