Skip to content

Commit 6c233c3

Browse files
committed
Sync from Bitbucket
1 parent 2a8d4bd commit 6c233c3

File tree

13 files changed

+323
-85
lines changed

13 files changed

+323
-85
lines changed

bash-completion

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
_coscale-cli()
2+
{
3+
local object action cur
4+
COMPREPLY=()
5+
object="${COMP_WORDS[1]}"
6+
action="${COMP_WORDS[2]}"
7+
cur="${COMP_WORDS[COMP_CWORD]}"
8+
9+
opts="event server servergroup metric metricgroup data alert config"
10+
auth="--api-url --app-id --access-token --rawOutput"
11+
12+
case "${object}" in
13+
event)
14+
case "${action}" in
15+
list) opts="${auth}" ;;
16+
get) opts="--id --name ${auth}" ;;
17+
delete) opts="--id --name ${auth}" ;;
18+
new) opts="--name --description --attributeDescriptions --source ${auth}" ;;
19+
update) opts="--id --name --description --attributeDescriptions --source ${auth}" ;;
20+
newdata) opts="--name --id --message --subject --attribute --timestamp --stopTime ${auth}" ;;
21+
updatedata) opts="--name --id --dataid --message --subject --attribute --timestamp --stopTime ${auth}" ;;
22+
deletedata) opts="--id --dataid ${auth}" ;;
23+
*) opts="list get delete new update newdata updatedata deletedata" ;;
24+
esac
25+
;;
26+
server)
27+
case "${action}" in
28+
list) opts="${auth}" ;;
29+
get) opts="--id --name ${auth}" ;;
30+
delete) opts="--id --name ${auth}" ;;
31+
new) opts="--name --description --serverType --source ${auth}" ;;
32+
update) opts="--name --id --description --serverType --state --source ${auth}" ;;
33+
*) opts="list get delete new update" ;;
34+
esac
35+
;;
36+
servergroup)
37+
case "${action}" in
38+
list) opts="${auth}" ;;
39+
get) opts="--id --name ${auth}" ;;
40+
delete) opts="--id --name ${auth}" ;;
41+
new) opts="--name --description --type --state --source ${auth}" ;;
42+
update) opts="--name --id --description --type --state --source ${auth}" ;;
43+
addServer) opts="--idServer --nameServer --idGroup --nameGroup ${auth}" ;;
44+
deleteServer) opts="--idServer --nameServer --idGroup --nameGroup ${auth}" ;;
45+
*) opts="list get delete new update addServer deleteServer" ;;
46+
esac
47+
;;
48+
metric)
49+
case "${action}" in
50+
list) opts="${auth}" ;;
51+
listbygroup) opts="--id --name ${auth}" ;;
52+
new) opts="--name --dataType --subject --period --description --unit --attachTo --source ${auth}" ;;
53+
update) opts="--name --id --description --dataType --subject --unit --period --attachTo --source ${auth}" ;;
54+
*) opts="list listbygroup new update"
55+
esac
56+
;;
57+
metricgroup)
58+
case "${action}" in
59+
list) opts="${auth}" ;;
60+
get) opts="--id --name ${auth}" ;;
61+
addMetric) opts="--idMetric --nameMetric --idGroup --nameGroup ${auth}" ;;
62+
deleteMetric) opts="--idMetric --nameMetric --idGroup --nameGroup ${auth}" ;;
63+
new) opts="--name --subject --description --state --source ${auth}" ;;
64+
update) opts="--name --id --description --type --state --source ${auth}" ;;
65+
*) opts="list get addMetric deleteMetric new update"
66+
esac
67+
;;
68+
data)
69+
case "${action}" in
70+
get) opts="--id --subjectIds --start --stop --aggregator --aggregateSubjects ${auth}" ;;
71+
insert) opts="--data ${auth}" ;;
72+
*) opts="get insert"
73+
esac
74+
;;
75+
alert)
76+
case "${action}" in
77+
list) opts="${auth}" ;;
78+
acknowledge) opts="--id --name ${auth}" ;;
79+
resolve) opts="--id --name ${auth}" ;;
80+
*) opts="list acknowledge resolve"
81+
esac
82+
;;
83+
config)
84+
case "${action}" in
85+
check) opts="" ;;
86+
set) opts="--api-url --app-id --access-token" ;;
87+
*) opts="check set"
88+
esac
89+
;;
90+
*)
91+
;;
92+
esac
93+
94+
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
95+
return 0
96+
97+
}
98+
complete -F _coscale-cli coscale-cli
99+

src/coscale/api/alert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (e Alert) GetId() int64 {
2424
//GetAlertsBy will use a custom query to get a alert by unresolved/unacknowledged
2525
func (api *Api) GetAlertsBy(query string) (string, error) {
2626
var result string
27-
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/alerts/?%s=false", api.appID, query), nil, true, &result); err != nil {
27+
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/alerts/?%s=false", api.AppID, query), nil, true, &result); err != nil {
2828
return "", err
2929
}
3030
return result, nil
@@ -36,7 +36,7 @@ func (api *Api) AlertSolution(alert *Alert, solutionType string) (string, error)
3636
"version": {fmt.Sprintf("%d", alert.Version)},
3737
}
3838
var result string
39-
if err := api.makeCall("PUT", fmt.Sprintf("/api/v1/app/%s/alerts/%d/%s/", api.appID, alert.ID, solutionType), data, true, &result); err != nil {
39+
if err := api.makeCall("PUT", fmt.Sprintf("/api/v1/app/%s/alerts/%d/%s/", api.AppID, alert.ID, solutionType), data, true, &result); err != nil {
4040
return "", err
4141
}
4242
return result, nil

src/coscale/api/api.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type NotFoundError string
3636
type RequestError string
3737
type Duplicate int64
3838
type Disabled string
39+
type InvalidConfig string
3940

4041
func (ue AuthenticationError) Error() string {
4142
return string(ue)
@@ -61,6 +62,10 @@ func (d Disabled) Error() string {
6162
return string(d)
6263
}
6364

65+
func (i InvalidConfig) Error() string {
66+
return string(i)
67+
}
68+
6469
// Checks if an error is a AuthenticationError.
6570
func IsAuthenticationError(err error) bool {
6671
_, ok := err.(AuthenticationError)
@@ -95,18 +100,30 @@ func IsDisabled(err error) bool {
95100
return ok
96101
}
97102

103+
// Check if an error is caused by an invalid api configuration.
104+
func IsInvalidConfig(err error) bool {
105+
_, ok := err.(InvalidConfig)
106+
return ok
107+
}
108+
98109
type Api struct {
99-
baseUrl string
100-
accessToken string
101-
token string
102-
appID string
110+
BaseUrl string
111+
AccessToken string
112+
AppID string
103113
rawOutput bool
104-
lastDataSent time.Time
114+
token string
115+
validConfig bool
105116
}
106117

107118
// NewApi creates a new Api connector using an email and a password.
108119
func NewApi(baseUrl string, accessToken string, appID string, rawOutput bool) *Api {
109-
api := &Api{baseUrl, accessToken, "", appID, rawOutput, time.Now()}
120+
api := &Api{baseUrl, accessToken, appID, rawOutput, "", true}
121+
return api
122+
}
123+
124+
// NewApi creates a new Api connector using an email and a password.
125+
func NewFakeApi() *Api {
126+
api := &Api{"", "", "", true, "", false}
110127
return api
111128
}
112129

@@ -207,11 +224,11 @@ type LoginData struct {
207224
func (api *Api) Login() error {
208225

209226
data := map[string][]string{
210-
"accessToken": {api.accessToken},
227+
"accessToken": {api.AccessToken},
211228
}
212229

213230
var loginData LoginData
214-
bytes, err := api.doHttpRequest("POST", fmt.Sprintf("%s/api/v1/app/%s/login/", api.baseUrl, api.appID), "", data, readWriteTimeout)
231+
bytes, err := api.doHttpRequest("POST", fmt.Sprintf("%s/api/v1/app/%s/login/", api.BaseUrl, api.AppID), "", data, readWriteTimeout)
215232
if err != nil {
216233
return AuthenticationError(fmt.Sprintf("Authentication error: %s", err.Error()))
217234
}
@@ -234,7 +251,7 @@ func (api *Api) makeRawCall(method string, uri string, data map[string][]string,
234251
}
235252

236253
// Do the actual request.
237-
bytes, err := api.doHttpRequest(method, api.baseUrl+uri, api.token, data, timeout)
254+
bytes, err := api.doHttpRequest(method, api.BaseUrl + uri, api.token, data, timeout)
238255
if err != nil {
239256
if _, ok := err.(UnauthorizedError); ok {
240257
// unauthorizedError: the token might have experied. Performing login again
@@ -243,7 +260,7 @@ func (api *Api) makeRawCall(method string, uri string, data map[string][]string,
243260
api.token = ""
244261
return nil, err
245262
}
246-
return api.doHttpRequest(method, api.baseUrl+uri, api.token, data, timeout)
263+
return api.doHttpRequest(method, api.BaseUrl + uri, api.token, data, timeout)
247264
}
248265
return bytes, err
249266
}
@@ -253,6 +270,10 @@ func (api *Api) makeRawCall(method string, uri string, data map[string][]string,
253270

254271
// makeCall Make a call to the api and parse the json response into target.
255272
func (api *Api) makeCall(method string, uri string, data map[string][]string, jsonOut bool, target interface{}) error {
273+
if !api.validConfig {
274+
return InvalidConfig("Could not find valid authentication configuration.")
275+
}
276+
256277
b, err := api.makeRawCall(method, uri, data, readWriteTimeout)
257278
if err != nil {
258279
return err

src/coscale/api/common.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
//GetObjects will get all the objects (json) specified by objectName. eg: all the metrics or all the servers
1212
func (api *Api) GetObjects(objectName string) (string, error) {
1313
var result string
14-
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/", api.appID, objectName), nil, true, &result); err != nil {
14+
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/", api.AppID, objectName), nil, true, &result); err != nil {
1515
return "", err
1616
}
1717
return result, nil
@@ -20,15 +20,15 @@ func (api *Api) GetObjects(objectName string) (string, error) {
2020
// GetObject will return the object (json) specified by objectName that have a certain id
2121
func (api *Api) GetObject(objectName string, id int64) (string, error) {
2222
var result string
23-
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/%d/", api.appID, objectName, id), nil, true, &result); err != nil {
23+
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/%d/", api.AppID, objectName, id), nil, true, &result); err != nil {
2424
return "", err
2525
}
2626
return result, nil
2727
}
2828

2929
//GetObjectRef will put in result a reference to a object specified by objectName and that have a certain id
3030
func (api *Api) GetObjectRef(objectName string, id int64, result Object) error {
31-
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/%d/", api.appID, objectName, id), nil, false, &result); err != nil {
31+
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/%d/", api.AppID, objectName, id), nil, false, &result); err != nil {
3232
return err
3333
}
3434
return nil
@@ -38,7 +38,7 @@ func (api *Api) GetObjectRef(objectName string, id int64, result Object) error {
3838
func (api *Api) GetObjectByName(objectName string, name string) (string, error) {
3939
name = strings.Replace(name, " ", "%20", -1)
4040
var result string
41-
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/?selectByName=%s", api.appID, objectName, name), nil, true, &result); err != nil {
41+
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/?selectByName=%s", api.AppID, objectName, name), nil, true, &result); err != nil {
4242
return "", err
4343
}
4444
return result, nil
@@ -48,7 +48,7 @@ func (api *Api) GetObjectByName(objectName string, name string) (string, error)
4848
func (api *Api) GetObejctRefByName(objectName string, name string, result Object) error {
4949
name = strings.Replace(name, " ", "%20", -1)
5050
objects := []*Object{&result}
51-
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/?selectByName=%s", api.appID, objectName, name), nil, false, &objects); err != nil {
51+
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/?selectByName=%s", api.AppID, objectName, name), nil, false, &objects); err != nil {
5252
return err
5353
}
5454
if len(objects) == 0 {
@@ -61,15 +61,15 @@ func (api *Api) GetObejctRefByName(objectName string, name string, result Object
6161

6262
//DeleteObject will delete a object
6363
func (api *Api) DeleteObject(objectName string, object *Object) error {
64-
if err := api.makeCall("DELETE", fmt.Sprintf("/api/v1/app/%s/%ss/%d/", api.appID, objectName, (*object).GetId()), nil, false, nil); err != nil {
64+
if err := api.makeCall("DELETE", fmt.Sprintf("/api/v1/app/%s/%ss/%d/", api.AppID, objectName, (*object).GetId()), nil, false, nil); err != nil {
6565
return err
6666
}
6767
return nil
6868
}
6969

7070
// AddObjectToGroup adds a object (metric, event, etc) to a group of objects.
7171
func (api *Api) AddObjectToGroup(objectName string, object Object, group Object) error {
72-
if err := api.makeCall("POST", fmt.Sprintf("/api/v1/app/%s/%sgroups/%d/%ss/%d/", api.appID, objectName, group.GetId(), objectName, object.GetId()), nil, false, nil); err != nil {
72+
if err := api.makeCall("POST", fmt.Sprintf("/api/v1/app/%s/%sgroups/%d/%ss/%d/", api.AppID, objectName, group.GetId(), objectName, object.GetId()), nil, false, nil); err != nil {
7373
if IsRequestError(err) {
7474
// The object is already in the group. Ignore this error.
7575
} else {
@@ -81,5 +81,5 @@ func (api *Api) AddObjectToGroup(objectName string, object Object, group Object)
8181

8282
// AddObjectToGroup remove a object (metric, event, etc) from a group of objects.
8383
func (api *Api) DeleteObjectFromGroup(objectName string, object Object, group Object) error {
84-
return api.makeCall("DELETE", fmt.Sprintf("/api/v1/app/%s/%sgroups/%d/%ss/%d/", api.appID, objectName, group.GetId(), objectName, object.GetId()), nil, false, nil)
84+
return api.makeCall("DELETE", fmt.Sprintf("/api/v1/app/%s/%sgroups/%d/%ss/%d/", api.AppID, objectName, group.GetId(), objectName, object.GetId()), nil, false, nil)
8585
}

src/coscale/api/configuration.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ import (
88

99
// ApiConfiguration contains all information to connect with the api.
1010
type ApiConfiguration struct {
11-
BaseUrl string
12-
AccessToken string
13-
AppId string
14-
TemplateId int64
15-
Certificate string
11+
BaseUrl string `json:"baseurl"`
12+
AccessToken string `json:"accesstoken"`
13+
AppId string `json:"appid"`
1614
}
1715

1816
// ReadApiConfiguration reads the api configuration from a file.
@@ -44,3 +42,21 @@ func readConfig(filename string, target interface{}) error {
4442

4543
return nil
4644
}
45+
46+
// WriteApiConfiguration writes a configuration file: gzipped json file.
47+
func WriteApiConfiguration(filename string, config *ApiConfiguration) error {
48+
file, err := os.Create(filename)
49+
if err != nil {
50+
return err
51+
}
52+
53+
writer := gzip.NewWriter(file)
54+
defer writer.Close()
55+
56+
encoder := json.NewEncoder(writer)
57+
if err := encoder.Encode(config); err != nil {
58+
return err
59+
}
60+
61+
return nil
62+
}

src/coscale/api/data.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (api *Api) InsertData(data []*ApiData) (string, error) {
121121
"data": {apiDataToString(data)},
122122
}
123123
var result string
124-
if err := api.makeCall("POST", fmt.Sprintf("/api/v1/app/%s/data/", api.appID), postData, true, &result); err != nil {
124+
if err := api.makeCall("POST", fmt.Sprintf("/api/v1/app/%s/data/", api.AppID), postData, true, &result); err != nil {
125125
return "", err
126126
}
127127

@@ -155,7 +155,7 @@ func (api *Api) GetData(start, stop int, metricId int64, subjectIds, aggregator
155155
"data": {getBatchData(start, stop, metricId, subjectIds, aggregator, aggregateSubjects)},
156156
}
157157
var result string
158-
if err := api.makeCall("POST", fmt.Sprintf("/api/v1/app/%s/data/getBatch/", api.appID), postData, true, &result); err != nil {
158+
if err := api.makeCall("POST", fmt.Sprintf("/api/v1/app/%s/data/getBatch/", api.AppID), postData, true, &result); err != nil {
159159
return "", err
160160
}
161161
return result, nil

0 commit comments

Comments
 (0)