1package sql
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// JobTargetGroupsClient is the the Azure SQL Database management API provides a RESTful set of web services that
30// interact with Azure SQL Database services to manage your databases. The API enables you to create, retrieve, update,
31// and delete databases.
32type JobTargetGroupsClient struct {
33	BaseClient
34}
35
36// NewJobTargetGroupsClient creates an instance of the JobTargetGroupsClient client.
37func NewJobTargetGroupsClient(subscriptionID string) JobTargetGroupsClient {
38	return NewJobTargetGroupsClientWithBaseURI(DefaultBaseURI, subscriptionID)
39}
40
41// NewJobTargetGroupsClientWithBaseURI creates an instance of the JobTargetGroupsClient client using a custom endpoint.
42// Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
43func NewJobTargetGroupsClientWithBaseURI(baseURI string, subscriptionID string) JobTargetGroupsClient {
44	return JobTargetGroupsClient{NewWithBaseURI(baseURI, subscriptionID)}
45}
46
47// CreateOrUpdate creates or updates a target group.
48// Parameters:
49// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
50// from the Azure Resource Manager API or the portal.
51// serverName - the name of the server.
52// jobAgentName - the name of the job agent.
53// targetGroupName - the name of the target group.
54// parameters - the requested state of the target group.
55func (client JobTargetGroupsClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serverName string, jobAgentName string, targetGroupName string, parameters JobTargetGroup) (result JobTargetGroup, err error) {
56	if tracing.IsEnabled() {
57		ctx = tracing.StartSpan(ctx, fqdn+"/JobTargetGroupsClient.CreateOrUpdate")
58		defer func() {
59			sc := -1
60			if result.Response.Response != nil {
61				sc = result.Response.Response.StatusCode
62			}
63			tracing.EndSpan(ctx, sc, err)
64		}()
65	}
66	if err := validation.Validate([]validation.Validation{
67		{TargetValue: parameters,
68			Constraints: []validation.Constraint{{Target: "parameters.JobTargetGroupProperties", Name: validation.Null, Rule: false,
69				Chain: []validation.Constraint{{Target: "parameters.JobTargetGroupProperties.Members", Name: validation.Null, Rule: true, Chain: nil}}}}}}); err != nil {
70		return result, validation.NewError("sql.JobTargetGroupsClient", "CreateOrUpdate", err.Error())
71	}
72
73	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, serverName, jobAgentName, targetGroupName, parameters)
74	if err != nil {
75		err = autorest.NewErrorWithError(err, "sql.JobTargetGroupsClient", "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, "sql.JobTargetGroupsClient", "CreateOrUpdate", resp, "Failure sending request")
83		return
84	}
85
86	result, err = client.CreateOrUpdateResponder(resp)
87	if err != nil {
88		err = autorest.NewErrorWithError(err, "sql.JobTargetGroupsClient", "CreateOrUpdate", resp, "Failure responding to request")
89	}
90
91	return
92}
93
94// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
95func (client JobTargetGroupsClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serverName string, jobAgentName string, targetGroupName string, parameters JobTargetGroup) (*http.Request, error) {
96	pathParameters := map[string]interface{}{
97		"jobAgentName":      autorest.Encode("path", jobAgentName),
98		"resourceGroupName": autorest.Encode("path", resourceGroupName),
99		"serverName":        autorest.Encode("path", serverName),
100		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
101		"targetGroupName":   autorest.Encode("path", targetGroupName),
102	}
103
104	const APIVersion = "2017-03-01-preview"
105	queryParameters := map[string]interface{}{
106		"api-version": APIVersion,
107	}
108
109	preparer := autorest.CreatePreparer(
110		autorest.AsContentType("application/json; charset=utf-8"),
111		autorest.AsPut(),
112		autorest.WithBaseURL(client.BaseURI),
113		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/jobAgents/{jobAgentName}/targetGroups/{targetGroupName}", pathParameters),
114		autorest.WithJSON(parameters),
115		autorest.WithQueryParameters(queryParameters))
116	return preparer.Prepare((&http.Request{}).WithContext(ctx))
117}
118
119// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
120// http.Response Body if it receives an error.
121func (client JobTargetGroupsClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
122	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
123}
124
125// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
126// closes the http.Response Body.
127func (client JobTargetGroupsClient) CreateOrUpdateResponder(resp *http.Response) (result JobTargetGroup, err error) {
128	err = autorest.Respond(
129		resp,
130		client.ByInspecting(),
131		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
132		autorest.ByUnmarshallingJSON(&result),
133		autorest.ByClosing())
134	result.Response = autorest.Response{Response: resp}
135	return
136}
137
138// Delete deletes a target group.
139// Parameters:
140// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
141// from the Azure Resource Manager API or the portal.
142// serverName - the name of the server.
143// jobAgentName - the name of the job agent.
144// targetGroupName - the name of the target group.
145func (client JobTargetGroupsClient) Delete(ctx context.Context, resourceGroupName string, serverName string, jobAgentName string, targetGroupName string) (result autorest.Response, err error) {
146	if tracing.IsEnabled() {
147		ctx = tracing.StartSpan(ctx, fqdn+"/JobTargetGroupsClient.Delete")
148		defer func() {
149			sc := -1
150			if result.Response != nil {
151				sc = result.Response.StatusCode
152			}
153			tracing.EndSpan(ctx, sc, err)
154		}()
155	}
156	req, err := client.DeletePreparer(ctx, resourceGroupName, serverName, jobAgentName, targetGroupName)
157	if err != nil {
158		err = autorest.NewErrorWithError(err, "sql.JobTargetGroupsClient", "Delete", nil, "Failure preparing request")
159		return
160	}
161
162	resp, err := client.DeleteSender(req)
163	if err != nil {
164		result.Response = resp
165		err = autorest.NewErrorWithError(err, "sql.JobTargetGroupsClient", "Delete", resp, "Failure sending request")
166		return
167	}
168
169	result, err = client.DeleteResponder(resp)
170	if err != nil {
171		err = autorest.NewErrorWithError(err, "sql.JobTargetGroupsClient", "Delete", resp, "Failure responding to request")
172	}
173
174	return
175}
176
177// DeletePreparer prepares the Delete request.
178func (client JobTargetGroupsClient) DeletePreparer(ctx context.Context, resourceGroupName string, serverName string, jobAgentName string, targetGroupName string) (*http.Request, error) {
179	pathParameters := map[string]interface{}{
180		"jobAgentName":      autorest.Encode("path", jobAgentName),
181		"resourceGroupName": autorest.Encode("path", resourceGroupName),
182		"serverName":        autorest.Encode("path", serverName),
183		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
184		"targetGroupName":   autorest.Encode("path", targetGroupName),
185	}
186
187	const APIVersion = "2017-03-01-preview"
188	queryParameters := map[string]interface{}{
189		"api-version": APIVersion,
190	}
191
192	preparer := autorest.CreatePreparer(
193		autorest.AsDelete(),
194		autorest.WithBaseURL(client.BaseURI),
195		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/jobAgents/{jobAgentName}/targetGroups/{targetGroupName}", pathParameters),
196		autorest.WithQueryParameters(queryParameters))
197	return preparer.Prepare((&http.Request{}).WithContext(ctx))
198}
199
200// DeleteSender sends the Delete request. The method will close the
201// http.Response Body if it receives an error.
202func (client JobTargetGroupsClient) DeleteSender(req *http.Request) (*http.Response, error) {
203	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
204}
205
206// DeleteResponder handles the response to the Delete request. The method always
207// closes the http.Response Body.
208func (client JobTargetGroupsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
209	err = autorest.Respond(
210		resp,
211		client.ByInspecting(),
212		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
213		autorest.ByClosing())
214	result.Response = resp
215	return
216}
217
218// Get gets a target group.
219// Parameters:
220// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
221// from the Azure Resource Manager API or the portal.
222// serverName - the name of the server.
223// jobAgentName - the name of the job agent.
224// targetGroupName - the name of the target group.
225func (client JobTargetGroupsClient) Get(ctx context.Context, resourceGroupName string, serverName string, jobAgentName string, targetGroupName string) (result JobTargetGroup, err error) {
226	if tracing.IsEnabled() {
227		ctx = tracing.StartSpan(ctx, fqdn+"/JobTargetGroupsClient.Get")
228		defer func() {
229			sc := -1
230			if result.Response.Response != nil {
231				sc = result.Response.Response.StatusCode
232			}
233			tracing.EndSpan(ctx, sc, err)
234		}()
235	}
236	req, err := client.GetPreparer(ctx, resourceGroupName, serverName, jobAgentName, targetGroupName)
237	if err != nil {
238		err = autorest.NewErrorWithError(err, "sql.JobTargetGroupsClient", "Get", nil, "Failure preparing request")
239		return
240	}
241
242	resp, err := client.GetSender(req)
243	if err != nil {
244		result.Response = autorest.Response{Response: resp}
245		err = autorest.NewErrorWithError(err, "sql.JobTargetGroupsClient", "Get", resp, "Failure sending request")
246		return
247	}
248
249	result, err = client.GetResponder(resp)
250	if err != nil {
251		err = autorest.NewErrorWithError(err, "sql.JobTargetGroupsClient", "Get", resp, "Failure responding to request")
252	}
253
254	return
255}
256
257// GetPreparer prepares the Get request.
258func (client JobTargetGroupsClient) GetPreparer(ctx context.Context, resourceGroupName string, serverName string, jobAgentName string, targetGroupName string) (*http.Request, error) {
259	pathParameters := map[string]interface{}{
260		"jobAgentName":      autorest.Encode("path", jobAgentName),
261		"resourceGroupName": autorest.Encode("path", resourceGroupName),
262		"serverName":        autorest.Encode("path", serverName),
263		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
264		"targetGroupName":   autorest.Encode("path", targetGroupName),
265	}
266
267	const APIVersion = "2017-03-01-preview"
268	queryParameters := map[string]interface{}{
269		"api-version": APIVersion,
270	}
271
272	preparer := autorest.CreatePreparer(
273		autorest.AsGet(),
274		autorest.WithBaseURL(client.BaseURI),
275		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/jobAgents/{jobAgentName}/targetGroups/{targetGroupName}", pathParameters),
276		autorest.WithQueryParameters(queryParameters))
277	return preparer.Prepare((&http.Request{}).WithContext(ctx))
278}
279
280// GetSender sends the Get request. The method will close the
281// http.Response Body if it receives an error.
282func (client JobTargetGroupsClient) GetSender(req *http.Request) (*http.Response, error) {
283	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
284}
285
286// GetResponder handles the response to the Get request. The method always
287// closes the http.Response Body.
288func (client JobTargetGroupsClient) GetResponder(resp *http.Response) (result JobTargetGroup, err error) {
289	err = autorest.Respond(
290		resp,
291		client.ByInspecting(),
292		azure.WithErrorUnlessStatusCode(http.StatusOK),
293		autorest.ByUnmarshallingJSON(&result),
294		autorest.ByClosing())
295	result.Response = autorest.Response{Response: resp}
296	return
297}
298
299// ListByAgent gets all target groups in an agent.
300// Parameters:
301// resourceGroupName - the name of the resource group that contains the resource. You can obtain this value
302// from the Azure Resource Manager API or the portal.
303// serverName - the name of the server.
304// jobAgentName - the name of the job agent.
305func (client JobTargetGroupsClient) ListByAgent(ctx context.Context, resourceGroupName string, serverName string, jobAgentName string) (result JobTargetGroupListResultPage, err error) {
306	if tracing.IsEnabled() {
307		ctx = tracing.StartSpan(ctx, fqdn+"/JobTargetGroupsClient.ListByAgent")
308		defer func() {
309			sc := -1
310			if result.jtglr.Response.Response != nil {
311				sc = result.jtglr.Response.Response.StatusCode
312			}
313			tracing.EndSpan(ctx, sc, err)
314		}()
315	}
316	result.fn = client.listByAgentNextResults
317	req, err := client.ListByAgentPreparer(ctx, resourceGroupName, serverName, jobAgentName)
318	if err != nil {
319		err = autorest.NewErrorWithError(err, "sql.JobTargetGroupsClient", "ListByAgent", nil, "Failure preparing request")
320		return
321	}
322
323	resp, err := client.ListByAgentSender(req)
324	if err != nil {
325		result.jtglr.Response = autorest.Response{Response: resp}
326		err = autorest.NewErrorWithError(err, "sql.JobTargetGroupsClient", "ListByAgent", resp, "Failure sending request")
327		return
328	}
329
330	result.jtglr, err = client.ListByAgentResponder(resp)
331	if err != nil {
332		err = autorest.NewErrorWithError(err, "sql.JobTargetGroupsClient", "ListByAgent", resp, "Failure responding to request")
333	}
334
335	return
336}
337
338// ListByAgentPreparer prepares the ListByAgent request.
339func (client JobTargetGroupsClient) ListByAgentPreparer(ctx context.Context, resourceGroupName string, serverName string, jobAgentName string) (*http.Request, error) {
340	pathParameters := map[string]interface{}{
341		"jobAgentName":      autorest.Encode("path", jobAgentName),
342		"resourceGroupName": autorest.Encode("path", resourceGroupName),
343		"serverName":        autorest.Encode("path", serverName),
344		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
345	}
346
347	const APIVersion = "2017-03-01-preview"
348	queryParameters := map[string]interface{}{
349		"api-version": APIVersion,
350	}
351
352	preparer := autorest.CreatePreparer(
353		autorest.AsGet(),
354		autorest.WithBaseURL(client.BaseURI),
355		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/jobAgents/{jobAgentName}/targetGroups", pathParameters),
356		autorest.WithQueryParameters(queryParameters))
357	return preparer.Prepare((&http.Request{}).WithContext(ctx))
358}
359
360// ListByAgentSender sends the ListByAgent request. The method will close the
361// http.Response Body if it receives an error.
362func (client JobTargetGroupsClient) ListByAgentSender(req *http.Request) (*http.Response, error) {
363	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
364}
365
366// ListByAgentResponder handles the response to the ListByAgent request. The method always
367// closes the http.Response Body.
368func (client JobTargetGroupsClient) ListByAgentResponder(resp *http.Response) (result JobTargetGroupListResult, err error) {
369	err = autorest.Respond(
370		resp,
371		client.ByInspecting(),
372		azure.WithErrorUnlessStatusCode(http.StatusOK),
373		autorest.ByUnmarshallingJSON(&result),
374		autorest.ByClosing())
375	result.Response = autorest.Response{Response: resp}
376	return
377}
378
379// listByAgentNextResults retrieves the next set of results, if any.
380func (client JobTargetGroupsClient) listByAgentNextResults(ctx context.Context, lastResults JobTargetGroupListResult) (result JobTargetGroupListResult, err error) {
381	req, err := lastResults.jobTargetGroupListResultPreparer(ctx)
382	if err != nil {
383		return result, autorest.NewErrorWithError(err, "sql.JobTargetGroupsClient", "listByAgentNextResults", nil, "Failure preparing next results request")
384	}
385	if req == nil {
386		return
387	}
388	resp, err := client.ListByAgentSender(req)
389	if err != nil {
390		result.Response = autorest.Response{Response: resp}
391		return result, autorest.NewErrorWithError(err, "sql.JobTargetGroupsClient", "listByAgentNextResults", resp, "Failure sending next results request")
392	}
393	result, err = client.ListByAgentResponder(resp)
394	if err != nil {
395		err = autorest.NewErrorWithError(err, "sql.JobTargetGroupsClient", "listByAgentNextResults", resp, "Failure responding to next results request")
396	}
397	return
398}
399
400// ListByAgentComplete enumerates all values, automatically crossing page boundaries as required.
401func (client JobTargetGroupsClient) ListByAgentComplete(ctx context.Context, resourceGroupName string, serverName string, jobAgentName string) (result JobTargetGroupListResultIterator, err error) {
402	if tracing.IsEnabled() {
403		ctx = tracing.StartSpan(ctx, fqdn+"/JobTargetGroupsClient.ListByAgent")
404		defer func() {
405			sc := -1
406			if result.Response().Response.Response != nil {
407				sc = result.page.Response().Response.Response.StatusCode
408			}
409			tracing.EndSpan(ctx, sc, err)
410		}()
411	}
412	result.page, err = client.ListByAgent(ctx, resourceGroupName, serverName, jobAgentName)
413	return
414}
415