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 azartifacts
9
10import (
11	"context"
12	"errors"
13	"fmt"
14	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
15	"net/http"
16	"net/url"
17	"strings"
18	"time"
19)
20
21// LinkedServiceClient contains the methods for the LinkedService group.
22// Don't use this type directly, use NewLinkedServiceClient() instead.
23type LinkedServiceClient struct {
24	con *Connection
25}
26
27// NewLinkedServiceClient creates a new instance of LinkedServiceClient with the specified values.
28func NewLinkedServiceClient(con *Connection) *LinkedServiceClient {
29	return &LinkedServiceClient{con: con}
30}
31
32// BeginCreateOrUpdateLinkedService - Creates or updates a linked service.
33// If the operation fails it returns the *CloudError error type.
34func (client *LinkedServiceClient) BeginCreateOrUpdateLinkedService(ctx context.Context, linkedServiceName string, linkedService LinkedServiceResource, options *LinkedServiceBeginCreateOrUpdateLinkedServiceOptions) (LinkedServiceResourcePollerResponse, error) {
35	resp, err := client.createOrUpdateLinkedService(ctx, linkedServiceName, linkedService, options)
36	if err != nil {
37		return LinkedServiceResourcePollerResponse{}, err
38	}
39	result := LinkedServiceResourcePollerResponse{
40		RawResponse: resp.Response,
41	}
42	pt, err := azcore.NewLROPoller("LinkedServiceClient.CreateOrUpdateLinkedService", resp, client.con.Pipeline(), client.createOrUpdateLinkedServiceHandleError)
43	if err != nil {
44		return LinkedServiceResourcePollerResponse{}, err
45	}
46	poller := &linkedServiceResourcePoller{
47		pt: pt,
48	}
49	result.Poller = poller
50	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (LinkedServiceResourceResponse, error) {
51		return poller.pollUntilDone(ctx, frequency)
52	}
53	return result, nil
54}
55
56// ResumeCreateOrUpdateLinkedService creates a new LinkedServiceResourcePoller from the specified resume token.
57// token - The value must come from a previous call to LinkedServiceResourcePoller.ResumeToken().
58func (client *LinkedServiceClient) ResumeCreateOrUpdateLinkedService(ctx context.Context, token string) (LinkedServiceResourcePollerResponse, error) {
59	pt, err := azcore.NewLROPollerFromResumeToken("LinkedServiceClient.CreateOrUpdateLinkedService", token, client.con.Pipeline(), client.createOrUpdateLinkedServiceHandleError)
60	if err != nil {
61		return LinkedServiceResourcePollerResponse{}, err
62	}
63	poller := &linkedServiceResourcePoller{
64		pt: pt,
65	}
66	resp, err := poller.Poll(ctx)
67	if err != nil {
68		return LinkedServiceResourcePollerResponse{}, err
69	}
70	result := LinkedServiceResourcePollerResponse{
71		RawResponse: resp,
72	}
73	result.Poller = poller
74	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (LinkedServiceResourceResponse, error) {
75		return poller.pollUntilDone(ctx, frequency)
76	}
77	return result, nil
78}
79
80// CreateOrUpdateLinkedService - Creates or updates a linked service.
81// If the operation fails it returns the *CloudError error type.
82func (client *LinkedServiceClient) createOrUpdateLinkedService(ctx context.Context, linkedServiceName string, linkedService LinkedServiceResource, options *LinkedServiceBeginCreateOrUpdateLinkedServiceOptions) (*azcore.Response, error) {
83	req, err := client.createOrUpdateLinkedServiceCreateRequest(ctx, linkedServiceName, linkedService, options)
84	if err != nil {
85		return nil, err
86	}
87	resp, err := client.con.Pipeline().Do(req)
88	if err != nil {
89		return nil, err
90	}
91	if !resp.HasStatusCode(http.StatusOK, http.StatusAccepted) {
92		return nil, client.createOrUpdateLinkedServiceHandleError(resp)
93	}
94	return resp, nil
95}
96
97// createOrUpdateLinkedServiceCreateRequest creates the CreateOrUpdateLinkedService request.
98func (client *LinkedServiceClient) createOrUpdateLinkedServiceCreateRequest(ctx context.Context, linkedServiceName string, linkedService LinkedServiceResource, options *LinkedServiceBeginCreateOrUpdateLinkedServiceOptions) (*azcore.Request, error) {
99	urlPath := "/linkedservices/{linkedServiceName}"
100	if linkedServiceName == "" {
101		return nil, errors.New("parameter linkedServiceName cannot be empty")
102	}
103	urlPath = strings.ReplaceAll(urlPath, "{linkedServiceName}", url.PathEscape(linkedServiceName))
104	req, err := azcore.NewRequest(ctx, http.MethodPut, azcore.JoinPaths(client.con.Endpoint(), urlPath))
105	if err != nil {
106		return nil, err
107	}
108	req.Telemetry(telemetryInfo)
109	reqQP := req.URL.Query()
110	reqQP.Set("api-version", "2019-06-01-preview")
111	req.URL.RawQuery = reqQP.Encode()
112	if options != nil && options.IfMatch != nil {
113		req.Header.Set("If-Match", *options.IfMatch)
114	}
115	req.Header.Set("Accept", "application/json")
116	return req, req.MarshalAsJSON(linkedService)
117}
118
119// createOrUpdateLinkedServiceHandleError handles the CreateOrUpdateLinkedService error response.
120func (client *LinkedServiceClient) createOrUpdateLinkedServiceHandleError(resp *azcore.Response) error {
121	body, err := resp.Payload()
122	if err != nil {
123		return azcore.NewResponseError(err, resp.Response)
124	}
125	errType := CloudError{raw: string(body)}
126	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
127		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
128	}
129	return azcore.NewResponseError(&errType, resp.Response)
130}
131
132// BeginDeleteLinkedService - Deletes a linked service.
133// If the operation fails it returns the *CloudError error type.
134func (client *LinkedServiceClient) BeginDeleteLinkedService(ctx context.Context, linkedServiceName string, options *LinkedServiceBeginDeleteLinkedServiceOptions) (HTTPPollerResponse, error) {
135	resp, err := client.deleteLinkedService(ctx, linkedServiceName, options)
136	if err != nil {
137		return HTTPPollerResponse{}, err
138	}
139	result := HTTPPollerResponse{
140		RawResponse: resp.Response,
141	}
142	pt, err := azcore.NewLROPoller("LinkedServiceClient.DeleteLinkedService", resp, client.con.Pipeline(), client.deleteLinkedServiceHandleError)
143	if err != nil {
144		return HTTPPollerResponse{}, err
145	}
146	poller := &httpPoller{
147		pt: pt,
148	}
149	result.Poller = poller
150	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (*http.Response, error) {
151		return poller.pollUntilDone(ctx, frequency)
152	}
153	return result, nil
154}
155
156// ResumeDeleteLinkedService creates a new HTTPPoller from the specified resume token.
157// token - The value must come from a previous call to HTTPPoller.ResumeToken().
158func (client *LinkedServiceClient) ResumeDeleteLinkedService(ctx context.Context, token string) (HTTPPollerResponse, error) {
159	pt, err := azcore.NewLROPollerFromResumeToken("LinkedServiceClient.DeleteLinkedService", token, client.con.Pipeline(), client.deleteLinkedServiceHandleError)
160	if err != nil {
161		return HTTPPollerResponse{}, err
162	}
163	poller := &httpPoller{
164		pt: pt,
165	}
166	resp, err := poller.Poll(ctx)
167	if err != nil {
168		return HTTPPollerResponse{}, err
169	}
170	result := HTTPPollerResponse{
171		RawResponse: resp,
172	}
173	result.Poller = poller
174	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (*http.Response, error) {
175		return poller.pollUntilDone(ctx, frequency)
176	}
177	return result, nil
178}
179
180// DeleteLinkedService - Deletes a linked service.
181// If the operation fails it returns the *CloudError error type.
182func (client *LinkedServiceClient) deleteLinkedService(ctx context.Context, linkedServiceName string, options *LinkedServiceBeginDeleteLinkedServiceOptions) (*azcore.Response, error) {
183	req, err := client.deleteLinkedServiceCreateRequest(ctx, linkedServiceName, options)
184	if err != nil {
185		return nil, err
186	}
187	resp, err := client.con.Pipeline().Do(req)
188	if err != nil {
189		return nil, err
190	}
191	if !resp.HasStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent) {
192		return nil, client.deleteLinkedServiceHandleError(resp)
193	}
194	return resp, nil
195}
196
197// deleteLinkedServiceCreateRequest creates the DeleteLinkedService request.
198func (client *LinkedServiceClient) deleteLinkedServiceCreateRequest(ctx context.Context, linkedServiceName string, options *LinkedServiceBeginDeleteLinkedServiceOptions) (*azcore.Request, error) {
199	urlPath := "/linkedservices/{linkedServiceName}"
200	if linkedServiceName == "" {
201		return nil, errors.New("parameter linkedServiceName cannot be empty")
202	}
203	urlPath = strings.ReplaceAll(urlPath, "{linkedServiceName}", url.PathEscape(linkedServiceName))
204	req, err := azcore.NewRequest(ctx, http.MethodDelete, azcore.JoinPaths(client.con.Endpoint(), urlPath))
205	if err != nil {
206		return nil, err
207	}
208	req.Telemetry(telemetryInfo)
209	reqQP := req.URL.Query()
210	reqQP.Set("api-version", "2019-06-01-preview")
211	req.URL.RawQuery = reqQP.Encode()
212	req.Header.Set("Accept", "application/json")
213	return req, nil
214}
215
216// deleteLinkedServiceHandleError handles the DeleteLinkedService error response.
217func (client *LinkedServiceClient) deleteLinkedServiceHandleError(resp *azcore.Response) error {
218	body, err := resp.Payload()
219	if err != nil {
220		return azcore.NewResponseError(err, resp.Response)
221	}
222	errType := CloudError{raw: string(body)}
223	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
224		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
225	}
226	return azcore.NewResponseError(&errType, resp.Response)
227}
228
229// GetLinkedService - Gets a linked service.
230// If the operation fails it returns the *CloudError error type.
231func (client *LinkedServiceClient) GetLinkedService(ctx context.Context, linkedServiceName string, options *LinkedServiceGetLinkedServiceOptions) (LinkedServiceResourceResponse, error) {
232	req, err := client.getLinkedServiceCreateRequest(ctx, linkedServiceName, options)
233	if err != nil {
234		return LinkedServiceResourceResponse{}, err
235	}
236	resp, err := client.con.Pipeline().Do(req)
237	if err != nil {
238		return LinkedServiceResourceResponse{}, err
239	}
240	if !resp.HasStatusCode(http.StatusOK, http.StatusNotModified) {
241		return LinkedServiceResourceResponse{}, client.getLinkedServiceHandleError(resp)
242	}
243	return client.getLinkedServiceHandleResponse(resp)
244}
245
246// getLinkedServiceCreateRequest creates the GetLinkedService request.
247func (client *LinkedServiceClient) getLinkedServiceCreateRequest(ctx context.Context, linkedServiceName string, options *LinkedServiceGetLinkedServiceOptions) (*azcore.Request, error) {
248	urlPath := "/linkedservices/{linkedServiceName}"
249	if linkedServiceName == "" {
250		return nil, errors.New("parameter linkedServiceName cannot be empty")
251	}
252	urlPath = strings.ReplaceAll(urlPath, "{linkedServiceName}", url.PathEscape(linkedServiceName))
253	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
254	if err != nil {
255		return nil, err
256	}
257	req.Telemetry(telemetryInfo)
258	reqQP := req.URL.Query()
259	reqQP.Set("api-version", "2019-06-01-preview")
260	req.URL.RawQuery = reqQP.Encode()
261	if options != nil && options.IfNoneMatch != nil {
262		req.Header.Set("If-None-Match", *options.IfNoneMatch)
263	}
264	req.Header.Set("Accept", "application/json")
265	return req, nil
266}
267
268// getLinkedServiceHandleResponse handles the GetLinkedService response.
269func (client *LinkedServiceClient) getLinkedServiceHandleResponse(resp *azcore.Response) (LinkedServiceResourceResponse, error) {
270	var val *LinkedServiceResource
271	if err := resp.UnmarshalAsJSON(&val); err != nil {
272		return LinkedServiceResourceResponse{}, err
273	}
274	return LinkedServiceResourceResponse{RawResponse: resp.Response, LinkedServiceResource: val}, nil
275}
276
277// getLinkedServiceHandleError handles the GetLinkedService error response.
278func (client *LinkedServiceClient) getLinkedServiceHandleError(resp *azcore.Response) error {
279	body, err := resp.Payload()
280	if err != nil {
281		return azcore.NewResponseError(err, resp.Response)
282	}
283	errType := CloudError{raw: string(body)}
284	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
285		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
286	}
287	return azcore.NewResponseError(&errType, resp.Response)
288}
289
290// GetLinkedServicesByWorkspace - Lists linked services.
291// If the operation fails it returns the *CloudError error type.
292func (client *LinkedServiceClient) GetLinkedServicesByWorkspace(options *LinkedServiceGetLinkedServicesByWorkspaceOptions) LinkedServiceListResponsePager {
293	return &linkedServiceListResponsePager{
294		pipeline: client.con.Pipeline(),
295		requester: func(ctx context.Context) (*azcore.Request, error) {
296			return client.getLinkedServicesByWorkspaceCreateRequest(ctx, options)
297		},
298		responder: client.getLinkedServicesByWorkspaceHandleResponse,
299		errorer:   client.getLinkedServicesByWorkspaceHandleError,
300		advancer: func(ctx context.Context, resp LinkedServiceListResponseResponse) (*azcore.Request, error) {
301			return azcore.NewRequest(ctx, http.MethodGet, *resp.LinkedServiceListResponse.NextLink)
302		},
303		statusCodes: []int{http.StatusOK},
304	}
305}
306
307// getLinkedServicesByWorkspaceCreateRequest creates the GetLinkedServicesByWorkspace request.
308func (client *LinkedServiceClient) getLinkedServicesByWorkspaceCreateRequest(ctx context.Context, options *LinkedServiceGetLinkedServicesByWorkspaceOptions) (*azcore.Request, error) {
309	urlPath := "/linkedservices"
310	req, err := azcore.NewRequest(ctx, http.MethodGet, azcore.JoinPaths(client.con.Endpoint(), urlPath))
311	if err != nil {
312		return nil, err
313	}
314	req.Telemetry(telemetryInfo)
315	reqQP := req.URL.Query()
316	reqQP.Set("api-version", "2019-06-01-preview")
317	req.URL.RawQuery = reqQP.Encode()
318	req.Header.Set("Accept", "application/json")
319	return req, nil
320}
321
322// getLinkedServicesByWorkspaceHandleResponse handles the GetLinkedServicesByWorkspace response.
323func (client *LinkedServiceClient) getLinkedServicesByWorkspaceHandleResponse(resp *azcore.Response) (LinkedServiceListResponseResponse, error) {
324	var val *LinkedServiceListResponse
325	if err := resp.UnmarshalAsJSON(&val); err != nil {
326		return LinkedServiceListResponseResponse{}, err
327	}
328	return LinkedServiceListResponseResponse{RawResponse: resp.Response, LinkedServiceListResponse: val}, nil
329}
330
331// getLinkedServicesByWorkspaceHandleError handles the GetLinkedServicesByWorkspace error response.
332func (client *LinkedServiceClient) getLinkedServicesByWorkspaceHandleError(resp *azcore.Response) error {
333	body, err := resp.Payload()
334	if err != nil {
335		return azcore.NewResponseError(err, resp.Response)
336	}
337	errType := CloudError{raw: string(body)}
338	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
339		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
340	}
341	return azcore.NewResponseError(&errType, resp.Response)
342}
343
344// BeginRenameLinkedService - Renames a linked service.
345// If the operation fails it returns the *CloudError error type.
346func (client *LinkedServiceClient) BeginRenameLinkedService(ctx context.Context, linkedServiceName string, request ArtifactRenameRequest, options *LinkedServiceBeginRenameLinkedServiceOptions) (HTTPPollerResponse, error) {
347	resp, err := client.renameLinkedService(ctx, linkedServiceName, request, options)
348	if err != nil {
349		return HTTPPollerResponse{}, err
350	}
351	result := HTTPPollerResponse{
352		RawResponse: resp.Response,
353	}
354	pt, err := azcore.NewLROPoller("LinkedServiceClient.RenameLinkedService", resp, client.con.Pipeline(), client.renameLinkedServiceHandleError)
355	if err != nil {
356		return HTTPPollerResponse{}, err
357	}
358	poller := &httpPoller{
359		pt: pt,
360	}
361	result.Poller = poller
362	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (*http.Response, error) {
363		return poller.pollUntilDone(ctx, frequency)
364	}
365	return result, nil
366}
367
368// ResumeRenameLinkedService creates a new HTTPPoller from the specified resume token.
369// token - The value must come from a previous call to HTTPPoller.ResumeToken().
370func (client *LinkedServiceClient) ResumeRenameLinkedService(ctx context.Context, token string) (HTTPPollerResponse, error) {
371	pt, err := azcore.NewLROPollerFromResumeToken("LinkedServiceClient.RenameLinkedService", token, client.con.Pipeline(), client.renameLinkedServiceHandleError)
372	if err != nil {
373		return HTTPPollerResponse{}, err
374	}
375	poller := &httpPoller{
376		pt: pt,
377	}
378	resp, err := poller.Poll(ctx)
379	if err != nil {
380		return HTTPPollerResponse{}, err
381	}
382	result := HTTPPollerResponse{
383		RawResponse: resp,
384	}
385	result.Poller = poller
386	result.PollUntilDone = func(ctx context.Context, frequency time.Duration) (*http.Response, error) {
387		return poller.pollUntilDone(ctx, frequency)
388	}
389	return result, nil
390}
391
392// RenameLinkedService - Renames a linked service.
393// If the operation fails it returns the *CloudError error type.
394func (client *LinkedServiceClient) renameLinkedService(ctx context.Context, linkedServiceName string, request ArtifactRenameRequest, options *LinkedServiceBeginRenameLinkedServiceOptions) (*azcore.Response, error) {
395	req, err := client.renameLinkedServiceCreateRequest(ctx, linkedServiceName, request, options)
396	if err != nil {
397		return nil, err
398	}
399	resp, err := client.con.Pipeline().Do(req)
400	if err != nil {
401		return nil, err
402	}
403	if !resp.HasStatusCode(http.StatusOK, http.StatusAccepted) {
404		return nil, client.renameLinkedServiceHandleError(resp)
405	}
406	return resp, nil
407}
408
409// renameLinkedServiceCreateRequest creates the RenameLinkedService request.
410func (client *LinkedServiceClient) renameLinkedServiceCreateRequest(ctx context.Context, linkedServiceName string, request ArtifactRenameRequest, options *LinkedServiceBeginRenameLinkedServiceOptions) (*azcore.Request, error) {
411	urlPath := "/linkedservices/{linkedServiceName}/rename"
412	if linkedServiceName == "" {
413		return nil, errors.New("parameter linkedServiceName cannot be empty")
414	}
415	urlPath = strings.ReplaceAll(urlPath, "{linkedServiceName}", url.PathEscape(linkedServiceName))
416	req, err := azcore.NewRequest(ctx, http.MethodPost, azcore.JoinPaths(client.con.Endpoint(), urlPath))
417	if err != nil {
418		return nil, err
419	}
420	req.Telemetry(telemetryInfo)
421	reqQP := req.URL.Query()
422	reqQP.Set("api-version", "2019-06-01-preview")
423	req.URL.RawQuery = reqQP.Encode()
424	req.Header.Set("Accept", "application/json")
425	return req, req.MarshalAsJSON(request)
426}
427
428// renameLinkedServiceHandleError handles the RenameLinkedService error response.
429func (client *LinkedServiceClient) renameLinkedServiceHandleError(resp *azcore.Response) error {
430	body, err := resp.Payload()
431	if err != nil {
432		return azcore.NewResponseError(err, resp.Response)
433	}
434	errType := CloudError{raw: string(body)}
435	if err := resp.UnmarshalAsJSON(&errType.InnerError); err != nil {
436		return azcore.NewResponseError(fmt.Errorf("%s\n%s", string(body), err), resp.Response)
437	}
438	return azcore.NewResponseError(&errType, resp.Response)
439}
440