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