forked from akkeris/es-aws-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
508 lines (448 loc) · 14.1 KB
/
server.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
package main
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elasticsearchservice"
"github.com/go-martini/martini"
_ "github.com/lib/pq"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/render"
"github.com/nu7hatch/gouuid"
"os"
vault "github.com/akkeris/vault-client"
"strings"
)
var svc *elasticsearchservice.ElasticsearchService
var region string
var plans map[string]string
var brokerdb_pool *sql.DB
type tagspec struct {
Resource string `json:"resource"`
Name string `json:"name"`
Value string `json:"value"`
}
type ErrorResponse struct {
Error string `json:"error"`
}
type provisionspec struct {
Plan string `json:"plan"`
Billingcode string `json:"billingcode"`
}
type ESSpec struct {
ES_URL string `json:"ES_URL"`
KIBANA_URL string `json:"KIBANA_URL"`
}
func vaulthelper(secret vault.VaultSecret, which string) (v string) {
for _, element := range secret.Fields {
if element.Key == which {
return element.Value
}
}
return ""
}
func setenv() {
plans = make(map[string]string)
plans["micro"] = "Micro - 1xCPU - 2 GB RAM - 10 GB Disk"
plans["small"] = "Small - 2xCPU - 4 GB RAM - 20 GB Disk"
plans["medium"] = "Medium - 2xCPU - 8 GB RAM - 40 GB Disk - Encryption at Rest"
plans["large"] = "Large - 4xCPU - 16 GB RAM - 80 GB Disk - Encryption at Rest"
plans["premium-0"] = "Production - 3 masters - 4 data nodes - multi-AZ - 2xCPU - 8 GB RAM - 100 GB Disk each - Encryption at Rest"
region = os.Getenv("REGION")
svc = elasticsearchservice.New(session.New(&aws.Config{
Region: aws.String(region),
}))
secret := vault.GetSecret(os.Getenv("BROKERDB_SECRET"))
_brokerdb := vaulthelper(secret, "location")
var err error
brokerdb_pool, err = getDB(_brokerdb)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = createdb()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func main() {
setenv()
m := martini.Classic()
m.Use(render.Renderer())
m.Post("/v1/es/instance", binding.Json(provisionspec{}), provision_handler)
m.Get("/v1/es/instance/:domainname/status", status_handler)
m.Get("/v1/es/plans", plans_handler)
m.Get("/v1/es/url/:domainname", status_handler)
m.Delete("/v1/es/instance/:domainname", delete_handler)
m.Post("/v1/es/tag", binding.Json(tagspec{}), tag_handler)
m.Run()
}
func delete_handler(params martini.Params, r render.Render) {
domainname := params["domainname"]
err := delete(domainname)
if err != nil {
fmt.Println(err)
errorout := make(map[string]interface{})
errorout["error"] = err
r.JSON(500, errorout)
return
}
r.JSON(200, map[string]string{"message": "deleted"})
}
func delete(domainname string) (e error) {
params := &elasticsearchservice.DeleteElasticsearchDomainInput{
DomainName: aws.String(domainname), // Required
}
_, err := svc.DeleteElasticsearchDomain(params)
if err != nil {
return err
}
err = deletefromdb(domainname)
if err != nil {
return err
}
return nil
}
func tag_handler(spec tagspec, berr binding.Errors, r render.Render) {
if berr != nil {
fmt.Println(berr)
errorout := make(map[string]interface{})
errorout["error"] = berr
r.JSON(500, errorout)
return
}
err := tag(spec)
if err != nil {
fmt.Println(err)
errorout := make(map[string]interface{})
errorout["error"] = err
r.JSON(500, errorout)
return
}
r.JSON(201, map[string]interface{}{"response": "tag added"})
}
func tag(spec tagspec) (e error) {
accountnumber := os.Getenv("ACCOUNTNUMBER")
name := spec.Resource
arnname := "arn:aws:es:" + region + ":" + accountnumber + ":domain/" + name
params := &elasticsearchservice.AddTagsInput{
ARN: aws.String(arnname),
TagList: []*elasticsearchservice.Tag{ // Required
{
Key: aws.String(spec.Name),
Value: aws.String(spec.Value),
},
},
}
_, err := svc.AddTags(params)
if err != nil {
fmt.Println(err.Error())
return err
}
return nil
}
func plans_handler(params martini.Params, r render.Render) {
r.JSON(200, plans)
}
func status_handler(param martini.Params, r render.Render) {
domainname := param["domainname"]
spec, err := status(domainname)
if err != nil {
fmt.Println(err)
var er ErrorResponse
er.Error = err.Error()
r.Header().Add("x-ignore-errors","true")
r.JSON(503, er)
return
}
bodybytes, err := json.Marshal(spec)
if err != nil {
fmt.Println(err)
var er ErrorResponse
er.Error = err.Error()
r.Header().Add("x-ignore-errors","true")
r.JSON(503, er)
return
}
fmt.Println(string(bodybytes))
r.JSON(200, spec)
}
func provision_handler(pspec provisionspec, berr binding.Errors, r render.Render) {
if berr != nil {
fmt.Println(berr)
var er ErrorResponse
er.Error = berr[0].Message
r.JSON(500, er)
return
}
ok := validate_plan(pspec.Plan)
if !ok {
var er ErrorResponse
er.Error = "Invalid plan"
r.JSON(500, er)
return
}
domainname, err := generate_name()
if err != nil {
fmt.Println(err)
var er ErrorResponse
er.Error = err.Error()
r.JSON(500, er)
return
}
err = provision(domainname, pspec)
if err != nil {
fmt.Println(err)
var er ErrorResponse
er.Error = err.Error()
r.JSON(500, er)
return
}
r.JSON(201, map[string]string{"message": "creation requested", "spec": "es:" + domainname})
}
func provision(domainname string, spec provisionspec) (e error) {
esversion := os.Getenv("ES_VERSION")
var volumesize int64
var volumetype string
var instancetype string
var masterinstancetype string
var mastercount int64
var instancecount int64
var kmspossible bool
if spec.Plan == "micro" {
volumesize = int64(10)
volumetype = "gp2"
instancetype = "t2.small.elasticsearch"
kmspossible = false
}
if spec.Plan == "small" {
volumesize = int64(20)
volumetype = "gp2"
instancetype = "t2.medium.elasticsearch"
kmspossible = false
}
if spec.Plan == "medium" {
volumesize = int64(40)
volumetype = "gp2"
instancetype = "m4.large.elasticsearch"
kmspossible = true
}
if spec.Plan == "large" {
volumesize = int64(80)
volumetype = "gp2"
instancetype = "m4.xlarge.elasticsearch"
kmspossible = true
}
if spec.Plan == "premium-0" {
volumesize = int64(100)
volumetype = "gp2"
instancetype = "m4.large.elasticsearch"
masterinstancetype = "m4.large.elasticsearch"
mastercount=3
instancecount=4
}
accountnumber := os.Getenv("ACCOUNTNUMBER")
snidcsv := os.Getenv("SUBNET_ID")
snidarray := strings.Split(snidcsv,",")
var snids []*string
for _, element := range snidarray {
newelement := fmt.Sprintf("%v",element)
snids = append(snids, &newelement)
}
sgid := os.Getenv("SECURITY_GROUP_ID")
sgids := []*string{&sgid}
if !strings.Contains(spec.Plan,"premium") && !kmspossible {
params := &elasticsearchservice.CreateElasticsearchDomainInput{
DomainName: aws.String(domainname),
ElasticsearchVersion: aws.String(esversion),
AccessPolicies: aws.String("{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"es:*\",\"Resource\":\"arn:aws:es:" + region + ":" + accountnumber + ":domain/" + domainname + "/*\"}]}"),
EBSOptions: &elasticsearchservice.EBSOptions{
EBSEnabled: aws.Bool(true),
VolumeSize: aws.Int64(volumesize),
VolumeType: aws.String(volumetype),
},
ElasticsearchClusterConfig: &elasticsearchservice.ElasticsearchClusterConfig{
DedicatedMasterEnabled: aws.Bool(false),
InstanceCount: aws.Int64(1),
InstanceType: aws.String(instancetype),
ZoneAwarenessEnabled: aws.Bool(false),
},
VPCOptions: &elasticsearchservice.VPCOptions{
SecurityGroupIds: sgids,
SubnetIds: snids[1:],
},
}
resp, err := svc.CreateElasticsearchDomain(params)
if err != nil {
fmt.Println(err.Error())
return err
}
fmt.Println(resp)
}
if !strings.Contains(spec.Plan,"premium") && kmspossible {
params := &elasticsearchservice.CreateElasticsearchDomainInput{
DomainName: aws.String(domainname),
ElasticsearchVersion: aws.String(esversion),
AccessPolicies: aws.String("{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"es:*\",\"Resource\":\"arn:aws:es:" + region + ":" + accountnumber + ":domain/" + domainname + "/*\"}]}"),
EBSOptions: &elasticsearchservice.EBSOptions{
EBSEnabled: aws.Bool(true),
VolumeSize: aws.Int64(volumesize),
VolumeType: aws.String(volumetype),
},
ElasticsearchClusterConfig: &elasticsearchservice.ElasticsearchClusterConfig{
DedicatedMasterEnabled: aws.Bool(false),
InstanceCount: aws.Int64(1),
InstanceType: aws.String(instancetype),
ZoneAwarenessEnabled: aws.Bool(false),
},
EncryptionAtRestOptions: &elasticsearchservice.EncryptionAtRestOptions {
Enabled: aws.Bool(true),
KmsKeyId: aws.String(os.Getenv("KMSKEYID")),
},
VPCOptions: &elasticsearchservice.VPCOptions{
SecurityGroupIds: sgids,
SubnetIds: snids[1:],
},
}
resp, err := svc.CreateElasticsearchDomain(params)
if err != nil {
fmt.Println(err.Error())
return err
}
fmt.Println(resp)
}
if strings.Contains(spec.Plan,"premium") {
params := &elasticsearchservice.CreateElasticsearchDomainInput{
DomainName: aws.String(domainname),
ElasticsearchVersion: aws.String(esversion),
AccessPolicies: aws.String("{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"es:*\",\"Resource\":\"arn:aws:es:" + region + ":" + accountnumber + ":domain/" + domainname + "/*\"}]}"),
EBSOptions: &elasticsearchservice.EBSOptions{
EBSEnabled: aws.Bool(true),
VolumeSize: aws.Int64(volumesize),
VolumeType: aws.String(volumetype),
},
ElasticsearchClusterConfig: &elasticsearchservice.ElasticsearchClusterConfig{
DedicatedMasterEnabled: aws.Bool(true),
DedicatedMasterCount: aws.Int64(mastercount),
DedicatedMasterType: aws.String(masterinstancetype),
InstanceCount: aws.Int64(instancecount),
InstanceType: aws.String(instancetype),
ZoneAwarenessEnabled: aws.Bool(true),
},
EncryptionAtRestOptions: &elasticsearchservice.EncryptionAtRestOptions {
Enabled: aws.Bool(true),
KmsKeyId: aws.String(os.Getenv("KMSKEYID")),
},
VPCOptions: &elasticsearchservice.VPCOptions{
SubnetIds: snids,
SecurityGroupIds: sgids,
},
}
resp, err := svc.CreateElasticsearchDomain(params)
if err != nil {
fmt.Println(err.Error())
return err
}
fmt.Println(resp)
}
var billingcode tagspec
billingcode.Resource = domainname
billingcode.Name = "billingcode"
billingcode.Value = spec.Billingcode
err := tag(billingcode)
if err != nil {
fmt.Println(err.Error())
return err
}
err = addtodb(domainname, spec)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func status(domainname string) (s ESSpec, e error) {
var spec ESSpec
params := &elasticsearchservice.DescribeElasticsearchDomainInput{
DomainName: aws.String(domainname), // Required
}
resp, err := svc.DescribeElasticsearchDomain(params)
if err != nil {
return spec, err
}
if resp.DomainStatus.Endpoints != nil {
spec.ES_URL = "https://" + *resp.DomainStatus.Endpoints["vpc"]
spec.KIBANA_URL = "https://" + *resp.DomainStatus.Endpoints["vpc"] + "/_plugin/kibana"
} else {
return spec, errors.New("not available")
}
return spec, nil
}
func generate_name() (n string, e error) {
var toreturn string
domainname, err := uuid.NewV4()
if err != nil {
fmt.Println(err)
return toreturn, err
}
toreturn = os.Getenv("NAME_PREFIX") + strings.Split(domainname.String(), "-")[0]
return toreturn, nil
}
func validate_plan(plan string) (r bool) {
ok := plans[plan]
if ok != "" {
return true
} else {
return false
}
}
func createdb() (e error) {
sqlStatement := `CREATE TABLE if not exists public.provision ( name character varying(200) NOT NULL, plan character varying(200), claimed character varying(200), make_date timestamp without time zone DEFAULT now(), CONSTRAINT name_pkey PRIMARY KEY (name) )`
_, err := brokerdb_pool.Exec(sqlStatement)
if err != nil {
fmt.Println("unable to set up db")
return err
}
return nil
}
func addtodb(domainname string, spec provisionspec) (e error) {
_, err := brokerdb_pool.Exec("INSERT INTO provision(name,plan,claimed) VALUES($1,$2,$3) returning name;", domainname, spec.Plan, "yes")
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func deletefromdb(domainname string) (e error) {
stmt, err := brokerdb_pool.Prepare("delete from provision where name=$1")
if err != nil {
fmt.Println(err)
return err
}
res, err := stmt.Exec(domainname)
if err != nil {
fmt.Println(err)
return err
}
affect, err := res.RowsAffected()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(affect, "rows removed from provision table")
return nil
}
func getDB(uri string) (d *sql.DB, e error) {
db, dberr := sql.Open("postgres", uri)
if dberr != nil {
fmt.Println(dberr)
return nil, dberr
}
db.SetMaxIdleConns(5)
db.SetMaxOpenConns(20)
return db, nil
}