-
Notifications
You must be signed in to change notification settings - Fork 787
/
elasticsearch.go
142 lines (113 loc) · 3.01 KB
/
elasticsearch.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
package pipline_events
import (
"encoding/json"
"io/ioutil"
"net/http"
"fmt"
"bytes"
"strings"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
)
type result interface {
}
type ESIssue struct {
v1.IssueSummary
Environment map[string]string
}
type Index struct {
Created bool
Id string `json:"_id,omitempty"`
}
// ElasticsearchProvider implements PipelineEventsProvider interface for elasticsearch
type ElasticsearchProvider struct {
Client *http.Client
BasicAuth string
BaseURL string
}
func NewElasticsearchProvider(server *auth.AuthServer, user *auth.UserAuth) (PipelineEventsProvider, error) {
basicAuth := util.BasicAuth(user.Username, user.Password)
provider := ElasticsearchProvider{
BaseURL: server.URL,
BasicAuth: basicAuth,
Client: http.DefaultClient,
}
return &provider, nil
}
func (e ElasticsearchProvider) SendActivity(a *v1.PipelineActivity) error {
data, err := json.Marshal(a)
if err != nil {
return err
}
var index *Index
err = e.post("activities", string(a.UID), data, &index)
if err != nil {
return err
}
if index.Id == "" {
return fmt.Errorf("activity %s not created, no elasticsearch id returned from POST\n", a.Name)
}
return nil
}
func (e ElasticsearchProvider) SendRelease(r *v1.Release) error {
data, err := json.Marshal(r)
if err != nil {
return err
}
var index *Index
err = e.post("releases", string(r.UID), data, &index)
if err != nil {
return err
}
if index.Id == "" {
return fmt.Errorf("release %s not created, no elasticsearch id returned from POST\n", r.Name)
}
for _, i := range r.Spec.Issues {
esissue := ESIssue{
i, map[string]string{
r.Namespace: r.CreationTimestamp.String(),
},
}
e.SendIssue(&esissue)
}
return nil
}
func (e ElasticsearchProvider) SendIssue(i *ESIssue) error {
id := strings.Replace(i.URL, ":", "-", -1)
id = strings.Replace(id, "/", "-", -1)
data, err := json.Marshal(i)
if err != nil {
return err
}
var index *Index
log.Infof("sending issue %s\n", id)
err = e.post("issues", id, data, &index)
if err != nil {
return err
}
if index.Id == "" {
return fmt.Errorf("issue %s not created, no elasticsearch id returned from POST\n", id)
}
return nil
}
func (e ElasticsearchProvider) post(index, indexID string, body []byte, rs result) error {
url := fmt.Sprintf("%s/%s/event/%s", e.BaseURL, index, indexID)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
req.Header.Add("Authorization", "Basic "+e.BasicAuth)
req.Header.Set("Content-Type", "application/json")
resp, err := e.Client.Do(req)
if err != nil {
return fmt.Errorf("error POSTing to elasticsearch %v", err)
}
if resp.StatusCode != 200 && resp.StatusCode != 201 {
return fmt.Errorf("error response POSTing to elasticsearch: %s", resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(data, &rs)
if err != nil {
return fmt.Errorf("error unmarshalling %v", err)
}
return nil
}