1package network 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 network Client 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. 40func NewUsagesClientWithBaseURI(baseURI string, subscriptionID string) UsagesClient { 41 return UsagesClient{NewWithBaseURI(baseURI, subscriptionID)} 42} 43 44// List list network usages for a subscription. 45// Parameters: 46// location - the location where resource usage is queried. 47func (client UsagesClient) List(ctx context.Context, location string) (result UsagesListResultPage, err error) { 48 if tracing.IsEnabled() { 49 ctx = tracing.StartSpan(ctx, fqdn+"/UsagesClient.List") 50 defer func() { 51 sc := -1 52 if result.ulr.Response.Response != nil { 53 sc = result.ulr.Response.Response.StatusCode 54 } 55 tracing.EndSpan(ctx, sc, err) 56 }() 57 } 58 if err := validation.Validate([]validation.Validation{ 59 {TargetValue: location, 60 Constraints: []validation.Constraint{{Target: "location", Name: validation.Pattern, Rule: `^[-\w\._ ]+$`, Chain: nil}}}}); err != nil { 61 return result, validation.NewError("network.UsagesClient", "List", err.Error()) 62 } 63 64 result.fn = client.listNextResults 65 req, err := client.ListPreparer(ctx, location) 66 if err != nil { 67 err = autorest.NewErrorWithError(err, "network.UsagesClient", "List", nil, "Failure preparing request") 68 return 69 } 70 71 resp, err := client.ListSender(req) 72 if err != nil { 73 result.ulr.Response = autorest.Response{Response: resp} 74 err = autorest.NewErrorWithError(err, "network.UsagesClient", "List", resp, "Failure sending request") 75 return 76 } 77 78 result.ulr, err = client.ListResponder(resp) 79 if err != nil { 80 err = autorest.NewErrorWithError(err, "network.UsagesClient", "List", resp, "Failure responding to request") 81 } 82 83 return 84} 85 86// ListPreparer prepares the List request. 87func (client UsagesClient) ListPreparer(ctx context.Context, location string) (*http.Request, error) { 88 pathParameters := map[string]interface{}{ 89 "location": autorest.Encode("path", location), 90 "subscriptionId": autorest.Encode("path", client.SubscriptionID), 91 } 92 93 const APIVersion = "2019-07-01" 94 queryParameters := map[string]interface{}{ 95 "api-version": APIVersion, 96 } 97 98 preparer := autorest.CreatePreparer( 99 autorest.AsGet(), 100 autorest.WithBaseURL(client.BaseURI), 101 autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Network/locations/{location}/usages", pathParameters), 102 autorest.WithQueryParameters(queryParameters)) 103 return preparer.Prepare((&http.Request{}).WithContext(ctx)) 104} 105 106// ListSender sends the List request. The method will close the 107// http.Response Body if it receives an error. 108func (client UsagesClient) ListSender(req *http.Request) (*http.Response, error) { 109 sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client)) 110 return autorest.SendWithSender(client, req, sd...) 111} 112 113// ListResponder handles the response to the List request. The method always 114// closes the http.Response Body. 115func (client UsagesClient) ListResponder(resp *http.Response) (result UsagesListResult, err error) { 116 err = autorest.Respond( 117 resp, 118 client.ByInspecting(), 119 azure.WithErrorUnlessStatusCode(http.StatusOK), 120 autorest.ByUnmarshallingJSON(&result), 121 autorest.ByClosing()) 122 result.Response = autorest.Response{Response: resp} 123 return 124} 125 126// listNextResults retrieves the next set of results, if any. 127func (client UsagesClient) listNextResults(ctx context.Context, lastResults UsagesListResult) (result UsagesListResult, err error) { 128 req, err := lastResults.usagesListResultPreparer(ctx) 129 if err != nil { 130 return result, autorest.NewErrorWithError(err, "network.UsagesClient", "listNextResults", nil, "Failure preparing next results request") 131 } 132 if req == nil { 133 return 134 } 135 resp, err := client.ListSender(req) 136 if err != nil { 137 result.Response = autorest.Response{Response: resp} 138 return result, autorest.NewErrorWithError(err, "network.UsagesClient", "listNextResults", resp, "Failure sending next results request") 139 } 140 result, err = client.ListResponder(resp) 141 if err != nil { 142 err = autorest.NewErrorWithError(err, "network.UsagesClient", "listNextResults", resp, "Failure responding to next results request") 143 } 144 return 145} 146 147// ListComplete enumerates all values, automatically crossing page boundaries as required. 148func (client UsagesClient) ListComplete(ctx context.Context, location string) (result UsagesListResultIterator, err error) { 149 if tracing.IsEnabled() { 150 ctx = tracing.StartSpan(ctx, fqdn+"/UsagesClient.List") 151 defer func() { 152 sc := -1 153 if result.Response().Response.Response != nil { 154 sc = result.page.Response().Response.Response.StatusCode 155 } 156 tracing.EndSpan(ctx, sc, err) 157 }() 158 } 159 result.page, err = client.List(ctx, location) 160 return 161} 162