1package apimanagement
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// DelegationSettingsClient is the apiManagement Client
19type DelegationSettingsClient struct {
20	BaseClient
21}
22
23// NewDelegationSettingsClient creates an instance of the DelegationSettingsClient client.
24func NewDelegationSettingsClient(subscriptionID string) DelegationSettingsClient {
25	return NewDelegationSettingsClientWithBaseURI(DefaultBaseURI, subscriptionID)
26}
27
28// NewDelegationSettingsClientWithBaseURI creates an instance of the DelegationSettingsClient client using a custom
29// endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure
30// stack).
31func NewDelegationSettingsClientWithBaseURI(baseURI string, subscriptionID string) DelegationSettingsClient {
32	return DelegationSettingsClient{NewWithBaseURI(baseURI, subscriptionID)}
33}
34
35// CreateOrUpdate create or Update Delegation settings.
36// Parameters:
37// resourceGroupName - the name of the resource group.
38// serviceName - the name of the API Management service.
39// parameters - create or update parameters.
40// ifMatch - eTag of the Entity. Not required when creating an entity, but required when updating an entity.
41func (client DelegationSettingsClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serviceName string, parameters PortalDelegationSettings, ifMatch string) (result PortalDelegationSettings, err error) {
42	if tracing.IsEnabled() {
43		ctx = tracing.StartSpan(ctx, fqdn+"/DelegationSettingsClient.CreateOrUpdate")
44		defer func() {
45			sc := -1
46			if result.Response.Response != nil {
47				sc = result.Response.Response.StatusCode
48			}
49			tracing.EndSpan(ctx, sc, err)
50		}()
51	}
52	if err := validation.Validate([]validation.Validation{
53		{TargetValue: serviceName,
54			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
55				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
56				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
57		return result, validation.NewError("apimanagement.DelegationSettingsClient", "CreateOrUpdate", err.Error())
58	}
59
60	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, serviceName, parameters, ifMatch)
61	if err != nil {
62		err = autorest.NewErrorWithError(err, "apimanagement.DelegationSettingsClient", "CreateOrUpdate", nil, "Failure preparing request")
63		return
64	}
65
66	resp, err := client.CreateOrUpdateSender(req)
67	if err != nil {
68		result.Response = autorest.Response{Response: resp}
69		err = autorest.NewErrorWithError(err, "apimanagement.DelegationSettingsClient", "CreateOrUpdate", resp, "Failure sending request")
70		return
71	}
72
73	result, err = client.CreateOrUpdateResponder(resp)
74	if err != nil {
75		err = autorest.NewErrorWithError(err, "apimanagement.DelegationSettingsClient", "CreateOrUpdate", resp, "Failure responding to request")
76		return
77	}
78
79	return
80}
81
82// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
83func (client DelegationSettingsClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serviceName string, parameters PortalDelegationSettings, ifMatch string) (*http.Request, error) {
84	pathParameters := map[string]interface{}{
85		"resourceGroupName": autorest.Encode("path", resourceGroupName),
86		"serviceName":       autorest.Encode("path", serviceName),
87		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
88	}
89
90	const APIVersion = "2019-12-01-preview"
91	queryParameters := map[string]interface{}{
92		"api-version": APIVersion,
93	}
94
95	preparer := autorest.CreatePreparer(
96		autorest.AsContentType("application/json; charset=utf-8"),
97		autorest.AsPut(),
98		autorest.WithBaseURL(client.BaseURI),
99		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/portalsettings/delegation", pathParameters),
100		autorest.WithJSON(parameters),
101		autorest.WithQueryParameters(queryParameters))
102	if len(ifMatch) > 0 {
103		preparer = autorest.DecoratePreparer(preparer,
104			autorest.WithHeader("If-Match", autorest.String(ifMatch)))
105	}
106	return preparer.Prepare((&http.Request{}).WithContext(ctx))
107}
108
109// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
110// http.Response Body if it receives an error.
111func (client DelegationSettingsClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
112	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
113}
114
115// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
116// closes the http.Response Body.
117func (client DelegationSettingsClient) CreateOrUpdateResponder(resp *http.Response) (result PortalDelegationSettings, err error) {
118	err = autorest.Respond(
119		resp,
120		azure.WithErrorUnlessStatusCode(http.StatusOK),
121		autorest.ByUnmarshallingJSON(&result),
122		autorest.ByClosing())
123	result.Response = autorest.Response{Response: resp}
124	return
125}
126
127// Get get Delegation Settings for the Portal.
128// Parameters:
129// resourceGroupName - the name of the resource group.
130// serviceName - the name of the API Management service.
131func (client DelegationSettingsClient) Get(ctx context.Context, resourceGroupName string, serviceName string) (result PortalDelegationSettings, err error) {
132	if tracing.IsEnabled() {
133		ctx = tracing.StartSpan(ctx, fqdn+"/DelegationSettingsClient.Get")
134		defer func() {
135			sc := -1
136			if result.Response.Response != nil {
137				sc = result.Response.Response.StatusCode
138			}
139			tracing.EndSpan(ctx, sc, err)
140		}()
141	}
142	if err := validation.Validate([]validation.Validation{
143		{TargetValue: serviceName,
144			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
145				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
146				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
147		return result, validation.NewError("apimanagement.DelegationSettingsClient", "Get", err.Error())
148	}
149
150	req, err := client.GetPreparer(ctx, resourceGroupName, serviceName)
151	if err != nil {
152		err = autorest.NewErrorWithError(err, "apimanagement.DelegationSettingsClient", "Get", nil, "Failure preparing request")
153		return
154	}
155
156	resp, err := client.GetSender(req)
157	if err != nil {
158		result.Response = autorest.Response{Response: resp}
159		err = autorest.NewErrorWithError(err, "apimanagement.DelegationSettingsClient", "Get", resp, "Failure sending request")
160		return
161	}
162
163	result, err = client.GetResponder(resp)
164	if err != nil {
165		err = autorest.NewErrorWithError(err, "apimanagement.DelegationSettingsClient", "Get", resp, "Failure responding to request")
166		return
167	}
168
169	return
170}
171
172// GetPreparer prepares the Get request.
173func (client DelegationSettingsClient) GetPreparer(ctx context.Context, resourceGroupName string, serviceName string) (*http.Request, error) {
174	pathParameters := map[string]interface{}{
175		"resourceGroupName": autorest.Encode("path", resourceGroupName),
176		"serviceName":       autorest.Encode("path", serviceName),
177		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
178	}
179
180	const APIVersion = "2019-12-01-preview"
181	queryParameters := map[string]interface{}{
182		"api-version": APIVersion,
183	}
184
185	preparer := autorest.CreatePreparer(
186		autorest.AsGet(),
187		autorest.WithBaseURL(client.BaseURI),
188		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/portalsettings/delegation", pathParameters),
189		autorest.WithQueryParameters(queryParameters))
190	return preparer.Prepare((&http.Request{}).WithContext(ctx))
191}
192
193// GetSender sends the Get request. The method will close the
194// http.Response Body if it receives an error.
195func (client DelegationSettingsClient) GetSender(req *http.Request) (*http.Response, error) {
196	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
197}
198
199// GetResponder handles the response to the Get request. The method always
200// closes the http.Response Body.
201func (client DelegationSettingsClient) GetResponder(resp *http.Response) (result PortalDelegationSettings, err error) {
202	err = autorest.Respond(
203		resp,
204		azure.WithErrorUnlessStatusCode(http.StatusOK),
205		autorest.ByUnmarshallingJSON(&result),
206		autorest.ByClosing())
207	result.Response = autorest.Response{Response: resp}
208	return
209}
210
211// GetEntityTag gets the entity state (Etag) version of the DelegationSettings.
212// Parameters:
213// resourceGroupName - the name of the resource group.
214// serviceName - the name of the API Management service.
215func (client DelegationSettingsClient) GetEntityTag(ctx context.Context, resourceGroupName string, serviceName string) (result autorest.Response, err error) {
216	if tracing.IsEnabled() {
217		ctx = tracing.StartSpan(ctx, fqdn+"/DelegationSettingsClient.GetEntityTag")
218		defer func() {
219			sc := -1
220			if result.Response != nil {
221				sc = result.Response.StatusCode
222			}
223			tracing.EndSpan(ctx, sc, err)
224		}()
225	}
226	if err := validation.Validate([]validation.Validation{
227		{TargetValue: serviceName,
228			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
229				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
230				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
231		return result, validation.NewError("apimanagement.DelegationSettingsClient", "GetEntityTag", err.Error())
232	}
233
234	req, err := client.GetEntityTagPreparer(ctx, resourceGroupName, serviceName)
235	if err != nil {
236		err = autorest.NewErrorWithError(err, "apimanagement.DelegationSettingsClient", "GetEntityTag", nil, "Failure preparing request")
237		return
238	}
239
240	resp, err := client.GetEntityTagSender(req)
241	if err != nil {
242		result.Response = resp
243		err = autorest.NewErrorWithError(err, "apimanagement.DelegationSettingsClient", "GetEntityTag", resp, "Failure sending request")
244		return
245	}
246
247	result, err = client.GetEntityTagResponder(resp)
248	if err != nil {
249		err = autorest.NewErrorWithError(err, "apimanagement.DelegationSettingsClient", "GetEntityTag", resp, "Failure responding to request")
250		return
251	}
252
253	return
254}
255
256// GetEntityTagPreparer prepares the GetEntityTag request.
257func (client DelegationSettingsClient) GetEntityTagPreparer(ctx context.Context, resourceGroupName string, serviceName string) (*http.Request, error) {
258	pathParameters := map[string]interface{}{
259		"resourceGroupName": autorest.Encode("path", resourceGroupName),
260		"serviceName":       autorest.Encode("path", serviceName),
261		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
262	}
263
264	const APIVersion = "2019-12-01-preview"
265	queryParameters := map[string]interface{}{
266		"api-version": APIVersion,
267	}
268
269	preparer := autorest.CreatePreparer(
270		autorest.AsHead(),
271		autorest.WithBaseURL(client.BaseURI),
272		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/portalsettings/delegation", pathParameters),
273		autorest.WithQueryParameters(queryParameters))
274	return preparer.Prepare((&http.Request{}).WithContext(ctx))
275}
276
277// GetEntityTagSender sends the GetEntityTag request. The method will close the
278// http.Response Body if it receives an error.
279func (client DelegationSettingsClient) GetEntityTagSender(req *http.Request) (*http.Response, error) {
280	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
281}
282
283// GetEntityTagResponder handles the response to the GetEntityTag request. The method always
284// closes the http.Response Body.
285func (client DelegationSettingsClient) GetEntityTagResponder(resp *http.Response) (result autorest.Response, err error) {
286	err = autorest.Respond(
287		resp,
288		azure.WithErrorUnlessStatusCode(http.StatusOK),
289		autorest.ByClosing())
290	result.Response = resp
291	return
292}
293
294// ListSecrets gets the secret validation key of the DelegationSettings.
295// Parameters:
296// resourceGroupName - the name of the resource group.
297// serviceName - the name of the API Management service.
298func (client DelegationSettingsClient) ListSecrets(ctx context.Context, resourceGroupName string, serviceName string) (result PortalSettingValidationKeyContract, err error) {
299	if tracing.IsEnabled() {
300		ctx = tracing.StartSpan(ctx, fqdn+"/DelegationSettingsClient.ListSecrets")
301		defer func() {
302			sc := -1
303			if result.Response.Response != nil {
304				sc = result.Response.Response.StatusCode
305			}
306			tracing.EndSpan(ctx, sc, err)
307		}()
308	}
309	if err := validation.Validate([]validation.Validation{
310		{TargetValue: serviceName,
311			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
312				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
313				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
314		return result, validation.NewError("apimanagement.DelegationSettingsClient", "ListSecrets", err.Error())
315	}
316
317	req, err := client.ListSecretsPreparer(ctx, resourceGroupName, serviceName)
318	if err != nil {
319		err = autorest.NewErrorWithError(err, "apimanagement.DelegationSettingsClient", "ListSecrets", nil, "Failure preparing request")
320		return
321	}
322
323	resp, err := client.ListSecretsSender(req)
324	if err != nil {
325		result.Response = autorest.Response{Response: resp}
326		err = autorest.NewErrorWithError(err, "apimanagement.DelegationSettingsClient", "ListSecrets", resp, "Failure sending request")
327		return
328	}
329
330	result, err = client.ListSecretsResponder(resp)
331	if err != nil {
332		err = autorest.NewErrorWithError(err, "apimanagement.DelegationSettingsClient", "ListSecrets", resp, "Failure responding to request")
333		return
334	}
335
336	return
337}
338
339// ListSecretsPreparer prepares the ListSecrets request.
340func (client DelegationSettingsClient) ListSecretsPreparer(ctx context.Context, resourceGroupName string, serviceName string) (*http.Request, error) {
341	pathParameters := map[string]interface{}{
342		"resourceGroupName": autorest.Encode("path", resourceGroupName),
343		"serviceName":       autorest.Encode("path", serviceName),
344		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
345	}
346
347	const APIVersion = "2019-12-01-preview"
348	queryParameters := map[string]interface{}{
349		"api-version": APIVersion,
350	}
351
352	preparer := autorest.CreatePreparer(
353		autorest.AsPost(),
354		autorest.WithBaseURL(client.BaseURI),
355		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/portalsettings/delegation/listSecrets", pathParameters),
356		autorest.WithQueryParameters(queryParameters))
357	return preparer.Prepare((&http.Request{}).WithContext(ctx))
358}
359
360// ListSecretsSender sends the ListSecrets request. The method will close the
361// http.Response Body if it receives an error.
362func (client DelegationSettingsClient) ListSecretsSender(req *http.Request) (*http.Response, error) {
363	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
364}
365
366// ListSecretsResponder handles the response to the ListSecrets request. The method always
367// closes the http.Response Body.
368func (client DelegationSettingsClient) ListSecretsResponder(resp *http.Response) (result PortalSettingValidationKeyContract, err error) {
369	err = autorest.Respond(
370		resp,
371		azure.WithErrorUnlessStatusCode(http.StatusOK),
372		autorest.ByUnmarshallingJSON(&result),
373		autorest.ByClosing())
374	result.Response = autorest.Response{Response: resp}
375	return
376}
377
378// Update update Delegation settings.
379// Parameters:
380// resourceGroupName - the name of the resource group.
381// serviceName - the name of the API Management service.
382// parameters - update Delegation settings.
383// ifMatch - eTag of the Entity. ETag should match the current entity state from the header response of the GET
384// request or it should be * for unconditional update.
385func (client DelegationSettingsClient) Update(ctx context.Context, resourceGroupName string, serviceName string, parameters PortalDelegationSettings, ifMatch string) (result autorest.Response, err error) {
386	if tracing.IsEnabled() {
387		ctx = tracing.StartSpan(ctx, fqdn+"/DelegationSettingsClient.Update")
388		defer func() {
389			sc := -1
390			if result.Response != nil {
391				sc = result.Response.StatusCode
392			}
393			tracing.EndSpan(ctx, sc, err)
394		}()
395	}
396	if err := validation.Validate([]validation.Validation{
397		{TargetValue: serviceName,
398			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
399				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
400				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
401		return result, validation.NewError("apimanagement.DelegationSettingsClient", "Update", err.Error())
402	}
403
404	req, err := client.UpdatePreparer(ctx, resourceGroupName, serviceName, parameters, ifMatch)
405	if err != nil {
406		err = autorest.NewErrorWithError(err, "apimanagement.DelegationSettingsClient", "Update", nil, "Failure preparing request")
407		return
408	}
409
410	resp, err := client.UpdateSender(req)
411	if err != nil {
412		result.Response = resp
413		err = autorest.NewErrorWithError(err, "apimanagement.DelegationSettingsClient", "Update", resp, "Failure sending request")
414		return
415	}
416
417	result, err = client.UpdateResponder(resp)
418	if err != nil {
419		err = autorest.NewErrorWithError(err, "apimanagement.DelegationSettingsClient", "Update", resp, "Failure responding to request")
420		return
421	}
422
423	return
424}
425
426// UpdatePreparer prepares the Update request.
427func (client DelegationSettingsClient) UpdatePreparer(ctx context.Context, resourceGroupName string, serviceName string, parameters PortalDelegationSettings, ifMatch string) (*http.Request, error) {
428	pathParameters := map[string]interface{}{
429		"resourceGroupName": autorest.Encode("path", resourceGroupName),
430		"serviceName":       autorest.Encode("path", serviceName),
431		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
432	}
433
434	const APIVersion = "2019-12-01-preview"
435	queryParameters := map[string]interface{}{
436		"api-version": APIVersion,
437	}
438
439	preparer := autorest.CreatePreparer(
440		autorest.AsContentType("application/json; charset=utf-8"),
441		autorest.AsPatch(),
442		autorest.WithBaseURL(client.BaseURI),
443		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/portalsettings/delegation", pathParameters),
444		autorest.WithJSON(parameters),
445		autorest.WithQueryParameters(queryParameters),
446		autorest.WithHeader("If-Match", autorest.String(ifMatch)))
447	return preparer.Prepare((&http.Request{}).WithContext(ctx))
448}
449
450// UpdateSender sends the Update request. The method will close the
451// http.Response Body if it receives an error.
452func (client DelegationSettingsClient) UpdateSender(req *http.Request) (*http.Response, error) {
453	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
454}
455
456// UpdateResponder handles the response to the Update request. The method always
457// closes the http.Response Body.
458func (client DelegationSettingsClient) UpdateResponder(resp *http.Response) (result autorest.Response, err error) {
459	err = autorest.Respond(
460		resp,
461		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
462		autorest.ByClosing())
463	result.Response = resp
464	return
465}
466