-
Notifications
You must be signed in to change notification settings - Fork 47
/
ImageHandler.go
139 lines (108 loc) · 3.42 KB
/
ImageHandler.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
// Cloud Driver Interface of CB-Spider.
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// This is Mini Driver.
//
// by CB-Spider Team, 2021.11.
package resources
import (
"time"
"errors"
"encoding/json"
cblog "github.com/cloud-barista/cb-log"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
"github.com/cloud-barista/cb-spider/interface/api"
"github.com/go-redis/redis"
)
type MiniImageHandler struct {
MiniAddr string
AuthToken string
ConnectionName string
}
func init() {
}
func (imageHandler *MiniImageHandler) ListImage() ([]*irs.ImageInfo, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mini Driver: called ListImage()!")
miniAddr := imageHandler.MiniAddr
// 1. Create CloudResourceHandler
crh := api.NewCloudResourceHandler()
// 2. Setup env.
err := crh.SetServerAddr(miniAddr)
if err != nil {
cblogger.Error(err)
}
err = crh.SetTimeout(1800 * time.Second)
if err != nil {
cblogger.Error(err)
}
// 3. Open New Session
err = crh.Open()
if err != nil {
cblogger.Error(err)
return nil, err
}
// 4. Close (with defer)
defer crh.Close()
// 5. get ImageList
//connName := "aws-ohio-config"
//result, err := crh.ListImageInfo(connName)
result, err := crh.ListImageByParam(imageHandler.ConnectionName)
if err != nil {
cblogger.Error(err)
return nil, err
}
var jsonResult struct {
Result []*irs.ImageInfo `json:"image"`
}
json.Unmarshal([]byte(result), &jsonResult)
return jsonResult.Result, nil
}
func (imageHandler *MiniImageHandler) ListImage2() ([]*irs.ImageInfo, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mini Driver: called ListImage()!")
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
result, err := client.Get("imageinfo:aws:ohio").Result()
if err != nil {
cblogger.Error(err)
return nil, err
}
var jsonResult struct {
Result []*irs.ImageInfo `json:"image"`
}
json.Unmarshal([]byte(result), &jsonResult)
return jsonResult.Result, nil
}
func (imageHandler *MiniImageHandler) GetImage(imageIID irs.IID) (irs.ImageInfo, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mini Driver: called GetImage()!")
imgInfoList, err := imageHandler.ListImage()
if err != nil {
cblogger.Error(err)
return irs.ImageInfo{}, err
}
for _, info := range imgInfoList {
if (*info).IId.NameId == imageIID.NameId {
return *info, nil
}
}
return irs.ImageInfo{}, errors.New(imageIID.NameId + " image does not exist!!")
}
//---
func (imageHandler *MiniImageHandler) CreateImage(imageReqInfo irs.ImageReqInfo) (irs.ImageInfo, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mini Driver: called CreateImage()!")
return irs.ImageInfo{}, errors.New("not implemented")
}
func (imageHandler *MiniImageHandler) DeleteImage(imageIID irs.IID) (bool, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mini Driver: called DeleteImage()!")
return false, errors.New("not implemented")
}