-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnamespace.go
155 lines (140 loc) · 4 KB
/
namespace.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
package main
import (
"errors"
"krak8s/app"
"github.com/goadesign/goa"
)
// NamespaceController implements the namespace resource.
type NamespaceController struct {
*goa.Controller
ds *DataStore
backend *Runner
}
// NewNamespaceController creates a namespace controller.
func NewNamespaceController(service *goa.Service, store *DataStore, backend *Runner) *NamespaceController {
return &NamespaceController{
Controller: service.NewController("NamespaceController"),
ds: store,
backend: backend,
}
}
// MarshaApplicationRef to project media type
func MarshaApplicationRef(obj *ObjectLink) *app.ApplicationRef {
return &app.ApplicationRef{
Oid: obj.OID,
URL: obj.URL,
}
}
// MarshalNamespaceObject to project media type
func MarshalNamespaceObject(obj *NamespaceObject) *app.Namespace {
ns := &app.Namespace{
ID: obj.OID,
Type: obj.ObjType,
Name: obj.Name,
CreatedAt: obj.CreatedAt,
}
if obj.Resources != nil {
ns.Resources = &app.ClusterRef{Oid: obj.Resources.OID, URL: obj.Resources.URL}
}
count := len(obj.Applications)
if count > 0 {
ns.Applications = make(app.ApplicationRefCollection, count)
i := 0
for _, link := range obj.Applications {
ns.Applications[i] = MarshaApplicationRef(link)
i++
}
}
return ns
}
// Create runs the create action.
func (c *NamespaceController) Create(ctx *app.CreateNamespaceContext) error {
// NamespaceController_Create: start_implement
proj, ok := c.ds.Project(ctx.Projectid)
if !ok {
return ctx.NotFound()
}
ns := c.ds.NewNamespace(ctx.Payload.Name)
if ns == nil {
return ctx.InternalServerError()
}
url := APIVersion + APIProjects + ctx.Projectid + APINamespaces + ns.OID
proj.Namespaces = append(proj.Namespaces, &ObjectLink{OID: ns.OID, URL: url})
return ctx.Created(MarshalNamespaceObject(ns))
// NamespaceController_Create: end_implement
}
// Delete runs the delete action.
func (c *NamespaceController) Delete(ctx *app.DeleteNamespaceContext) error {
// NamespaceController_Delete: start_implement
proj, ok := c.ds.Project(ctx.Projectid)
if !ok {
return ctx.NotFound()
}
ns, ok := c.ds.Namespace(ctx.Namespaceid)
if !ok {
return ctx.NotFound()
}
// ensure that the namespace specified in the request is a member of this project
index := 0
found := false
for i, val := range proj.Namespaces {
if val.OID == ctx.Namespaceid {
index = i
found = true
break
}
}
if !found {
return ctx.BadRequest(errors.New("Inavlid Namespace Object ID specified in request"))
}
for _, applink := range ns.Applications {
if app, ok := c.ds.Application(applink.OID); ok {
c.backend.ChartRequest(RemoveChart, c.ds, proj, ns, app)
}
}
if ns.Resources != nil {
if res, ok := c.ds.Resource(ns.Resources.OID); ok {
c.backend.ProjectRequest(RemoveProject, c.ds, proj, ns, res)
}
}
c.ds.DeleteNamespace(ns)
copy(proj.Namespaces[index:], proj.Namespaces[index+1:])
proj.Namespaces[len(proj.Namespaces)-1] = nil
proj.Namespaces = proj.Namespaces[:len(proj.Namespaces)-1]
return ctx.NoContent()
// NamespaceController_Delete: end_implement
}
// Get runs the get action.
func (c *NamespaceController) Get(ctx *app.GetNamespaceContext) error {
// NamespaceController_Get: start_implement
if _, ok := c.ds.Project(ctx.Projectid); !ok {
return ctx.NotFound()
}
ns, ok := c.ds.Namespace(ctx.Namespaceid)
if !ok {
return ctx.NotFound()
}
res := MarshalNamespaceObject(ns)
return ctx.OK(res)
// NamespaceController_Get: end_implement
}
// List runs the list action.
func (c *NamespaceController) List(ctx *app.ListNamespaceContext) error {
// NamespaceController_List: start_implement
if _, ok := c.ds.Project(ctx.Projectid); !ok {
return ctx.NotFound()
}
collection := app.NamespaceCollection{}
nses := c.ds.NamespacesCollection(ctx.Projectid)
count := len(nses)
if count > 0 {
collection = make(app.NamespaceCollection, count)
i := 0
for _, obj := range nses {
collection[i] = MarshalNamespaceObject(obj)
i++
}
}
return ctx.OK(collection)
// NamespaceController_List: end_implement
}