1package documentdb
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	"net/http"
26)
27
28// DatabaseAccountRegionClient is the azure Cosmos DB Database Service Resource Provider REST API
29type DatabaseAccountRegionClient struct {
30	BaseClient
31}
32
33// NewDatabaseAccountRegionClient creates an instance of the DatabaseAccountRegionClient client.
34func NewDatabaseAccountRegionClient(subscriptionID string) DatabaseAccountRegionClient {
35	return NewDatabaseAccountRegionClientWithBaseURI(DefaultBaseURI, subscriptionID)
36}
37
38// NewDatabaseAccountRegionClientWithBaseURI creates an instance of the DatabaseAccountRegionClient client.
39func NewDatabaseAccountRegionClientWithBaseURI(baseURI string, subscriptionID string) DatabaseAccountRegionClient {
40	return DatabaseAccountRegionClient{NewWithBaseURI(baseURI, subscriptionID)}
41}
42
43// ListMetrics retrieves the metrics determined by the given filter for the given database account and region.
44// Parameters:
45// resourceGroupName - name of an Azure resource group.
46// accountName - cosmos DB database account name.
47// region - cosmos DB region, with spaces between words and each word capitalized.
48// filter - an OData filter expression that describes a subset of metrics to return. The parameters that can be
49// filtered are name.value (name of the metric, can have an or of multiple names), startTime, endTime, and
50// timeGrain. The supported operator is eq.
51func (client DatabaseAccountRegionClient) ListMetrics(ctx context.Context, resourceGroupName string, accountName string, region string, filter string) (result MetricListResult, err error) {
52	if err := validation.Validate([]validation.Validation{
53		{TargetValue: resourceGroupName,
54			Constraints: []validation.Constraint{{Target: "resourceGroupName", Name: validation.MaxLength, Rule: 90, Chain: nil},
55				{Target: "resourceGroupName", Name: validation.MinLength, Rule: 1, Chain: nil},
56				{Target: "resourceGroupName", Name: validation.Pattern, Rule: `^[-\w\._\(\)]+$`, Chain: nil}}},
57		{TargetValue: accountName,
58			Constraints: []validation.Constraint{{Target: "accountName", Name: validation.MaxLength, Rule: 50, Chain: nil},
59				{Target: "accountName", Name: validation.MinLength, Rule: 3, Chain: nil}}}}); err != nil {
60		return result, validation.NewError("documentdb.DatabaseAccountRegionClient", "ListMetrics", err.Error())
61	}
62
63	req, err := client.ListMetricsPreparer(ctx, resourceGroupName, accountName, region, filter)
64	if err != nil {
65		err = autorest.NewErrorWithError(err, "documentdb.DatabaseAccountRegionClient", "ListMetrics", nil, "Failure preparing request")
66		return
67	}
68
69	resp, err := client.ListMetricsSender(req)
70	if err != nil {
71		result.Response = autorest.Response{Response: resp}
72		err = autorest.NewErrorWithError(err, "documentdb.DatabaseAccountRegionClient", "ListMetrics", resp, "Failure sending request")
73		return
74	}
75
76	result, err = client.ListMetricsResponder(resp)
77	if err != nil {
78		err = autorest.NewErrorWithError(err, "documentdb.DatabaseAccountRegionClient", "ListMetrics", resp, "Failure responding to request")
79	}
80
81	return
82}
83
84// ListMetricsPreparer prepares the ListMetrics request.
85func (client DatabaseAccountRegionClient) ListMetricsPreparer(ctx context.Context, resourceGroupName string, accountName string, region string, filter string) (*http.Request, error) {
86	pathParameters := map[string]interface{}{
87		"accountName":       autorest.Encode("path", accountName),
88		"region":            autorest.Encode("path", region),
89		"resourceGroupName": autorest.Encode("path", resourceGroupName),
90		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
91	}
92
93	const APIVersion = "2015-04-08"
94	queryParameters := map[string]interface{}{
95		"$filter":     autorest.Encode("query", filter),
96		"api-version": APIVersion,
97	}
98
99	preparer := autorest.CreatePreparer(
100		autorest.AsGet(),
101		autorest.WithBaseURL(client.BaseURI),
102		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DocumentDB/databaseAccounts/{accountName}/region/{region}/metrics", pathParameters),
103		autorest.WithQueryParameters(queryParameters))
104	return preparer.Prepare((&http.Request{}).WithContext(ctx))
105}
106
107// ListMetricsSender sends the ListMetrics request. The method will close the
108// http.Response Body if it receives an error.
109func (client DatabaseAccountRegionClient) ListMetricsSender(req *http.Request) (*http.Response, error) {
110	return autorest.SendWithSender(client, req,
111		azure.DoRetryWithRegistration(client.Client))
112}
113
114// ListMetricsResponder handles the response to the ListMetrics request. The method always
115// closes the http.Response Body.
116func (client DatabaseAccountRegionClient) ListMetricsResponder(resp *http.Response) (result MetricListResult, err error) {
117	err = autorest.Respond(
118		resp,
119		client.ByInspecting(),
120		azure.WithErrorUnlessStatusCode(http.StatusOK),
121		autorest.ByUnmarshallingJSON(&result),
122		autorest.ByClosing())
123	result.Response = autorest.Response{Response: resp}
124	return
125}
126