Browse code

Added MultiRESTMapper into pkg/api/meta

Michal Fojtik authored on 2014/11/24 21:40:53
Showing 4 changed files
... ...
@@ -83,8 +83,5 @@ func init() {
83 83
 
84 84
 	// For Origin we use MultiRESTMapper that handles both Origin and Kubernetes
85 85
 	// objects
86
-	RESTMapper = meta.MultiRESTMapper{
87
-		originMapper,
88
-		kubeMapper,
89
-	}
86
+	RESTMapper = meta.MultiRESTMapper{originMapper, kubeMapper}
90 87
 }
... ...
@@ -1,9 +1,26 @@
1 1
 package meta
2 2
 
3
-import "github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
3
+import kmeta "github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
4 4
 
5 5
 // MultiRESTMapper is a wrapper for multiple RESTMappers.
6
-type MultiRESTMapper []meta.RESTMapper
6
+type MultiRESTMapper []kmeta.RESTMapper
7
+
8
+const (
9
+	OriginAPI     = "origin"
10
+	KubernetesAPI = "kubernetes"
11
+)
12
+
13
+// TODO: This list have to be maintained manually if a new API resource is added
14
+// into the Origin API.
15
+var OriginTypes = []string{
16
+	"Build", "BuildConfig",
17
+	"Deployment", "DeploymentConfig",
18
+	"Image", "ImageRepository", "ImageRepositoryMapping",
19
+	"Route",
20
+	"Project",
21
+	"User",
22
+	"OAuth",
23
+}
7 24
 
8 25
 // VersionAndKindForResource provides the Version and Kind  mappings for the
9 26
 // REST resources. This implementation supports multiple REST schemas and return
... ...
@@ -21,7 +38,7 @@ func (m MultiRESTMapper) VersionAndKindForResource(resource string) (defaultVers
21 21
 // RESTMapping provides the REST mapping for the resource based on the resource
22 22
 // kind and version. This implementation supports multiple REST schemas and
23 23
 // return the first match.
24
-func (m MultiRESTMapper) RESTMapping(version, kind string) (mapping *meta.RESTMapping, err error) {
24
+func (m MultiRESTMapper) RESTMapping(version, kind string) (mapping *kmeta.RESTMapping, err error) {
25 25
 	for _, t := range m {
26 26
 		mapping, err = t.RESTMapping(version, kind)
27 27
 		if err == nil {
... ...
@@ -30,3 +47,14 @@ func (m MultiRESTMapper) RESTMapping(version, kind string) (mapping *meta.RESTMa
30 30
 	}
31 31
 	return
32 32
 }
33
+
34
+// APINameForResource provides information about the API name that manages the
35
+// given resource and version.
36
+func (m MultiRESTMapper) APINameForResource(version, kind string) string {
37
+	for _, t := range OriginTypes {
38
+		if t == kind {
39
+			return OriginAPI
40
+		}
41
+	}
42
+	return KubernetesAPI
43
+}
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"testing"
5 5
 
6 6
 	"github.com/openshift/origin/pkg/api/latest"
7
+	"github.com/openshift/origin/pkg/api/meta"
7 8
 )
8 9
 
9 10
 func TestMultiRESTMapperVersionAndKindForResource(t *testing.T) {
... ...
@@ -46,3 +47,21 @@ func TestMultiRESTMapperRESTMapping(t *testing.T) {
46 46
 		t.Errorf("Expected error for 'unknown' Kind")
47 47
 	}
48 48
 }
49
+
50
+func TestObjectOwner(t *testing.T) {
51
+	mapper := latest.RESTMapper.(meta.MultiRESTMapper)
52
+
53
+	if o := mapper.APINameForResource("v1beta2", "Pod"); o != meta.KubernetesAPI {
54
+		t.Errorf("The owner of Pod resource should be kubernetes, is %s", o)
55
+	}
56
+
57
+	for _, s := range meta.OriginTypes {
58
+		if o := mapper.APINameForResource("v1beta1", s); o != meta.OriginAPI {
59
+			t.Errorf("The owner of %s resource should be origin, is %s", s, o)
60
+		}
61
+	}
62
+
63
+	if o := mapper.APINameForResource("v1beta1", "Unknown"); o != meta.KubernetesAPI {
64
+		t.Errorf("The owner of Unknown resource should be kubernetes, is %s", o)
65
+	}
66
+}
... ...
@@ -7,6 +7,10 @@ import (
7 7
 
8 8
 type RESTClient interface {
9 9
 	Verb(verb string) *client.Request
10
+	Put() *client.Request
11
+	Post() *client.Request
12
+	Delete() *client.Request
13
+	Get() *client.Request
10 14
 }
11 15
 
12 16
 type ClientMappings map[string]struct {