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// GatewayHostnameConfigurationClient contains the methods for the GatewayHostnameConfiguration group.
23// Don't use this type directly, use NewGatewayHostnameConfigurationClient() instead.
24type GatewayHostnameConfigurationClient struct {
25	con            *armcore.Connection
26	subscriptionID string
27}
28
29// NewGatewayHostnameConfigurationClient creates a new instance of GatewayHostnameConfigurationClient with the specified values.
30func NewGatewayHostnameConfigurationClient(con *armcore.Connection, subscriptionID string) *GatewayHostnameConfigurationClient {
31	return &GatewayHostnameConfigurationClient{con: con, subscriptionID: subscriptionID}
32}
33
34// CreateOrUpdate - Creates of updates hostname configuration for a Gateway.
35// If the operation fails it returns the *ErrorResponse error type.
36func (client *GatewayHostnameConfigurationClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, hcID string, parameters GatewayHostnameConfigurationContract, options *GatewayHostnameConfigurationCreateOrUpdateOptions) (GatewayHostnameConfigurationCreateOrUpdateResponse, error) {
37	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, serviceName, gatewayID, hcID, parameters, options)
38	if err != nil {
39		return GatewayHostnameConfigurationCreateOrUpdateResponse{}, err
40	}
41	resp, err := client.con.Pipeline().Do(req)
42	if err != nil {
43		return GatewayHostnameConfigurationCreateOrUpdateResponse{}, err
44	}
45	if !resp.HasStatusCode(http.StatusOK, http.StatusCreated) {
46		return GatewayHostnameConfigurationCreateOrUpdateResponse{}, client.createOrUpdateHandleError(resp)
47	}
48	return client.createOrUpdateHandleResponse(resp)
49}
50
51// createOrUpdateCreateRequest creates the CreateOrUpdate request.
52func (client *GatewayHostnameConfigurationClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, hcID string, parameters GatewayHostnameConfigurationContract, options *GatewayHostnameConfigurationCreateOrUpdateOptions) (*azcore.Request, error) {
53	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/gateways/{gatewayId}/hostnameConfigurations/{hcId}"
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 gatewayID == "" {
63		return nil, errors.New("parameter gatewayID cannot be empty")
64	}
65	urlPath = strings.ReplaceAll(urlPath, "{gatewayId}", url.PathEscape(gatewayID))
66	if hcID == "" {
67		return nil, errors.New("parameter hcID cannot be empty")
68	}
69	urlPath = strings.ReplaceAll(urlPath, "{hcId}", url.PathEscape(hcID))
70	if client.subscriptionID == "" {
71		return nil, errors.New("parameter client.subscriptionID cannot be empty")
72	}
73	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
74	req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath))
75	if err != nil {
76		return nil, err
77	}
78	req.Telemetry(telemetryInfo)
79	reqQP := req.URL.Query()
80	reqQP.Set("api-version", "2020-12-01")
81	req.URL.RawQuery = reqQP.Encode()
82	if options != nil && options.IfMatch != nil {
83		req.Header.Set("If-Match", *options.IfMatch)
84	}
85	req.Header.Set("Accept", "application/json")
86	return req, req.MarshalAsJSON(parameters)
87}
88
89// createOrUpdateHandleResponse handles the CreateOrUpdate response.
90func (client *GatewayHostnameConfigurationClient) createOrUpdateHandleResponse(resp *azcore.Response) (GatewayHostnameConfigurationCreateOrUpdateResponse, error) {
91	result := GatewayHostnameConfigurationCreateOrUpdateResponse{RawResponse: resp.Response}
92	if val := resp.Header.Get("ETag"); val != "" {
93		result.ETag = &val
94	}
95	if err := resp.UnmarshalAsJSON(&result.GatewayHostnameConfigurationContract); err != nil {
96		return GatewayHostnameConfigurationCreateOrUpdateResponse{}, err
97	}
98	return result, nil
99}
100
101// createOrUpdateHandleError handles the CreateOrUpdate error response.
102func (client *GatewayHostnameConfigurationClient) createOrUpdateHandleError(resp *azcore.Response) error {
103	body, err := resp.Payload()
104	if err != nil {
105		return azcore.NewResponseError(err, resp.Response)
106	}
107	errType := ErrorResponse{raw: string(body)}
108	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
109		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
110	}
111	return azcore.NewResponseError(&errType, resp.Response)
112}
113
114// Delete - Deletes the specified hostname configuration from the specified Gateway.
115// If the operation fails it returns the *ErrorResponse error type.
116func (client *GatewayHostnameConfigurationClient) Delete(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, hcID string, ifMatch string, options *GatewayHostnameConfigurationDeleteOptions) (GatewayHostnameConfigurationDeleteResponse, error) {
117	req, err := client.deleteCreateRequest(ctx, resourceGroupName, serviceName, gatewayID, hcID, ifMatch, options)
118	if err != nil {
119		return GatewayHostnameConfigurationDeleteResponse{}, err
120	}
121	resp, err := client.con.Pipeline().Do(req)
122	if err != nil {
123		return GatewayHostnameConfigurationDeleteResponse{}, err
124	}
125	if !resp.HasStatusCode(http.StatusOK, http.StatusNoContent) {
126		return GatewayHostnameConfigurationDeleteResponse{}, client.deleteHandleError(resp)
127	}
128	return GatewayHostnameConfigurationDeleteResponse{RawResponse: resp.Response}, nil
129}
130
131// deleteCreateRequest creates the Delete request.
132func (client *GatewayHostnameConfigurationClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, hcID string, ifMatch string, options *GatewayHostnameConfigurationDeleteOptions) (*azcore.Request, error) {
133	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/gateways/{gatewayId}/hostnameConfigurations/{hcId}"
134	if resourceGroupName == "" {
135		return nil, errors.New("parameter resourceGroupName cannot be empty")
136	}
137	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
138	if serviceName == "" {
139		return nil, errors.New("parameter serviceName cannot be empty")
140	}
141	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
142	if gatewayID == "" {
143		return nil, errors.New("parameter gatewayID cannot be empty")
144	}
145	urlPath = strings.ReplaceAll(urlPath, "{gatewayId}", url.PathEscape(gatewayID))
146	if hcID == "" {
147		return nil, errors.New("parameter hcID cannot be empty")
148	}
149	urlPath = strings.ReplaceAll(urlPath, "{hcId}", url.PathEscape(hcID))
150	if client.subscriptionID == "" {
151		return nil, errors.New("parameter client.subscriptionID cannot be empty")
152	}
153	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
154	req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath))
155	if err != nil {
156		return nil, err
157	}
158	req.Telemetry(telemetryInfo)
159	reqQP := req.URL.Query()
160	reqQP.Set("api-version", "2020-12-01")
161	req.URL.RawQuery = reqQP.Encode()
162	req.Header.Set("If-Match", ifMatch)
163	req.Header.Set("Accept", "application/json")
164	return req, nil
165}
166
167// deleteHandleError handles the Delete error response.
168func (client *GatewayHostnameConfigurationClient) deleteHandleError(resp *azcore.Response) error {
169	body, err := resp.Payload()
170	if err != nil {
171		return azcore.NewResponseError(err, resp.Response)
172	}
173	errType := ErrorResponse{raw: string(body)}
174	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
175		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
176	}
177	return azcore.NewResponseError(&errType, resp.Response)
178}
179
180// Get - Get details of a hostname configuration
181// If the operation fails it returns the *ErrorResponse error type.
182func (client *GatewayHostnameConfigurationClient) Get(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, hcID string, options *GatewayHostnameConfigurationGetOptions) (GatewayHostnameConfigurationGetResponse, error) {
183	req, err := client.getCreateRequest(ctx, resourceGroupName, serviceName, gatewayID, hcID, options)
184	if err != nil {
185		return GatewayHostnameConfigurationGetResponse{}, err
186	}
187	resp, err := client.con.Pipeline().Do(req)
188	if err != nil {
189		return GatewayHostnameConfigurationGetResponse{}, err
190	}
191	if !resp.HasStatusCode(http.StatusOK) {
192		return GatewayHostnameConfigurationGetResponse{}, client.getHandleError(resp)
193	}
194	return client.getHandleResponse(resp)
195}
196
197// getCreateRequest creates the Get request.
198func (client *GatewayHostnameConfigurationClient) getCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, hcID string, options *GatewayHostnameConfigurationGetOptions) (*azcore.Request, error) {
199	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/gateways/{gatewayId}/hostnameConfigurations/{hcId}"
200	if resourceGroupName == "" {
201		return nil, errors.New("parameter resourceGroupName cannot be empty")
202	}
203	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
204	if serviceName == "" {
205		return nil, errors.New("parameter serviceName cannot be empty")
206	}
207	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
208	if gatewayID == "" {
209		return nil, errors.New("parameter gatewayID cannot be empty")
210	}
211	urlPath = strings.ReplaceAll(urlPath, "{gatewayId}", url.PathEscape(gatewayID))
212	if hcID == "" {
213		return nil, errors.New("parameter hcID cannot be empty")
214	}
215	urlPath = strings.ReplaceAll(urlPath, "{hcId}", url.PathEscape(hcID))
216	if client.subscriptionID == "" {
217		return nil, errors.New("parameter client.subscriptionID cannot be empty")
218	}
219	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
220	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
221	if err != nil {
222		return nil, err
223	}
224	req.Telemetry(telemetryInfo)
225	reqQP := req.URL.Query()
226	reqQP.Set("api-version", "2020-12-01")
227	req.URL.RawQuery = reqQP.Encode()
228	req.Header.Set("Accept", "application/json")
229	return req, nil
230}
231
232// getHandleResponse handles the Get response.
233func (client *GatewayHostnameConfigurationClient) getHandleResponse(resp *azcore.Response) (GatewayHostnameConfigurationGetResponse, error) {
234	result := GatewayHostnameConfigurationGetResponse{RawResponse: resp.Response}
235	if val := resp.Header.Get("ETag"); val != "" {
236		result.ETag = &val
237	}
238	if err := resp.UnmarshalAsJSON(&result.GatewayHostnameConfigurationContract); err != nil {
239		return GatewayHostnameConfigurationGetResponse{}, err
240	}
241	return result, nil
242}
243
244// getHandleError handles the Get error response.
245func (client *GatewayHostnameConfigurationClient) getHandleError(resp *azcore.Response) error {
246	body, err := resp.Payload()
247	if err != nil {
248		return azcore.NewResponseError(err, resp.Response)
249	}
250	errType := ErrorResponse{raw: string(body)}
251	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
252		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
253	}
254	return azcore.NewResponseError(&errType, resp.Response)
255}
256
257// GetEntityTag - Checks that hostname configuration entity specified by identifier exists for specified Gateway entity.
258// If the operation fails it returns the *ErrorResponse error type.
259func (client *GatewayHostnameConfigurationClient) GetEntityTag(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, hcID string, options *GatewayHostnameConfigurationGetEntityTagOptions) (GatewayHostnameConfigurationGetEntityTagResponse, error) {
260	req, err := client.getEntityTagCreateRequest(ctx, resourceGroupName, serviceName, gatewayID, hcID, options)
261	if err != nil {
262		return GatewayHostnameConfigurationGetEntityTagResponse{}, err
263	}
264	resp, err := client.con.Pipeline().Do(req)
265	if err != nil {
266		return GatewayHostnameConfigurationGetEntityTagResponse{}, err
267	}
268	return client.getEntityTagHandleResponse(resp)
269}
270
271// getEntityTagCreateRequest creates the GetEntityTag request.
272func (client *GatewayHostnameConfigurationClient) getEntityTagCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, hcID string, options *GatewayHostnameConfigurationGetEntityTagOptions) (*azcore.Request, error) {
273	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/gateways/{gatewayId}/hostnameConfigurations/{hcId}"
274	if resourceGroupName == "" {
275		return nil, errors.New("parameter resourceGroupName cannot be empty")
276	}
277	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
278	if serviceName == "" {
279		return nil, errors.New("parameter serviceName cannot be empty")
280	}
281	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
282	if gatewayID == "" {
283		return nil, errors.New("parameter gatewayID cannot be empty")
284	}
285	urlPath = strings.ReplaceAll(urlPath, "{gatewayId}", url.PathEscape(gatewayID))
286	if hcID == "" {
287		return nil, errors.New("parameter hcID cannot be empty")
288	}
289	urlPath = strings.ReplaceAll(urlPath, "{hcId}", url.PathEscape(hcID))
290	if client.subscriptionID == "" {
291		return nil, errors.New("parameter client.subscriptionID cannot be empty")
292	}
293	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
294	req, err := azcore.NewRequest(ctx, http.MethodHead, azcore.JoinPaths(client.con.Endpoint(), urlPath))
295	if err != nil {
296		return nil, err
297	}
298	req.Telemetry(telemetryInfo)
299	reqQP := req.URL.Query()
300	reqQP.Set("api-version", "2020-12-01")
301	req.URL.RawQuery = reqQP.Encode()
302	req.Header.Set("Accept", "application/json")
303	return req, nil
304}
305
306// getEntityTagHandleResponse handles the GetEntityTag response.
307func (client *GatewayHostnameConfigurationClient) getEntityTagHandleResponse(resp *azcore.Response) (GatewayHostnameConfigurationGetEntityTagResponse, error) {
308	result := GatewayHostnameConfigurationGetEntityTagResponse{RawResponse: resp.Response}
309	if val := resp.Header.Get("ETag"); val != "" {
310		result.ETag = &val
311	}
312	if resp.StatusCode >= 200 && resp.StatusCode < 300 {
313		result.Success = true
314	}
315	return result, nil
316}
317
318// ListByService - Lists the collection of hostname configurations for the specified gateway.
319// If the operation fails it returns the *ErrorResponse error type.
320func (client *GatewayHostnameConfigurationClient) ListByService(resourceGroupName string, serviceName string, gatewayID string, options *GatewayHostnameConfigurationListByServiceOptions) GatewayHostnameConfigurationListByServicePager {
321	return &gatewayHostnameConfigurationListByServicePager{
322		client: client,
323		requester: func(ctx context.Context) (*azcore.Request, error) {
324			return client.listByServiceCreateRequest(ctx, resourceGroupName, serviceName, gatewayID, options)
325		},
326		advancer: func(ctx context.Context, resp GatewayHostnameConfigurationListByServiceResponse) (*azcore.Request, error) {
327			return azcore.NewRequest(ctx, http.MethodGet, *resp.GatewayHostnameConfigurationCollection.NextLink)
328		},
329	}
330}
331
332// listByServiceCreateRequest creates the ListByService request.
333func (client *GatewayHostnameConfigurationClient) listByServiceCreateRequest(ctx context.Context, resourceGroupName string, serviceName string, gatewayID string, options *GatewayHostnameConfigurationListByServiceOptions) (*azcore.Request, error) {
334	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/gateways/{gatewayId}/hostnameConfigurations"
335	if resourceGroupName == "" {
336		return nil, errors.New("parameter resourceGroupName cannot be empty")
337	}
338	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
339	if serviceName == "" {
340		return nil, errors.New("parameter serviceName cannot be empty")
341	}
342	urlPath = strings.ReplaceAll(urlPath, "{serviceName}", url.PathEscape(serviceName))
343	if gatewayID == "" {
344		return nil, errors.New("parameter gatewayID cannot be empty")
345	}
346	urlPath = strings.ReplaceAll(urlPath, "{gatewayId}", url.PathEscape(gatewayID))
347	if client.subscriptionID == "" {
348		return nil, errors.New("parameter client.subscriptionID cannot be empty")
349	}
350	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
351	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
352	if err != nil {
353		return nil, err
354	}
355	req.Telemetry(telemetryInfo)
356	reqQP := req.URL.Query()
357	if options != nil && options.Filter != nil {
358		reqQP.Set("$filter", *options.Filter)
359	}
360	if options != nil && options.Top != nil {
361		reqQP.Set("$top", strconv.FormatInt(int64(*options.Top), 10))
362	}
363	if options != nil && options.Skip != nil {
364		reqQP.Set("$skip", strconv.FormatInt(int64(*options.Skip), 10))
365	}
366	reqQP.Set("api-version", "2020-12-01")
367	req.URL.RawQuery = reqQP.Encode()
368	req.Header.Set("Accept", "application/json")
369	return req, nil
370}
371
372// listByServiceHandleResponse handles the ListByService response.
373func (client *GatewayHostnameConfigurationClient) listByServiceHandleResponse(resp *azcore.Response) (GatewayHostnameConfigurationListByServiceResponse, error) {
374	result := GatewayHostnameConfigurationListByServiceResponse{RawResponse: resp.Response}
375	if err := resp.UnmarshalAsJSON(&result.GatewayHostnameConfigurationCollection); err != nil {
376		return GatewayHostnameConfigurationListByServiceResponse{}, err
377	}
378	return result, nil
379}
380
381// listByServiceHandleError handles the ListByService error response.
382func (client *GatewayHostnameConfigurationClient) listByServiceHandleError(resp *azcore.Response) error {
383	body, err := resp.Payload()
384	if err != nil {
385		return azcore.NewResponseError(err, resp.Response)
386	}
387	errType := ErrorResponse{raw: string(body)}
388	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
389		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
390	}
391	return azcore.NewResponseError(&errType, resp.Response)
392}
393