This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
instance.go
172 lines (151 loc) · 4.89 KB
/
instance.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
/*
Copyright (c) 2016, Percona LLC and/or its affiliates. All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package controllers
import (
"encoding/json"
"io/ioutil"
"strings"
"github.com/nu7hatch/gouuid"
"github.com/percona/pmm/proto"
"github.com/percona/qan-api/app/db"
"github.com/percona/qan-api/app/instance"
"github.com/percona/qan-api/app/shared"
"github.com/revel/revel"
)
type Instance struct {
BackEnd
}
// GET /instances
func (c *Instance) List() revel.Result {
dbm := c.Args["dbm"].(db.Manager)
if err := dbm.Open(); err != nil {
return c.Error(err, "Instance.List: dbm.Open")
}
instanceHandler := instance.NewMySQLHandler(dbm)
var instanceType, instanceName, parentUUID string
c.Params.Bind(&instanceType, "type")
c.Params.Bind(&instanceName, "name")
c.Params.Bind(&parentUUID, "parent_uuid")
if instanceType != "" && instanceName != "" {
_, in, err := instanceHandler.GetByName(instanceType, instanceName, parentUUID)
if err != nil {
return c.Error(err, "Instance.List: ih.GetByName")
}
if in == nil {
return c.Error(shared.ErrNotFound, "Instance.List: ih.GetByName")
}
return c.RenderJSON(in)
} else {
instances, err := instanceHandler.GetAll()
if err != nil {
return c.Error(err, "Instance.List: ih.GetAll")
}
return c.RenderJSON(instances)
}
}
// POST /instances
func (c *Instance) Create() revel.Result {
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
return c.Error(err, "Instance.Create: ioutil.ReadAll")
}
if len(body) == 0 {
return c.BadRequest(nil, "empty body (no data posted)")
}
in := proto.Instance{}
err = json.Unmarshal(body, &in)
if err != nil {
return c.BadRequest(err, "cannot decode proto.Instance")
}
if in.UUID == "" {
u4, _ := uuid.NewV4()
in.UUID = strings.Replace(u4.String(), "-", "", -1)
}
dbm := c.Args["dbm"].(db.Manager)
if err := dbm.Open(); err != nil {
return c.Error(err, "Instance.Create: dbm.Open")
}
ih := instance.NewMySQLHandler(dbm)
if _, err := ih.Create(in); err != nil {
if err == shared.ErrDuplicateEntry {
id, _ := instance.GetInstanceId(dbm.DB(), in.UUID)
if id == 0 {
_, in2, err := ih.GetByName(in.Subsystem, in.Name, "")
if err != nil {
return c.Error(err, "Instance.Create: ih.GetByName")
}
in = *in2
}
uri := c.Args["httpBase"].(string) + "/instances/" + in.UUID
c.Response.Out.Header().Set("Location", uri)
}
return c.Error(err, "Instance.Create: ih.Create")
}
return c.RenderCreated(c.Args["httpBase"].(string) + "/instances/" + in.UUID)
}
// GET /instances/:uuid
func (c *Instance) Get(uuid string) revel.Result {
dbm := c.Args["dbm"].(db.Manager)
if err := dbm.Open(); err != nil {
return c.Error(err, "Instance.Get: dbm.Open")
}
instanceHandler := instance.NewMySQLHandler(dbm)
_, instance, err := instanceHandler.Get(uuid)
if err != nil {
return c.Error(err, "Instance.Get: ih.Get")
}
return c.RenderJSON(instance)
}
// PUT /instances/:uuid
func (c *Instance) Update(uuid string) revel.Result {
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
return c.Error(err, "Instance.Update: ioutil.ReadAll")
}
if len(body) == 0 {
return c.BadRequest(nil, "empty body (no data posted)")
}
in := proto.Instance{}
err = json.Unmarshal(body, &in)
if err != nil {
return c.BadRequest(err, "cannot decode proto.Instance")
}
// I don't want to use a different proto.Instance not having the uuid
// to avoid having a million of different structs, so, the body can have
// an uuid but I'm going to rewrite it with the value from the route.
dbm := c.Args["dbm"].(db.Manager)
if err := dbm.Open(); err != nil {
return c.Error(err, "Instance.Update: dbm.Open")
}
in.UUID = uuid
ih := instance.NewMySQLHandler(dbm)
if err := ih.Update(in); err != nil {
return c.Error(err, "Instance.Update: ih.Update")
}
uri := c.Args["httpBase"].(string) + "/instances/" + in.UUID
c.Response.Out.Header().Set("Location", uri)
return c.RenderNoContent()
}
// DELETE /instances/:uuid
func (c *Instance) Delete(uuid string) revel.Result {
dbm := c.Args["dbm"].(db.Manager)
if err := dbm.Open(); err != nil {
return c.Error(err, "Instance.Delete: dbm.Open")
}
ih := instance.NewMySQLHandler(dbm)
if err := ih.Delete(uuid); err != nil {
return c.Error(err, "Instance.Delete: ih.Delete")
}
return c.RenderNoContent()
}