diff options
Diffstat (limited to 'src/dcm/api')
-rw-r--r-- | src/dcm/api/api.go | 29 | ||||
-rw-r--r-- | src/dcm/api/clusterHandler.go | 92 | ||||
-rw-r--r-- | src/dcm/api/keyValueHandler.go | 45 | ||||
-rw-r--r-- | src/dcm/api/logicalCloudHandler.go | 90 | ||||
-rw-r--r-- | src/dcm/api/quotaHandler.go | 50 | ||||
-rw-r--r-- | src/dcm/api/userPermissionsHandler.go | 47 |
6 files changed, 241 insertions, 112 deletions
diff --git a/src/dcm/api/api.go b/src/dcm/api/api.go index 0f68a517..10856ba2 100644 --- a/src/dcm/api/api.go +++ b/src/dcm/api/api.go @@ -21,7 +21,6 @@ import ( // NewRouter creates a router that registers the various urls that are // supported - func NewRouter( logicalCloudClient module.LogicalCloudManager, clusterClient module.ClusterManager, @@ -55,7 +54,7 @@ func NewRouter( logicalCloudHandler.createHandler).Methods("POST") lcRouter.HandleFunc( "/logical-clouds", - logicalCloudHandler.getHandler).Methods("GET") + logicalCloudHandler.getAllHandler).Methods("GET") lcRouter.HandleFunc( "/logical-clouds/{logical-cloud-name}", logicalCloudHandler.getHandler).Methods("GET") @@ -71,18 +70,8 @@ func NewRouter( lcRouter.HandleFunc( "/logical-clouds/{logical-cloud-name}/terminate", logicalCloudHandler.terminateHandler).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( @@ -90,7 +79,7 @@ func NewRouter( clusterHandler.createHandler).Methods("POST") clusterRouter.HandleFunc( "/logical-clouds/{logical-cloud-name}/cluster-references", - clusterHandler.getHandler).Methods("GET") + clusterHandler.getAllHandler).Methods("GET") clusterRouter.HandleFunc( "/logical-clouds/{logical-cloud-name}/cluster-references/{cluster-reference}", clusterHandler.getHandler).Methods("GET") @@ -100,6 +89,10 @@ func NewRouter( clusterRouter.HandleFunc( "/logical-clouds/{logical-cloud-name}/cluster-references/{cluster-reference}", clusterHandler.deleteHandler).Methods("DELETE") + // Get kubeconfig for cluster of logical cloud + clusterRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/cluster-references/{cluster-reference}/kubeconfig", + clusterHandler.getConfigHandler).Methods("GET") // Set up User Permission API if userPermissionClient == nil { @@ -111,6 +104,9 @@ func NewRouter( "/logical-clouds/{logical-cloud-name}/user-permissions", userPermissionHandler.createHandler).Methods("POST") upRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/user-permissions", + userPermissionHandler.getAllHandler).Methods("GET") + upRouter.HandleFunc( "/logical-clouds/{logical-cloud-name}/user-permissions/{permission-name}", userPermissionHandler.getHandler).Methods("GET") upRouter.HandleFunc( @@ -121,13 +117,15 @@ func NewRouter( 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", + quotaHandler.getAllHandler).Methods("GET") + quotaRouter.HandleFunc( "/logical-clouds/{logical-cloud-name}/cluster-quotas/{quota-name}", quotaHandler.getHandler).Methods("GET") quotaRouter.HandleFunc( @@ -147,6 +145,9 @@ func NewRouter( "/logical-clouds/{logical-cloud-name}/kv-pairs", keyValueHandler.createHandler).Methods("POST") kvRouter.HandleFunc( + "/logical-clouds/{logical-cloud-name}/kv-pairs", + keyValueHandler.getAllHandler).Methods("GET") + kvRouter.HandleFunc( "/logical-clouds/{logical-cloud-name}/kv-pairs/{kv-pair-name}", keyValueHandler.getHandler).Methods("GET") kvRouter.HandleFunc( diff --git a/src/dcm/api/clusterHandler.go b/src/dcm/api/clusterHandler.go index d0c1e62c..1201611f 100644 --- a/src/dcm/api/clusterHandler.go +++ b/src/dcm/api/clusterHandler.go @@ -23,9 +23,8 @@ import ( "io" "net/http" - "github.com/onap/multicloud-k8s/src/dcm/pkg/module" - "github.com/gorilla/mux" + "github.com/onap/multicloud-k8s/src/dcm/pkg/module" ) // clusterHandler is used to store backend implementations objects @@ -33,8 +32,7 @@ type clusterHandler struct { client module.ClusterManager } -// CreateHandler handles creation of the cluster reference entry in the database - +// createHandler handles creation of the cluster reference entry in the database func (h clusterHandler) createHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) project := vars["project-name"] @@ -72,7 +70,31 @@ func (h clusterHandler) createHandler(w http.ResponseWriter, r *http.Request) { } } -// getHandler handle GET operations on a particular name +// getAllHandler handles GET operations over cluster references +// Returns a list of Cluster References +func (h clusterHandler) getAllHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + var ret interface{} + var err error + + ret, err = h.client.GetAllClusters(project, logicalCloud) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + +// getHandler handles GET operations on a particular name // Returns a Cluster Reference func (h clusterHandler) getHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) @@ -82,22 +104,14 @@ func (h clusterHandler) getHandler(w http.ResponseWriter, r *http.Request) { var ret interface{} var err error - if len(name) == 0 { - ret, err = h.client.GetAllClusters(project, logicalCloud) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } else { - ret, err = h.client.GetCluster(project, logicalCloud, name) - if err != nil { - if err.Error() == "Cluster Reference does not exist" { - http.Error(w, err.Error(), http.StatusNotFound) - return - } - http.Error(w, err.Error(), http.StatusInternalServerError) + ret, err = h.client.GetCluster(project, logicalCloud, name) + if err != nil { + if err.Error() == "Cluster Reference does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) return } + http.Error(w, err.Error(), http.StatusInternalServerError) + return } w.Header().Set("Content-Type", "application/json") @@ -168,3 +182,43 @@ func (h clusterHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) } + +// getConfigHandler handles GET operations on kubeconfigs +// Returns a kubeconfig file +func (h clusterHandler) getConfigHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["cluster-reference"] + var err error + + _, err = h.client.GetCluster(project, logicalCloud, name) + if err != nil { + if err.Error() == "Cluster Reference does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) + } else { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + return + } + + cfg, err := h.client.GetClusterConfig(project, logicalCloud, name) + if err != nil { + if err.Error() == "The certificate for this cluster hasn't been issued yet. Please try later." { + http.Error(w, err.Error(), http.StatusAccepted) + } else if err.Error() == "Logical Cloud hasn't been applied yet" { + http.Error(w, err.Error(), http.StatusBadRequest) + } else { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + return + } + + w.Header().Set("Content-Type", "application/yaml") + w.WriteHeader(http.StatusOK) + _, err = io.WriteString(w, cfg) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} diff --git a/src/dcm/api/keyValueHandler.go b/src/dcm/api/keyValueHandler.go index a4a4f14a..69333efb 100644 --- a/src/dcm/api/keyValueHandler.go +++ b/src/dcm/api/keyValueHandler.go @@ -33,7 +33,6 @@ type keyValueHandler struct { } // CreateHandler handles creation of the key value entry in the database - func (h keyValueHandler) createHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) project := vars["project-name"] @@ -71,6 +70,30 @@ func (h keyValueHandler) createHandler(w http.ResponseWriter, r *http.Request) { } } +// getHandler handles GET operations over key-value pairs +// Returns a list of Key Values +func (h keyValueHandler) getAllHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + var ret interface{} + var err error + + ret, err = h.client.GetAllKVPairs(project, logicalCloud) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + // getHandler handle GET operations on a particular name // Returns a Key Value func (h keyValueHandler) getHandler(w http.ResponseWriter, r *http.Request) { @@ -81,22 +104,14 @@ func (h keyValueHandler) getHandler(w http.ResponseWriter, r *http.Request) { var ret interface{} var err error - if len(name) == 0 { - ret, err = h.client.GetAllKVPairs(project, logicalCloud) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } else { - ret, err = h.client.GetKVPair(project, logicalCloud, name) - if err != nil { - if err.Error() == "KV Pair does not exist" { - http.Error(w, err.Error(), http.StatusNotFound) - return - } - http.Error(w, err.Error(), http.StatusInternalServerError) + ret, err = h.client.GetKVPair(project, logicalCloud, name) + if err != nil { + if err.Error() == "KV Pair does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) return } + http.Error(w, err.Error(), http.StatusInternalServerError) + return } w.Header().Set("Content-Type", "application/json") diff --git a/src/dcm/api/logicalCloudHandler.go b/src/dcm/api/logicalCloudHandler.go index fb0f0c63..b305b202 100644 --- a/src/dcm/api/logicalCloudHandler.go +++ b/src/dcm/api/logicalCloudHandler.go @@ -25,6 +25,7 @@ import ( "github.com/gorilla/mux" "github.com/onap/multicloud-k8s/src/dcm/pkg/module" + orch "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module" pkgerrors "github.com/pkg/errors" ) @@ -35,8 +36,7 @@ type logicalCloudHandler struct { quotaClient module.QuotaManager } -// CreateHandler handles creation of the logical cloud entry in the database - +// CreateHandler handles the creation of a logical cloud func (h logicalCloudHandler) createHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) @@ -59,6 +59,15 @@ func (h logicalCloudHandler) createHandler(w http.ResponseWriter, r *http.Reques return } + // Validate that the specified Project exists + // before associating a Logical Cloud with it + p := orch.NewProjectClient() + _, err = p.GetProject(project) + if err != nil { + http.Error(w, "The specified project does not exist.", http.StatusNotFound) + return + } + ret, err := h.client.Create(project, v) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -74,7 +83,30 @@ func (h logicalCloudHandler) createHandler(w http.ResponseWriter, r *http.Reques } } -// getHandler handle GET operations on a particular name +// getAllHandler handles GET operations over logical clouds +// Returns a list of Logical Clouds +func (h logicalCloudHandler) getAllHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + project := vars["project-name"] + var ret interface{} + var err error + + ret, err = h.client.GetAll(project) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + +// getHandler handles GET operations on a particular name // Returns a Logical Cloud func (h logicalCloudHandler) getHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) @@ -83,22 +115,14 @@ func (h logicalCloudHandler) getHandler(w http.ResponseWriter, r *http.Request) var ret interface{} var err error - if len(name) == 0 { - ret, err = h.client.GetAll(project) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } else { - ret, err = h.client.Get(project, name) - if err != nil { - if err.Error() == "Logical Cloud does not exist" { - http.Error(w, err.Error(), http.StatusNotFound) - return - } - http.Error(w, err.Error(), http.StatusInternalServerError) + ret, err = h.client.Get(project, name) + if err != nil { + if err.Error() == "Logical Cloud does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) return } + http.Error(w, err.Error(), http.StatusInternalServerError) + return } w.Header().Set("Content-Type", "application/json") @@ -110,7 +134,7 @@ func (h logicalCloudHandler) getHandler(w http.ResponseWriter, r *http.Request) } } -// UpdateHandler handles Update operations on a particular logical cloud +// updateHandler handles Update operations on a particular logical cloud func (h logicalCloudHandler) updateHandler(w http.ResponseWriter, r *http.Request) { var v module.LogicalCloud vars := mux.Vars(r) @@ -152,6 +176,7 @@ func (h logicalCloudHandler) updateHandler(w http.ResponseWriter, r *http.Reques } } +// deleteHandler handles Delete operations on a particular logical cloud func (h logicalCloudHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) project := vars["project-name"] @@ -163,6 +188,10 @@ func (h logicalCloudHandler) deleteHandler(w http.ResponseWriter, r *http.Reques http.Error(w, err.Error(), http.StatusNotFound) return } + if err.Error() == "The Logical Cloud can't be deleted yet, it is being terminated." { + http.Error(w, err.Error(), http.StatusConflict) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -170,6 +199,7 @@ func (h logicalCloudHandler) deleteHandler(w http.ResponseWriter, r *http.Reques w.WriteHeader(http.StatusNoContent) } +// applyHandler handles applying a particular logical cloud func (h logicalCloudHandler) applyHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) project := vars["project-name"] @@ -186,13 +216,6 @@ func (h logicalCloudHandler) applyHandler(w http.ResponseWriter, r *http.Request return } - _, ctxVal, err := h.client.GetLogicalCloudContext(name) - if ctxVal != "" { - err = pkgerrors.New("Logical Cloud already applied") - http.Error(w, err.Error(), http.StatusConflict) - return - } - // Get Clusters clusters, err := h.clusterClient.GetAllClusters(project, name) @@ -205,15 +228,20 @@ func (h logicalCloudHandler) applyHandler(w http.ResponseWriter, r *http.Request return } - //Get Quotas + // Get Quotas quotas, err := h.quotaClient.GetAllQuotas(project, name) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - err = module.CreateEtcdContext(lc, clusters, quotas) + // Apply the Logical Cloud + err = module.Apply(project, lc, clusters, quotas) if err != nil { + if err.Error() == "The Logical Cloud can't be re-applied yet, it is being terminated." { + http.Error(w, err.Error(), http.StatusConflict) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -221,6 +249,7 @@ func (h logicalCloudHandler) applyHandler(w http.ResponseWriter, r *http.Request return } +// applyHandler handles terminating a particular logical cloud func (h logicalCloudHandler) terminateHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) project := vars["project-name"] @@ -237,7 +266,7 @@ func (h logicalCloudHandler) terminateHandler(w http.ResponseWriter, r *http.Req return } - _, ctxVal, err := h.client.GetLogicalCloudContext(name) + _, ctxVal, err := h.client.GetLogicalCloudContext(project, name) if ctxVal == "" { err = pkgerrors.New("Logical Cloud hasn't been applied yet") http.Error(w, err.Error(), http.StatusConflict) @@ -252,14 +281,15 @@ func (h logicalCloudHandler) terminateHandler(w http.ResponseWriter, r *http.Req return } - //Get Quotas + // Get Quotas quotas, err := h.quotaClient.GetAllQuotas(project, name) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - err = module.DestroyEtcdContext(lc, clusters, quotas) + // Terminate the Logical Cloud + err = module.Terminate(project, lc, clusters, quotas) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/src/dcm/api/quotaHandler.go b/src/dcm/api/quotaHandler.go index fd9b40f8..1f0e45a5 100644 --- a/src/dcm/api/quotaHandler.go +++ b/src/dcm/api/quotaHandler.go @@ -23,9 +23,8 @@ import ( "io" "net/http" - "github.com/onap/multicloud-k8s/src/dcm/pkg/module" - "github.com/gorilla/mux" + "github.com/onap/multicloud-k8s/src/dcm/pkg/module" ) // quotaHandler is used to store backend implementations objects @@ -34,7 +33,6 @@ type quotaHandler struct { } // CreateHandler handles creation of the quota entry in the database - func (h quotaHandler) createHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) project := vars["project-name"] @@ -72,8 +70,32 @@ func (h quotaHandler) createHandler(w http.ResponseWriter, r *http.Request) { } } +// getHandler handles GET operations over quotas +// Returns a list of Quotas +func (h quotaHandler) getAllHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + var ret interface{} + var err error + + ret, err = h.client.GetAllQuotas(project, logicalCloud) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + // getHandler handle GET operations on a particular name -// Returns a quota +// Returns a Quota func (h quotaHandler) getHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) project := vars["project-name"] @@ -82,22 +104,14 @@ func (h quotaHandler) getHandler(w http.ResponseWriter, r *http.Request) { var ret interface{} var err error - if len(name) == 0 { - ret, err = h.client.GetAllQuotas(project, logicalCloud) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } else { - ret, err = h.client.GetQuota(project, logicalCloud, name) - if err != nil { - if err.Error() == "Cluster Quota does not exist" { - http.Error(w, err.Error(), http.StatusNotFound) - return - } - http.Error(w, err.Error(), http.StatusInternalServerError) + ret, err = h.client.GetQuota(project, logicalCloud, name) + if err != nil { + if err.Error() == "Cluster Quota does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) return } + http.Error(w, err.Error(), http.StatusInternalServerError) + return } w.Header().Set("Content-Type", "application/json") diff --git a/src/dcm/api/userPermissionsHandler.go b/src/dcm/api/userPermissionsHandler.go index 3ac955fa..6d88f573 100644 --- a/src/dcm/api/userPermissionsHandler.go +++ b/src/dcm/api/userPermissionsHandler.go @@ -33,7 +33,6 @@ type userPermissionHandler struct { } // CreateHandler handles creation of the user permission entry in the database - func (h userPermissionHandler) createHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) @@ -72,7 +71,31 @@ func (h userPermissionHandler) createHandler(w http.ResponseWriter, r *http.Requ } } -// getHandler handle GET operations on a particular name +// getAllHandler handles GET operations over user permissions +// Returns a list of User Permissions +func (h userPermissionHandler) getAllHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + var ret interface{} + var err error + + ret, err = h.client.GetAllUserPerms(project, logicalCloud) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + +// getHandler handles GET operations on a particular name // Returns a User Permission func (h userPermissionHandler) getHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) @@ -82,22 +105,14 @@ func (h userPermissionHandler) getHandler(w http.ResponseWriter, r *http.Request var ret interface{} var err error - if len(name) == 0 { - ret, err = h.client.GetAllUserPerms(project, logicalCloud) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } else { - ret, err = h.client.GetUserPerm(project, logicalCloud, name) - if err != nil { - if err.Error() == "User Permission does not exist" { - http.Error(w, err.Error(), http.StatusNotFound) - return - } - http.Error(w, err.Error(), http.StatusInternalServerError) + ret, err = h.client.GetUserPerm(project, logicalCloud, name) + if err != nil { + if err.Error() == "User Permission does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) return } + http.Error(w, err.Error(), http.StatusInternalServerError) + return } w.Header().Set("Content-Type", "application/json") |