1package machinelearningservices
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// UsagesClient is the these APIs allow end users to operate on Azure Machine Learning Workspace resources.
30type UsagesClient struct {
31	BaseClient
32}
33
34// NewUsagesClient creates an instance of the UsagesClient client.
35func NewUsagesClient(subscriptionID string) UsagesClient {
36	return NewUsagesClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewUsagesClientWithBaseURI creates an instance of the UsagesClient client using a custom endpoint.  Use this when
40// interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
41func NewUsagesClientWithBaseURI(baseURI string, subscriptionID string) UsagesClient {
42	return UsagesClient{NewWithBaseURI(baseURI, subscriptionID)}
43}
44
45// List gets the current usage information as well as limits for AML resources for given subscription and location.
46// Parameters:
47// location - the location for which resource usage is queried.
48func (client UsagesClient) List(ctx context.Context, location string) (result ListUsagesResultPage, err error) {
49	if tracing.IsEnabled() {
50		ctx = tracing.StartSpan(ctx, fqdn+"/UsagesClient.List")
51		defer func() {
52			sc := -1
53			if result.lur.Response.Response != nil {
54				sc = result.lur.Response.Response.StatusCode
55			}
56			tracing.EndSpan(ctx, sc, err)
57		}()
58	}
59	if err := validation.Validate([]validation.Validation{
60		{TargetValue: location,
61			Constraints: []validation.Constraint{{Target: "location", Name: validation.Pattern, Rule: `^[-\w\._]+$`, Chain: nil}}}}); err != nil {
62		return result, validation.NewError("machinelearningservices.UsagesClient", "List", err.Error())
63	}
64
65	result.fn = client.listNextResults
66	req, err := client.ListPreparer(ctx, location)
67	if err != nil {
68		err = autorest.NewErrorWithError(err, "machinelearningservices.UsagesClient", "List", nil, "Failure preparing request")
69		return
70	}
71
72	resp, err := client.ListSender(req)
73	if err != nil {
74		result.lur.Response = autorest.Response{Response: resp}
75		err = autorest.NewErrorWithError(err, "machinelearningservices.UsagesClient", "List", resp, "Failure sending request")
76		return
77	}
78
79	result.lur, err = client.ListResponder(resp)
80	if err != nil {
81		err = autorest.NewErrorWithError(err, "machinelearningservices.UsagesClient", "List", resp, "Failure responding to request")
82		return
83	}
84	if result.lur.hasNextLink() && result.lur.IsEmpty() {
85		err = result.NextWithContext(ctx)
86		return
87	}
88
89	return
90}
91
92// ListPreparer prepares the List request.
93func (client UsagesClient) ListPreparer(ctx context.Context, location string) (*http.Request, error) {
94	pathParameters := map[string]interface{}{
95		"location":       autorest.Encode("path", location),
96		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
97	}
98
99	const APIVersion = "2018-11-19"
100	queryParameters := map[string]interface{}{
101		"api-version": APIVersion,
102	}
103
104	preparer := autorest.CreatePreparer(
105		autorest.AsGet(),
106		autorest.WithBaseURL(client.BaseURI),
107		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.MachineLearningServices/locations/{location}/usages", pathParameters),
108		autorest.WithQueryParameters(queryParameters))
109	return preparer.Prepare((&http.Request{}).WithContext(ctx))
110}
111
112// ListSender sends the List request. The method will close the
113// http.Response Body if it receives an error.
114func (client UsagesClient) ListSender(req *http.Request) (*http.Response, error) {
115	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
116}
117
118// ListResponder handles the response to the List request. The method always
119// closes the http.Response Body.
120func (client UsagesClient) ListResponder(resp *http.Response) (result ListUsagesResult, err error) {
121	err = autorest.Respond(
122		resp,
123		azure.WithErrorUnlessStatusCode(http.StatusOK),
124		autorest.ByUnmarshallingJSON(&result),
125		autorest.ByClosing())
126	result.Response = autorest.Response{Response: resp}
127	return
128}
129
130// listNextResults retrieves the next set of results, if any.
131func (client UsagesClient) listNextResults(ctx context.Context, lastResults ListUsagesResult) (result ListUsagesResult, err error) {
132	req, err := lastResults.listUsagesResultPreparer(ctx)
133	if err != nil {
134		return result, autorest.NewErrorWithError(err, "machinelearningservices.UsagesClient", "listNextResults", nil, "Failure preparing next results request")
135	}
136	if req == nil {
137		return
138	}
139	resp, err := client.ListSender(req)
140	if err != nil {
141		result.Response = autorest.Response{Response: resp}
142		return result, autorest.NewErrorWithError(err, "machinelearningservices.UsagesClient", "listNextResults", resp, "Failure sending next results request")
143	}
144	result, err = client.ListResponder(resp)
145	if err != nil {
146		err = autorest.NewErrorWithError(err, "machinelearningservices.UsagesClient", "listNextResults", resp, "Failure responding to next results request")
147	}
148	return
149}
150
151// ListComplete enumerates all values, automatically crossing page boundaries as required.
152func (client UsagesClient) ListComplete(ctx context.Context, location string) (result ListUsagesResultIterator, err error) {
153	if tracing.IsEnabled() {
154		ctx = tracing.StartSpan(ctx, fqdn+"/UsagesClient.List")
155		defer func() {
156			sc := -1
157			if result.Response().Response.Response != nil {
158				sc = result.page.Response().Response.Response.StatusCode
159			}
160			tracing.EndSpan(ctx, sc, err)
161		}()
162	}
163	result.page, err = client.List(ctx, location)
164	return
165}
166