forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
111 lines (95 loc) · 3.35 KB
/
rest.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
package build
import (
"fmt"
"code.google.com/p/go-uuid/uuid"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
"github.com/openshift/origin/pkg/build/api"
"github.com/openshift/origin/pkg/build/api/validation"
)
// REST implements the RESTStorage interface in terms of an Registry.
type REST struct {
registry Registry
}
// NewREST creates a new REST for builds.
func NewREST(registry Registry) apiserver.RESTStorage {
return &REST{registry}
}
// New creates a new Build object
func (r *REST) New() runtime.Object {
return &api.Build{}
}
func (*REST) NewList() runtime.Object {
return &api.Build{}
}
// List obtains a list of Builds that match selector.
func (r *REST) List(ctx kapi.Context, selector, fields labels.Selector) (runtime.Object, error) {
builds, err := r.registry.ListBuilds(ctx, selector)
if err != nil {
return nil, err
}
return builds, err
}
// Get obtains the build specified by its id.
func (r *REST) Get(ctx kapi.Context, id string) (runtime.Object, error) {
build, err := r.registry.GetBuild(ctx, id)
if err != nil {
return nil, err
}
return build, err
}
// Delete asynchronously deletes the Build specified by its id.
func (r *REST) Delete(ctx kapi.Context, id string) (runtime.Object, error) {
return &kapi.Status{Status: kapi.StatusSuccess}, r.registry.DeleteBuild(ctx, id)
}
// Create registers a given new Build instance to r.registry.
func (r *REST) Create(ctx kapi.Context, obj runtime.Object) (runtime.Object, error) {
build, ok := obj.(*api.Build)
if !ok {
return nil, fmt.Errorf("not a build: %#v", obj)
}
if !kapi.ValidNamespace(ctx, &build.ObjectMeta) {
return nil, errors.NewConflict("build", build.Namespace, fmt.Errorf("Build.Namespace does not match the provided context"))
}
if len(build.Name) == 0 {
build.Name = uuid.NewUUID().String()
}
if len(build.Status) == 0 {
build.Status = api.BuildStatusNew
}
kapi.FillObjectMetaSystemFields(ctx, &build.ObjectMeta)
if errs := validation.ValidateBuild(build); len(errs) > 0 {
return nil, errors.NewInvalid("build", build.Name, errs)
}
err := r.registry.CreateBuild(ctx, build)
if err != nil {
return nil, err
}
return build, nil
}
// Update replaces a given Build instance with an existing instance in r.registry.
func (r *REST) Update(ctx kapi.Context, obj runtime.Object) (runtime.Object, bool, error) {
build, ok := obj.(*api.Build)
if !ok {
return nil, false, fmt.Errorf("not a build: %#v", obj)
}
if errs := validation.ValidateBuild(build); len(errs) > 0 {
return nil, false, errors.NewInvalid("build", build.Name, errs)
}
if !kapi.ValidNamespace(ctx, &build.ObjectMeta) {
return nil, false, errors.NewConflict("build", build.Namespace, fmt.Errorf("Build.Namespace does not match the provided context"))
}
err := r.registry.UpdateBuild(ctx, build)
if err != nil {
return nil, false, err
}
return build, false, nil
}
// Watch begins watching for new, changed, or deleted Builds.
func (r *REST) Watch(ctx kapi.Context, label, field labels.Selector, resourceVersion string) (watch.Interface, error) {
return r.registry.WatchBuilds(ctx, label, field, resourceVersion)
}