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// TenantConfigurationClient is the client for the TenantConfiguration methods of the Apimanagement service.
30type TenantConfigurationClient struct {
31	BaseClient
32}
33
34// NewTenantConfigurationClient creates an instance of the TenantConfigurationClient client.
35func NewTenantConfigurationClient() TenantConfigurationClient {
36	return TenantConfigurationClient{New()}
37}
38
39// Deploy this operation applies changes from the specified Git branch to the configuration database. This is a long
40// running operation and could take several minutes to complete.
41// Parameters:
42// apimBaseURL - the management endpoint of the API Management service, for example
43// https://myapimservice.management.azure-api.net.
44// parameters - deploy Configuration parameters.
45func (client TenantConfigurationClient) Deploy(ctx context.Context, apimBaseURL string, parameters DeployConfigurationParameters) (result TenantConfigurationDeployFuture, err error) {
46	if tracing.IsEnabled() {
47		ctx = tracing.StartSpan(ctx, fqdn+"/TenantConfigurationClient.Deploy")
48		defer func() {
49			sc := -1
50			if result.Response() != nil {
51				sc = result.Response().StatusCode
52			}
53			tracing.EndSpan(ctx, sc, err)
54		}()
55	}
56	if err := validation.Validate([]validation.Validation{
57		{TargetValue: parameters,
58			Constraints: []validation.Constraint{{Target: "parameters.Branch", Name: validation.Null, Rule: true, Chain: nil}}}}); err != nil {
59		return result, validation.NewError("apimanagement.TenantConfigurationClient", "Deploy", err.Error())
60	}
61
62	req, err := client.DeployPreparer(ctx, apimBaseURL, parameters)
63	if err != nil {
64		err = autorest.NewErrorWithError(err, "apimanagement.TenantConfigurationClient", "Deploy", nil, "Failure preparing request")
65		return
66	}
67
68	result, err = client.DeploySender(req)
69	if err != nil {
70		err = autorest.NewErrorWithError(err, "apimanagement.TenantConfigurationClient", "Deploy", result.Response(), "Failure sending request")
71		return
72	}
73
74	return
75}
76
77// DeployPreparer prepares the Deploy request.
78func (client TenantConfigurationClient) DeployPreparer(ctx context.Context, apimBaseURL string, parameters DeployConfigurationParameters) (*http.Request, error) {
79	urlParameters := map[string]interface{}{
80		"apimBaseUrl": apimBaseURL,
81	}
82
83	pathParameters := map[string]interface{}{
84		"configurationName": autorest.Encode("path", "configuration"),
85	}
86
87	const APIVersion = "2017-03-01"
88	queryParameters := map[string]interface{}{
89		"api-version": APIVersion,
90	}
91
92	preparer := autorest.CreatePreparer(
93		autorest.AsContentType("application/json; charset=utf-8"),
94		autorest.AsPost(),
95		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
96		autorest.WithPathParameters("/tenant/{configurationName}/deploy", pathParameters),
97		autorest.WithJSON(parameters),
98		autorest.WithQueryParameters(queryParameters))
99	return preparer.Prepare((&http.Request{}).WithContext(ctx))
100}
101
102// DeploySender sends the Deploy request. The method will close the
103// http.Response Body if it receives an error.
104func (client TenantConfigurationClient) DeploySender(req *http.Request) (future TenantConfigurationDeployFuture, err error) {
105	var resp *http.Response
106	resp, err = client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
107	if err != nil {
108		return
109	}
110	future.Future, err = azure.NewFutureFromResponse(resp)
111	return
112}
113
114// DeployResponder handles the response to the Deploy request. The method always
115// closes the http.Response Body.
116func (client TenantConfigurationClient) DeployResponder(resp *http.Response) (result OperationResultContract, err error) {
117	err = autorest.Respond(
118		resp,
119		client.ByInspecting(),
120		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
121		autorest.ByUnmarshallingJSON(&result),
122		autorest.ByClosing())
123	result.Response = autorest.Response{Response: resp}
124	return
125}
126
127// GetSyncState gets the status of the most recent synchronization between the configuration database and the Git
128// repository.
129// Parameters:
130// apimBaseURL - the management endpoint of the API Management service, for example
131// https://myapimservice.management.azure-api.net.
132func (client TenantConfigurationClient) GetSyncState(ctx context.Context, apimBaseURL string) (result TenantConfigurationSyncStateContract, err error) {
133	if tracing.IsEnabled() {
134		ctx = tracing.StartSpan(ctx, fqdn+"/TenantConfigurationClient.GetSyncState")
135		defer func() {
136			sc := -1
137			if result.Response.Response != nil {
138				sc = result.Response.Response.StatusCode
139			}
140			tracing.EndSpan(ctx, sc, err)
141		}()
142	}
143	req, err := client.GetSyncStatePreparer(ctx, apimBaseURL)
144	if err != nil {
145		err = autorest.NewErrorWithError(err, "apimanagement.TenantConfigurationClient", "GetSyncState", nil, "Failure preparing request")
146		return
147	}
148
149	resp, err := client.GetSyncStateSender(req)
150	if err != nil {
151		result.Response = autorest.Response{Response: resp}
152		err = autorest.NewErrorWithError(err, "apimanagement.TenantConfigurationClient", "GetSyncState", resp, "Failure sending request")
153		return
154	}
155
156	result, err = client.GetSyncStateResponder(resp)
157	if err != nil {
158		err = autorest.NewErrorWithError(err, "apimanagement.TenantConfigurationClient", "GetSyncState", resp, "Failure responding to request")
159	}
160
161	return
162}
163
164// GetSyncStatePreparer prepares the GetSyncState request.
165func (client TenantConfigurationClient) GetSyncStatePreparer(ctx context.Context, apimBaseURL string) (*http.Request, error) {
166	urlParameters := map[string]interface{}{
167		"apimBaseUrl": apimBaseURL,
168	}
169
170	pathParameters := map[string]interface{}{
171		"configurationName": autorest.Encode("path", "configuration"),
172	}
173
174	const APIVersion = "2017-03-01"
175	queryParameters := map[string]interface{}{
176		"api-version": APIVersion,
177	}
178
179	preparer := autorest.CreatePreparer(
180		autorest.AsGet(),
181		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
182		autorest.WithPathParameters("/tenant/{configurationName}/syncState", pathParameters),
183		autorest.WithQueryParameters(queryParameters))
184	return preparer.Prepare((&http.Request{}).WithContext(ctx))
185}
186
187// GetSyncStateSender sends the GetSyncState request. The method will close the
188// http.Response Body if it receives an error.
189func (client TenantConfigurationClient) GetSyncStateSender(req *http.Request) (*http.Response, error) {
190	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
191}
192
193// GetSyncStateResponder handles the response to the GetSyncState request. The method always
194// closes the http.Response Body.
195func (client TenantConfigurationClient) GetSyncStateResponder(resp *http.Response) (result TenantConfigurationSyncStateContract, err error) {
196	err = autorest.Respond(
197		resp,
198		client.ByInspecting(),
199		azure.WithErrorUnlessStatusCode(http.StatusOK),
200		autorest.ByUnmarshallingJSON(&result),
201		autorest.ByClosing())
202	result.Response = autorest.Response{Response: resp}
203	return
204}
205
206// Save this operation creates a commit with the current configuration snapshot to the specified branch in the
207// repository. This is a long running operation and could take several minutes to complete.
208// Parameters:
209// apimBaseURL - the management endpoint of the API Management service, for example
210// https://myapimservice.management.azure-api.net.
211// parameters - save Configuration parameters.
212func (client TenantConfigurationClient) Save(ctx context.Context, apimBaseURL string, parameters SaveConfigurationParameter) (result TenantConfigurationSaveFuture, err error) {
213	if tracing.IsEnabled() {
214		ctx = tracing.StartSpan(ctx, fqdn+"/TenantConfigurationClient.Save")
215		defer func() {
216			sc := -1
217			if result.Response() != nil {
218				sc = result.Response().StatusCode
219			}
220			tracing.EndSpan(ctx, sc, err)
221		}()
222	}
223	if err := validation.Validate([]validation.Validation{
224		{TargetValue: parameters,
225			Constraints: []validation.Constraint{{Target: "parameters.Branch", Name: validation.Null, Rule: true, Chain: nil}}}}); err != nil {
226		return result, validation.NewError("apimanagement.TenantConfigurationClient", "Save", err.Error())
227	}
228
229	req, err := client.SavePreparer(ctx, apimBaseURL, parameters)
230	if err != nil {
231		err = autorest.NewErrorWithError(err, "apimanagement.TenantConfigurationClient", "Save", nil, "Failure preparing request")
232		return
233	}
234
235	result, err = client.SaveSender(req)
236	if err != nil {
237		err = autorest.NewErrorWithError(err, "apimanagement.TenantConfigurationClient", "Save", result.Response(), "Failure sending request")
238		return
239	}
240
241	return
242}
243
244// SavePreparer prepares the Save request.
245func (client TenantConfigurationClient) SavePreparer(ctx context.Context, apimBaseURL string, parameters SaveConfigurationParameter) (*http.Request, error) {
246	urlParameters := map[string]interface{}{
247		"apimBaseUrl": apimBaseURL,
248	}
249
250	pathParameters := map[string]interface{}{
251		"configurationName": autorest.Encode("path", "configuration"),
252	}
253
254	const APIVersion = "2017-03-01"
255	queryParameters := map[string]interface{}{
256		"api-version": APIVersion,
257	}
258
259	preparer := autorest.CreatePreparer(
260		autorest.AsContentType("application/json; charset=utf-8"),
261		autorest.AsPost(),
262		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
263		autorest.WithPathParameters("/tenant/{configurationName}/save", pathParameters),
264		autorest.WithJSON(parameters),
265		autorest.WithQueryParameters(queryParameters))
266	return preparer.Prepare((&http.Request{}).WithContext(ctx))
267}
268
269// SaveSender sends the Save request. The method will close the
270// http.Response Body if it receives an error.
271func (client TenantConfigurationClient) SaveSender(req *http.Request) (future TenantConfigurationSaveFuture, err error) {
272	var resp *http.Response
273	resp, err = client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
274	if err != nil {
275		return
276	}
277	future.Future, err = azure.NewFutureFromResponse(resp)
278	return
279}
280
281// SaveResponder handles the response to the Save request. The method always
282// closes the http.Response Body.
283func (client TenantConfigurationClient) SaveResponder(resp *http.Response) (result OperationResultContract, err error) {
284	err = autorest.Respond(
285		resp,
286		client.ByInspecting(),
287		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
288		autorest.ByUnmarshallingJSON(&result),
289		autorest.ByClosing())
290	result.Response = autorest.Response{Response: resp}
291	return
292}
293
294// Validate this operation validates the changes in the specified Git branch. This is a long running operation and
295// could take several minutes to complete.
296// Parameters:
297// apimBaseURL - the management endpoint of the API Management service, for example
298// https://myapimservice.management.azure-api.net.
299// parameters - validate Configuration parameters.
300func (client TenantConfigurationClient) Validate(ctx context.Context, apimBaseURL string, parameters DeployConfigurationParameters) (result TenantConfigurationValidateFuture, err error) {
301	if tracing.IsEnabled() {
302		ctx = tracing.StartSpan(ctx, fqdn+"/TenantConfigurationClient.Validate")
303		defer func() {
304			sc := -1
305			if result.Response() != nil {
306				sc = result.Response().StatusCode
307			}
308			tracing.EndSpan(ctx, sc, err)
309		}()
310	}
311	if err := validation.Validate([]validation.Validation{
312		{TargetValue: parameters,
313			Constraints: []validation.Constraint{{Target: "parameters.Branch", Name: validation.Null, Rule: true, Chain: nil}}}}); err != nil {
314		return result, validation.NewError("apimanagement.TenantConfigurationClient", "Validate", err.Error())
315	}
316
317	req, err := client.ValidatePreparer(ctx, apimBaseURL, parameters)
318	if err != nil {
319		err = autorest.NewErrorWithError(err, "apimanagement.TenantConfigurationClient", "Validate", nil, "Failure preparing request")
320		return
321	}
322
323	result, err = client.ValidateSender(req)
324	if err != nil {
325		err = autorest.NewErrorWithError(err, "apimanagement.TenantConfigurationClient", "Validate", result.Response(), "Failure sending request")
326		return
327	}
328
329	return
330}
331
332// ValidatePreparer prepares the Validate request.
333func (client TenantConfigurationClient) ValidatePreparer(ctx context.Context, apimBaseURL string, parameters DeployConfigurationParameters) (*http.Request, error) {
334	urlParameters := map[string]interface{}{
335		"apimBaseUrl": apimBaseURL,
336	}
337
338	pathParameters := map[string]interface{}{
339		"configurationName": autorest.Encode("path", "configuration"),
340	}
341
342	const APIVersion = "2017-03-01"
343	queryParameters := map[string]interface{}{
344		"api-version": APIVersion,
345	}
346
347	preparer := autorest.CreatePreparer(
348		autorest.AsContentType("application/json; charset=utf-8"),
349		autorest.AsPost(),
350		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
351		autorest.WithPathParameters("/tenant/{configurationName}/validate", pathParameters),
352		autorest.WithJSON(parameters),
353		autorest.WithQueryParameters(queryParameters))
354	return preparer.Prepare((&http.Request{}).WithContext(ctx))
355}
356
357// ValidateSender sends the Validate request. The method will close the
358// http.Response Body if it receives an error.
359func (client TenantConfigurationClient) ValidateSender(req *http.Request) (future TenantConfigurationValidateFuture, err error) {
360	var resp *http.Response
361	resp, err = client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
362	if err != nil {
363		return
364	}
365	future.Future, err = azure.NewFutureFromResponse(resp)
366	return
367}
368
369// ValidateResponder handles the response to the Validate request. The method always
370// closes the http.Response Body.
371func (client TenantConfigurationClient) ValidateResponder(resp *http.Response) (result OperationResultContract, err error) {
372	err = autorest.Respond(
373		resp,
374		client.ByInspecting(),
375		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
376		autorest.ByUnmarshallingJSON(&result),
377		autorest.ByClosing())
378	result.Response = autorest.Response{Response: resp}
379	return
380}
381