1package resources
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	"github.com/Azure/go-autorest/autorest"
22	"github.com/Azure/go-autorest/autorest/azure"
23	"net/http"
24)
25
26// ProvidersClient is the provides operations for working with resources and resource groups.
27type ProvidersClient struct {
28	ManagementClient
29}
30
31// NewProvidersClient creates an instance of the ProvidersClient client.
32func NewProvidersClient(subscriptionID string) ProvidersClient {
33	return NewProvidersClientWithBaseURI(DefaultBaseURI, subscriptionID)
34}
35
36// NewProvidersClientWithBaseURI creates an instance of the ProvidersClient client.
37func NewProvidersClientWithBaseURI(baseURI string, subscriptionID string) ProvidersClient {
38	return ProvidersClient{NewWithBaseURI(baseURI, subscriptionID)}
39}
40
41// Get gets the specified resource provider.
42//
43// resourceProviderNamespace is the namespace of the resource provider. expand is the $expand query parameter. For
44// example, to include property aliases in response, use $expand=resourceTypes/aliases.
45func (client ProvidersClient) Get(resourceProviderNamespace string, expand string) (result Provider, err error) {
46	req, err := client.GetPreparer(resourceProviderNamespace, expand)
47	if err != nil {
48		err = autorest.NewErrorWithError(err, "resources.ProvidersClient", "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, "resources.ProvidersClient", "Get", resp, "Failure sending request")
56		return
57	}
58
59	result, err = client.GetResponder(resp)
60	if err != nil {
61		err = autorest.NewErrorWithError(err, "resources.ProvidersClient", "Get", resp, "Failure responding to request")
62	}
63
64	return
65}
66
67// GetPreparer prepares the Get request.
68func (client ProvidersClient) GetPreparer(resourceProviderNamespace string, expand string) (*http.Request, error) {
69	pathParameters := map[string]interface{}{
70		"resourceProviderNamespace": autorest.Encode("path", resourceProviderNamespace),
71		"subscriptionId":            autorest.Encode("path", client.SubscriptionID),
72	}
73
74	const APIVersion = "2017-05-10"
75	queryParameters := map[string]interface{}{
76		"api-version": APIVersion,
77	}
78	if len(expand) > 0 {
79		queryParameters["$expand"] = autorest.Encode("query", expand)
80	}
81
82	preparer := autorest.CreatePreparer(
83		autorest.AsGet(),
84		autorest.WithBaseURL(client.BaseURI),
85		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/{resourceProviderNamespace}", pathParameters),
86		autorest.WithQueryParameters(queryParameters))
87	return preparer.Prepare(&http.Request{})
88}
89
90// GetSender sends the Get request. The method will close the
91// http.Response Body if it receives an error.
92func (client ProvidersClient) GetSender(req *http.Request) (*http.Response, error) {
93	return autorest.SendWithSender(client,
94		req,
95		azure.DoRetryWithRegistration(client.Client))
96}
97
98// GetResponder handles the response to the Get request. The method always
99// closes the http.Response Body.
100func (client ProvidersClient) GetResponder(resp *http.Response) (result Provider, err error) {
101	err = autorest.Respond(
102		resp,
103		client.ByInspecting(),
104		azure.WithErrorUnlessStatusCode(http.StatusOK),
105		autorest.ByUnmarshallingJSON(&result),
106		autorest.ByClosing())
107	result.Response = autorest.Response{Response: resp}
108	return
109}
110
111// List gets all resource providers for a subscription.
112//
113// top is the number of results to return. If null is passed returns all deployments. expand is the properties to
114// include in the results. For example, use &$expand=metadata in the query string to retrieve resource provider
115// metadata. To include property aliases in response, use $expand=resourceTypes/aliases.
116func (client ProvidersClient) List(top *int32, expand string) (result ProviderListResult, err error) {
117	req, err := client.ListPreparer(top, expand)
118	if err != nil {
119		err = autorest.NewErrorWithError(err, "resources.ProvidersClient", "List", nil, "Failure preparing request")
120		return
121	}
122
123	resp, err := client.ListSender(req)
124	if err != nil {
125		result.Response = autorest.Response{Response: resp}
126		err = autorest.NewErrorWithError(err, "resources.ProvidersClient", "List", resp, "Failure sending request")
127		return
128	}
129
130	result, err = client.ListResponder(resp)
131	if err != nil {
132		err = autorest.NewErrorWithError(err, "resources.ProvidersClient", "List", resp, "Failure responding to request")
133	}
134
135	return
136}
137
138// ListPreparer prepares the List request.
139func (client ProvidersClient) ListPreparer(top *int32, expand string) (*http.Request, error) {
140	pathParameters := map[string]interface{}{
141		"subscriptionId": autorest.Encode("path", client.SubscriptionID),
142	}
143
144	const APIVersion = "2017-05-10"
145	queryParameters := map[string]interface{}{
146		"api-version": APIVersion,
147	}
148	if top != nil {
149		queryParameters["$top"] = autorest.Encode("query", *top)
150	}
151	if len(expand) > 0 {
152		queryParameters["$expand"] = autorest.Encode("query", expand)
153	}
154
155	preparer := autorest.CreatePreparer(
156		autorest.AsGet(),
157		autorest.WithBaseURL(client.BaseURI),
158		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers", pathParameters),
159		autorest.WithQueryParameters(queryParameters))
160	return preparer.Prepare(&http.Request{})
161}
162
163// ListSender sends the List request. The method will close the
164// http.Response Body if it receives an error.
165func (client ProvidersClient) ListSender(req *http.Request) (*http.Response, error) {
166	return autorest.SendWithSender(client,
167		req,
168		azure.DoRetryWithRegistration(client.Client))
169}
170
171// ListResponder handles the response to the List request. The method always
172// closes the http.Response Body.
173func (client ProvidersClient) ListResponder(resp *http.Response) (result ProviderListResult, err error) {
174	err = autorest.Respond(
175		resp,
176		client.ByInspecting(),
177		azure.WithErrorUnlessStatusCode(http.StatusOK),
178		autorest.ByUnmarshallingJSON(&result),
179		autorest.ByClosing())
180	result.Response = autorest.Response{Response: resp}
181	return
182}
183
184// ListNextResults retrieves the next set of results, if any.
185func (client ProvidersClient) ListNextResults(lastResults ProviderListResult) (result ProviderListResult, err error) {
186	req, err := lastResults.ProviderListResultPreparer()
187	if err != nil {
188		return result, autorest.NewErrorWithError(err, "resources.ProvidersClient", "List", nil, "Failure preparing next results request")
189	}
190	if req == nil {
191		return
192	}
193
194	resp, err := client.ListSender(req)
195	if err != nil {
196		result.Response = autorest.Response{Response: resp}
197		return result, autorest.NewErrorWithError(err, "resources.ProvidersClient", "List", resp, "Failure sending next results request")
198	}
199
200	result, err = client.ListResponder(resp)
201	if err != nil {
202		err = autorest.NewErrorWithError(err, "resources.ProvidersClient", "List", resp, "Failure responding to next results request")
203	}
204
205	return
206}
207
208// ListComplete gets all elements from the list without paging.
209func (client ProvidersClient) ListComplete(top *int32, expand string, cancel <-chan struct{}) (<-chan Provider, <-chan error) {
210	resultChan := make(chan Provider)
211	errChan := make(chan error, 1)
212	go func() {
213		defer func() {
214			close(resultChan)
215			close(errChan)
216		}()
217		list, err := client.List(top, expand)
218		if err != nil {
219			errChan <- err
220			return
221		}
222		if list.Value != nil {
223			for _, item := range *list.Value {
224				select {
225				case <-cancel:
226					return
227				case resultChan <- item:
228					// Intentionally left blank
229				}
230			}
231		}
232		for list.NextLink != nil {
233			list, err = client.ListNextResults(list)
234			if err != nil {
235				errChan <- err
236				return
237			}
238			if list.Value != nil {
239				for _, item := range *list.Value {
240					select {
241					case <-cancel:
242						return
243					case resultChan <- item:
244						// Intentionally left blank
245					}
246				}
247			}
248		}
249	}()
250	return resultChan, errChan
251}
252
253// Register registers a subscription with a resource provider.
254//
255// resourceProviderNamespace is the namespace of the resource provider to register.
256func (client ProvidersClient) Register(resourceProviderNamespace string) (result Provider, err error) {
257	req, err := client.RegisterPreparer(resourceProviderNamespace)
258	if err != nil {
259		err = autorest.NewErrorWithError(err, "resources.ProvidersClient", "Register", nil, "Failure preparing request")
260		return
261	}
262
263	resp, err := client.RegisterSender(req)
264	if err != nil {
265		result.Response = autorest.Response{Response: resp}
266		err = autorest.NewErrorWithError(err, "resources.ProvidersClient", "Register", resp, "Failure sending request")
267		return
268	}
269
270	result, err = client.RegisterResponder(resp)
271	if err != nil {
272		err = autorest.NewErrorWithError(err, "resources.ProvidersClient", "Register", resp, "Failure responding to request")
273	}
274
275	return
276}
277
278// RegisterPreparer prepares the Register request.
279func (client ProvidersClient) RegisterPreparer(resourceProviderNamespace string) (*http.Request, error) {
280	pathParameters := map[string]interface{}{
281		"resourceProviderNamespace": autorest.Encode("path", resourceProviderNamespace),
282		"subscriptionId":            autorest.Encode("path", client.SubscriptionID),
283	}
284
285	const APIVersion = "2017-05-10"
286	queryParameters := map[string]interface{}{
287		"api-version": APIVersion,
288	}
289
290	preparer := autorest.CreatePreparer(
291		autorest.AsPost(),
292		autorest.WithBaseURL(client.BaseURI),
293		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/{resourceProviderNamespace}/register", pathParameters),
294		autorest.WithQueryParameters(queryParameters))
295	return preparer.Prepare(&http.Request{})
296}
297
298// RegisterSender sends the Register request. The method will close the
299// http.Response Body if it receives an error.
300func (client ProvidersClient) RegisterSender(req *http.Request) (*http.Response, error) {
301	return autorest.SendWithSender(client,
302		req,
303		azure.DoRetryWithRegistration(client.Client))
304}
305
306// RegisterResponder handles the response to the Register request. The method always
307// closes the http.Response Body.
308func (client ProvidersClient) RegisterResponder(resp *http.Response) (result Provider, err error) {
309	err = autorest.Respond(
310		resp,
311		client.ByInspecting(),
312		azure.WithErrorUnlessStatusCode(http.StatusOK),
313		autorest.ByUnmarshallingJSON(&result),
314		autorest.ByClosing())
315	result.Response = autorest.Response{Response: resp}
316	return
317}
318
319// Unregister unregisters a subscription from a resource provider.
320//
321// resourceProviderNamespace is the namespace of the resource provider to unregister.
322func (client ProvidersClient) Unregister(resourceProviderNamespace string) (result Provider, err error) {
323	req, err := client.UnregisterPreparer(resourceProviderNamespace)
324	if err != nil {
325		err = autorest.NewErrorWithError(err, "resources.ProvidersClient", "Unregister", nil, "Failure preparing request")
326		return
327	}
328
329	resp, err := client.UnregisterSender(req)
330	if err != nil {
331		result.Response = autorest.Response{Response: resp}
332		err = autorest.NewErrorWithError(err, "resources.ProvidersClient", "Unregister", resp, "Failure sending request")
333		return
334	}
335
336	result, err = client.UnregisterResponder(resp)
337	if err != nil {
338		err = autorest.NewErrorWithError(err, "resources.ProvidersClient", "Unregister", resp, "Failure responding to request")
339	}
340
341	return
342}
343
344// UnregisterPreparer prepares the Unregister request.
345func (client ProvidersClient) UnregisterPreparer(resourceProviderNamespace string) (*http.Request, error) {
346	pathParameters := map[string]interface{}{
347		"resourceProviderNamespace": autorest.Encode("path", resourceProviderNamespace),
348		"subscriptionId":            autorest.Encode("path", client.SubscriptionID),
349	}
350
351	const APIVersion = "2017-05-10"
352	queryParameters := map[string]interface{}{
353		"api-version": APIVersion,
354	}
355
356	preparer := autorest.CreatePreparer(
357		autorest.AsPost(),
358		autorest.WithBaseURL(client.BaseURI),
359		autorest.WithPathParameters("/subscriptions/{subscriptionId}/providers/{resourceProviderNamespace}/unregister", pathParameters),
360		autorest.WithQueryParameters(queryParameters))
361	return preparer.Prepare(&http.Request{})
362}
363
364// UnregisterSender sends the Unregister request. The method will close the
365// http.Response Body if it receives an error.
366func (client ProvidersClient) UnregisterSender(req *http.Request) (*http.Response, error) {
367	return autorest.SendWithSender(client,
368		req,
369		azure.DoRetryWithRegistration(client.Client))
370}
371
372// UnregisterResponder handles the response to the Unregister request. The method always
373// closes the http.Response Body.
374func (client ProvidersClient) UnregisterResponder(resp *http.Response) (result Provider, err error) {
375	err = autorest.Respond(
376		resp,
377		client.ByInspecting(),
378		azure.WithErrorUnlessStatusCode(http.StatusOK),
379		autorest.ByUnmarshallingJSON(&result),
380		autorest.ByClosing())
381	result.Response = autorest.Response{Response: resp}
382	return
383}
384