/
images.go
100 lines (95 loc) · 2.84 KB
/
images.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
package main
import (
"github.com/Huawei/gophercloud/auth/token"
"github.com/Huawei/gophercloud/openstack"
"github.com/Huawei/gophercloud"
"fmt"
"github.com/Huawei/gophercloud/openstack/compute/v2/images"
"encoding/json"
)
func main() {
fmt.Println("main start...")
//Set authentication parameters
tokenOpts := token.TokenOptions{
IdentityEndpoint: "https://iam.xxx.yyy.com/v3",
Username: "{Username}",
Password: "{Password}",
DomainID: "{DomainID}",
ProjectID: "{ProjectID}",
}
//Init provider client
provider, authErr := openstack.AuthenticatedClient(tokenOpts)
if authErr != nil {
fmt.Println("Failed to get the AuthenticatedClient: ", authErr)
return
}
//Init service client
client, clientErr := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{})
if clientErr != nil {
fmt.Println("Failed to get the NewComputeV2 client: ", clientErr)
return
}
imageId := "{imageId}"
ImagesList(client)
ImageDelete(client, imageId)
ImageGet(client, imageId)
fmt.Println("main end...")
}
//Query images list
func ImagesList(client *gophercloud.ServiceClient) {
listOpts := images.ListOpts{
Status: "active",
}
// Query all images list information
allPages, allPagesErr := images.ListDetail(client, listOpts).AllPages()
if allPagesErr != nil {
fmt.Println("allPagesErr:", allPagesErr)
if ue, ok := allPagesErr.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
// Transform images structure
allImages, allImagesErr := images.ExtractImages(allPages)
if allImagesErr != nil {
fmt.Println("allImagesErr:", allImagesErr)
if ue, ok := allImagesErr.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
fmt.Println("images list is : ")
for _, image := range allImages {
imageJson, _ := json.MarshalIndent(image, "", " ")
fmt.Println(string(imageJson))
}
}
//Delete image
func ImageDelete(client *gophercloud.ServiceClient, imageId string) {
deleteErr := images.Delete(client, imageId).ExtractErr()
if deleteErr != nil {
fmt.Println("deleteErr:", deleteErr)
if ue, ok := deleteErr.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
fmt.Println("image delete success!")
}
//Get image details
func ImageGet(client *gophercloud.ServiceClient, imageId string) {
image, imagesGetErr := images.Get(client, imageId).Extract()
if imagesGetErr != nil {
fmt.Println("imagesGetErr:", imagesGetErr)
if ue, ok := imagesGetErr.(*gophercloud.UnifiedError); ok {
fmt.Println("ErrCode:", ue.ErrorCode())
fmt.Println("Message:", ue.Message())
}
return
}
imageJson, _ := json.MarshalIndent(image, "", " ")
fmt.Println("image detail is : " + string(imageJson))
}