-
Notifications
You must be signed in to change notification settings - Fork 5
/
cluster.go
119 lines (104 loc) · 2.97 KB
/
cluster.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
package main
import (
"errors"
"krak8s/app"
"github.com/goadesign/goa"
)
// ClusterController implements the cluster resource.
type ClusterController struct {
*goa.Controller
ds *DataStore
backend *Runner
}
// NewClusterController creates a cluster controller.
func NewClusterController(service *goa.Service, store *DataStore, backend *Runner) *ClusterController {
return &ClusterController{
Controller: service.NewController("ClusterController"),
ds: store,
backend: backend,
}
}
// MarshalResourcesObject to project media type
func MarshalResourcesObject(obj *ResourceObject) *app.Cluster {
return &app.Cluster{
ID: obj.OID,
Type: obj.ObjType,
NodePoolSize: obj.NodePoolSize,
NamespaceID: obj.NamespaceID,
State: obj.State,
CreatedAt: obj.CreatedAt,
UpdatedAt: obj.UpdatedAt,
}
}
// Create runs the create action.
func (c *ClusterController) Create(ctx *app.CreateClusterContext) error {
// ClusterController_Create: start_implement
proj, ok := c.ds.Project(ctx.Projectid)
if !ok {
return ctx.NotFound()
}
ns, ok := c.ds.Namespace(ctx.Payload.NamespaceID)
if !ok {
return ctx.NotFound()
} else if ns.Resources != nil {
return ctx.Conflict()
}
found := false
for _, val := range proj.Namespaces {
if val.OID == ctx.Payload.NamespaceID {
found = true
break
}
}
if !found {
return ctx.BadRequest(errors.New("Inavlid Namespace Object ID specified in request"))
}
res := c.ds.NewResource(ctx.Payload.NamespaceID, ctx.Payload.NodePoolSize)
if res == nil {
return ctx.InternalServerError()
}
url := APIVersion + APIProjects + ctx.Projectid + APICluster + res.OID
ns.Resources = &ObjectLink{OID: res.OID, URL: url}
c.backend.ProjectRequest(AddProject, c.ds, proj, ns, res)
return ctx.Accepted(MarshalResourcesObject(res))
// ClusterController_Create: end_implement
}
// Delete runs the delete action.
func (c *ClusterController) Delete(ctx *app.DeleteClusterContext) error {
// ClusterController_Delete: start_implement
proj, ok := c.ds.Project(ctx.Projectid)
if !ok {
return ctx.NotFound()
}
res, ok := c.ds.Resource(ctx.ResourceID)
if !ok {
return ctx.NotFound()
}
ns, ok := c.ds.Namespace(res.NamespaceID)
if !ok {
return ctx.NotFound()
}
if ns.Resources.OID != ctx.ResourceID {
return ctx.BadRequest(errors.New("Inavlid Cluster Resource Object ID specified in request"))
}
res.State = ResourceDeleteRequested
c.backend.ProjectRequest(RemoveProject, c.ds, proj, ns, res)
c.ds.DeleteResource(res)
ns.Resources = nil
return ctx.NoContent()
// ClusterController_Delete: end_implement
}
// Get runs the get action.
func (c *ClusterController) Get(ctx *app.GetClusterContext) error {
// ClusterController_Get: start_implement
if _, ok := c.ds.Project(ctx.Projectid); !ok {
return ctx.NotFound()
}
resource, ok := c.ds.Resource(ctx.ResourceID)
if !ok {
return ctx.NotFound()
}
res := MarshalResourcesObject(resource)
return ctx.OK(res)
// ClusterController_Get: end_implement
}