/
cosmos_offers.go
125 lines (104 loc) · 3.09 KB
/
cosmos_offers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package azcosmos
import (
"context"
"fmt"
"net/http"
azruntime "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
)
type cosmosOffers struct {
client *Client
}
type cosmosOffersResponse struct {
Offers []ThroughputProperties `json:"Offers"`
}
func (c cosmosOffers) ReadThroughputIfExists(
ctx context.Context,
targetRID string,
requestOptions *ThroughputOptions) (ThroughputResponse, error) {
// TODO: might want to replace with query iterator once that is in
operationContext := pipelineRequestOptions{
resourceType: resourceTypeOffer,
resourceAddress: "",
}
path, err := generatePathForNameBased(resourceTypeOffer, "", true)
if err != nil {
return ThroughputResponse{}, err
}
azResponse, err := c.client.sendQueryRequest(
path,
ctx,
fmt.Sprintf(`SELECT * FROM c WHERE c.offerResourceId = '%s'`, targetRID),
nil,
operationContext,
requestOptions,
nil)
if err != nil {
return ThroughputResponse{}, err
}
var theOffers cosmosOffersResponse
err = azruntime.UnmarshalAsJSON(azResponse, &theOffers)
if err != nil {
return ThroughputResponse{}, err
}
queryRequestCharge := newResponse(azResponse).RequestCharge
if len(theOffers.Offers) == 0 {
azResponse.StatusCode = http.StatusNotFound
azResponse.Header.Add(cosmosHeaderRequestCharge, fmt.Sprint(queryRequestCharge))
return ThroughputResponse{}, azruntime.NewResponseErrorWithErrorCode(azResponse, azResponse.Status)
}
// Now read the individual offer
operationContext = pipelineRequestOptions{
resourceType: resourceTypeOffer,
resourceAddress: theOffers.Offers[0].offerId,
isRidBased: true,
}
path, err = generatePathForNameBased(resourceTypeOffer, theOffers.Offers[0].selfLink, false)
if err != nil {
return ThroughputResponse{}, err
}
azResponse, err = c.client.sendGetRequest(
path,
ctx,
operationContext,
requestOptions,
nil)
if err != nil {
return ThroughputResponse{}, err
}
return newThroughputResponse(azResponse, &queryRequestCharge)
}
func (c cosmosOffers) ReplaceThroughputIfExists(
ctx context.Context,
properties ThroughputProperties,
targetRID string,
requestOptions *ThroughputOptions) (ThroughputResponse, error) {
readResponse, err := c.ReadThroughputIfExists(ctx, targetRID, requestOptions)
if err != nil {
return ThroughputResponse{}, err
}
readRequestCharge := readResponse.RequestCharge
readResponse.ThroughputProperties.offer = properties.offer
operationContext := pipelineRequestOptions{
resourceType: resourceTypeOffer,
resourceAddress: readResponse.ThroughputProperties.offerId,
isRidBased: true,
isWriteOperation: true,
}
path, err := generatePathForNameBased(resourceTypeOffer, readResponse.ThroughputProperties.selfLink, false)
if err != nil {
return ThroughputResponse{}, err
}
azResponse, err := c.client.sendPutRequest(
path,
ctx,
readResponse.ThroughputProperties,
operationContext,
requestOptions,
nil)
if err != nil {
return ThroughputResponse{}, err
}
return newThroughputResponse(azResponse, &readRequestCharge)
}