1package billing
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/tracing"
14	"net/http"
15)
16
17// PropertyClient is the billing client provides access to billing resources for Azure subscriptions.
18type PropertyClient struct {
19	BaseClient
20}
21
22// NewPropertyClient creates an instance of the PropertyClient client.
23func NewPropertyClient(subscriptionID string) PropertyClient {
24	return NewPropertyClientWithBaseURI(DefaultBaseURI, subscriptionID)
25}
26
27// NewPropertyClientWithBaseURI creates an instance of the PropertyClient client using a custom endpoint.  Use this
28// when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
29func NewPropertyClientWithBaseURI(baseURI string, subscriptionID string) PropertyClient {
30	return PropertyClient{NewWithBaseURI(baseURI, subscriptionID)}
31}
32
33// Get get the billing properties for a subscription. This operation is not supported for billing accounts with
34// agreement type Enterprise Agreement.
35func (client PropertyClient) Get(ctx context.Context) (result Property, err error) {
36	if tracing.IsEnabled() {
37		ctx = tracing.StartSpan(ctx, fqdn+"/PropertyClient.Get")
38		defer func() {
39			sc := -1
40			if result.Response.Response != nil {
41				sc = result.Response.Response.StatusCode
42			}
43			tracing.EndSpan(ctx, sc, err)
44		}()
45	}
46	req, err := client.GetPreparer(ctx)
47	if err != nil {
48		err = autorest.NewErrorWithError(err, "billing.PropertyClient", "Get", nil, "Failure preparing request")
49		return
50	}
51
52	resp, err := client.GetSender(req)
53	if err != nil {
54		result.Response = autorest.Response{Response: resp}
55		err = autorest.NewErrorWithError(err, "billing.PropertyClient", "Get", resp, "Failure sending request")
56		return
57	}
58
59	result, err = client.GetResponder(resp)
60	if err != nil {
61		err = autorest.NewErrorWithError(err, "billing.PropertyClient", "Get", resp, "Failure responding to request")
62		return
63	}
64
65	return
66}
67
68// GetPreparer prepares the Get request.
69func (client PropertyClient) GetPreparer(ctx context.Context) (*http.Request, error) {
70	pathParameters := map[string]interface{}{
71		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
72	}
73
74	const APIVersion = "2020-05-01"
75	queryParameters := map[string]interface{}{
76		"api-version": APIVersion,
77	}
78
79	preparer := autorest.CreatePreparer(
80		autorest.AsGet(),
81		autorest.WithBaseURL(client.BaseURI),
82		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Billing/billingProperty/default", pathParameters),
83		autorest.WithQueryParameters(queryParameters))
84	return preparer.Prepare((&http.Request{}).WithContext(ctx))
85}
86
87// GetSender sends the Get request. The method will close the
88// http.Response Body if it receives an error.
89func (client PropertyClient) GetSender(req *http.Request) (*http.Response, error) {
90	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
91}
92
93// GetResponder handles the response to the Get request. The method always
94// closes the http.Response Body.
95func (client PropertyClient) GetResponder(resp *http.Response) (result Property, err error) {
96	err = autorest.Respond(
97		resp,
98		azure.WithErrorUnlessStatusCode(http.StatusOK),
99		autorest.ByUnmarshallingJSON(&result),
100		autorest.ByClosing())
101	result.Response = autorest.Response{Response: resp}
102	return
103}
104
105// Update updates the billing property of a subscription. Currently, cost center can be updated. The operation is
106// supported only for billing accounts with agreement type Microsoft Customer Agreement.
107// Parameters:
108// parameters - request parameters that are provided to the update billing property operation.
109func (client PropertyClient) Update(ctx context.Context, parameters Property) (result Property, err error) {
110	if tracing.IsEnabled() {
111		ctx = tracing.StartSpan(ctx, fqdn+"/PropertyClient.Update")
112		defer func() {
113			sc := -1
114			if result.Response.Response != nil {
115				sc = result.Response.Response.StatusCode
116			}
117			tracing.EndSpan(ctx, sc, err)
118		}()
119	}
120	req, err := client.UpdatePreparer(ctx, parameters)
121	if err != nil {
122		err = autorest.NewErrorWithError(err, "billing.PropertyClient", "Update", nil, "Failure preparing request")
123		return
124	}
125
126	resp, err := client.UpdateSender(req)
127	if err != nil {
128		result.Response = autorest.Response{Response: resp}
129		err = autorest.NewErrorWithError(err, "billing.PropertyClient", "Update", resp, "Failure sending request")
130		return
131	}
132
133	result, err = client.UpdateResponder(resp)
134	if err != nil {
135		err = autorest.NewErrorWithError(err, "billing.PropertyClient", "Update", resp, "Failure responding to request")
136		return
137	}
138
139	return
140}
141
142// UpdatePreparer prepares the Update request.
143func (client PropertyClient) UpdatePreparer(ctx context.Context, parameters Property) (*http.Request, error) {
144	pathParameters := map[string]interface{}{
145		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
146	}
147
148	const APIVersion = "2020-05-01"
149	queryParameters := map[string]interface{}{
150		"api-version": APIVersion,
151	}
152
153	preparer := autorest.CreatePreparer(
154		autorest.AsContentType("application/json; charset=utf-8"),
155		autorest.AsPatch(),
156		autorest.WithBaseURL(client.BaseURI),
157		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/Microsoft.Billing/billingProperty/default", pathParameters),
158		autorest.WithJSON(parameters),
159		autorest.WithQueryParameters(queryParameters))
160	return preparer.Prepare((&http.Request{}).WithContext(ctx))
161}
162
163// UpdateSender sends the Update request. The method will close the
164// http.Response Body if it receives an error.
165func (client PropertyClient) UpdateSender(req *http.Request) (*http.Response, error) {
166	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
167}
168
169// UpdateResponder handles the response to the Update request. The method always
170// closes the http.Response Body.
171func (client PropertyClient) UpdateResponder(resp *http.Response) (result Property, err error) {
172	err = autorest.Respond(
173		resp,
174		azure.WithErrorUnlessStatusCode(http.StatusOK),
175		autorest.ByUnmarshallingJSON(&result),
176		autorest.ByClosing())
177	result.Response = autorest.Response{Response: resp}
178	return
179}
180