/
client.go
115 lines (95 loc) · 2.95 KB
/
client.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
package provider
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
)
var UrlBase *string
var HttpClient *http.Client
type Client struct {
apiVersion string
BaseUrl *url.URL
httpClient *http.Client
headers http.Header
}
// NewClient initializes a new Client instance to communicate with the CMDB api
func NewClient(h http.Header, port int, hostname, version string) *Client {
client := &Client{apiVersion: version}
client.httpClient = &http.Client{
Timeout: time.Second * 30,
}
urlstr := fmt.Sprintf("%s:%d/%s", hostname, port, version)
if u, err := url.Parse(urlstr); err != nil {
panic("Could not init Provider client to CMDB microservice")
} else {
client.BaseUrl = u
}
if h != nil {
client.headers = h
}
return client
}
// HTTP GET - /details?name=${name}
func (c *Client) doGetDetailsByName(baseURL, resourceName string) (b []byte, err error) {
path := fmt.Sprintf("http://%s/details", baseURL)
req, err := http.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, err
}
req.URL.RawQuery = fmt.Sprintf("name=%s", resourceName)
for name, value := range c.headers {
req.Header.Add(name, strings.Join(value, ""))
}
log.Printf("Calling %s\n", req.URL.String())
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("Unable to issue get details API call: %s", err.Error())
}
if resp.Header.Get("Content-Type") != "application/json" {
return nil, fmt.Errorf("Content-Type of HTTP response is invalid")
}
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Cannot parse response from name allocation API: %s", err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Name allocation API returned HTTP status code %d", resp.StatusCode)
}
log.Printf("Raw output: %v\n", string(b))
return
}
// HTTP PUT - /name?resource_type=${type}®ion=${region}
func (c *Client) doAllocateName(baseURL, resourceType, region string) (b []byte, err error) {
path := fmt.Sprintf("http://%s/name", baseURL)
req, err := http.NewRequest(http.MethodPut, path, nil)
if err != nil {
return nil, err
}
req.URL.RawQuery = fmt.Sprintf("resource_type=%s®ion=%s", resourceType, region)
for name, value := range c.headers {
req.Header.Add(name, strings.Join(value, ""))
}
log.Printf("Calling %s\n", req.URL.String())
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("Unable to issue name allocation API call: %s", err.Error())
}
if resp.Header.Get("Content-Type") != "application/json" {
return nil, fmt.Errorf("Content-Type of HTTP response is invalid")
}
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Cannot parse response from name allocation API: %s", err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Name allocation API returned HTTP status code %d", resp.StatusCode)
}
log.Printf("Raw output: %v\n", string(b))
return
}