1package resourcehealth
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// ChildResourcesClient is the the Resource Health Client.
18type ChildResourcesClient struct {
19	BaseClient
20}
21
22// NewChildResourcesClient creates an instance of the ChildResourcesClient client.
23func NewChildResourcesClient(subscriptionID string) ChildResourcesClient {
24	return NewChildResourcesClientWithBaseURI(DefaultBaseURI, subscriptionID)
25}
26
27// NewChildResourcesClientWithBaseURI creates an instance of the ChildResourcesClient client using a custom endpoint.
28// Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
29func NewChildResourcesClientWithBaseURI(baseURI string, subscriptionID string) ChildResourcesClient {
30	return ChildResourcesClient{NewWithBaseURI(baseURI, subscriptionID)}
31}
32
33// List lists the all the children and its current health status for a parent resource. Use the nextLink property in
34// the response to get the next page of children current health
35// Parameters:
36// resourceURI - the fully qualified ID of the resource, including the resource name and resource type.
37// Currently the API only support not nested parent resource type:
38// /subscriptions/{subscriptionId}/resourceGroups/{resource-group-name}/providers/{resource-provider-name}/{resource-type}/{resource-name}
39// filter - the filter to apply on the operation. For more information please see
40// https://docs.microsoft.com/en-us/rest/api/apimanagement/apis?redirectedfrom=MSDN
41// expand - setting $expand=recommendedactions in url query expands the recommendedactions in the response.
42func (client ChildResourcesClient) List(ctx context.Context, resourceURI string, filter string, expand string) (result AvailabilityStatusListResultPage, err error) {
43	if tracing.IsEnabled() {
44		ctx = tracing.StartSpan(ctx, fqdn+"/ChildResourcesClient.List")
45		defer func() {
46			sc := -1
47			if result.aslr.Response.Response != nil {
48				sc = result.aslr.Response.Response.StatusCode
49			}
50			tracing.EndSpan(ctx, sc, err)
51		}()
52	}
53	result.fn = client.listNextResults
54	req, err := client.ListPreparer(ctx, resourceURI, filter, expand)
55	if err != nil {
56		err = autorest.NewErrorWithError(err, "resourcehealth.ChildResourcesClient", "List", nil, "Failure preparing request")
57		return
58	}
59
60	resp, err := client.ListSender(req)
61	if err != nil {
62		result.aslr.Response = autorest.Response{Response: resp}
63		err = autorest.NewErrorWithError(err, "resourcehealth.ChildResourcesClient", "List", resp, "Failure sending request")
64		return
65	}
66
67	result.aslr, err = client.ListResponder(resp)
68	if err != nil {
69		err = autorest.NewErrorWithError(err, "resourcehealth.ChildResourcesClient", "List", resp, "Failure responding to request")
70		return
71	}
72	if result.aslr.hasNextLink() && result.aslr.IsEmpty() {
73		err = result.NextWithContext(ctx)
74		return
75	}
76
77	return
78}
79
80// ListPreparer prepares the List request.
81func (client ChildResourcesClient) ListPreparer(ctx context.Context, resourceURI string, filter string, expand string) (*http.Request, error) {
82	pathParameters := map[string]interface{}{
83		"resourceUri": resourceURI,
84	}
85
86	const APIVersion = "2015-01-01"
87	queryParameters := map[string]interface{}{
88		"api-version": APIVersion,
89	}
90	if len(filter) > 0 {
91		queryParameters["$filter"] = autorest.Encode("query", filter)
92	}
93	if len(expand) > 0 {
94		queryParameters["$expand"] = autorest.Encode("query", expand)
95	}
96
97	preparer := autorest.CreatePreparer(
98		autorest.AsGet(),
99		autorest.WithBaseURL(client.BaseURI),
100		autorest.WithPathParameters("/{resourceUri}/providers/Microsoft.ResourceHealth/childResources", pathParameters),
101		autorest.WithQueryParameters(queryParameters))
102	return preparer.Prepare((&http.Request{}).WithContext(ctx))
103}
104
105// ListSender sends the List request. The method will close the
106// http.Response Body if it receives an error.
107func (client ChildResourcesClient) ListSender(req *http.Request) (*http.Response, error) {
108	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
109}
110
111// ListResponder handles the response to the List request. The method always
112// closes the http.Response Body.
113func (client ChildResourcesClient) ListResponder(resp *http.Response) (result AvailabilityStatusListResult, err error) {
114	err = autorest.Respond(
115		resp,
116		azure.WithErrorUnlessStatusCode(http.StatusOK),
117		autorest.ByUnmarshallingJSON(&result),
118		autorest.ByClosing())
119	result.Response = autorest.Response{Response: resp}
120	return
121}
122
123// listNextResults retrieves the next set of results, if any.
124func (client ChildResourcesClient) listNextResults(ctx context.Context, lastResults AvailabilityStatusListResult) (result AvailabilityStatusListResult, err error) {
125	req, err := lastResults.availabilityStatusListResultPreparer(ctx)
126	if err != nil {
127		return result, autorest.NewErrorWithError(err, "resourcehealth.ChildResourcesClient", "listNextResults", nil, "Failure preparing next results request")
128	}
129	if req == nil {
130		return
131	}
132	resp, err := client.ListSender(req)
133	if err != nil {
134		result.Response = autorest.Response{Response: resp}
135		return result, autorest.NewErrorWithError(err, "resourcehealth.ChildResourcesClient", "listNextResults", resp, "Failure sending next results request")
136	}
137	result, err = client.ListResponder(resp)
138	if err != nil {
139		err = autorest.NewErrorWithError(err, "resourcehealth.ChildResourcesClient", "listNextResults", resp, "Failure responding to next results request")
140	}
141	return
142}
143
144// ListComplete enumerates all values, automatically crossing page boundaries as required.
145func (client ChildResourcesClient) ListComplete(ctx context.Context, resourceURI string, filter string, expand string) (result AvailabilityStatusListResultIterator, err error) {
146	if tracing.IsEnabled() {
147		ctx = tracing.StartSpan(ctx, fqdn+"/ChildResourcesClient.List")
148		defer func() {
149			sc := -1
150			if result.Response().Response.Response != nil {
151				sc = result.page.Response().Response.Response.StatusCode
152			}
153			tracing.EndSpan(ctx, sc, err)
154		}()
155	}
156	result.page, err = client.List(ctx, resourceURI, filter, expand)
157	return
158}
159