Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / k8s.io / apimachinery / pkg / api / meta / firsthit_restmapper.go
1 /*
2 Copyright 2014 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package meta
18
19 import (
20         "fmt"
21
22         "k8s.io/apimachinery/pkg/runtime/schema"
23         utilerrors "k8s.io/apimachinery/pkg/util/errors"
24 )
25
26 // FirstHitRESTMapper is a wrapper for multiple RESTMappers which returns the
27 // first successful result for the singular requests
28 type FirstHitRESTMapper struct {
29         MultiRESTMapper
30 }
31
32 func (m FirstHitRESTMapper) String() string {
33         return fmt.Sprintf("FirstHitRESTMapper{\n\t%v\n}", m.MultiRESTMapper)
34 }
35
36 func (m FirstHitRESTMapper) ResourceFor(resource schema.GroupVersionResource) (schema.GroupVersionResource, error) {
37         errors := []error{}
38         for _, t := range m.MultiRESTMapper {
39                 ret, err := t.ResourceFor(resource)
40                 if err == nil {
41                         return ret, nil
42                 }
43                 errors = append(errors, err)
44         }
45
46         return schema.GroupVersionResource{}, collapseAggregateErrors(errors)
47 }
48
49 func (m FirstHitRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
50         errors := []error{}
51         for _, t := range m.MultiRESTMapper {
52                 ret, err := t.KindFor(resource)
53                 if err == nil {
54                         return ret, nil
55                 }
56                 errors = append(errors, err)
57         }
58
59         return schema.GroupVersionKind{}, collapseAggregateErrors(errors)
60 }
61
62 // RESTMapping provides the REST mapping for the resource based on the
63 // kind and version. This implementation supports multiple REST schemas and
64 // return the first match.
65 func (m FirstHitRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*RESTMapping, error) {
66         errors := []error{}
67         for _, t := range m.MultiRESTMapper {
68                 ret, err := t.RESTMapping(gk, versions...)
69                 if err == nil {
70                         return ret, nil
71                 }
72                 errors = append(errors, err)
73         }
74
75         return nil, collapseAggregateErrors(errors)
76 }
77
78 // collapseAggregateErrors returns the minimal errors.  it handles empty as nil, handles one item in a list
79 // by returning the item, and collapses all NoMatchErrors to a single one (since they should all be the same)
80 func collapseAggregateErrors(errors []error) error {
81         if len(errors) == 0 {
82                 return nil
83         }
84         if len(errors) == 1 {
85                 return errors[0]
86         }
87
88         allNoMatchErrors := true
89         for _, err := range errors {
90                 allNoMatchErrors = allNoMatchErrors && IsNoMatchError(err)
91         }
92         if allNoMatchErrors {
93                 return errors[0]
94         }
95
96         return utilerrors.NewAggregate(errors)
97 }