1package storsimple
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// BandwidthSettingsClient is the client for the BandwidthSettings methods of the Storsimple service.
30type BandwidthSettingsClient struct {
31	BaseClient
32}
33
34// NewBandwidthSettingsClient creates an instance of the BandwidthSettingsClient client.
35func NewBandwidthSettingsClient(subscriptionID string) BandwidthSettingsClient {
36	return NewBandwidthSettingsClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewBandwidthSettingsClientWithBaseURI creates an instance of the BandwidthSettingsClient client using a custom
40// endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure
41// stack).
42func NewBandwidthSettingsClientWithBaseURI(baseURI string, subscriptionID string) BandwidthSettingsClient {
43	return BandwidthSettingsClient{NewWithBaseURI(baseURI, subscriptionID)}
44}
45
46// CreateOrUpdate creates or updates the bandwidth setting
47// Parameters:
48// bandwidthSettingName - the bandwidth setting name.
49// parameters - the bandwidth setting to be added or updated.
50// resourceGroupName - the resource group name
51// managerName - the manager name
52func (client BandwidthSettingsClient) CreateOrUpdate(ctx context.Context, bandwidthSettingName string, parameters BandwidthSetting, resourceGroupName string, managerName string) (result BandwidthSettingsCreateOrUpdateFuture, err error) {
53	if tracing.IsEnabled() {
54		ctx = tracing.StartSpan(ctx, fqdn+"/BandwidthSettingsClient.CreateOrUpdate")
55		defer func() {
56			sc := -1
57			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
58				sc = result.FutureAPI.Response().StatusCode
59			}
60			tracing.EndSpan(ctx, sc, err)
61		}()
62	}
63	if err := validation.Validate([]validation.Validation{
64		{TargetValue: parameters,
65			Constraints: []validation.Constraint{{Target: "parameters.BandwidthRateSettingProperties", Name: validation.Null, Rule: true,
66				Chain: []validation.Constraint{{Target: "parameters.BandwidthRateSettingProperties.Schedules", Name: validation.Null, Rule: true, Chain: nil}}}}},
67		{TargetValue: managerName,
68			Constraints: []validation.Constraint{{Target: "managerName", Name: validation.MaxLength, Rule: 50, Chain: nil},
69				{Target: "managerName", Name: validation.MinLength, Rule: 2, Chain: nil}}}}); err != nil {
70		return result, validation.NewError("storsimple.BandwidthSettingsClient", "CreateOrUpdate", err.Error())
71	}
72
73	req, err := client.CreateOrUpdatePreparer(ctx, bandwidthSettingName, parameters, resourceGroupName, managerName)
74	if err != nil {
75		err = autorest.NewErrorWithError(err, "storsimple.BandwidthSettingsClient", "CreateOrUpdate", nil, "Failure preparing request")
76		return
77	}
78
79	result, err = client.CreateOrUpdateSender(req)
80	if err != nil {
81		err = autorest.NewErrorWithError(err, "storsimple.BandwidthSettingsClient", "CreateOrUpdate", nil, "Failure sending request")
82		return
83	}
84
85	return
86}
87
88// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
89func (client BandwidthSettingsClient) CreateOrUpdatePreparer(ctx context.Context, bandwidthSettingName string, parameters BandwidthSetting, resourceGroupName string, managerName string) (*http.Request, error) {
90	pathParameters := map[string]interface{}{
91		"bandwidthSettingName": bandwidthSettingName,
92		"managerName":          managerName,
93		"resourceGroupName":    resourceGroupName,
94		"subscriptionId":       client.SubscriptionID,
95	}
96
97	const APIVersion = "2017-06-01"
98	queryParameters := map[string]interface{}{
99		"api-version": APIVersion,
100	}
101
102	preparer := autorest.CreatePreparer(
103		autorest.AsContentType("application/json; charset=utf-8"),
104		autorest.AsPut(),
105		autorest.WithBaseURL(client.BaseURI),
106		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.StorSimple/managers/{managerName}/bandwidthSettings/{bandwidthSettingName}", pathParameters),
107		autorest.WithJSON(parameters),
108		autorest.WithQueryParameters(queryParameters))
109	return preparer.Prepare((&http.Request{}).WithContext(ctx))
110}
111
112// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
113// http.Response Body if it receives an error.
114func (client BandwidthSettingsClient) CreateOrUpdateSender(req *http.Request) (future BandwidthSettingsCreateOrUpdateFuture, err error) {
115	var resp *http.Response
116	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
117	if err != nil {
118		return
119	}
120	var azf azure.Future
121	azf, err = azure.NewFutureFromResponse(resp)
122	future.FutureAPI = &azf
123	future.Result = func(client BandwidthSettingsClient) (bs BandwidthSetting, err error) {
124		var done bool
125		done, err = future.DoneWithContext(context.Background(), client)
126		if err != nil {
127			err = autorest.NewErrorWithError(err, "storsimple.BandwidthSettingsCreateOrUpdateFuture", "Result", future.Response(), "Polling failure")
128			return
129		}
130		if !done {
131			err = azure.NewAsyncOpIncompleteError("storsimple.BandwidthSettingsCreateOrUpdateFuture")
132			return
133		}
134		sender := autorest.DecorateSender(client, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
135		bs.Response.Response, err = future.GetResult(sender)
136		if bs.Response.Response == nil && err == nil {
137			err = autorest.NewErrorWithError(err, "storsimple.BandwidthSettingsCreateOrUpdateFuture", "Result", nil, "received nil response and error")
138		}
139		if err == nil && bs.Response.Response.StatusCode != http.StatusNoContent {
140			bs, err = client.CreateOrUpdateResponder(bs.Response.Response)
141			if err != nil {
142				err = autorest.NewErrorWithError(err, "storsimple.BandwidthSettingsCreateOrUpdateFuture", "Result", bs.Response.Response, "Failure responding to request")
143			}
144		}
145		return
146	}
147	return
148}
149
150// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
151// closes the http.Response Body.
152func (client BandwidthSettingsClient) CreateOrUpdateResponder(resp *http.Response) (result BandwidthSetting, err error) {
153	err = autorest.Respond(
154		resp,
155		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted),
156		autorest.ByUnmarshallingJSON(&result),
157		autorest.ByClosing())
158	result.Response = autorest.Response{Response: resp}
159	return
160}
161
162// Delete deletes the bandwidth setting
163// Parameters:
164// bandwidthSettingName - the name of the bandwidth setting.
165// resourceGroupName - the resource group name
166// managerName - the manager name
167func (client BandwidthSettingsClient) Delete(ctx context.Context, bandwidthSettingName string, resourceGroupName string, managerName string) (result BandwidthSettingsDeleteFuture, err error) {
168	if tracing.IsEnabled() {
169		ctx = tracing.StartSpan(ctx, fqdn+"/BandwidthSettingsClient.Delete")
170		defer func() {
171			sc := -1
172			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
173				sc = result.FutureAPI.Response().StatusCode
174			}
175			tracing.EndSpan(ctx, sc, err)
176		}()
177	}
178	if err := validation.Validate([]validation.Validation{
179		{TargetValue: managerName,
180			Constraints: []validation.Constraint{{Target: "managerName", Name: validation.MaxLength, Rule: 50, Chain: nil},
181				{Target: "managerName", Name: validation.MinLength, Rule: 2, Chain: nil}}}}); err != nil {
182		return result, validation.NewError("storsimple.BandwidthSettingsClient", "Delete", err.Error())
183	}
184
185	req, err := client.DeletePreparer(ctx, bandwidthSettingName, resourceGroupName, managerName)
186	if err != nil {
187		err = autorest.NewErrorWithError(err, "storsimple.BandwidthSettingsClient", "Delete", nil, "Failure preparing request")
188		return
189	}
190
191	result, err = client.DeleteSender(req)
192	if err != nil {
193		err = autorest.NewErrorWithError(err, "storsimple.BandwidthSettingsClient", "Delete", nil, "Failure sending request")
194		return
195	}
196
197	return
198}
199
200// DeletePreparer prepares the Delete request.
201func (client BandwidthSettingsClient) DeletePreparer(ctx context.Context, bandwidthSettingName string, resourceGroupName string, managerName string) (*http.Request, error) {
202	pathParameters := map[string]interface{}{
203		"bandwidthSettingName": bandwidthSettingName,
204		"managerName":          managerName,
205		"resourceGroupName":    resourceGroupName,
206		"subscriptionId":       client.SubscriptionID,
207	}
208
209	const APIVersion = "2017-06-01"
210	queryParameters := map[string]interface{}{
211		"api-version": APIVersion,
212	}
213
214	preparer := autorest.CreatePreparer(
215		autorest.AsDelete(),
216		autorest.WithBaseURL(client.BaseURI),
217		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.StorSimple/managers/{managerName}/bandwidthSettings/{bandwidthSettingName}", pathParameters),
218		autorest.WithQueryParameters(queryParameters))
219	return preparer.Prepare((&http.Request{}).WithContext(ctx))
220}
221
222// DeleteSender sends the Delete request. The method will close the
223// http.Response Body if it receives an error.
224func (client BandwidthSettingsClient) DeleteSender(req *http.Request) (future BandwidthSettingsDeleteFuture, err error) {
225	var resp *http.Response
226	resp, err = client.Send(req, azure.DoRetryWithRegistration(client.Client))
227	if err != nil {
228		return
229	}
230	var azf azure.Future
231	azf, err = azure.NewFutureFromResponse(resp)
232	future.FutureAPI = &azf
233	future.Result = func(client BandwidthSettingsClient) (ar autorest.Response, err error) {
234		var done bool
235		done, err = future.DoneWithContext(context.Background(), client)
236		if err != nil {
237			err = autorest.NewErrorWithError(err, "storsimple.BandwidthSettingsDeleteFuture", "Result", future.Response(), "Polling failure")
238			return
239		}
240		if !done {
241			err = azure.NewAsyncOpIncompleteError("storsimple.BandwidthSettingsDeleteFuture")
242			return
243		}
244		ar.Response = future.Response()
245		return
246	}
247	return
248}
249
250// DeleteResponder handles the response to the Delete request. The method always
251// closes the http.Response Body.
252func (client BandwidthSettingsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
253	err = autorest.Respond(
254		resp,
255		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNoContent),
256		autorest.ByClosing())
257	result.Response = resp
258	return
259}
260
261// Get returns the properties of the specified bandwidth setting name.
262// Parameters:
263// bandwidthSettingName - the name of bandwidth setting to be fetched.
264// resourceGroupName - the resource group name
265// managerName - the manager name
266func (client BandwidthSettingsClient) Get(ctx context.Context, bandwidthSettingName string, resourceGroupName string, managerName string) (result BandwidthSetting, err error) {
267	if tracing.IsEnabled() {
268		ctx = tracing.StartSpan(ctx, fqdn+"/BandwidthSettingsClient.Get")
269		defer func() {
270			sc := -1
271			if result.Response.Response != nil {
272				sc = result.Response.Response.StatusCode
273			}
274			tracing.EndSpan(ctx, sc, err)
275		}()
276	}
277	if err := validation.Validate([]validation.Validation{
278		{TargetValue: managerName,
279			Constraints: []validation.Constraint{{Target: "managerName", Name: validation.MaxLength, Rule: 50, Chain: nil},
280				{Target: "managerName", Name: validation.MinLength, Rule: 2, Chain: nil}}}}); err != nil {
281		return result, validation.NewError("storsimple.BandwidthSettingsClient", "Get", err.Error())
282	}
283
284	req, err := client.GetPreparer(ctx, bandwidthSettingName, resourceGroupName, managerName)
285	if err != nil {
286		err = autorest.NewErrorWithError(err, "storsimple.BandwidthSettingsClient", "Get", nil, "Failure preparing request")
287		return
288	}
289
290	resp, err := client.GetSender(req)
291	if err != nil {
292		result.Response = autorest.Response{Response: resp}
293		err = autorest.NewErrorWithError(err, "storsimple.BandwidthSettingsClient", "Get", resp, "Failure sending request")
294		return
295	}
296
297	result, err = client.GetResponder(resp)
298	if err != nil {
299		err = autorest.NewErrorWithError(err, "storsimple.BandwidthSettingsClient", "Get", resp, "Failure responding to request")
300		return
301	}
302
303	return
304}
305
306// GetPreparer prepares the Get request.
307func (client BandwidthSettingsClient) GetPreparer(ctx context.Context, bandwidthSettingName string, resourceGroupName string, managerName string) (*http.Request, error) {
308	pathParameters := map[string]interface{}{
309		"bandwidthSettingName": bandwidthSettingName,
310		"managerName":          managerName,
311		"resourceGroupName":    resourceGroupName,
312		"subscriptionId":       client.SubscriptionID,
313	}
314
315	const APIVersion = "2017-06-01"
316	queryParameters := map[string]interface{}{
317		"api-version": APIVersion,
318	}
319
320	preparer := autorest.CreatePreparer(
321		autorest.AsGet(),
322		autorest.WithBaseURL(client.BaseURI),
323		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.StorSimple/managers/{managerName}/bandwidthSettings/{bandwidthSettingName}", pathParameters),
324		autorest.WithQueryParameters(queryParameters))
325	return preparer.Prepare((&http.Request{}).WithContext(ctx))
326}
327
328// GetSender sends the Get request. The method will close the
329// http.Response Body if it receives an error.
330func (client BandwidthSettingsClient) GetSender(req *http.Request) (*http.Response, error) {
331	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
332}
333
334// GetResponder handles the response to the Get request. The method always
335// closes the http.Response Body.
336func (client BandwidthSettingsClient) GetResponder(resp *http.Response) (result BandwidthSetting, err error) {
337	err = autorest.Respond(
338		resp,
339		azure.WithErrorUnlessStatusCode(http.StatusOK),
340		autorest.ByUnmarshallingJSON(&result),
341		autorest.ByClosing())
342	result.Response = autorest.Response{Response: resp}
343	return
344}
345
346// ListByManager retrieves all the bandwidth setting in a manager.
347// Parameters:
348// resourceGroupName - the resource group name
349// managerName - the manager name
350func (client BandwidthSettingsClient) ListByManager(ctx context.Context, resourceGroupName string, managerName string) (result BandwidthSettingList, err error) {
351	if tracing.IsEnabled() {
352		ctx = tracing.StartSpan(ctx, fqdn+"/BandwidthSettingsClient.ListByManager")
353		defer func() {
354			sc := -1
355			if result.Response.Response != nil {
356				sc = result.Response.Response.StatusCode
357			}
358			tracing.EndSpan(ctx, sc, err)
359		}()
360	}
361	if err := validation.Validate([]validation.Validation{
362		{TargetValue: managerName,
363			Constraints: []validation.Constraint{{Target: "managerName", Name: validation.MaxLength, Rule: 50, Chain: nil},
364				{Target: "managerName", Name: validation.MinLength, Rule: 2, Chain: nil}}}}); err != nil {
365		return result, validation.NewError("storsimple.BandwidthSettingsClient", "ListByManager", err.Error())
366	}
367
368	req, err := client.ListByManagerPreparer(ctx, resourceGroupName, managerName)
369	if err != nil {
370		err = autorest.NewErrorWithError(err, "storsimple.BandwidthSettingsClient", "ListByManager", nil, "Failure preparing request")
371		return
372	}
373
374	resp, err := client.ListByManagerSender(req)
375	if err != nil {
376		result.Response = autorest.Response{Response: resp}
377		err = autorest.NewErrorWithError(err, "storsimple.BandwidthSettingsClient", "ListByManager", resp, "Failure sending request")
378		return
379	}
380
381	result, err = client.ListByManagerResponder(resp)
382	if err != nil {
383		err = autorest.NewErrorWithError(err, "storsimple.BandwidthSettingsClient", "ListByManager", resp, "Failure responding to request")
384		return
385	}
386
387	return
388}
389
390// ListByManagerPreparer prepares the ListByManager request.
391func (client BandwidthSettingsClient) ListByManagerPreparer(ctx context.Context, resourceGroupName string, managerName string) (*http.Request, error) {
392	pathParameters := map[string]interface{}{
393		"managerName":       managerName,
394		"resourceGroupName": resourceGroupName,
395		"subscriptionId":    client.SubscriptionID,
396	}
397
398	const APIVersion = "2017-06-01"
399	queryParameters := map[string]interface{}{
400		"api-version": APIVersion,
401	}
402
403	preparer := autorest.CreatePreparer(
404		autorest.AsGet(),
405		autorest.WithBaseURL(client.BaseURI),
406		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.StorSimple/managers/{managerName}/bandwidthSettings", pathParameters),
407		autorest.WithQueryParameters(queryParameters))
408	return preparer.Prepare((&http.Request{}).WithContext(ctx))
409}
410
411// ListByManagerSender sends the ListByManager request. The method will close the
412// http.Response Body if it receives an error.
413func (client BandwidthSettingsClient) ListByManagerSender(req *http.Request) (*http.Response, error) {
414	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
415}
416
417// ListByManagerResponder handles the response to the ListByManager request. The method always
418// closes the http.Response Body.
419func (client BandwidthSettingsClient) ListByManagerResponder(resp *http.Response) (result BandwidthSettingList, err error) {
420	err = autorest.Respond(
421		resp,
422		azure.WithErrorUnlessStatusCode(http.StatusOK),
423		autorest.ByUnmarshallingJSON(&result),
424		autorest.ByClosing())
425	result.Response = autorest.Response{Response: resp}
426	return
427}
428