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