1// +build go1.13
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// Code generated by Microsoft (R) AutoRest Code Generator.
6// Changes may cause incorrect behavior and will be lost if the code is regenerated.
7
8package armapimanagement
9
10import (
11	"context"
12	"errors"
13	"fmt"
14	"github.com/Azure/azure-sdk-for-go/sdk/armcore"
15	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
16	"net/http"
17	"net/url"
18	"strconv"
19	"strings"
20)
21
22// CacheClient contains the methods for the Cache group.
23// Don't use this type directly, use NewCacheClient() instead.
24type CacheClient struct {
25	con            *armcore.Connection
26	subscriptionID string
27}
28
29// NewCacheClient creates a new instance of CacheClient with the specified values.
30func NewCacheClient(con *armcore.Connection, subscriptionID string) *CacheClient {
31	return &CacheClient{con: con, subscriptionID: subscriptionID}
32}
33
34// CreateOrUpdate - Creates or updates an External Cache to be used in Api Management instance.
35// If the operation fails it returns the *ErrorResponse error type.
36func (client *CacheClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serviceName string, cacheID string, parameters CacheContract, options *CacheCreateOrUpdateOptions) (CacheCreateOrUpdateResponse, error) {
37	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, serviceName, cacheID, parameters, options)
38	if err != nil {
39		return CacheCreateOrUpdateResponse{}, err
40	}
41	resp, err := client.con.Pipeline().Do(req)
42	if err != nil {
43		return CacheCreateOrUpdateResponse{}, err
44	}
45	if !resp.HasStatusCode(http.StatusOK, http.StatusCreated) {
46		return CacheCreateOrUpdateResponse{}, client.createOrUpdateHandleError(resp)
47	}
48	return client.createOrUpdateHandleResponse(resp)
49}
50
51// createOrUpdateCreateRequest creates the CreateOrUpdate request.
52func (client *CacheClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, cacheID string, parameters CacheContract, options *CacheCreateOrUpdateOptions) (*azcore.Request, error) {
53	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/caches/{cacheId}"
54	if resourceGroupName == "" {
55		return nil, errors.New("parameter resourceGroupName cannot be empty")
56	}
57	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
58	if serviceName == "" {
59		return nil, errors.New("parameter serviceName cannot be empty")
60	}
61	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
62	if cacheID == "" {
63		return nil, errors.New("parameter cacheID cannot be empty")
64	}
65	urlPath = strings.ReplaceAll(urlPath, "{cacheId}", url.PathEscape(cacheID))
66	if client.subscriptionID == "" {
67		return nil, errors.New("parameter client.subscriptionID cannot be empty")
68	}
69	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
70	req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath))
71	if err != nil {
72		return nil, err
73	}
74	req.Telemetry(telemetryInfo)
75	reqQP := req.URL.Query()
76	reqQP.Set("api-version", "2020-12-01")
77	req.URL.RawQuery = reqQP.Encode()
78	if options != nil && options.IfMatch != nil {
79		req.Header.Set("If-Match", *options.IfMatch)
80	}
81	req.Header.Set("Accept", "application/json")
82	return req, req.MarshalAsJSON(parameters)
83}
84
85// createOrUpdateHandleResponse handles the CreateOrUpdate response.
86func (client *CacheClient) createOrUpdateHandleResponse(resp *azcore.Response) (CacheCreateOrUpdateResponse, error) {
87	result := CacheCreateOrUpdateResponse{RawResponse: resp.Response}
88	if val := resp.Header.Get("ETag"); val != "" {
89		result.ETag = &val
90	}
91	if err := resp.UnmarshalAsJSON(&result.CacheContract); err != nil {
92		return CacheCreateOrUpdateResponse{}, err
93	}
94	return result, nil
95}
96
97// createOrUpdateHandleError handles the CreateOrUpdate error response.
98func (client *CacheClient) createOrUpdateHandleError(resp *azcore.Response) error {
99	body, err := resp.Payload()
100	if err != nil {
101		return azcore.NewResponseError(err, resp.Response)
102	}
103	errType := ErrorResponse{raw: string(body)}
104	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
105		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
106	}
107	return azcore.NewResponseError(&errType, resp.Response)
108}
109
110// Delete - Deletes specific Cache.
111// If the operation fails it returns the *ErrorResponse error type.
112func (client *CacheClient) Delete(ctx context.Context, resourceGroupName string, serviceName string, cacheID string, ifMatch string, options *CacheDeleteOptions) (CacheDeleteResponse, error) {
113	req, err := client.deleteCreateRequest(ctx, resourceGroupName, serviceName, cacheID, ifMatch, options)
114	if err != nil {
115		return CacheDeleteResponse{}, err
116	}
117	resp, err := client.con.Pipeline().Do(req)
118	if err != nil {
119		return CacheDeleteResponse{}, err
120	}
121	if !resp.HasStatusCode(http.StatusOK, http.StatusNoContent) {
122		return CacheDeleteResponse{}, client.deleteHandleError(resp)
123	}
124	return CacheDeleteResponse{RawResponse: resp.Response}, nil
125}
126
127// deleteCreateRequest creates the Delete request.
128func (client *CacheClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, cacheID string, ifMatch string, options *CacheDeleteOptions) (*azcore.Request, error) {
129	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/caches/{cacheId}"
130	if resourceGroupName == "" {
131		return nil, errors.New("parameter resourceGroupName cannot be empty")
132	}
133	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
134	if serviceName == "" {
135		return nil, errors.New("parameter serviceName cannot be empty")
136	}
137	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
138	if cacheID == "" {
139		return nil, errors.New("parameter cacheID cannot be empty")
140	}
141	urlPath = strings.ReplaceAll(urlPath, "{cacheId}", url.PathEscape(cacheID))
142	if client.subscriptionID == "" {
143		return nil, errors.New("parameter client.subscriptionID cannot be empty")
144	}
145	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
146	req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath))
147	if err != nil {
148		return nil, err
149	}
150	req.Telemetry(telemetryInfo)
151	reqQP := req.URL.Query()
152	reqQP.Set("api-version", "2020-12-01")
153	req.URL.RawQuery = reqQP.Encode()
154	req.Header.Set("If-Match", ifMatch)
155	req.Header.Set("Accept", "application/json")
156	return req, nil
157}
158
159// deleteHandleError handles the Delete error response.
160func (client *CacheClient) deleteHandleError(resp *azcore.Response) error {
161	body, err := resp.Payload()
162	if err != nil {
163		return azcore.NewResponseError(err, resp.Response)
164	}
165	errType := ErrorResponse{raw: string(body)}
166	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
167		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
168	}
169	return azcore.NewResponseError(&errType, resp.Response)
170}
171
172// Get - Gets the details of the Cache specified by its identifier.
173// If the operation fails it returns the *ErrorResponse error type.
174func (client *CacheClient) Get(ctx context.Context, resourceGroupName string, serviceName string, cacheID string, options *CacheGetOptions) (CacheGetResponse, error) {
175	req, err := client.getCreateRequest(ctx, resourceGroupName, serviceName, cacheID, options)
176	if err != nil {
177		return CacheGetResponse{}, err
178	}
179	resp, err := client.con.Pipeline().Do(req)
180	if err != nil {
181		return CacheGetResponse{}, err
182	}
183	if !resp.HasStatusCode(http.StatusOK) {
184		return CacheGetResponse{}, client.getHandleError(resp)
185	}
186	return client.getHandleResponse(resp)
187}
188
189// getCreateRequest creates the Get request.
190func (client *CacheClient) getCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, cacheID string, options *CacheGetOptions) (*azcore.Request, error) {
191	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/caches/{cacheId}"
192	if resourceGroupName == "" {
193		return nil, errors.New("parameter resourceGroupName cannot be empty")
194	}
195	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
196	if serviceName == "" {
197		return nil, errors.New("parameter serviceName cannot be empty")
198	}
199	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
200	if cacheID == "" {
201		return nil, errors.New("parameter cacheID cannot be empty")
202	}
203	urlPath = strings.ReplaceAll(urlPath, "{cacheId}", url.PathEscape(cacheID))
204	if client.subscriptionID == "" {
205		return nil, errors.New("parameter client.subscriptionID cannot be empty")
206	}
207	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
208	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
209	if err != nil {
210		return nil, err
211	}
212	req.Telemetry(telemetryInfo)
213	reqQP := req.URL.Query()
214	reqQP.Set("api-version", "2020-12-01")
215	req.URL.RawQuery = reqQP.Encode()
216	req.Header.Set("Accept", "application/json")
217	return req, nil
218}
219
220// getHandleResponse handles the Get response.
221func (client *CacheClient) getHandleResponse(resp *azcore.Response) (CacheGetResponse, error) {
222	result := CacheGetResponse{RawResponse: resp.Response}
223	if val := resp.Header.Get("ETag"); val != "" {
224		result.ETag = &val
225	}
226	if err := resp.UnmarshalAsJSON(&result.CacheContract); err != nil {
227		return CacheGetResponse{}, err
228	}
229	return result, nil
230}
231
232// getHandleError handles the Get error response.
233func (client *CacheClient) getHandleError(resp *azcore.Response) error {
234	body, err := resp.Payload()
235	if err != nil {
236		return azcore.NewResponseError(err, resp.Response)
237	}
238	errType := ErrorResponse{raw: string(body)}
239	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
240		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
241	}
242	return azcore.NewResponseError(&errType, resp.Response)
243}
244
245// GetEntityTag - Gets the entity state (Etag) version of the Cache specified by its identifier.
246// If the operation fails it returns the *ErrorResponse error type.
247func (client *CacheClient) GetEntityTag(ctx context.Context, resourceGroupName string, serviceName string, cacheID string, options *CacheGetEntityTagOptions) (CacheGetEntityTagResponse, error) {
248	req, err := client.getEntityTagCreateRequest(ctx, resourceGroupName, serviceName, cacheID, options)
249	if err != nil {
250		return CacheGetEntityTagResponse{}, err
251	}
252	resp, err := client.con.Pipeline().Do(req)
253	if err != nil {
254		return CacheGetEntityTagResponse{}, err
255	}
256	return client.getEntityTagHandleResponse(resp)
257}
258
259// getEntityTagCreateRequest creates the GetEntityTag request.
260func (client *CacheClient) getEntityTagCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, cacheID string, options *CacheGetEntityTagOptions) (*azcore.Request, error) {
261	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/caches/{cacheId}"
262	if resourceGroupName == "" {
263		return nil, errors.New("parameter resourceGroupName cannot be empty")
264	}
265	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
266	if serviceName == "" {
267		return nil, errors.New("parameter serviceName cannot be empty")
268	}
269	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
270	if cacheID == "" {
271		return nil, errors.New("parameter cacheID cannot be empty")
272	}
273	urlPath = strings.ReplaceAll(urlPath, "{cacheId}", url.PathEscape(cacheID))
274	if client.subscriptionID == "" {
275		return nil, errors.New("parameter client.subscriptionID cannot be empty")
276	}
277	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
278	req, err := azcore.NewRequest(ctx, http.MethodHead, azcore.JoinPaths(client.con.Endpoint(), urlPath))
279	if err != nil {
280		return nil, err
281	}
282	req.Telemetry(telemetryInfo)
283	reqQP := req.URL.Query()
284	reqQP.Set("api-version", "2020-12-01")
285	req.URL.RawQuery = reqQP.Encode()
286	req.Header.Set("Accept", "application/json")
287	return req, nil
288}
289
290// getEntityTagHandleResponse handles the GetEntityTag response.
291func (client *CacheClient) getEntityTagHandleResponse(resp *azcore.Response) (CacheGetEntityTagResponse, error) {
292	result := CacheGetEntityTagResponse{RawResponse: resp.Response}
293	if val := resp.Header.Get("ETag"); val != "" {
294		result.ETag = &val
295	}
296	if resp.StatusCode >= 200 && resp.StatusCode < 300 {
297		result.Success = true
298	}
299	return result, nil
300}
301
302// ListByService - Lists a collection of all external Caches in the specified service instance.
303// If the operation fails it returns the *ErrorResponse error type.
304func (client *CacheClient) ListByService(resourceGroupName string, serviceName string, options *CacheListByServiceOptions) CacheListByServicePager {
305	return &cacheListByServicePager{
306		client: client,
307		requester: func(ctx context.Context) (*azcore.Request, error) {
308			return client.listByServiceCreateRequest(ctx, resourceGroupName, serviceName, options)
309		},
310		advancer: func(ctx context.Context, resp CacheListByServiceResponse) (*azcore.Request, error) {
311			return azcore.NewRequest(ctx, http.MethodGet, *resp.CacheCollection.NextLink)
312		},
313	}
314}
315
316// listByServiceCreateRequest creates the ListByService request.
317func (client *CacheClient) listByServiceCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, options *CacheListByServiceOptions) (*azcore.Request, error) {
318	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/caches"
319	if resourceGroupName == "" {
320		return nil, errors.New("parameter resourceGroupName cannot be empty")
321	}
322	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
323	if serviceName == "" {
324		return nil, errors.New("parameter serviceName cannot be empty")
325	}
326	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
327	if client.subscriptionID == "" {
328		return nil, errors.New("parameter client.subscriptionID cannot be empty")
329	}
330	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
331	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
332	if err != nil {
333		return nil, err
334	}
335	req.Telemetry(telemetryInfo)
336	reqQP := req.URL.Query()
337	if options != nil && options.Top != nil {
338		reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10))
339	}
340	if options != nil && options.Skip != nil {
341		reqQP.Set("$skip", strconv.FormatInt(int64(*options.Skip), 10))
342	}
343	reqQP.Set("api-version", "2020-12-01")
344	req.URL.RawQuery = reqQP.Encode()
345	req.Header.Set("Accept", "application/json")
346	return req, nil
347}
348
349// listByServiceHandleResponse handles the ListByService response.
350func (client *CacheClient) listByServiceHandleResponse(resp *azcore.Response) (CacheListByServiceResponse, error) {
351	result := CacheListByServiceResponse{RawResponse: resp.Response}
352	if err := resp.UnmarshalAsJSON(&result.CacheCollection); err != nil {
353		return CacheListByServiceResponse{}, err
354	}
355	return result, nil
356}
357
358// listByServiceHandleError handles the ListByService error response.
359func (client *CacheClient) listByServiceHandleError(resp *azcore.Response) error {
360	body, err := resp.Payload()
361	if err != nil {
362		return azcore.NewResponseError(err, resp.Response)
363	}
364	errType := ErrorResponse{raw: string(body)}
365	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
366		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
367	}
368	return azcore.NewResponseError(&errType, resp.Response)
369}
370
371// Update - Updates the details of the cache specified by its identifier.
372// If the operation fails it returns the *ErrorResponse error type.
373func (client *CacheClient) Update(ctx context.Context, resourceGroupName string, serviceName string, cacheID string, ifMatch string, parameters CacheUpdateParameters, options *CacheUpdateOptions) (CacheUpdateResponse, error) {
374	req, err := client.updateCreateRequest(ctx, resourceGroupName, serviceName, cacheID, ifMatch, parameters, options)
375	if err != nil {
376		return CacheUpdateResponse{}, err
377	}
378	resp, err := client.con.Pipeline().Do(req)
379	if err != nil {
380		return CacheUpdateResponse{}, err
381	}
382	if !resp.HasStatusCode(http.StatusOK) {
383		return CacheUpdateResponse{}, client.updateHandleError(resp)
384	}
385	return client.updateHandleResponse(resp)
386}
387
388// updateCreateRequest creates the Update request.
389func (client *CacheClient) updateCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, cacheID string, ifMatch string, parameters CacheUpdateParameters, options *CacheUpdateOptions) (*azcore.Request, error) {
390	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/caches/{cacheId}"
391	if resourceGroupName == "" {
392		return nil, errors.New("parameter resourceGroupName cannot be empty")
393	}
394	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
395	if serviceName == "" {
396		return nil, errors.New("parameter serviceName cannot be empty")
397	}
398	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
399	if cacheID == "" {
400		return nil, errors.New("parameter cacheID cannot be empty")
401	}
402	urlPath = strings.ReplaceAll(urlPath, "{cacheId}", url.PathEscape(cacheID))
403	if client.subscriptionID == "" {
404		return nil, errors.New("parameter client.subscriptionID cannot be empty")
405	}
406	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
407	req, err := azcore.NewRequest(ctx, http.MethodPatch, azcore.JoinPaths(client.con.Endpoint(), urlPath))
408	if err != nil {
409		return nil, err
410	}
411	req.Telemetry(telemetryInfo)
412	reqQP := req.URL.Query()
413	reqQP.Set("api-version", "2020-12-01")
414	req.URL.RawQuery = reqQP.Encode()
415	req.Header.Set("If-Match", ifMatch)
416	req.Header.Set("Accept", "application/json")
417	return req, req.MarshalAsJSON(parameters)
418}
419
420// updateHandleResponse handles the Update response.
421func (client *CacheClient) updateHandleResponse(resp *azcore.Response) (CacheUpdateResponse, error) {
422	result := CacheUpdateResponse{RawResponse: resp.Response}
423	if val := resp.Header.Get("ETag"); val != "" {
424		result.ETag = &val
425	}
426	if err := resp.UnmarshalAsJSON(&result.CacheContract); err != nil {
427		return CacheUpdateResponse{}, err
428	}
429	return result, nil
430}
431
432// updateHandleError handles the Update error response.
433func (client *CacheClient) updateHandleError(resp *azcore.Response) error {
434	body, err := resp.Payload()
435	if err != nil {
436		return azcore.NewResponseError(err, resp.Response)
437	}
438	errType := ErrorResponse{raw: string(body)}
439	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
440		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
441	}
442	return azcore.NewResponseError(&errType, resp.Response)
443}
444