-
Notifications
You must be signed in to change notification settings - Fork 0
/
osaasclient.go
165 lines (131 loc) · 3.94 KB
/
osaasclient.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
153
154
155
156
157
158
159
160
161
162
163
164
165
package osaasclient
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/url"
)
type Port struct {
ExternalIP string `json:"externalIp"`
ExternalPort int `json:"externalPort"`
InternalPort int `json:"internalPort"`
}
type FetchError struct {
HTTPCode int
Message string
}
func (e FetchError) Error() string {
return fmt.Sprintf("HTTP Code: %d, Message: %s", e.HTTPCode, e.Message)
}
type UnauthorizedError struct{}
func (e UnauthorizedError) Error() string {
return "Unauthorized access"
}
func GetService(ctx *Context, serviceId string) (*Service, error) {
serviceURL := fmt.Sprintf("https://catalog.svc.%s.osaas.io/mysubscriptions", ctx.GetEnvironment())
var services []Service
err := createFetch(serviceURL, "GET", nil, &services,
Auth{"x-pat-jwt", fmt.Sprintf("Bearer %s", ctx.GetPersonalAccessToken())})
if err != nil {
return nil, err
}
for _, service := range services {
if service.ServiceId == serviceId {
return &service, nil
}
}
return nil, errors.New("service not found in your subscriptions")
}
func CreateInstance(ctx *Context, serviceId string, token string,
body map[string]interface{}) (map[string]interface{}, error) {
service, err := GetService(ctx, serviceId)
if err != nil {
return nil, err
}
instanceURL := service.ApiUrl
bodyBytes, _ := json.Marshal(body)
bodyBuffer := bytes.NewBuffer(bodyBytes)
var instance map[string]interface{}
err = createFetch(instanceURL, "POST", bodyBuffer, &instance, Auth{"x-jwt", fmt.Sprintf("Bearer %s", token)})
if err != nil {
return nil, err
}
return instance, nil
}
func RemoveInstance(ctx *Context, serviceId string, name string, token string) error {
service, err := GetService(ctx, serviceId)
if err != nil {
return err
}
instanceURL := fmt.Sprintf("%s/%s", service.ApiUrl, name)
slog.Debug(fmt.Sprintf("Deleting on instanceUrl: %s", instanceURL))
var target string
return createFetch(instanceURL, "DELETE", nil, &target, Auth{"x-jwt", fmt.Sprintf("Bearer %s", token)})
}
func GetInstance(ctx *Context, serviceId string, name string, token string) (map[string]interface{}, error) {
service, err := GetService(ctx, serviceId)
if err != nil {
return nil, err
}
instanceURL := fmt.Sprintf("%s/%s", service.ApiUrl, name)
var instance map[string]interface{}
err = createFetch(instanceURL, "GET", nil, &instance, Auth{"x-jwt", fmt.Sprintf("Bearer %s", token)})
if err != nil {
if fetchErr, ok := err.(FetchError); ok {
if fetchErr.HTTPCode == 404 {
return nil, nil
}
}
return nil, err
}
return instance, nil
}
func ListInstances(context *Context, serviceId, token string) ([]map[string]interface{}, error) {
service, err := GetService(context, serviceId)
if err != nil {
return nil, err
}
instanceUrl := service.ApiUrl
var instances []map[string]interface{}
err = createFetch(instanceUrl, "GET", nil, &instances, Auth{"x-jwt", fmt.Sprintf("Bearer %s", token)})
if err != nil {
return nil, err
}
return instances, nil
}
func GetPortsForInstance(context *Context, serviceId, name, token string) ([]Port, error) {
service, err := GetService(context, serviceId)
if err != nil {
return nil, err
}
instanceUrl, err := url.Parse(service.ApiUrl)
if err != nil {
return nil, err
}
portsUrl := fmt.Sprintf("https://%s/ports/%s", instanceUrl.Host, name)
var ports []Port
err = createFetch(portsUrl, "GET", nil, &ports, Auth{"x-jwt", fmt.Sprintf("Bearer %s", token)})
if err != nil {
return nil, err
}
return ports, nil
}
func GetLogsForInstance(context *Context, serviceId, name, token string) ([]string, error) {
service, err := GetService(context, serviceId)
if err != nil {
return nil, err
}
instanceUrl, err := url.Parse(service.ApiUrl)
if err != nil {
return nil, err
}
logsUrl := fmt.Sprintf("https://%s/logs/%s", instanceUrl.Host, name)
var logs []string
err = createFetch(logsUrl, "GET", nil, &logs, Auth{"x-jwt", fmt.Sprintf("Bearer %s", token)})
if err != nil {
return nil, err
}
return logs, nil
}