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