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	"io"
16	"net/http"
17)
18
19// TenantPolicyClient is the apiManagement Client
20type TenantPolicyClient struct {
21	BaseClient
22}
23
24// NewTenantPolicyClient creates an instance of the TenantPolicyClient client.
25func NewTenantPolicyClient(subscriptionID string) TenantPolicyClient {
26	return NewTenantPolicyClientWithBaseURI(DefaultBaseURI, subscriptionID)
27}
28
29// NewTenantPolicyClientWithBaseURI creates an instance of the TenantPolicyClient client using a custom endpoint.  Use
30// this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
31func NewTenantPolicyClientWithBaseURI(baseURI string, subscriptionID string) TenantPolicyClient {
32	return TenantPolicyClient{NewWithBaseURI(baseURI, subscriptionID)}
33}
34
35// CreateOrUpdate creates or updates global policy configuration for the tenant.
36// Parameters:
37// resourceGroupName - the name of the resource group.
38// serviceName - the name of the API Management service.
39// parameters - the policy content details.
40// ifMatch - the entity state (Etag) version of the tenant policy to update. A value of "*" can be used for
41// If-Match to unconditionally apply the operation.
42func (client TenantPolicyClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serviceName string, parameters io.ReadCloser, ifMatch string) (result autorest.Response, err error) {
43	if tracing.IsEnabled() {
44		ctx = tracing.StartSpan(ctx, fqdn+"/TenantPolicyClient.CreateOrUpdate")
45		defer func() {
46			sc := -1
47			if result.Response != nil {
48				sc = result.Response.StatusCode
49			}
50			tracing.EndSpan(ctx, sc, err)
51		}()
52	}
53	if err := validation.Validate([]validation.Validation{
54		{TargetValue: serviceName,
55			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
56				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
57				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
58		return result, validation.NewError("apimanagement.TenantPolicyClient", "CreateOrUpdate", err.Error())
59	}
60
61	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, serviceName, parameters, ifMatch)
62	if err != nil {
63		err = autorest.NewErrorWithError(err, "apimanagement.TenantPolicyClient", "CreateOrUpdate", nil, "Failure preparing request")
64		return
65	}
66
67	resp, err := client.CreateOrUpdateSender(req)
68	if err != nil {
69		result.Response = resp
70		err = autorest.NewErrorWithError(err, "apimanagement.TenantPolicyClient", "CreateOrUpdate", resp, "Failure sending request")
71		return
72	}
73
74	result, err = client.CreateOrUpdateResponder(resp)
75	if err != nil {
76		err = autorest.NewErrorWithError(err, "apimanagement.TenantPolicyClient", "CreateOrUpdate", resp, "Failure responding to request")
77		return
78	}
79
80	return
81}
82
83// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
84func (client TenantPolicyClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serviceName string, parameters io.ReadCloser, ifMatch string) (*http.Request, error) {
85	pathParameters := map[string]interface{}{
86		"resourceGroupName": autorest.Encode("path", resourceGroupName),
87		"serviceName":       autorest.Encode("path", serviceName),
88		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
89	}
90
91	const APIVersion = "2016-10-10"
92	queryParameters := map[string]interface{}{
93		"api-version": APIVersion,
94	}
95
96	preparer := autorest.CreatePreparer(
97		autorest.AsContentType("application/vnd.ms-azure-apim.policy+xml"),
98		autorest.AsPut(),
99		autorest.WithBaseURL(client.BaseURI),
100		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/tenant/policy", pathParameters),
101		autorest.WithFile(parameters),
102		autorest.WithQueryParameters(queryParameters),
103		autorest.WithHeader("If-Match", autorest.String(ifMatch)))
104	return preparer.Prepare((&http.Request{}).WithContext(ctx))
105}
106
107// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
108// http.Response Body if it receives an error.
109func (client TenantPolicyClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
110	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
111}
112
113// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
114// closes the http.Response Body.
115func (client TenantPolicyClient) CreateOrUpdateResponder(resp *http.Response) (result autorest.Response, err error) {
116	err = autorest.Respond(
117		resp,
118		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated, http.StatusNoContent),
119		autorest.ByClosing())
120	result.Response = resp
121	return
122}
123
124// Delete deletes the global tenant policy configuration.
125// Parameters:
126// resourceGroupName - the name of the resource group.
127// serviceName - the name of the API Management service.
128// ifMatch - the entity state (Etag) version of the tenant policy to update. A value of "*" can be used for
129// If-Match to unconditionally apply the operation.
130func (client TenantPolicyClient) Delete(ctx context.Context, resourceGroupName string, serviceName string, ifMatch string) (result autorest.Response, err error) {
131	if tracing.IsEnabled() {
132		ctx = tracing.StartSpan(ctx, fqdn+"/TenantPolicyClient.Delete")
133		defer func() {
134			sc := -1
135			if result.Response != nil {
136				sc = result.Response.StatusCode
137			}
138			tracing.EndSpan(ctx, sc, err)
139		}()
140	}
141	if err := validation.Validate([]validation.Validation{
142		{TargetValue: serviceName,
143			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
144				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
145				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
146		return result, validation.NewError("apimanagement.TenantPolicyClient", "Delete", err.Error())
147	}
148
149	req, err := client.DeletePreparer(ctx, resourceGroupName, serviceName, ifMatch)
150	if err != nil {
151		err = autorest.NewErrorWithError(err, "apimanagement.TenantPolicyClient", "Delete", nil, "Failure preparing request")
152		return
153	}
154
155	resp, err := client.DeleteSender(req)
156	if err != nil {
157		result.Response = resp
158		err = autorest.NewErrorWithError(err, "apimanagement.TenantPolicyClient", "Delete", resp, "Failure sending request")
159		return
160	}
161
162	result, err = client.DeleteResponder(resp)
163	if err != nil {
164		err = autorest.NewErrorWithError(err, "apimanagement.TenantPolicyClient", "Delete", resp, "Failure responding to request")
165		return
166	}
167
168	return
169}
170
171// DeletePreparer prepares the Delete request.
172func (client TenantPolicyClient) DeletePreparer(ctx context.Context, resourceGroupName string, serviceName string, ifMatch string) (*http.Request, error) {
173	pathParameters := map[string]interface{}{
174		"resourceGroupName": autorest.Encode("path", resourceGroupName),
175		"serviceName":       autorest.Encode("path", serviceName),
176		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
177	}
178
179	const APIVersion = "2016-10-10"
180	queryParameters := map[string]interface{}{
181		"api-version": APIVersion,
182	}
183
184	preparer := autorest.CreatePreparer(
185		autorest.AsDelete(),
186		autorest.WithBaseURL(client.BaseURI),
187		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/tenant/policy", pathParameters),
188		autorest.WithQueryParameters(queryParameters),
189		autorest.WithHeader("If-Match", autorest.String(ifMatch)))
190	return preparer.Prepare((&http.Request{}).WithContext(ctx))
191}
192
193// DeleteSender sends the Delete request. The method will close the
194// http.Response Body if it receives an error.
195func (client TenantPolicyClient) DeleteSender(req *http.Request) (*http.Response, error) {
196	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
197}
198
199// DeleteResponder handles the response to the Delete request. The method always
200// closes the http.Response Body.
201func (client TenantPolicyClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
202	err = autorest.Respond(
203		resp,
204		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
205		autorest.ByClosing())
206	result.Response = resp
207	return
208}
209
210// Get get the global policy configuration of the tenant.
211// Parameters:
212// resourceGroupName - the name of the resource group.
213// serviceName - the name of the API Management service.
214func (client TenantPolicyClient) Get(ctx context.Context, resourceGroupName string, serviceName string) (result ReadCloser, err error) {
215	if tracing.IsEnabled() {
216		ctx = tracing.StartSpan(ctx, fqdn+"/TenantPolicyClient.Get")
217		defer func() {
218			sc := -1
219			if result.Response.Response != nil {
220				sc = result.Response.Response.StatusCode
221			}
222			tracing.EndSpan(ctx, sc, err)
223		}()
224	}
225	if err := validation.Validate([]validation.Validation{
226		{TargetValue: serviceName,
227			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
228				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
229				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
230		return result, validation.NewError("apimanagement.TenantPolicyClient", "Get", err.Error())
231	}
232
233	req, err := client.GetPreparer(ctx, resourceGroupName, serviceName)
234	if err != nil {
235		err = autorest.NewErrorWithError(err, "apimanagement.TenantPolicyClient", "Get", nil, "Failure preparing request")
236		return
237	}
238
239	resp, err := client.GetSender(req)
240	if err != nil {
241		result.Response = autorest.Response{Response: resp}
242		err = autorest.NewErrorWithError(err, "apimanagement.TenantPolicyClient", "Get", resp, "Failure sending request")
243		return
244	}
245
246	result, err = client.GetResponder(resp)
247	if err != nil {
248		err = autorest.NewErrorWithError(err, "apimanagement.TenantPolicyClient", "Get", resp, "Failure responding to request")
249		return
250	}
251
252	return
253}
254
255// GetPreparer prepares the Get request.
256func (client TenantPolicyClient) GetPreparer(ctx context.Context, resourceGroupName string, serviceName string) (*http.Request, error) {
257	pathParameters := map[string]interface{}{
258		"resourceGroupName": autorest.Encode("path", resourceGroupName),
259		"serviceName":       autorest.Encode("path", serviceName),
260		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
261	}
262
263	const APIVersion = "2016-10-10"
264	queryParameters := map[string]interface{}{
265		"api-version": APIVersion,
266	}
267
268	preparer := autorest.CreatePreparer(
269		autorest.AsGet(),
270		autorest.WithBaseURL(client.BaseURI),
271		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/tenant/policy", pathParameters),
272		autorest.WithQueryParameters(queryParameters))
273	return preparer.Prepare((&http.Request{}).WithContext(ctx))
274}
275
276// GetSender sends the Get request. The method will close the
277// http.Response Body if it receives an error.
278func (client TenantPolicyClient) GetSender(req *http.Request) (*http.Response, error) {
279	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
280}
281
282// GetResponder handles the response to the Get request. The method always
283// closes the http.Response Body.
284func (client TenantPolicyClient) GetResponder(resp *http.Response) (result ReadCloser, err error) {
285	result.Value = &resp.Body
286	err = autorest.Respond(
287		resp,
288		azure.WithErrorUnlessStatusCode(http.StatusOK))
289	result.Response = autorest.Response{Response: resp}
290	return
291}
292