forked from huaweicloud/huaweicloud-sdk-go
/
notifications.go
108 lines (84 loc) · 2.62 KB
/
notifications.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
package main
import (
"fmt"
"github.com/gophercloud/gophercloud/functiontest/common"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/as/v1/notifications"
"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
}
TestEnableNotification(sc)
TestDeleteNotification(sc)
TestListNotification(sc)
fmt.Println("main end...")
}
func TestListNotification(client *gophercloud.ServiceClient) {
id := "cfdd14a3-e24d-414c-a3ce-b191386b9b83"
result, err := notifications.List(client, id).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
}
b, _ := json.MarshalIndent(result, "", " ")
fmt.Println(string(b))
fmt.Printf("Notification: %+v\r\n", result)
fmt.Println("Test List success!")
}
func TestEnableNotification(client *gophercloud.ServiceClient) {
id := "cfdd14a3-e24d-414c-a3ce-b191386b9b83"
opts := notifications.ConfigNotificationOpts{
TopicScene: []string{"SCALING_UP_FAIL"},
TopicUrn: "urn:smn:southchina:128a7bf965154373a7b73c89eb6b65aa:newtest",
}
result, err := notifications.ConfigNotification(client, id, 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
}
b, _ := json.MarshalIndent(result, "", " ")
fmt.Println(string(b))
fmt.Printf("Notification: %+v\r\n", result)
fmt.Println("Test TestEnableNotification success!")
}
func TestDeleteNotification(client *gophercloud.ServiceClient) {
id := "f9642b84-06c8-4c8a-aa06-7f2bdd04667f"
topicUrn := "urn:smn:southchina:128a7bf965154373a7b73c89eb6b65aa:newtest"
err := notifications.Delete(client, id, topicUrn).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 TestDeleteNotification success!")
}