summaryrefslogtreecommitdiffstats
path: root/src/dcm/api/api.go
blob: de1d5c970fbbc05cc1e9100389d4fe94a163451a (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Copyright 2020 Intel Corporation.
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 api

import (
	"github.com/onap/multicloud-k8s/src/dcm/pkg/module"

	"github.com/gorilla/mux"
)

// NewRouter creates a router that registers the various urls that are
// supported

func NewRouter(
	logicalCloudClient module.LogicalCloudManager,
	clusterClient module.ClusterManager,
	userPermissionClient module.UserPermissionManager,
	quotaClient module.QuotaManager,
	keyValueClient module.KeyValueManager) *mux.Router {

	router := mux.NewRouter()

	// Set up Logical Cloud handler routes
	if logicalCloudClient == nil {
		logicalCloudClient = module.NewLogicalCloudClient()
	}

	if clusterClient == nil {
		clusterClient = module.NewClusterClient()
	}

	if quotaClient == nil {
		quotaClient = module.NewQuotaClient()
	}

	logicalCloudHandler := logicalCloudHandler{client: logicalCloudClient,
		clusterClient: clusterClient,
		quotaClient:   quotaClient,
	}
	lcRouter := router.PathPrefix("/v2/projects/{project-name}").Subrouter()
	lcRouter.HandleFunc(
		"/logical-clouds",
		logicalCloudHandler.createHandler).Methods("POST")
	lcRouter.HandleFunc(
		"/logical-clouds",
		logicalCloudHandler.getHandler).Methods("GET")
	lcRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}",
		logicalCloudHandler.getHandler).Methods("GET")
	lcRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}",
		logicalCloudHandler.deleteHandler).Methods("DELETE")
	lcRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}",
		logicalCloudHandler.updateHandler).Methods("PUT")
	lcRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/apply",
		logicalCloudHandler.applyHandler).Methods("POST")
	// To Do
	// get kubeconfig
	/*lcRouter.HandleFunc(
	       "/logical-clouds/{name}/kubeconfig?cluster-reference={cluster}",
	       logicalCloudHandler.getConfigHandler).Methods("GET")
	  //get status
	  lcRouter.HandleFunc(
	      "/logical-clouds/{name}/cluster-references/",
	      logicalCloudHandler.associateHandler).Methods("GET")*/

	// Set up Cluster API

	clusterHandler := clusterHandler{client: clusterClient}
	clusterRouter := router.PathPrefix("/v2/projects/{project-name}").Subrouter()
	clusterRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/cluster-references",
		clusterHandler.createHandler).Methods("POST")
	clusterRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/cluster-references",
		clusterHandler.getHandler).Methods("GET")
	clusterRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/cluster-references/{cluster-reference}",
		clusterHandler.getHandler).Methods("GET")
	clusterRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/cluster-references/{cluster-reference}",
		clusterHandler.updateHandler).Methods("PUT")
	clusterRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/cluster-references/{cluster-reference}",
		clusterHandler.deleteHandler).Methods("DELETE")

	// Set up User Permission API
	if userPermissionClient == nil {
		userPermissionClient = module.NewUserPermissionClient()
	}
	userPermissionHandler := userPermissionHandler{client: userPermissionClient}
	upRouter := router.PathPrefix("/v2/projects/{project-name}").Subrouter()
	upRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/user-permissions",
		userPermissionHandler.createHandler).Methods("POST")
	upRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/user-permissions/{permission-name}",
		userPermissionHandler.getHandler).Methods("GET")
	upRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/user-permissions/{permission-name}",
		userPermissionHandler.updateHandler).Methods("PUT")
	upRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/user-permissions/{permission-name}",
		userPermissionHandler.deleteHandler).Methods("DELETE")

	// Set up Quota API

	quotaHandler := quotaHandler{client: quotaClient}
	quotaRouter := router.PathPrefix("/v2/projects/{project-name}").Subrouter()
	quotaRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/cluster-quotas",
		quotaHandler.createHandler).Methods("POST")
	quotaRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/cluster-quotas/{quota-name}",
		quotaHandler.getHandler).Methods("GET")
	quotaRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/cluster-quotas/{quota-name}",
		quotaHandler.updateHandler).Methods("PUT")
	quotaRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/cluster-quotas/{quota-name}",
		quotaHandler.deleteHandler).Methods("DELETE")

	// Set up Key Value API
	if keyValueClient == nil {
		keyValueClient = module.NewKeyValueClient()
	}
	keyValueHandler := keyValueHandler{client: keyValueClient}
	kvRouter := router.PathPrefix("/v2/projects/{project-name}").Subrouter()
	kvRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/kv-pairs",
		keyValueHandler.createHandler).Methods("POST")
	kvRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/kv-pairs/{kv-pair-name}",
		keyValueHandler.getHandler).Methods("GET")
	kvRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/kv-pairs/{kv-pair-name}",
		keyValueHandler.updateHandler).Methods("PUT")
	kvRouter.HandleFunc(
		"/logical-clouds/{logical-cloud-name}/kv-pairs/{kv-pair-name}",
		keyValueHandler.deleteHandler).Methods("DELETE")
	return router
}