diff options
Diffstat (limited to 'src/orchestrator/pkg/module/deployment_intent_groups.go')
-rw-r--r-- | src/orchestrator/pkg/module/deployment_intent_groups.go | 130 |
1 files changed, 127 insertions, 3 deletions
diff --git a/src/orchestrator/pkg/module/deployment_intent_groups.go b/src/orchestrator/pkg/module/deployment_intent_groups.go index 16a14c7b..f8e434f4 100644 --- a/src/orchestrator/pkg/module/deployment_intent_groups.go +++ b/src/orchestrator/pkg/module/deployment_intent_groups.go @@ -19,8 +19,11 @@ package module import ( "encoding/json" "reflect" + "time" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/state" pkgerrors "github.com/pkg/errors" ) @@ -44,6 +47,7 @@ type DepSpecData struct { Profile string `json:"profile"` Version string `json:"version"` OverrideValuesObj []OverrideValues `json:"override-values"` + LogicalCloud string `json:"logical-cloud"` } // OverrideValues has appName and ValuesObj @@ -61,7 +65,9 @@ type OverrideValues struct { type DeploymentIntentGroupManager interface { CreateDeploymentIntentGroup(d DeploymentIntentGroup, p string, ca string, v string) (DeploymentIntentGroup, error) GetDeploymentIntentGroup(di string, p string, ca string, v string) (DeploymentIntentGroup, error) + GetDeploymentIntentGroupState(di string, p string, ca string, v string) (state.StateInfo, error) DeleteDeploymentIntentGroup(di string, p string, ca string, v string) error + GetAllDeploymentIntentGroups(p string, ca string, v string) ([]DeploymentIntentGroup, error) } // DeploymentIntentGroupKey consists of Name of the deployment group, project name, CompositeApp name, CompositeApp version @@ -86,6 +92,7 @@ func (dk DeploymentIntentGroupKey) String() string { type DeploymentIntentGroupClient struct { storeName string tagMetaData string + tagState string } // NewDeploymentIntentGroupClient return an instance of DeploymentIntentGroupClient which implements DeploymentIntentGroupManager @@ -93,6 +100,7 @@ func NewDeploymentIntentGroupClient() *DeploymentIntentGroupClient { return &DeploymentIntentGroupClient{ storeName: "orchestrator", tagMetaData: "deploymentintentgroupmetadata", + tagState: "stateInfo", } } @@ -102,7 +110,7 @@ func (c *DeploymentIntentGroupClient) CreateDeploymentIntentGroup(d DeploymentIn res, err := c.GetDeploymentIntentGroup(d.MetaData.Name, p, ca, v) if !reflect.DeepEqual(res, DeploymentIntentGroup{}) { - return DeploymentIntentGroup{}, pkgerrors.New("AppIntent already exists") + return DeploymentIntentGroup{}, pkgerrors.New("DeploymentIntent already exists") } //Check if project exists @@ -129,6 +137,20 @@ func (c *DeploymentIntentGroupClient) CreateDeploymentIntentGroup(d DeploymentIn return DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Create DB entry error") } + // Add the stateInfo record + s := state.StateInfo{} + a := state.ActionEntry{ + State: state.StateEnum.Created, + ContextId: "", + TimeStamp: time.Now(), + } + s.Actions = append(s.Actions, a) + + err = db.DBconn.Insert(c.storeName, gkey, nil, c.tagState, s) + if err != nil { + return DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Error updating the stateInfo of the DeploymentIntentGroup: "+d.MetaData.Name) + } + return d, nil } @@ -160,6 +182,73 @@ func (c *DeploymentIntentGroupClient) GetDeploymentIntentGroup(di string, p stri } +// GetAllDeploymentIntentGroups returns all the deploymentIntentGroups under a specific project, compositeApp and version +func (c *DeploymentIntentGroupClient) GetAllDeploymentIntentGroups(p string, ca string, v string) ([]DeploymentIntentGroup, error) { + + key := DeploymentIntentGroupKey{ + Name: "", + Project: p, + CompositeApp: ca, + Version: v, + } + + //Check if project exists + _, err := NewProjectClient().GetProject(p) + if err != nil { + return []DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Unable to find the project") + } + + //check if compositeApp exists + _, err = NewCompositeAppClient().GetCompositeApp(ca, v, p) + if err != nil { + return []DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Unable to find the composite-app, check CompositeAppName and Version") + } + var diList []DeploymentIntentGroup + result, err := db.DBconn.Find(c.storeName, key, c.tagMetaData) + if err != nil { + return []DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Get DeploymentIntentGroup error") + } + + for _, value := range result { + di := DeploymentIntentGroup{} + err = db.DBconn.Unmarshal(value, &di) + if err != nil { + return []DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Unmarshaling DeploymentIntentGroup") + } + diList = append(diList, di) + } + + return diList, nil + +} + +// GetDeploymentIntentGroupState returns the AppContent with a given DeploymentIntentname, project, compositeAppName and version of compositeApp +func (c *DeploymentIntentGroupClient) GetDeploymentIntentGroupState(di string, p string, ca string, v string) (state.StateInfo, error) { + + key := DeploymentIntentGroupKey{ + Name: di, + Project: p, + CompositeApp: ca, + Version: v, + } + + result, err := db.DBconn.Find(c.storeName, key, c.tagState) + if err != nil { + return state.StateInfo{}, pkgerrors.Wrap(err, "Get DeploymentIntentGroup StateInfo error") + } + + if result != nil { + s := state.StateInfo{} + err = db.DBconn.Unmarshal(result[0], &s) + if err != nil { + return state.StateInfo{}, pkgerrors.Wrap(err, "Unmarshalling DeploymentIntentGroup StateInfo") + } + return s, nil + } + + return state.StateInfo{}, pkgerrors.New("Error getting DeploymentIntentGroup StateInfo") +} + // DeleteDeploymentIntentGroup deletes a DeploymentIntentGroup func (c *DeploymentIntentGroupClient) DeleteDeploymentIntentGroup(di string, p string, ca string, v string) error { k := DeploymentIntentGroupKey{ @@ -168,10 +257,45 @@ func (c *DeploymentIntentGroupClient) DeleteDeploymentIntentGroup(di string, p s CompositeApp: ca, Version: v, } + s, err := c.GetDeploymentIntentGroupState(di, p, ca, v) + if err != nil { + return pkgerrors.Errorf("Error getting stateInfo from DeploymentIntentGroup: " + di) + } + + stateVal, err := state.GetCurrentStateFromStateInfo(s) + if err != nil { + return pkgerrors.Errorf("Error getting current state from DeploymentIntentGroup stateInfo: " + di) + } + + if stateVal == state.StateEnum.Instantiated { + return pkgerrors.Errorf("DeploymentIntentGroup must be terminated before it can be deleted " + di) + } + + // remove the app contexts associated with thie Deployment Intent Group + if stateVal == state.StateEnum.Terminated { + // Verify that the appcontext has completed terminating + ctxid := state.GetLastContextIdFromStateInfo(s) + acStatus, err := state.GetAppContextStatus(ctxid) + if err == nil && + !(acStatus.Status == appcontext.AppContextStatusEnum.Terminated || acStatus.Status == appcontext.AppContextStatusEnum.TerminateFailed) { + return pkgerrors.Errorf("DeploymentIntentGroup has not completed terminating: " + di) + } + + for _, id := range state.GetContextIdsFromStateInfo(s) { + context, err := state.GetAppContextFromId(id) + if err != nil { + return pkgerrors.Wrap(err, "Error getting appcontext from Deployment Intent Group StateInfo") + } + err = context.DeleteCompositeApp() + if err != nil { + return pkgerrors.Wrap(err, "Error deleting appcontext for Deployment Intent Group") + } + } + } - err := db.DBconn.Remove(c.storeName, k) + err = db.DBconn.Remove(c.storeName, k) if err != nil { - return pkgerrors.Wrap(err, "Delete DeploymentIntentGroup entry;") + return pkgerrors.Wrap(err, "Error deleting DeploymentIntentGroup entry") } return nil |