forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest.go
108 lines (93 loc) · 3.63 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
package buildconfig
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/fields"
"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 is an implementation of RESTStorage for the api server.
type REST struct {
registry Registry
}
// NewREST creates a new REST for BuildConfig.
func NewREST(registry Registry) apiserver.RESTStorage {
return &REST{registry}
}
// New creates a new BuildConfig.
func (r *REST) New() runtime.Object {
return &api.BuildConfig{}
}
func (*REST) NewList() runtime.Object {
return &api.BuildConfig{}
}
// List obtains a list of BuildConfigs that match label.
func (r *REST) List(ctx kapi.Context, label labels.Selector, field fields.Selector) (runtime.Object, error) {
builds, err := r.registry.ListBuildConfigs(ctx, label)
if err != nil {
return nil, err
}
return builds, err
}
// Get obtains the BuildConfig specified by its id.
func (r *REST) Get(ctx kapi.Context, id string) (runtime.Object, error) {
buildConfig, err := r.registry.GetBuildConfig(ctx, id)
if err != nil {
return nil, err
}
return buildConfig, err
}
// Delete asynchronously deletes the BuildConfig specified by its id.
func (r *REST) Delete(ctx kapi.Context, id string) (runtime.Object, error) {
return &kapi.Status{Status: kapi.StatusSuccess}, r.registry.DeleteBuildConfig(ctx, id)
}
// Create registers a given new BuildConfig instance to r.registry.
func (r *REST) Create(ctx kapi.Context, obj runtime.Object) (runtime.Object, error) {
buildConfig, ok := obj.(*api.BuildConfig)
if !ok {
return nil, fmt.Errorf("not a buildConfig: %#v", obj)
}
if !kapi.ValidNamespace(ctx, &buildConfig.ObjectMeta) {
return nil, errors.NewConflict("buildConfig", buildConfig.Namespace, fmt.Errorf("BuildConfig.Namespace does not match the provided context"))
}
if len(buildConfig.Name) == 0 {
buildConfig.Name = uuid.NewUUID().String()
}
kapi.FillObjectMetaSystemFields(ctx, &buildConfig.ObjectMeta)
if errs := validation.ValidateBuildConfig(buildConfig); len(errs) > 0 {
return nil, errors.NewInvalid("buildConfig", buildConfig.Name, errs)
}
err := r.registry.CreateBuildConfig(ctx, buildConfig)
if err != nil {
return nil, err
}
return buildConfig, nil
}
// Update replaces a given BuildConfig instance with an existing instance in r.registry.
func (r *REST) Update(ctx kapi.Context, obj runtime.Object) (runtime.Object, bool, error) {
buildConfig, ok := obj.(*api.BuildConfig)
if !ok {
return nil, false, fmt.Errorf("not a buildConfig: %#v", obj)
}
if errs := validation.ValidateBuildConfig(buildConfig); len(errs) > 0 {
return nil, false, errors.NewInvalid("buildConfig", buildConfig.Name, errs)
}
if !kapi.ValidNamespace(ctx, &buildConfig.ObjectMeta) {
return nil, false, errors.NewConflict("buildConfig", buildConfig.Namespace, fmt.Errorf("BuildConfig.Namespace does not match the provided context"))
}
err := r.registry.UpdateBuildConfig(ctx, buildConfig)
if err != nil {
return nil, false, err
}
return buildConfig, false, nil
}
// Watch begins watching for new, changed, or deleted BuildConfigs.
func (r *REST) Watch(ctx kapi.Context, label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
return r.registry.WatchBuildConfigs(ctx, label, field, resourceVersion)
}