Skip to content

Commit dc7d92f

Browse files
authored
Merge pull request #1206 from ioito/hotfix/qx-azure-reservation-list
fix(azure): add reservation list
2 parents e047e5c + 0df5d15 commit dc7d92f

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

pkg/multicloud/azure/azure_v2.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ func (self *SAzureClient) _post_v2(service string, resource, apiVersion string,
147147

148148
func (self *SAzureClient) _request_v2(service string, method httputils.THttpMethod, resource, apiVersion string, params url.Values, body map[string]interface{}) (jsonutils.JSONObject, error) {
149149
value := []jsonutils.JSONObject{}
150+
if gotypes.IsNil(params) {
151+
params = url.Values{}
152+
}
150153
for {
151154
resp, err := self.__request_v2(service, method, resource, apiVersion, params, body)
152155
if err != nil {
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright 2019 Yunion
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package azure
16+
17+
import "time"
18+
19+
type SReservationOrder struct {
20+
Id string
21+
Name string
22+
Type string
23+
Properties struct {
24+
OriginalQuantity int
25+
RequestDateTime time.Time
26+
BillingPlan string
27+
Reservations []struct {
28+
Id string
29+
}
30+
Term string
31+
DisplayName string
32+
CreatedDateTime time.Time
33+
BenefitStartTime time.Time
34+
ProvisioningState string
35+
BillingProfileId string
36+
BillingAccountId string
37+
ExpiryDate time.Time
38+
ExpiryDateTime time.Time
39+
}
40+
}
41+
42+
func (client *SAzureClient) ListReservationOrders() ([]SReservationOrder, error) {
43+
result := []SReservationOrder{}
44+
resp, err := client.list_v2("/providers/Microsoft.Capacity/reservationOrders", "2022-11-01", nil)
45+
if err != nil {
46+
return nil, err
47+
}
48+
err = resp.Unmarshal(&result, "value")
49+
if err != nil {
50+
return nil, err
51+
}
52+
return result, nil
53+
}
54+
55+
type SReservation struct {
56+
Id string
57+
Name string
58+
Type string
59+
Location string
60+
Etag int
61+
Sku struct {
62+
Name string
63+
}
64+
Properties struct {
65+
ReservedResourceType string
66+
UserFriendlyRenewState string
67+
SkuDescription string
68+
Renew bool
69+
Archived bool
70+
Quantity int
71+
AppliedScopeType string
72+
DisplayName string
73+
ProvisioningState string
74+
Term string
75+
DisplayProvisioningState string
76+
UserFriendlyAppliedScopeType string
77+
ExpiryDateTime time.Time
78+
PurchaseDateTime time.Time
79+
BenefitStartTime time.Time
80+
LastUpdatedDateTime time.Time
81+
ExpiryDate string
82+
PurchaseDate string
83+
EffectiveDateTime time.Time
84+
InstanceFlexibility string
85+
Utilization struct {
86+
Trend string
87+
Aggregates []struct {
88+
Grain float64
89+
GrainUnit string
90+
Value float64
91+
ValueUnit string
92+
}
93+
}
94+
BillingPlan string
95+
BillingScopeId string
96+
}
97+
}
98+
99+
func (client *SAzureClient) ListReservations() ([]SReservation, error) {
100+
result := []SReservation{}
101+
resp, err := client.list_v2("/providers/Microsoft.Capacity/reservations", "2022-11-01", nil)
102+
if err != nil {
103+
return nil, err
104+
}
105+
err = resp.Unmarshal(&result, "value")
106+
if err != nil {
107+
return nil, err
108+
}
109+
return result, nil
110+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2019 Yunion
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package shell
16+
17+
import (
18+
"yunion.io/x/pkg/util/shellutils"
19+
20+
"yunion.io/x/cloudmux/pkg/multicloud/azure"
21+
)
22+
23+
func init() {
24+
type ReservationOrderListOptions struct {
25+
}
26+
shellutils.R(&ReservationOrderListOptions{}, "reservation-order-list", "List reservation orders", func(cli *azure.SRegion, args *ReservationOrderListOptions) error {
27+
ret, err := cli.GetClient().ListReservationOrders()
28+
if err != nil {
29+
return err
30+
}
31+
printList(ret, len(ret), 0, 0, []string{})
32+
return nil
33+
})
34+
35+
type ReservationListOptions struct {
36+
}
37+
shellutils.R(&ReservationListOptions{}, "reservation-list", "List reservations", func(cli *azure.SRegion, args *ReservationListOptions) error {
38+
ret, err := cli.GetClient().ListReservations()
39+
if err != nil {
40+
return err
41+
}
42+
printList(ret, len(ret), 0, 0, []string{})
43+
return nil
44+
})
45+
46+
}

0 commit comments

Comments
 (0)