1package storagecache
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// StorageTargetsClient is the a Storage Cache provides scalable caching service for NAS clients, serving data from
19// either NFSv3 or Blob at-rest storage (referred to as "Storage Targets"). These operations allow you to manage
20// Caches.
21type StorageTargetsClient struct {
22	BaseClient
23}
24
25// NewStorageTargetsClient creates an instance of the StorageTargetsClient client.
26func NewStorageTargetsClient(subscriptionID string) StorageTargetsClient {
27	return NewStorageTargetsClientWithBaseURI(DefaultBaseURI, subscriptionID)
28}
29
30// NewStorageTargetsClientWithBaseURI creates an instance of the StorageTargetsClient 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 NewStorageTargetsClientWithBaseURI(baseURI string, subscriptionID string) StorageTargetsClient {
33	return StorageTargetsClient{NewWithBaseURI(baseURI, subscriptionID)}
34}
35
36// CreateOrUpdate create or update a Storage Target. This operation is allowed at any time, but if the Cache is down or
37// unhealthy, the actual creation/modification of the Storage Target may be delayed until the Cache is healthy again.
38// Parameters:
39// resourceGroupName - target resource group.
40// cacheName - name of Cache. Length of name must be not greater than 80 and chars must be in list of
41// [-0-9a-zA-Z_] char class.
42// storageTargetName - name of the Storage Target. Length of name must be not greater than 80 and chars must be
43// in list of [-0-9a-zA-Z_] char class.
44// storagetarget - object containing the definition of a Storage Target.
45func (client StorageTargetsClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, cacheName string, storageTargetName string, storagetarget *StorageTarget) (result StorageTargetsCreateOrUpdateFuture, err error) {
46	if tracing.IsEnabled() {
47		ctx = tracing.StartSpan(ctx, fqdn+"/StorageTargetsClient.CreateOrUpdate")
48		defer func() {
49			sc := -1
50			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
51				sc = result.FutureAPI.Response().StatusCode
52			}
53			tracing.EndSpan(ctx, sc, err)
54		}()
55	}
56	if err := validation.Validate([]validation.Validation{
57		{TargetValue: cacheName,
58			Constraints: []validation.Constraint{{Target: "cacheName", Name: validation.Pattern, Rule: `^[-0-9a-zA-Z_]{1,80}$`, Chain: nil}}},
59		{TargetValue: storageTargetName,
60			Constraints: []validation.Constraint{{Target: "storageTargetName", Name: validation.Pattern, Rule: `^[-0-9a-zA-Z_]{1,80}$`, Chain: nil}}}}); err != nil {
61		return result, validation.NewError("storagecache.StorageTargetsClient", "CreateOrUpdate", err.Error())
62	}
63
64	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, cacheName, storageTargetName, storagetarget)
65	if err != nil {
66		err = autorest.NewErrorWithError(err, "storagecache.StorageTargetsClient", "CreateOrUpdate", nil, "Failure preparing request")
67		return
68	}
69
70	result, err = client.CreateOrUpdateSender(req)
71	if err != nil {
72		err = autorest.NewErrorWithError(err, "storagecache.StorageTargetsClient", "CreateOrUpdate", nil, "Failure sending request")
73		return
74	}
75
76	return
77}
78
79// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
80func (client StorageTargetsClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, cacheName string, storageTargetName string, storagetarget *StorageTarget) (*http.Request, error) {
81	pathParameters := map[string]interface{}{
82		"cacheName":         autorest.Encode("path", cacheName),
83		"resourceGroupName": autorest.Encode("path", resourceGroupName),
84		"storageTargetName": autorest.Encode("path", storageTargetName),
85		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
86	}
87
88	const APIVersion = "2020-03-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.StorageCache/caches/{cacheName}/storageTargets/{storageTargetName}", pathParameters),
98		autorest.WithQueryParameters(queryParameters))
99	if storagetarget != nil {
100		preparer = autorest.DecoratePreparer(preparer,
101			autorest.WithJSON(storagetarget))
102	}
103	return preparer.Prepare((&http.Request{}).WithContext(ctx))
104}
105
106// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
107// http.Response Body if it receives an error.
108func (client StorageTargetsClient) CreateOrUpdateSender(req *http.Request) (future StorageTargetsCreateOrUpdateFuture, err error) {
109	var resp *http.Response
110	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
111	if err != nil {
112		return
113	}
114	var azf azure.Future
115	azf, err = azure.NewFutureFromResponse(resp)
116	future.FutureAPI = &azf
117	future.Result = future.result
118	return
119}
120
121// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
122// closes the http.Response Body.
123func (client StorageTargetsClient) CreateOrUpdateResponder(resp *http.Response) (result StorageTarget, err error) {
124	err = autorest.Respond(
125		resp,
126		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated, http.StatusAccepted),
127		autorest.ByUnmarshallingJSON(&result),
128		autorest.ByClosing())
129	result.Response = autorest.Response{Response: resp}
130	return
131}
132
133// Delete removes a Storage Target from a Cache. This operation is allowed at any time, but if the Cache is down or
134// unhealthy, the actual removal of the Storage Target may be delayed until the Cache is healthy again. Note that if
135// the Cache has data to flush to the Storage Target, the data will be flushed before the Storage Target will be
136// deleted.
137// Parameters:
138// resourceGroupName - target resource group.
139// cacheName - name of Cache. Length of name must be not greater than 80 and chars must be in list of
140// [-0-9a-zA-Z_] char class.
141// storageTargetName - name of Storage Target.
142func (client StorageTargetsClient) Delete(ctx context.Context, resourceGroupName string, cacheName string, storageTargetName string) (result StorageTargetsDeleteFuture, err error) {
143	if tracing.IsEnabled() {
144		ctx = tracing.StartSpan(ctx, fqdn+"/StorageTargetsClient.Delete")
145		defer func() {
146			sc := -1
147			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
148				sc = result.FutureAPI.Response().StatusCode
149			}
150			tracing.EndSpan(ctx, sc, err)
151		}()
152	}
153	if err := validation.Validate([]validation.Validation{
154		{TargetValue: cacheName,
155			Constraints: []validation.Constraint{{Target: "cacheName", Name: validation.Pattern, Rule: `^[-0-9a-zA-Z_]{1,80}$`, Chain: nil}}},
156		{TargetValue: storageTargetName,
157			Constraints: []validation.Constraint{{Target: "storageTargetName", Name: validation.Pattern, Rule: `^[-0-9a-zA-Z_]{1,80}$`, Chain: nil}}}}); err != nil {
158		return result, validation.NewError("storagecache.StorageTargetsClient", "Delete", err.Error())
159	}
160
161	req, err := client.DeletePreparer(ctx, resourceGroupName, cacheName, storageTargetName)
162	if err != nil {
163		err = autorest.NewErrorWithError(err, "storagecache.StorageTargetsClient", "Delete", nil, "Failure preparing request")
164		return
165	}
166
167	result, err = client.DeleteSender(req)
168	if err != nil {
169		err = autorest.NewErrorWithError(err, "storagecache.StorageTargetsClient", "Delete", nil, "Failure sending request")
170		return
171	}
172
173	return
174}
175
176// DeletePreparer prepares the Delete request.
177func (client StorageTargetsClient) DeletePreparer(ctx context.Context, resourceGroupName string, cacheName string, storageTargetName string) (*http.Request, error) {
178	pathParameters := map[string]interface{}{
179		"cacheName":         autorest.Encode("path", cacheName),
180		"resourceGroupName": autorest.Encode("path", resourceGroupName),
181		"storageTargetName": autorest.Encode("path", storageTargetName),
182		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
183	}
184
185	const APIVersion = "2020-03-01"
186	queryParameters := map[string]interface{}{
187		"api-version": APIVersion,
188	}
189
190	preparer := autorest.CreatePreparer(
191		autorest.AsDelete(),
192		autorest.WithBaseURL(client.BaseURI),
193		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.StorageCache/caches/{cacheName}/storageTargets/{storageTargetName}", pathParameters),
194		autorest.WithQueryParameters(queryParameters))
195	return preparer.Prepare((&http.Request{}).WithContext(ctx))
196}
197
198// DeleteSender sends the Delete request. The method will close the
199// http.Response Body if it receives an error.
200func (client StorageTargetsClient) DeleteSender(req *http.Request) (future StorageTargetsDeleteFuture, err error) {
201	var resp *http.Response
202	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
203	if err != nil {
204		return
205	}
206	var azf azure.Future
207	azf, err = azure.NewFutureFromResponse(resp)
208	future.FutureAPI = &azf
209	future.Result = future.result
210	return
211}
212
213// DeleteResponder handles the response to the Delete request. The method always
214// closes the http.Response Body.
215func (client StorageTargetsClient) DeleteResponder(resp *http.Response) (result SetObject, err error) {
216	err = autorest.Respond(
217		resp,
218		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
219		autorest.ByUnmarshallingJSON(&result),
220		autorest.ByClosing())
221	result.Response = autorest.Response{Response: resp}
222	return
223}
224
225// Get returns a Storage Target from a Cache.
226// Parameters:
227// resourceGroupName - target resource group.
228// cacheName - name of Cache. Length of name must be not greater than 80 and chars must be in list of
229// [-0-9a-zA-Z_] char class.
230// storageTargetName - name of the Storage Target. Length of name must be not greater than 80 and chars must be
231// in list of [-0-9a-zA-Z_] char class.
232func (client StorageTargetsClient) Get(ctx context.Context, resourceGroupName string, cacheName string, storageTargetName string) (result StorageTarget, err error) {
233	if tracing.IsEnabled() {
234		ctx = tracing.StartSpan(ctx, fqdn+"/StorageTargetsClient.Get")
235		defer func() {
236			sc := -1
237			if result.Response.Response != nil {
238				sc = result.Response.Response.StatusCode
239			}
240			tracing.EndSpan(ctx, sc, err)
241		}()
242	}
243	if err := validation.Validate([]validation.Validation{
244		{TargetValue: cacheName,
245			Constraints: []validation.Constraint{{Target: "cacheName", Name: validation.Pattern, Rule: `^[-0-9a-zA-Z_]{1,80}$`, Chain: nil}}},
246		{TargetValue: storageTargetName,
247			Constraints: []validation.Constraint{{Target: "storageTargetName", Name: validation.Pattern, Rule: `^[-0-9a-zA-Z_]{1,80}$`, Chain: nil}}}}); err != nil {
248		return result, validation.NewError("storagecache.StorageTargetsClient", "Get", err.Error())
249	}
250
251	req, err := client.GetPreparer(ctx, resourceGroupName, cacheName, storageTargetName)
252	if err != nil {
253		err = autorest.NewErrorWithError(err, "storagecache.StorageTargetsClient", "Get", nil, "Failure preparing request")
254		return
255	}
256
257	resp, err := client.GetSender(req)
258	if err != nil {
259		result.Response = autorest.Response{Response: resp}
260		err = autorest.NewErrorWithError(err, "storagecache.StorageTargetsClient", "Get", resp, "Failure sending request")
261		return
262	}
263
264	result, err = client.GetResponder(resp)
265	if err != nil {
266		err = autorest.NewErrorWithError(err, "storagecache.StorageTargetsClient", "Get", resp, "Failure responding to request")
267		return
268	}
269
270	return
271}
272
273// GetPreparer prepares the Get request.
274func (client StorageTargetsClient) GetPreparer(ctx context.Context, resourceGroupName string, cacheName string, storageTargetName string) (*http.Request, error) {
275	pathParameters := map[string]interface{}{
276		"cacheName":         autorest.Encode("path", cacheName),
277		"resourceGroupName": autorest.Encode("path", resourceGroupName),
278		"storageTargetName": autorest.Encode("path", storageTargetName),
279		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
280	}
281
282	const APIVersion = "2020-03-01"
283	queryParameters := map[string]interface{}{
284		"api-version": APIVersion,
285	}
286
287	preparer := autorest.CreatePreparer(
288		autorest.AsGet(),
289		autorest.WithBaseURL(client.BaseURI),
290		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.StorageCache/caches/{cacheName}/storageTargets/{storageTargetName}", pathParameters),
291		autorest.WithQueryParameters(queryParameters))
292	return preparer.Prepare((&http.Request{}).WithContext(ctx))
293}
294
295// GetSender sends the Get request. The method will close the
296// http.Response Body if it receives an error.
297func (client StorageTargetsClient) GetSender(req *http.Request) (*http.Response, error) {
298	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
299}
300
301// GetResponder handles the response to the Get request. The method always
302// closes the http.Response Body.
303func (client StorageTargetsClient) GetResponder(resp *http.Response) (result StorageTarget, err error) {
304	err = autorest.Respond(
305		resp,
306		azure.WithErrorUnlessStatusCode(http.StatusOK),
307		autorest.ByUnmarshallingJSON(&result),
308		autorest.ByClosing())
309	result.Response = autorest.Response{Response: resp}
310	return
311}
312
313// ListByCache returns a list of Storage Targets for the specified Cache.
314// Parameters:
315// resourceGroupName - target resource group.
316// cacheName - name of Cache. Length of name must be not greater than 80 and chars must be in list of
317// [-0-9a-zA-Z_] char class.
318func (client StorageTargetsClient) ListByCache(ctx context.Context, resourceGroupName string, cacheName string) (result StorageTargetsResultPage, err error) {
319	if tracing.IsEnabled() {
320		ctx = tracing.StartSpan(ctx, fqdn+"/StorageTargetsClient.ListByCache")
321		defer func() {
322			sc := -1
323			if result.str.Response.Response != nil {
324				sc = result.str.Response.Response.StatusCode
325			}
326			tracing.EndSpan(ctx, sc, err)
327		}()
328	}
329	if err := validation.Validate([]validation.Validation{
330		{TargetValue: cacheName,
331			Constraints: []validation.Constraint{{Target: "cacheName", Name: validation.Pattern, Rule: `^[-0-9a-zA-Z_]{1,80}$`, Chain: nil}}}}); err != nil {
332		return result, validation.NewError("storagecache.StorageTargetsClient", "ListByCache", err.Error())
333	}
334
335	result.fn = client.listByCacheNextResults
336	req, err := client.ListByCachePreparer(ctx, resourceGroupName, cacheName)
337	if err != nil {
338		err = autorest.NewErrorWithError(err, "storagecache.StorageTargetsClient", "ListByCache", nil, "Failure preparing request")
339		return
340	}
341
342	resp, err := client.ListByCacheSender(req)
343	if err != nil {
344		result.str.Response = autorest.Response{Response: resp}
345		err = autorest.NewErrorWithError(err, "storagecache.StorageTargetsClient", "ListByCache", resp, "Failure sending request")
346		return
347	}
348
349	result.str, err = client.ListByCacheResponder(resp)
350	if err != nil {
351		err = autorest.NewErrorWithError(err, "storagecache.StorageTargetsClient", "ListByCache", resp, "Failure responding to request")
352		return
353	}
354	if result.str.hasNextLink() && result.str.IsEmpty() {
355		err = result.NextWithContext(ctx)
356		return
357	}
358
359	return
360}
361
362// ListByCachePreparer prepares the ListByCache request.
363func (client StorageTargetsClient) ListByCachePreparer(ctx context.Context, resourceGroupName string, cacheName string) (*http.Request, error) {
364	pathParameters := map[string]interface{}{
365		"cacheName":         autorest.Encode("path", cacheName),
366		"resourceGroupName": autorest.Encode("path", resourceGroupName),
367		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
368	}
369
370	const APIVersion = "2020-03-01"
371	queryParameters := map[string]interface{}{
372		"api-version": APIVersion,
373	}
374
375	preparer := autorest.CreatePreparer(
376		autorest.AsGet(),
377		autorest.WithBaseURL(client.BaseURI),
378		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.StorageCache/caches/{cacheName}/storageTargets", pathParameters),
379		autorest.WithQueryParameters(queryParameters))
380	return preparer.Prepare((&http.Request{}).WithContext(ctx))
381}
382
383// ListByCacheSender sends the ListByCache request. The method will close the
384// http.Response Body if it receives an error.
385func (client StorageTargetsClient) ListByCacheSender(req *http.Request) (*http.Response, error) {
386	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
387}
388
389// ListByCacheResponder handles the response to the ListByCache request. The method always
390// closes the http.Response Body.
391func (client StorageTargetsClient) ListByCacheResponder(resp *http.Response) (result StorageTargetsResult, err error) {
392	err = autorest.Respond(
393		resp,
394		azure.WithErrorUnlessStatusCode(http.StatusOK),
395		autorest.ByUnmarshallingJSON(&result),
396		autorest.ByClosing())
397	result.Response = autorest.Response{Response: resp}
398	return
399}
400
401// listByCacheNextResults retrieves the next set of results, if any.
402func (client StorageTargetsClient) listByCacheNextResults(ctx context.Context, lastResults StorageTargetsResult) (result StorageTargetsResult, err error) {
403	req, err := lastResults.storageTargetsResultPreparer(ctx)
404	if err != nil {
405		return result, autorest.NewErrorWithError(err, "storagecache.StorageTargetsClient", "listByCacheNextResults", nil, "Failure preparing next results request")
406	}
407	if req == nil {
408		return
409	}
410	resp, err := client.ListByCacheSender(req)
411	if err != nil {
412		result.Response = autorest.Response{Response: resp}
413		return result, autorest.NewErrorWithError(err, "storagecache.StorageTargetsClient", "listByCacheNextResults", resp, "Failure sending next results request")
414	}
415	result, err = client.ListByCacheResponder(resp)
416	if err != nil {
417		err = autorest.NewErrorWithError(err, "storagecache.StorageTargetsClient", "listByCacheNextResults", resp, "Failure responding to next results request")
418	}
419	return
420}
421
422// ListByCacheComplete enumerates all values, automatically crossing page boundaries as required.
423func (client StorageTargetsClient) ListByCacheComplete(ctx context.Context, resourceGroupName string, cacheName string) (result StorageTargetsResultIterator, err error) {
424	if tracing.IsEnabled() {
425		ctx = tracing.StartSpan(ctx, fqdn+"/StorageTargetsClient.ListByCache")
426		defer func() {
427			sc := -1
428			if result.Response().Response.Response != nil {
429				sc = result.page.Response().Response.Response.StatusCode
430			}
431			tracing.EndSpan(ctx, sc, err)
432		}()
433	}
434	result.page, err = client.ListByCache(ctx, resourceGroupName, cacheName)
435	return
436}
437