1package managedservices
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/autorest/validation"
14	"github.com/Azure/go-autorest/tracing"
15	"net/http"
16)
17
18// RegistrationDefinitionsClient is the specification for ManagedServices.
19type RegistrationDefinitionsClient struct {
20	BaseClient
21}
22
23// NewRegistrationDefinitionsClient creates an instance of the RegistrationDefinitionsClient client.
24func NewRegistrationDefinitionsClient() RegistrationDefinitionsClient {
25	return NewRegistrationDefinitionsClientWithBaseURI(DefaultBaseURI)
26}
27
28// NewRegistrationDefinitionsClientWithBaseURI creates an instance of the RegistrationDefinitionsClient client using a
29// custom endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds,
30// Azure stack).
31func NewRegistrationDefinitionsClientWithBaseURI(baseURI string) RegistrationDefinitionsClient {
32	return RegistrationDefinitionsClient{NewWithBaseURI(baseURI)}
33}
34
35// CreateOrUpdate creates or updates a registration definition.
36// Parameters:
37// registrationDefinitionID - guid of the registration definition.
38// scope - scope of the resource.
39// requestBody - the parameters required to create new registration definition.
40func (client RegistrationDefinitionsClient) CreateOrUpdate(ctx context.Context, registrationDefinitionID string, scope string, requestBody RegistrationDefinition) (result RegistrationDefinitionsCreateOrUpdateFuture, err error) {
41	if tracing.IsEnabled() {
42		ctx = tracing.StartSpan(ctx, fqdn+"/RegistrationDefinitionsClient.CreateOrUpdate")
43		defer func() {
44			sc := -1
45			if result.FutureAPI != nil && result.FutureAPI.Response() != nil {
46				sc = result.FutureAPI.Response().StatusCode
47			}
48			tracing.EndSpan(ctx, sc, err)
49		}()
50	}
51	if err := validation.Validate([]validation.Validation{
52		{TargetValue: requestBody,
53			Constraints: []validation.Constraint{{Target: "requestBody.Properties", Name: validation.Null, Rule: false,
54				Chain: []validation.Constraint{{Target: "requestBody.Properties.Authorizations", Name: validation.Null, Rule: true, Chain: nil},
55					{Target: "requestBody.Properties.ManagedByTenantID", Name: validation.Null, Rule: true, Chain: nil},
56				}},
57				{Target: "requestBody.Plan", Name: validation.Null, Rule: false,
58					Chain: []validation.Constraint{{Target: "requestBody.Plan.Name", Name: validation.Null, Rule: true, Chain: nil},
59						{Target: "requestBody.Plan.Publisher", Name: validation.Null, Rule: true, Chain: nil},
60						{Target: "requestBody.Plan.Product", Name: validation.Null, Rule: true, Chain: nil},
61						{Target: "requestBody.Plan.Version", Name: validation.Null, Rule: true, Chain: nil},
62					}}}}}); err != nil {
63		return result, validation.NewError("managedservices.RegistrationDefinitionsClient", "CreateOrUpdate", err.Error())
64	}
65
66	req, err := client.CreateOrUpdatePreparer(ctx, registrationDefinitionID, scope, requestBody)
67	if err != nil {
68		err = autorest.NewErrorWithError(err, "managedservices.RegistrationDefinitionsClient", "CreateOrUpdate", nil, "Failure preparing request")
69		return
70	}
71
72	result, err = client.CreateOrUpdateSender(req)
73	if err != nil {
74		err = autorest.NewErrorWithError(err, "managedservices.RegistrationDefinitionsClient", "CreateOrUpdate", nil, "Failure sending request")
75		return
76	}
77
78	return
79}
80
81// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
82func (client RegistrationDefinitionsClient) CreateOrUpdatePreparer(ctx context.Context, registrationDefinitionID string, scope string, requestBody RegistrationDefinition) (*http.Request, error) {
83	pathParameters := map[string]interface{}{
84		"registrationDefinitionId": autorest.Encode("path", registrationDefinitionID),
85		"scope":                    scope,
86	}
87
88	const APIVersion = "2019-06-01"
89	queryParameters := map[string]interface{}{
90		"api-version": APIVersion,
91	}
92
93	requestBody.ID = nil
94	requestBody.Type = nil
95	requestBody.Name = nil
96	preparer := autorest.CreatePreparer(
97		autorest.AsContentType("application/json; charset=utf-8"),
98		autorest.AsPut(),
99		autorest.WithBaseURL(client.BaseURI),
100		autorest.WithPathParameters("/{scope}/providers/Microsoft.ManagedServices/registrationDefinitions/{registrationDefinitionId}", pathParameters),
101		autorest.WithJSON(requestBody),
102		autorest.WithQueryParameters(queryParameters))
103	return preparer.Prepare((&http.Request{}).WithContext(ctx))
104}
105
106// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
107// http.Response Body if it receives an error.
108func (client RegistrationDefinitionsClient) CreateOrUpdateSender(req *http.Request) (future RegistrationDefinitionsCreateOrUpdateFuture, err error) {
109	var resp *http.Response
110	resp, err = client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
111	if err != nil {
112		return
113	}
114	var azf azure.Future
115	azf, err = azure.NewFutureFromResponse(resp)
116	future.FutureAPI = &azf
117	future.Result = future.result
118	return
119}
120
121// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
122// closes the http.Response Body.
123func (client RegistrationDefinitionsClient) CreateOrUpdateResponder(resp *http.Response) (result RegistrationDefinition, err error) {
124	err = autorest.Respond(
125		resp,
126		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
127		autorest.ByUnmarshallingJSON(&result),
128		autorest.ByClosing())
129	result.Response = autorest.Response{Response: resp}
130	return
131}
132
133// Delete deletes the registration definition.
134// Parameters:
135// registrationDefinitionID - guid of the registration definition.
136// scope - scope of the resource.
137func (client RegistrationDefinitionsClient) Delete(ctx context.Context, registrationDefinitionID string, scope string) (result autorest.Response, err error) {
138	if tracing.IsEnabled() {
139		ctx = tracing.StartSpan(ctx, fqdn+"/RegistrationDefinitionsClient.Delete")
140		defer func() {
141			sc := -1
142			if result.Response != nil {
143				sc = result.Response.StatusCode
144			}
145			tracing.EndSpan(ctx, sc, err)
146		}()
147	}
148	req, err := client.DeletePreparer(ctx, registrationDefinitionID, scope)
149	if err != nil {
150		err = autorest.NewErrorWithError(err, "managedservices.RegistrationDefinitionsClient", "Delete", nil, "Failure preparing request")
151		return
152	}
153
154	resp, err := client.DeleteSender(req)
155	if err != nil {
156		result.Response = resp
157		err = autorest.NewErrorWithError(err, "managedservices.RegistrationDefinitionsClient", "Delete", resp, "Failure sending request")
158		return
159	}
160
161	result, err = client.DeleteResponder(resp)
162	if err != nil {
163		err = autorest.NewErrorWithError(err, "managedservices.RegistrationDefinitionsClient", "Delete", resp, "Failure responding to request")
164		return
165	}
166
167	return
168}
169
170// DeletePreparer prepares the Delete request.
171func (client RegistrationDefinitionsClient) DeletePreparer(ctx context.Context, registrationDefinitionID string, scope string) (*http.Request, error) {
172	pathParameters := map[string]interface{}{
173		"registrationDefinitionId": autorest.Encode("path", registrationDefinitionID),
174		"scope":                    scope,
175	}
176
177	const APIVersion = "2019-06-01"
178	queryParameters := map[string]interface{}{
179		"api-version": APIVersion,
180	}
181
182	preparer := autorest.CreatePreparer(
183		autorest.AsDelete(),
184		autorest.WithBaseURL(client.BaseURI),
185		autorest.WithPathParameters("/{scope}/providers/Microsoft.ManagedServices/registrationDefinitions/{registrationDefinitionId}", pathParameters),
186		autorest.WithQueryParameters(queryParameters))
187	return preparer.Prepare((&http.Request{}).WithContext(ctx))
188}
189
190// DeleteSender sends the Delete request. The method will close the
191// http.Response Body if it receives an error.
192func (client RegistrationDefinitionsClient) DeleteSender(req *http.Request) (*http.Response, error) {
193	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
194}
195
196// DeleteResponder handles the response to the Delete request. The method always
197// closes the http.Response Body.
198func (client RegistrationDefinitionsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
199	err = autorest.Respond(
200		resp,
201		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
202		autorest.ByClosing())
203	result.Response = resp
204	return
205}
206
207// Get gets the registration definition details.
208// Parameters:
209// scope - scope of the resource.
210// registrationDefinitionID - guid of the registration definition.
211func (client RegistrationDefinitionsClient) Get(ctx context.Context, scope string, registrationDefinitionID string) (result RegistrationDefinition, err error) {
212	if tracing.IsEnabled() {
213		ctx = tracing.StartSpan(ctx, fqdn+"/RegistrationDefinitionsClient.Get")
214		defer func() {
215			sc := -1
216			if result.Response.Response != nil {
217				sc = result.Response.Response.StatusCode
218			}
219			tracing.EndSpan(ctx, sc, err)
220		}()
221	}
222	req, err := client.GetPreparer(ctx, scope, registrationDefinitionID)
223	if err != nil {
224		err = autorest.NewErrorWithError(err, "managedservices.RegistrationDefinitionsClient", "Get", nil, "Failure preparing request")
225		return
226	}
227
228	resp, err := client.GetSender(req)
229	if err != nil {
230		result.Response = autorest.Response{Response: resp}
231		err = autorest.NewErrorWithError(err, "managedservices.RegistrationDefinitionsClient", "Get", resp, "Failure sending request")
232		return
233	}
234
235	result, err = client.GetResponder(resp)
236	if err != nil {
237		err = autorest.NewErrorWithError(err, "managedservices.RegistrationDefinitionsClient", "Get", resp, "Failure responding to request")
238		return
239	}
240
241	return
242}
243
244// GetPreparer prepares the Get request.
245func (client RegistrationDefinitionsClient) GetPreparer(ctx context.Context, scope string, registrationDefinitionID string) (*http.Request, error) {
246	pathParameters := map[string]interface{}{
247		"registrationDefinitionId": autorest.Encode("path", registrationDefinitionID),
248		"scope":                    scope,
249	}
250
251	const APIVersion = "2019-06-01"
252	queryParameters := map[string]interface{}{
253		"api-version": APIVersion,
254	}
255
256	preparer := autorest.CreatePreparer(
257		autorest.AsGet(),
258		autorest.WithBaseURL(client.BaseURI),
259		autorest.WithPathParameters("/{scope}/providers/Microsoft.ManagedServices/registrationDefinitions/{registrationDefinitionId}", pathParameters),
260		autorest.WithQueryParameters(queryParameters))
261	return preparer.Prepare((&http.Request{}).WithContext(ctx))
262}
263
264// GetSender sends the Get request. The method will close the
265// http.Response Body if it receives an error.
266func (client RegistrationDefinitionsClient) GetSender(req *http.Request) (*http.Response, error) {
267	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
268}
269
270// GetResponder handles the response to the Get request. The method always
271// closes the http.Response Body.
272func (client RegistrationDefinitionsClient) GetResponder(resp *http.Response) (result RegistrationDefinition, err error) {
273	err = autorest.Respond(
274		resp,
275		azure.WithErrorUnlessStatusCode(http.StatusOK),
276		autorest.ByUnmarshallingJSON(&result),
277		autorest.ByClosing())
278	result.Response = autorest.Response{Response: resp}
279	return
280}
281
282// List gets a list of the registration definitions.
283// Parameters:
284// scope - scope of the resource.
285func (client RegistrationDefinitionsClient) List(ctx context.Context, scope string) (result RegistrationDefinitionListPage, err error) {
286	if tracing.IsEnabled() {
287		ctx = tracing.StartSpan(ctx, fqdn+"/RegistrationDefinitionsClient.List")
288		defer func() {
289			sc := -1
290			if result.rdl.Response.Response != nil {
291				sc = result.rdl.Response.Response.StatusCode
292			}
293			tracing.EndSpan(ctx, sc, err)
294		}()
295	}
296	result.fn = client.listNextResults
297	req, err := client.ListPreparer(ctx, scope)
298	if err != nil {
299		err = autorest.NewErrorWithError(err, "managedservices.RegistrationDefinitionsClient", "List", nil, "Failure preparing request")
300		return
301	}
302
303	resp, err := client.ListSender(req)
304	if err != nil {
305		result.rdl.Response = autorest.Response{Response: resp}
306		err = autorest.NewErrorWithError(err, "managedservices.RegistrationDefinitionsClient", "List", resp, "Failure sending request")
307		return
308	}
309
310	result.rdl, err = client.ListResponder(resp)
311	if err != nil {
312		err = autorest.NewErrorWithError(err, "managedservices.RegistrationDefinitionsClient", "List", resp, "Failure responding to request")
313		return
314	}
315	if result.rdl.hasNextLink() && result.rdl.IsEmpty() {
316		err = result.NextWithContext(ctx)
317		return
318	}
319
320	return
321}
322
323// ListPreparer prepares the List request.
324func (client RegistrationDefinitionsClient) ListPreparer(ctx context.Context, scope string) (*http.Request, error) {
325	pathParameters := map[string]interface{}{
326		"scope": scope,
327	}
328
329	const APIVersion = "2019-06-01"
330	queryParameters := map[string]interface{}{
331		"api-version": APIVersion,
332	}
333
334	preparer := autorest.CreatePreparer(
335		autorest.AsGet(),
336		autorest.WithBaseURL(client.BaseURI),
337		autorest.WithPathParameters("/{scope}/providers/Microsoft.ManagedServices/registrationDefinitions", pathParameters),
338		autorest.WithQueryParameters(queryParameters))
339	return preparer.Prepare((&http.Request{}).WithContext(ctx))
340}
341
342// ListSender sends the List request. The method will close the
343// http.Response Body if it receives an error.
344func (client RegistrationDefinitionsClient) ListSender(req *http.Request) (*http.Response, error) {
345	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
346}
347
348// ListResponder handles the response to the List request. The method always
349// closes the http.Response Body.
350func (client RegistrationDefinitionsClient) ListResponder(resp *http.Response) (result RegistrationDefinitionList, err error) {
351	err = autorest.Respond(
352		resp,
353		azure.WithErrorUnlessStatusCode(http.StatusOK),
354		autorest.ByUnmarshallingJSON(&result),
355		autorest.ByClosing())
356	result.Response = autorest.Response{Response: resp}
357	return
358}
359
360// listNextResults retrieves the next set of results, if any.
361func (client RegistrationDefinitionsClient) listNextResults(ctx context.Context, lastResults RegistrationDefinitionList) (result RegistrationDefinitionList, err error) {
362	req, err := lastResults.registrationDefinitionListPreparer(ctx)
363	if err != nil {
364		return result, autorest.NewErrorWithError(err, "managedservices.RegistrationDefinitionsClient", "listNextResults", nil, "Failure preparing next results request")
365	}
366	if req == nil {
367		return
368	}
369	resp, err := client.ListSender(req)
370	if err != nil {
371		result.Response = autorest.Response{Response: resp}
372		return result, autorest.NewErrorWithError(err, "managedservices.RegistrationDefinitionsClient", "listNextResults", resp, "Failure sending next results request")
373	}
374	result, err = client.ListResponder(resp)
375	if err != nil {
376		err = autorest.NewErrorWithError(err, "managedservices.RegistrationDefinitionsClient", "listNextResults", resp, "Failure responding to next results request")
377	}
378	return
379}
380
381// ListComplete enumerates all values, automatically crossing page boundaries as required.
382func (client RegistrationDefinitionsClient) ListComplete(ctx context.Context, scope string) (result RegistrationDefinitionListIterator, err error) {
383	if tracing.IsEnabled() {
384		ctx = tracing.StartSpan(ctx, fqdn+"/RegistrationDefinitionsClient.List")
385		defer func() {
386			sc := -1
387			if result.Response().Response.Response != nil {
388				sc = result.page.Response().Response.Response.StatusCode
389			}
390			tracing.EndSpan(ctx, sc, err)
391		}()
392	}
393	result.page, err = client.List(ctx, scope)
394	return
395}
396