-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud-info-mgmt.go
96 lines (80 loc) · 1.94 KB
/
cloud-info-mgmt.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
type cloudDriverRegisterRequestInfo struct {
DriverName string
ProviderName string
DriverLibFileName string
}
type KeyValue struct {
Key string
Value string
}
type cloudCredentialRegisterRequestInfo struct {
CredentialName string
ProviderName string
KeyValueInfoList []KeyValue
}
type cloudRegionRegisterRequestInfo struct {
RegionName string
ProviderName string
KeyValueInfoList []KeyValue
}
type cloudConnectionConfigCreateRequestInfo struct {
ConfigName string
ProviderName string
DriverName string
CredentialName string
RegionName string
}
func registerCloudInfo(resourceType string, param interface{}) error {
url := ""
if resourceType == "driver" ||
resourceType == "credential" ||
resourceType == "region" ||
resourceType == "connectionconfig" {
url = SPIDER_URL + "/" + resourceType
} else {
err := fmt.Errorf("resourceType must be one of these: driver, credential, region, connectionconfig")
return err
}
method := "POST"
payload, _ := json.MarshalIndent(param, "", " ")
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
req, err := http.NewRequest(method, url, strings.NewReader(string(payload)))
if err != nil {
fmt.Println(err)
}
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
cblog.Error(err)
return err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
if err != nil {
cblog.Error(err)
return err
}
fmt.Println("HTTP Status code " + strconv.Itoa(res.StatusCode))
switch {
case res.StatusCode >= 400 || res.StatusCode < 200:
err := fmt.Errorf("HTTP Status code " + strconv.Itoa(res.StatusCode))
fmt.Println("body: ", string(body))
cblog.Error(err)
return err
}
return nil
}