forked from huaweicloud/huaweicloud-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groups.go
179 lines (156 loc) · 4.28 KB
/
groups.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"fmt"
"github.com/YoungPioneers/huaweicloud-sdk-go/functiontest/common"
"github.com/YoungPioneers/huaweicloud-sdk-go/pagination"
"github.com/YoungPioneers/huaweicloud-sdk-go"
"github.com/YoungPioneers/huaweicloud-sdk-go/openstack"
"github.com/YoungPioneers/huaweicloud-sdk-go/openstack/as/v1/groups"
"encoding/json"
)
func main() {
fmt.Println("main start...")
provider, err := common.AuthAKSK()
if err != nil {
fmt.Println("get provider client failed")
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
sc, err := openstack.NewASV1(provider, gophercloud.EndpointOpts{})
if err != nil {
fmt.Println("get as v1 client failed")
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
TestCreateASGroup(sc)
TestListAsGroup(sc)
TestUpdateAsGroup(sc)
TestGetAsGroup(sc)
TestDelAsGroup(sc)
fmt.Println("main end...")
}
func TestCreateASGroup(client *gophercloud.ServiceClient) {
net := groups.Network{
ID: "6df498a2-3480-4faf-b6e7-ac25a053bbbc",
}
opts := groups.CreateOpts{
ScalingGroupName: "KAKAnew",
VpcId: "90de16ce-3bd5-42e8-a6b3-d275d26ceb33",
Networks: []groups.Network{net},
}
result, err := groups.Create(client, opts).Extract()
if err != nil {
fmt.Println(err)
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
fmt.Printf("groups: %+v\r\n", result)
fmt.Println("Test Create success!")
}
func TestListAsGroup(client *gophercloud.ServiceClient) {
opts := groups.ListOpts{
Limit: 1,
//StartNumber: 1,
}
page, err := groups.List(client, opts).AllPages()
if err != nil {
fmt.Println(err)
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
resp, err := groups.ExtractGroups(page)
if err != nil {
fmt.Println(err)
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
b, _ := json.MarshalIndent(resp, "", " ")
fmt.Println(string(b))
fmt.Println("Test List success!")
}
func TestListAsGroupEach(client *gophercloud.ServiceClient) {
opts := groups.ListOpts{
Limit: 1,
StartNumber: 1,
}
err := groups.List(client, opts).EachPage(func(page pagination.Page) (bool, error) {
resp, err := groups.ExtractGroups(page)
if err != nil {
return false, err
}
b, _ := json.MarshalIndent(resp, "", " ")
fmt.Println(string(b))
return true, nil
})
if err != nil {
fmt.Println(err)
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
fmt.Println("Test List success!")
}
func TestGetAsGroup(client *gophercloud.ServiceClient) {
groupID := "f9642b84-06c8-4c8a-aa06-7f2bdd04667f"
result, err := groups.Get(client, groupID).Extract()
if err != nil {
fmt.Println(err)
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
fmt.Printf("groups: %+v\r\n", result)
fmt.Println("Test Get success!")
}
func TestUpdateAsGroup(client *gophercloud.ServiceClient) {
groupID := "2558d0b9-4ed0-4ec3-a603-449cfce36322"
opts := groups.UpdateOpts{
ScalingGroupName: "myName",
}
result, err := groups.Update(client, groupID, opts).Extract()
if err != nil {
fmt.Println(err)
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
fmt.Printf("groups: %+v\r\n", result)
fmt.Println("Test update success!")
}
func TestDelAsGroup(client *gophercloud.ServiceClient) {
groupID := "42d782f3-cb37-439c-9b73-73cf465dab4f"
opts := groups.DeleteOpts{
ForceDelete: "no",
}
err := groups.Delete(client, groupID, opts).ExtractErr()
if err != nil {
fmt.Println(err)
if ue, ok := err.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
fmt.Println("Test delete success!")
}