forked from aiven/aiven-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
152 lines (124 loc) · 3.68 KB
/
service.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
140
141
142
143
144
145
146
147
148
149
150
151
152
package aiven
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
)
type (
Service struct {
CloudName string `json:"cloud_name"`
CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"`
GroupList []string `json:"group_list"`
NodeCount int `json:"node_count"`
Plan string `json:"plan"`
Name string `json:"service_name"`
Type string `json:"service_type"`
Uri string `json:"service_uri"`
State string `json:"state"`
Metadata interface{} `json:"metadata"`
Users []*ServiceUser `json:"users"`
}
ServicesHandler struct {
client *Client
}
CreateServiceRequest struct {
Cloud string `json:"cloud,omitempty"`
GroupName string `json:"group_name,omitempty"`
Plan string `json:"plan,omitempty"`
ServiceName string `json:"service_name"`
ServiceType string `json:"service_type"`
}
UpdateServiceRequest struct {
Cloud string `json:"cloud,omitempty"`
GroupName string `json:"group_name,omitempty"`
Plan string `json:"plan,omitempty"`
Powered bool `json:"powered"` // TODO: figure out if we can overwrite the default?
}
ServiceResponse struct {
APIResponse
Service *Service `json:"service"`
}
ServiceListResponse struct {
APIResponse
Services []*Service `json:"services"`
}
)
// Hostname parses the hostname out of the Service URI.
func (s *Service) Hostname() (string, error) {
hn, _, err := getHostPort(s.Uri)
return hn, err
}
// Port parses the port out of the service URI.
func (s *Service) Port() (string, error) {
_, port, err := getHostPort(s.Uri)
return port, err
}
func getHostPort(uri string) (string, string, error) {
hostUrl, err := url.Parse(uri)
if err != nil {
return "", "", err
}
if hostUrl.Host == "" {
return hostUrl.Scheme, hostUrl.Opaque, nil
}
sp := strings.Split(hostUrl.Host, ":")
if len(sp) != 2 {
return "", "", ErrInvalidHost
}
return sp[0], sp[1], nil
}
func (h *ServicesHandler) Create(project string, req CreateServiceRequest) (*Service, error) {
rsp, err := h.client.doPostRequest(fmt.Sprintf("/project/%s/service", project), req)
if err != nil {
return nil, err
}
return parseServiceResponse(rsp)
}
func (h *ServicesHandler) Get(project, service string) (*Service, error) {
rsp, err := h.client.doGetRequest(fmt.Sprintf("/project/%s/service/%s", project, service), nil)
if err != nil {
return nil, err
}
return parseServiceResponse(rsp)
}
func (h *ServicesHandler) Update(project, service string, req UpdateServiceRequest) (*Service, error) {
rsp, err := h.client.doPutRequest(fmt.Sprintf("/project/%s/service/%s", project, service), req)
if err != nil {
return nil, err
}
return parseServiceResponse(rsp)
}
func (h *ServicesHandler) Delete(project, service string) error {
bts, err := h.client.doDeleteRequest(fmt.Sprintf("/project/%s/service/%s", project, service), nil)
if err != nil {
return err
}
return handleDeleteResponse(bts)
}
func (h *ServicesHandler) List(project string) ([]*Service, error) {
rsp, err := h.client.doGetRequest(fmt.Sprintf("/project/%s/service", project), nil)
if err != nil {
return nil, err
}
var response *ServiceListResponse
if err := json.Unmarshal(rsp, &response); err != nil {
return nil, err
}
if len(response.Errors) != 0 {
return nil, errors.New(response.Message)
}
return response.Services, nil
}
func parseServiceResponse(rsp []byte) (*Service, error) {
var response *ServiceResponse
if err := json.Unmarshal(rsp, &response); err != nil {
return nil, err
}
if len(response.Errors) != 0 {
return nil, errors.New(response.Message)
}
return response.Service, nil
}