1package operationalinsights
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// LinkedServicesClient is the operational Insights Client
30type LinkedServicesClient struct {
31	BaseClient
32}
33
34// NewLinkedServicesClient creates an instance of the LinkedServicesClient client.
35func NewLinkedServicesClient(subscriptionID string) LinkedServicesClient {
36	return NewLinkedServicesClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewLinkedServicesClientWithBaseURI creates an instance of the LinkedServicesClient client using a custom endpoint.
40// Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
41func NewLinkedServicesClientWithBaseURI(baseURI string, subscriptionID string) LinkedServicesClient {
42	return LinkedServicesClient{NewWithBaseURI(baseURI, subscriptionID)}
43}
44
45// CreateOrUpdate create or update a linked service.
46// Parameters:
47// resourceGroupName - the name of the resource group to get. The name is case insensitive.
48// workspaceName - name of the Log Analytics Workspace that will contain the linkedServices resource
49// linkedServiceName - name of the linkedServices resource
50// parameters - the parameters required to create or update a linked service.
51func (client LinkedServicesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, workspaceName string, linkedServiceName string, parameters LinkedService) (result LinkedService, err error) {
52	if tracing.IsEnabled() {
53		ctx = tracing.StartSpan(ctx, fqdn+"/LinkedServicesClient.CreateOrUpdate")
54		defer func() {
55			sc := -1
56			if result.Response.Response != nil {
57				sc = result.Response.Response.StatusCode
58			}
59			tracing.EndSpan(ctx, sc, err)
60		}()
61	}
62	if err := validation.Validate([]validation.Validation{
63		{TargetValue: resourceGroupName,
64			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
65				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
66				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}},
67		{TargetValue: parameters,
68			Constraints: []validation.Constraint{{Target: "parameters.LinkedServiceProperties", Name: validation.Null, Rule: true,
69				Chain: []validation.Constraint{{Target: "parameters.LinkedServiceProperties.ResourceID", Name: validation.Null, Rule: true, Chain: nil}}}}}}); err != nil {
70		return result, validation.NewError("operationalinsights.LinkedServicesClient", "CreateOrUpdate", err.Error())
71	}
72
73	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, workspaceName, linkedServiceName, parameters)
74	if err != nil {
75		err = autorest.NewErrorWithError(err, "operationalinsights.LinkedServicesClient", "CreateOrUpdate", nil, "Failure preparing request")
76		return
77	}
78
79	resp, err := client.CreateOrUpdateSender(req)
80	if err != nil {
81		result.Response = autorest.Response{Response: resp}
82		err = autorest.NewErrorWithError(err, "operationalinsights.LinkedServicesClient", "CreateOrUpdate", resp, "Failure sending request")
83		return
84	}
85
86	result, err = client.CreateOrUpdateResponder(resp)
87	if err != nil {
88		err = autorest.NewErrorWithError(err, "operationalinsights.LinkedServicesClient", "CreateOrUpdate", resp, "Failure responding to request")
89	}
90
91	return
92}
93
94// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
95func (client LinkedServicesClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, workspaceName string, linkedServiceName string, parameters LinkedService) (*http.Request, error) {
96	pathParameters := map[string]interface{}{
97		"linkedServiceName": autorest.Encode("path", linkedServiceName),
98		"resourceGroupName": autorest.Encode("path", resourceGroupName),
99		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
100		"workspaceName":     autorest.Encode("path", workspaceName),
101	}
102
103	const APIVersion = "2015-11-01-preview"
104	queryParameters := map[string]interface{}{
105		"api-version": APIVersion,
106	}
107
108	preparer := autorest.CreatePreparer(
109		autorest.AsContentType("application/json; charset=utf-8"),
110		autorest.AsPut(),
111		autorest.WithBaseURL(client.BaseURI),
112		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/linkedServices/{linkedServiceName}", pathParameters),
113		autorest.WithJSON(parameters),
114		autorest.WithQueryParameters(queryParameters))
115	return preparer.Prepare((&http.Request{}).WithContext(ctx))
116}
117
118// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
119// http.Response Body if it receives an error.
120func (client LinkedServicesClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
121	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
122}
123
124// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
125// closes the http.Response Body.
126func (client LinkedServicesClient) CreateOrUpdateResponder(resp *http.Response) (result LinkedService, err error) {
127	err = autorest.Respond(
128		resp,
129		client.ByInspecting(),
130		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
131		autorest.ByUnmarshallingJSON(&result),
132		autorest.ByClosing())
133	result.Response = autorest.Response{Response: resp}
134	return
135}
136
137// Delete deletes a linked service instance.
138// Parameters:
139// resourceGroupName - the name of the resource group to get. The name is case insensitive.
140// workspaceName - name of the Log Analytics Workspace that contains the linkedServices resource
141// linkedServiceName - name of the linked service.
142func (client LinkedServicesClient) Delete(ctx context.Context, resourceGroupName string, workspaceName string, linkedServiceName string) (result autorest.Response, err error) {
143	if tracing.IsEnabled() {
144		ctx = tracing.StartSpan(ctx, fqdn+"/LinkedServicesClient.Delete")
145		defer func() {
146			sc := -1
147			if result.Response != nil {
148				sc = result.Response.StatusCode
149			}
150			tracing.EndSpan(ctx, sc, err)
151		}()
152	}
153	if err := validation.Validate([]validation.Validation{
154		{TargetValue: resourceGroupName,
155			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
156				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
157				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}}}); err != nil {
158		return result, validation.NewError("operationalinsights.LinkedServicesClient", "Delete", err.Error())
159	}
160
161	req, err := client.DeletePreparer(ctx, resourceGroupName, workspaceName, linkedServiceName)
162	if err != nil {
163		err = autorest.NewErrorWithError(err, "operationalinsights.LinkedServicesClient", "Delete", nil, "Failure preparing request")
164		return
165	}
166
167	resp, err := client.DeleteSender(req)
168	if err != nil {
169		result.Response = resp
170		err = autorest.NewErrorWithError(err, "operationalinsights.LinkedServicesClient", "Delete", resp, "Failure sending request")
171		return
172	}
173
174	result, err = client.DeleteResponder(resp)
175	if err != nil {
176		err = autorest.NewErrorWithError(err, "operationalinsights.LinkedServicesClient", "Delete", resp, "Failure responding to request")
177	}
178
179	return
180}
181
182// DeletePreparer prepares the Delete request.
183func (client LinkedServicesClient) DeletePreparer(ctx context.Context, resourceGroupName string, workspaceName string, linkedServiceName string) (*http.Request, error) {
184	pathParameters := map[string]interface{}{
185		"linkedServiceName": autorest.Encode("path", linkedServiceName),
186		"resourceGroupName": autorest.Encode("path", resourceGroupName),
187		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
188		"workspaceName":     autorest.Encode("path", workspaceName),
189	}
190
191	const APIVersion = "2015-11-01-preview"
192	queryParameters := map[string]interface{}{
193		"api-version": APIVersion,
194	}
195
196	preparer := autorest.CreatePreparer(
197		autorest.AsDelete(),
198		autorest.WithBaseURL(client.BaseURI),
199		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/linkedServices/{linkedServiceName}", pathParameters),
200		autorest.WithQueryParameters(queryParameters))
201	return preparer.Prepare((&http.Request{}).WithContext(ctx))
202}
203
204// DeleteSender sends the Delete request. The method will close the
205// http.Response Body if it receives an error.
206func (client LinkedServicesClient) DeleteSender(req *http.Request) (*http.Response, error) {
207	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
208}
209
210// DeleteResponder handles the response to the Delete request. The method always
211// closes the http.Response Body.
212func (client LinkedServicesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
213	err = autorest.Respond(
214		resp,
215		client.ByInspecting(),
216		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
217		autorest.ByClosing())
218	result.Response = resp
219	return
220}
221
222// Get gets a linked service instance.
223// Parameters:
224// resourceGroupName - the name of the resource group to get. The name is case insensitive.
225// workspaceName - name of the Log Analytics Workspace that contains the linkedServices resource
226// linkedServiceName - name of the linked service.
227func (client LinkedServicesClient) Get(ctx context.Context, resourceGroupName string, workspaceName string, linkedServiceName string) (result LinkedService, err error) {
228	if tracing.IsEnabled() {
229		ctx = tracing.StartSpan(ctx, fqdn+"/LinkedServicesClient.Get")
230		defer func() {
231			sc := -1
232			if result.Response.Response != nil {
233				sc = result.Response.Response.StatusCode
234			}
235			tracing.EndSpan(ctx, sc, err)
236		}()
237	}
238	if err := validation.Validate([]validation.Validation{
239		{TargetValue: resourceGroupName,
240			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
241				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
242				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}}}); err != nil {
243		return result, validation.NewError("operationalinsights.LinkedServicesClient", "Get", err.Error())
244	}
245
246	req, err := client.GetPreparer(ctx, resourceGroupName, workspaceName, linkedServiceName)
247	if err != nil {
248		err = autorest.NewErrorWithError(err, "operationalinsights.LinkedServicesClient", "Get", nil, "Failure preparing request")
249		return
250	}
251
252	resp, err := client.GetSender(req)
253	if err != nil {
254		result.Response = autorest.Response{Response: resp}
255		err = autorest.NewErrorWithError(err, "operationalinsights.LinkedServicesClient", "Get", resp, "Failure sending request")
256		return
257	}
258
259	result, err = client.GetResponder(resp)
260	if err != nil {
261		err = autorest.NewErrorWithError(err, "operationalinsights.LinkedServicesClient", "Get", resp, "Failure responding to request")
262	}
263
264	return
265}
266
267// GetPreparer prepares the Get request.
268func (client LinkedServicesClient) GetPreparer(ctx context.Context, resourceGroupName string, workspaceName string, linkedServiceName string) (*http.Request, error) {
269	pathParameters := map[string]interface{}{
270		"linkedServiceName": autorest.Encode("path", linkedServiceName),
271		"resourceGroupName": autorest.Encode("path", resourceGroupName),
272		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
273		"workspaceName":     autorest.Encode("path", workspaceName),
274	}
275
276	const APIVersion = "2015-11-01-preview"
277	queryParameters := map[string]interface{}{
278		"api-version": APIVersion,
279	}
280
281	preparer := autorest.CreatePreparer(
282		autorest.AsGet(),
283		autorest.WithBaseURL(client.BaseURI),
284		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/linkedServices/{linkedServiceName}", pathParameters),
285		autorest.WithQueryParameters(queryParameters))
286	return preparer.Prepare((&http.Request{}).WithContext(ctx))
287}
288
289// GetSender sends the Get request. The method will close the
290// http.Response Body if it receives an error.
291func (client LinkedServicesClient) GetSender(req *http.Request) (*http.Response, error) {
292	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
293}
294
295// GetResponder handles the response to the Get request. The method always
296// closes the http.Response Body.
297func (client LinkedServicesClient) GetResponder(resp *http.Response) (result LinkedService, err error) {
298	err = autorest.Respond(
299		resp,
300		client.ByInspecting(),
301		azure.WithErrorUnlessStatusCode(http.StatusOK),
302		autorest.ByUnmarshallingJSON(&result),
303		autorest.ByClosing())
304	result.Response = autorest.Response{Response: resp}
305	return
306}
307
308// ListByWorkspace gets the linked services instances in a workspace.
309// Parameters:
310// resourceGroupName - the name of the resource group to get. The name is case insensitive.
311// workspaceName - name of the Log Analytics Workspace that contains the linked services.
312func (client LinkedServicesClient) ListByWorkspace(ctx context.Context, resourceGroupName string, workspaceName string) (result LinkedServiceListResult, err error) {
313	if tracing.IsEnabled() {
314		ctx = tracing.StartSpan(ctx, fqdn+"/LinkedServicesClient.ListByWorkspace")
315		defer func() {
316			sc := -1
317			if result.Response.Response != nil {
318				sc = result.Response.Response.StatusCode
319			}
320			tracing.EndSpan(ctx, sc, err)
321		}()
322	}
323	if err := validation.Validate([]validation.Validation{
324		{TargetValue: resourceGroupName,
325			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
326				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
327				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}}}); err != nil {
328		return result, validation.NewError("operationalinsights.LinkedServicesClient", "ListByWorkspace", err.Error())
329	}
330
331	req, err := client.ListByWorkspacePreparer(ctx, resourceGroupName, workspaceName)
332	if err != nil {
333		err = autorest.NewErrorWithError(err, "operationalinsights.LinkedServicesClient", "ListByWorkspace", nil, "Failure preparing request")
334		return
335	}
336
337	resp, err := client.ListByWorkspaceSender(req)
338	if err != nil {
339		result.Response = autorest.Response{Response: resp}
340		err = autorest.NewErrorWithError(err, "operationalinsights.LinkedServicesClient", "ListByWorkspace", resp, "Failure sending request")
341		return
342	}
343
344	result, err = client.ListByWorkspaceResponder(resp)
345	if err != nil {
346		err = autorest.NewErrorWithError(err, "operationalinsights.LinkedServicesClient", "ListByWorkspace", resp, "Failure responding to request")
347	}
348
349	return
350}
351
352// ListByWorkspacePreparer prepares the ListByWorkspace request.
353func (client LinkedServicesClient) ListByWorkspacePreparer(ctx context.Context, resourceGroupName string, workspaceName string) (*http.Request, error) {
354	pathParameters := map[string]interface{}{
355		"resourceGroupName": autorest.Encode("path", resourceGroupName),
356		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
357		"workspaceName":     autorest.Encode("path", workspaceName),
358	}
359
360	const APIVersion = "2015-11-01-preview"
361	queryParameters := map[string]interface{}{
362		"api-version": APIVersion,
363	}
364
365	preparer := autorest.CreatePreparer(
366		autorest.AsGet(),
367		autorest.WithBaseURL(client.BaseURI),
368		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/linkedServices", pathParameters),
369		autorest.WithQueryParameters(queryParameters))
370	return preparer.Prepare((&http.Request{}).WithContext(ctx))
371}
372
373// ListByWorkspaceSender sends the ListByWorkspace request. The method will close the
374// http.Response Body if it receives an error.
375func (client LinkedServicesClient) ListByWorkspaceSender(req *http.Request) (*http.Response, error) {
376	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
377}
378
379// ListByWorkspaceResponder handles the response to the ListByWorkspace request. The method always
380// closes the http.Response Body.
381func (client LinkedServicesClient) ListByWorkspaceResponder(resp *http.Response) (result LinkedServiceListResult, err error) {
382	err = autorest.Respond(
383		resp,
384		client.ByInspecting(),
385		azure.WithErrorUnlessStatusCode(http.StatusOK),
386		autorest.ByUnmarshallingJSON(&result),
387		autorest.ByClosing())
388	result.Response = autorest.Response{Response: resp}
389	return
390}
391