2014-10-22 10:42:13 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-10-22 10:42:13 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2014-11-20 22:24:10 +00:00
|
|
|
"fmt"
|
|
|
|
|
2014-10-22 10:42:13 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2015-03-22 21:40:47 +00:00
|
|
|
errs "github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
|
2014-10-22 10:42:13 +00:00
|
|
|
)
|
|
|
|
|
2014-11-17 18:29:45 +00:00
|
|
|
type RESTClientPoster interface {
|
|
|
|
Post() *client.Request
|
|
|
|
}
|
|
|
|
|
2014-10-22 10:42:13 +00:00
|
|
|
// ClientFunc returns the RESTClient defined for given resource
|
2014-11-17 18:29:45 +00:00
|
|
|
type ClientPosterFunc func(mapping *meta.RESTMapping) (RESTClientPoster, error)
|
2014-10-22 10:42:13 +00:00
|
|
|
|
2014-11-04 14:51:43 +00:00
|
|
|
// CreateObjects creates bulk of resources provided by items list. Each item must
|
2014-10-22 10:42:13 +00:00
|
|
|
// be valid API type. It requires ObjectTyper to parse the Version and Kind and
|
|
|
|
// RESTMapper to get the resource URI and REST client that knows how to create
|
|
|
|
// given type
|
2014-12-12 18:56:31 +00:00
|
|
|
func CreateObjects(typer runtime.ObjectTyper, mapper meta.RESTMapper, clientFor ClientPosterFunc, objects []runtime.Object) []error {
|
|
|
|
var allErrors []error
|
2014-10-22 10:42:13 +00:00
|
|
|
for i, obj := range objects {
|
|
|
|
version, kind, err := typer.ObjectVersionAndKind(obj)
|
|
|
|
if err != nil {
|
2014-11-20 22:24:10 +00:00
|
|
|
allErrors = append(allErrors, fmt.Errorf("Config.item[%d] kind: %v", i, err))
|
2014-10-22 10:42:13 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-12-12 19:05:27 +00:00
|
|
|
mapping, err := mapper.RESTMapping(kind, version)
|
2014-10-22 10:42:13 +00:00
|
|
|
if err != nil {
|
2014-11-20 22:24:10 +00:00
|
|
|
allErrors = append(allErrors, fmt.Errorf("Config.item[%d] mapping: %v", i, err))
|
2014-10-22 10:42:13 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := clientFor(mapping)
|
|
|
|
if err != nil {
|
2014-11-20 22:24:10 +00:00
|
|
|
allErrors = append(allErrors, fmt.Errorf("Config.item[%d] client: %v", i, err))
|
2014-10-22 10:42:13 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := CreateObject(client, mapping, obj); err != nil {
|
2014-11-20 22:24:10 +00:00
|
|
|
allErrors = append(allErrors, fmt.Errorf("Config.item[%d]: %v", i, err))
|
2014-10-22 10:42:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 22:24:10 +00:00
|
|
|
return allErrors
|
2014-10-22 10:42:13 +00:00
|
|
|
}
|
|
|
|
|
2014-11-04 14:51:43 +00:00
|
|
|
// CreateObject creates the obj using the provided clients and the resource URI
|
2014-10-22 10:42:13 +00:00
|
|
|
// mapping. It reports ValidationError when the object is missing the Metadata
|
|
|
|
// or the Name and it will report any error occured during create REST call
|
2014-11-17 18:29:45 +00:00
|
|
|
func CreateObject(client RESTClientPoster, mapping *meta.RESTMapping, obj runtime.Object) *errs.ValidationError {
|
2014-10-22 10:42:13 +00:00
|
|
|
name, err := mapping.MetadataAccessor.Name(obj)
|
|
|
|
if err != nil || name == "" {
|
2015-03-11 14:57:19 +00:00
|
|
|
return errs.NewFieldRequired("name")
|
2014-10-22 10:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace, err := mapping.Namespace(obj)
|
|
|
|
if err != nil {
|
2015-03-11 14:57:19 +00:00
|
|
|
return errs.NewFieldRequired("namespace")
|
2014-10-22 10:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This should be using RESTHelper
|
2014-12-26 20:06:25 +00:00
|
|
|
err = client.Post().Resource(mapping.Resource).Namespace(namespace).Body(obj).Do().Error()
|
2014-10-22 10:42:13 +00:00
|
|
|
if err != nil {
|
2014-11-20 22:24:10 +00:00
|
|
|
return errs.NewFieldInvalid(name, obj, err.Error())
|
2014-10-22 10:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|