"io"
"net/http"
+ "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/validation"
moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module"
"github.com/gorilla/mux"
}
}
+/*
+getIntentByNameHandler handles the URL
+URL: /v2/projects/{project-name}/composite-apps/{composite-app-name}/{version}/
+deployment-intent-groups/{deployment-intent-group-name}/intents?intent=<intent>
+*/
+func (h intentHandler) getIntentByNameHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ pList := []string{"project-name", "composite-app-name", "composite-app-version", "deployment-intent-group-name"}
+ err := validation.IsValidParameterPresent(vars, pList)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
+ }
+ p := vars["project-name"]
+ ca := vars["composite-app-name"]
+ v := vars["composite-app-version"]
+ di := vars["deployment-intent-group-name"]
+
+ iN := r.URL.Query().Get("intent")
+ if iN == "" {
+ http.Error(w, "Missing appName in GET request", http.StatusBadRequest)
+ return
+ }
+
+
+ mapOfIntents, err := h.client.GetIntentByName(iN, p, ca, v, di)
+ 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(mapOfIntents)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+}
+
+/*
+getAllIntentsHandler handles the URL
+URL: /v2/projects/{project-name}/composite-apps/{composite-app-name}/{version}/
+deployment-intent-groups/{deployment-intent-group-name}/intents
+*/
+func (h intentHandler) getAllIntentsHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ pList := []string{"project-name", "composite-app-name", "composite-app-version", "deployment-intent-group-name"}
+ err := validation.IsValidParameterPresent(vars, pList)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
+ }
+
+ p := vars["project-name"]
+ ca := vars["composite-app-name"]
+ v := vars["composite-app-version"]
+ di := vars["deployment-intent-group-name"]
+
+ mapOfIntents, err := h.client.GetAllIntents(p, ca, v, di)
+ 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(mapOfIntents)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+}
+
func (h intentHandler) getIntentHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
// IntentSpecData has Intent
type IntentSpecData struct {
- Intent IntentObj `json:"intent"`
+ Intent map[string]string `json:"intent"`
}
-// IntentObj has name of the generic placement intent
-type IntentObj struct {
- Generic string `json:"generic"`
-}
// ListOfIntents is a list of intents
type ListOfIntents struct {
ListOfIntents []map[string]string `json:"intent"`
}
+
// IntentManager is an interface which exposes the IntentManager functionality
type IntentManager interface {
AddIntent(a Intent, p string, ca string, v string, di string) (Intent, error)
GetIntent(i string, p string, ca string, v string, di string) (Intent, error)
+ GetAllIntents(p, ca, v, di string) (ListOfIntents, error)
+ GetIntentByName(i, p, ca, v, di string) (IntentSpecData, error)
DeleteIntent(i string, p string, ca string, v string, di string) error
}
}
}
-// AddIntent adds a given intent to the deployment-intent-group and stores in the db. Other input parameters for it - projectName, compositeAppName, version, DeploymentIntentgroupName
+/*
+AddIntent adds a given intent to the deployment-intent-group and stores in the db.
+Other input parameters for it - projectName, compositeAppName, version, DeploymentIntentgroupName
+*/
func (c *IntentClient) AddIntent(a Intent, p string, ca string, v string, di string) (Intent, error) {
//Check for the AddIntent already exists here.
res, err := c.GetIntent(a.MetaData.Name, p, ca, v, di)
if !reflect.DeepEqual(res, Intent{}) {
- return Intent{}, pkgerrors.New("AppIntent already exists")
+ return Intent{}, pkgerrors.New("Intent already exists")
}
//Check if project exists
return a, nil
}
-// GetIntent returns an Intent
+/*
+GetIntent takes in an IntentName, ProjectName, CompositeAppName, Version and DeploymentIntentGroup.
+It returns the Intent.
+*/
func (c *IntentClient) GetIntent(i string, p string, ca string, v string, di string) (Intent, error) {
k := IntentKey{
result, err := db.DBconn.Find(c.storeName, k, c.tagMetaData)
if err != nil {
- return Intent{}, pkgerrors.Wrap(err, "Get AppIntent error")
+ return Intent{}, pkgerrors.Wrap(err, "Get Intent error")
}
if result != nil {
return a, nil
}
- return Intent{}, pkgerrors.New("Error getting AppIntent")
+ return Intent{}, pkgerrors.New("Error getting Intent")
+}
+
+
+/*
+GetIntentByName takes in IntentName, projectName, CompositeAppName, CompositeAppVersion
+and deploymentIntentGroupName returns the list of intents under the IntentName.
+*/
+func (c IntentClient) GetIntentByName(i string, p string, ca string, v string, di string) (IntentSpecData, error) {
+ k := IntentKey{
+ Name: i,
+ Project: p,
+ CompositeApp: ca,
+ Version: v,
+ DeploymentIntentGroup: di,
+ }
+ result, err := db.DBconn.Find(c.storeName, k, c.tagMetaData)
+ if err != nil {
+ return IntentSpecData{}, pkgerrors.Wrap(err, "Get AppIntent error")
+ }
+ var a Intent
+ err = db.DBconn.Unmarshal(result[0], &a)
+ if err != nil {
+ return IntentSpecData{}, pkgerrors.Wrap(err, "Unmarshalling Intent")
+ }
+ return a.Spec, nil
+}
+
+
+/*
+GetAllIntents takes in projectName, CompositeAppName, CompositeAppVersion,
+DeploymentIntentName . It returns ListOfIntents.
+*/
+func (c IntentClient) GetAllIntents(p string, ca string, v string, di string) (ListOfIntents, error) {
+ k := IntentKey{
+ Name: "",
+ Project: p,
+ CompositeApp: ca,
+ Version: v,
+ DeploymentIntentGroup: di,
+ }
+
+ result, err := db.DBconn.Find(c.storeName, k, c.tagMetaData)
+ if err != nil {
+ return ListOfIntents{}, pkgerrors.Wrap(err, "Get AppIntent error")
+ }
+ var a Intent
+ var listOfMapOfIntents []map[string]string
+
+ if len(result) != 0 {
+ for i := range result {
+ a = Intent{}
+ err = db.DBconn.Unmarshal(result[i], &a)
+ if err != nil {
+ return ListOfIntents{}, pkgerrors.Wrap(err, "Unmarshalling Intent")
+ }
+ //mapOfIntents := ListOfIntents{a.Spec.Intent.ListOfIntents}
+ listOfMapOfIntents = append(listOfMapOfIntents, a.Spec.Intent)
+ }
+ return ListOfIntents{listOfMapOfIntents}, nil
+ }
+ return ListOfIntents{}, err
}
+
// DeleteIntent deletes a given intent tied to project, composite app and deployment intent group
func (c IntentClient) DeleteIntent(i string, p string, ca string, v string, di string) error {
k := IntentKey{
return pkgerrors.Wrap(err, "Delete Project entry;")
}
return nil
-
}