forked from civo/civogo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
204 lines (173 loc) · 6.04 KB
/
application.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package civogo
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"github.com/civo/civogo/utils"
)
// Application is the struct for the Application model
type Application struct {
Name string `json:"name" validate:"required"`
ID string `json:"id"`
NetworkID string `json:"network_id" validate:"required"`
Description string `json:"description"`
Image string `json:"image"`
Size string `json:"size"`
ProcessInfo []ProcessInfo `json:"process_info,omitempty"`
Domains []string `json:"domains,omitempty"`
SSHKeyIDs []string `json:"ssh_key_ids,omitempty"`
Config []EnvVar `json:"config,omitempty"`
// Status can be one of:
// - "building": Implies platform is building
// - "available": Implies platform is available to accept image
// - "ready": Implies app is ready
Status string `json:"status"`
}
// ApplicationConfig describes the parameters for a new CivoApp
type ApplicationConfig struct {
Name string `json:"name" validate:"required"`
NetworkID string `json:"network_id" validate:"required"`
Description string `json:"description"`
Size string `json:"size"`
SSHKeyIDs []string `json:"ssh_key_ids,omitempty"`
}
// UpdateApplicationRequest is the struct for the UpdateApplication request
type UpdateApplicationRequest struct {
Name string `json:"name"`
Advanced bool `json:"advanced"`
Image string `json:"image" `
Description string `json:"description"`
ProcessInfo []ProcessInfo `json:"process_info"`
Size string `json:"size" schema:"size"`
SSHKeyIDs []string `json:"ssh_key_ids" `
Config []EnvVar `json:"config"`
Domains []string `json:"domains"`
}
// PaginatedApplications returns a paginated list of Application object
type PaginatedApplications struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
Pages int `json:"pages"`
Items []Application `json:"items"`
}
// EnvVar holds key-value pairs for an application
type EnvVar struct {
Name string `json:"name"`
Value string `json:"value"`
}
// ProcessInfo contains the information about the process obtained from Procfile
type ProcessInfo struct {
ProcessType string `json:"processType"`
ProcessCount int `json:"processCount"`
}
// ErrAppDomainNotFound is returned when the domain is not found
var ErrAppDomainNotFound = fmt.Errorf("domain not found")
// ListApplications returns all applications in that specific region
func (c *Client) ListApplications() (*PaginatedApplications, error) {
resp, err := c.SendGetRequest("/v2/applications")
if err != nil {
return nil, decodeError(err)
}
application := &PaginatedApplications{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&application); err != nil {
return nil, decodeError(err)
}
return application, nil
}
// GetApplication returns an application by ID
func (c *Client) GetApplication(id string) (*Application, error) {
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/applications/%s", id))
if err != nil {
return nil, decodeError(err)
}
application := &Application{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&application); err != nil {
return nil, decodeError(err)
}
return application, nil
}
// NewApplicationConfig returns an initialized config for a new application
func (c *Client) NewApplicationConfig() (*ApplicationConfig, error) {
network, err := c.GetDefaultNetwork()
if err != nil {
return nil, decodeError(err)
}
return &ApplicationConfig{
Name: utils.RandomName(),
NetworkID: network.ID,
Description: "",
Size: "small",
SSHKeyIDs: []string{},
}, nil
}
// FindApplication finds an application by either part of the ID or part of the name
func (c *Client) FindApplication(search string) (*Application, error) {
apps, err := c.ListApplications()
if err != nil {
return nil, decodeError(err)
}
exactMatch := false
partialMatchesCount := 0
result := Application{}
for _, value := range apps.Items {
if value.Name == search || value.ID == search {
exactMatch = true
result = value
} else if strings.Contains(value.Name, search) || strings.Contains(value.ID, search) {
if !exactMatch {
result = value
partialMatchesCount++
}
}
}
if exactMatch || partialMatchesCount == 1 {
return &result, nil
} else if partialMatchesCount > 1 {
err := fmt.Errorf("unable to find %s because there were multiple matches", search)
return nil, MultipleMatchesError.wrap(err)
} else {
err := fmt.Errorf("unable to find %s, zero matches", search)
return nil, ZeroMatchesError.wrap(err)
}
}
// CreateApplication creates a new application
func (c *Client) CreateApplication(config *ApplicationConfig) (*Application, error) {
body, err := c.SendPostRequest("/v2/applications", config)
if err != nil {
return nil, decodeError(err)
}
var application Application
if err := json.NewDecoder(bytes.NewReader(body)).Decode(&application); err != nil {
return nil, err
}
return &application, nil
}
// UpdateApplication updates an application
func (c *Client) UpdateApplication(id string, application *UpdateApplicationRequest) (*Application, error) {
body, err := c.SendPutRequest(fmt.Sprintf("/v2/applications/%s", id), application)
if err != nil {
return nil, decodeError(err)
}
updatedApplication := &Application{}
if err := json.NewDecoder(bytes.NewReader(body)).Decode(updatedApplication); err != nil {
return nil, err
}
return updatedApplication, nil
}
// DeleteApplication deletes an application
func (c *Client) DeleteApplication(id string) (*SimpleResponse, error) {
resp, err := c.SendDeleteRequest(fmt.Sprintf("/v2/applications/%s", id))
if err != nil {
return nil, decodeError(err)
}
return c.DecodeSimpleResponse(resp)
}
// GetApplicationLogAuth returns an application log auth
func (c *Client) GetApplicationLogAuth(id string) (string, error) {
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/applications/%s/log_auth", id))
if err != nil {
return "", decodeError(err)
}
return string(resp), nil
}