Use project name provided by API in DCM 43/113343/2
authorIgor D.C <igor.duarte.cardoso@intel.com>
Tue, 29 Sep 2020 19:00:37 +0000 (19:00 +0000)
committerIgor D.C <igor.duarte.cardoso@intel.com>
Tue, 29 Sep 2020 23:45:47 +0000 (23:45 +0000)
DCM apply/terminate code was still using a hardcoded "test-project"
string for the project name that logical clouds should associate to.

This simple patch makes DCM use the project name provided by the API.

It also checks that the project exists before creating a Logical Cloud.

Issue-ID: MULTICLOUD-1143
Change-Id: I05c8a2309ed07a7c96da30bf2461c43abaccc9d5
Signed-off-by: Igor D.C <igor.duarte.cardoso@intel.com>
src/dcm/api/logicalCloudHandler.go
src/dcm/go.sum
src/dcm/pkg/module/apply.go
src/dcm/pkg/module/logicalcloud.go

index fb0f0c6..b5161ef 100644 (file)
@@ -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"
 )
 
@@ -59,6 +60,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)
@@ -186,7 +196,7 @@ func (h logicalCloudHandler) applyHandler(w http.ResponseWriter, r *http.Request
                return
        }
 
-       _, ctxVal, err := h.client.GetLogicalCloudContext(name)
+       _, ctxVal, err := h.client.GetLogicalCloudContext(project, name)
        if ctxVal != "" {
                err = pkgerrors.New("Logical Cloud already applied")
                http.Error(w, err.Error(), http.StatusConflict)
@@ -212,7 +222,7 @@ func (h logicalCloudHandler) applyHandler(w http.ResponseWriter, r *http.Request
                return
        }
 
-       err = module.CreateEtcdContext(lc, clusters, quotas)
+       err = module.CreateEtcdContext(project, lc, clusters, quotas)
        if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
@@ -237,7 +247,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)
@@ -259,7 +269,7 @@ func (h logicalCloudHandler) terminateHandler(w http.ResponseWriter, r *http.Req
                return
        }
 
-       err = module.DestroyEtcdContext(lc, clusters, quotas)
+       err = module.DestroyEtcdContext(project, lc, clusters, quotas)
        if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
index 983ceae..e858252 100644 (file)
@@ -807,6 +807,7 @@ github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn
 github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
 github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
 github.com/olekukonko/tablewriter v0.0.2/go.mod h1:rSAaSIOAGT9odnlyGlUfAJaoc5w2fSBUmeGDbRWPxyQ=
+github.com/onap/multicloud-k8s v0.0.0-20200928235143-603a68284970 h1:yWZDIjZBhwtbV7+fa8QB/WhPlHCR4qBhY2OG7K83wGs=
 github.com/onap/multicloud-k8s/src/ncm v0.0.0-20200515060444-c77850a75eee/go.mod h1:q6s8c45A2NN2V4lxciJ7OmCZFaS1uQSWaGxGG3UM3kM=
 github.com/onap/multicloud-k8s/src/rsync v0.0.0-20200630152613-7c20f73e7c5d h1:0aXmwqPN8MjyqjKK5L1IhhP/hpP5nGj6xMgo6AOzCPI=
 github.com/onap/multicloud-k8s/src/rsync v0.0.0-20200630152613-7c20f73e7c5d/go.mod h1:pVhhvg5N0Qy8QDJkYRnWCQbxLDV5GYLmPyzlndbGx7w=
index a866934..0b370f4 100644 (file)
@@ -305,12 +305,11 @@ func callRsyncUninstall(contextid interface{}) error {
        return nil
 }
 
-func CreateEtcdContext(logicalcloud LogicalCloud, clusterList []Cluster,
+func CreateEtcdContext(project string, logicalcloud LogicalCloud, clusterList []Cluster,
        quotaList []Quota) error {
 
        APP := "logical-cloud"
        logicalCloudName := logicalcloud.MetaData.LogicalCloudName
-       project := "test-project" // FIXME(igordc): temporary, need to do some rework in the LC structs
 
        //Resource Names
        namespaceName := strings.Join([]string{logicalcloud.MetaData.LogicalCloudName, "+namespace"}, "")
@@ -599,13 +598,12 @@ func CreateEtcdContext(logicalcloud LogicalCloud, clusterList []Cluster,
 
 // TODO: rename these methods
 // DestroyEtcdContext remove from rsync then delete appcontext and all resources
-func DestroyEtcdContext(logicalcloud LogicalCloud, clusterList []Cluster,
+func DestroyEtcdContext(project string, logicalcloud LogicalCloud, clusterList []Cluster,
        quotaList []Quota) error {
 
        logicalCloudName := logicalcloud.MetaData.LogicalCloudName
-       // project := "test-project" // FIXME(igordc): temporary, need to do some rework in the LC structs
 
-       _, ctxVal, err := NewLogicalCloudClient().GetLogicalCloudContext(logicalCloudName)
+       _, ctxVal, err := NewLogicalCloudClient().GetLogicalCloudContext(project, logicalCloudName)
        if err != nil {
                return pkgerrors.Wrapf(err, "Error finding AppContext for Logical Cloud: %v", logicalCloudName)
        }
index 61d7b7a..49a2a63 100644 (file)
@@ -73,7 +73,7 @@ type LogicalCloudManager interface {
        GetAll(project string) ([]LogicalCloud, error)
        Delete(project, name string) error
        Update(project, name string, c LogicalCloud) (LogicalCloud, error)
-       GetLogicalCloudContext(name string) (appcontext.AppContext, string, error)
+       GetLogicalCloudContext(project string, name string) (appcontext.AppContext, string, error)
 }
 
 // Interface facilitates unit testing by mocking functions
@@ -236,11 +236,11 @@ func (v *LogicalCloudClient) Update(project, logicalCloudName string, c LogicalC
 }
 
 // GetClusterContext returns the AppContext for corresponding provider and name
-func (v *LogicalCloudClient) GetLogicalCloudContext(name string) (appcontext.AppContext, string, error) {
+func (v *LogicalCloudClient) GetLogicalCloudContext(project string, name string) (appcontext.AppContext, string, error) {
        //Construct key and tag to select the entry
        key := LogicalCloudKey{
                LogicalCloudName: name,
-               Project:          "test-project", // FIXME(igordc): temporary, need to do some rework in the LC structs
+               Project:          project,
        }
 
        value, err := db.DBconn.Find(v.storeName, key, v.tagContext)