From: abhijit_onap Date: Mon, 25 May 2020 04:04:25 +0000 (+0530) Subject: Added model files for the Rest Messages X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=commitdiff_plain;h=caf01eef6ab04caccb08847930d2729794784b64;p=ealt-edge.git Added model files for the Rest Messages Modifed the CLI command for Application Management and Application Life Cycle Management. Log optimisation and bug fixes. Signed-off-by: abhijit_onap Change-Id: Ibbc2215ca00cfffa4e0def62c88bfe32f1bb65bf --- diff --git a/ocd/cli/ealt/cmd/adapter/converter.go b/ocd/cli/ealt/cmd/adapter/converter.go index 6586a46..8e64bc8 100644 --- a/ocd/cli/ealt/cmd/adapter/converter.go +++ b/ocd/cli/ealt/cmd/adapter/converter.go @@ -18,6 +18,7 @@ package adapter import ( "ealt/cmd/common" + model "ealt/cmd/model" "encoding/json" "fmt" "log" @@ -33,13 +34,12 @@ func BuilderRequest(valueArgs []string, command string) error { //Onboard Command //ealtedge/mepm/app_pkgm/v1/app_packages/ //read the file from the system. - URIString = common.AppmUri + URIString = common.AppmUriCreate var packageName string var body []byte body = jsonEmptyBodyFormat() packageName = strings.TrimSpace(valueArgs[0]) HttpMultiPartPostRequestBuilder(URIString, body, packageName) - fmt.Println(packageName) case "NewAppDeleteCommand": //The Delete Application Package URI @@ -47,27 +47,23 @@ func BuilderRequest(valueArgs []string, command string) error { var body []byte URIString = common.AppmUri + strings.TrimSpace(valueArgs[0]) body = jsonEmptyBodyFormat() - fmt.Println(URIString) HttpDeleteRequestBuilder(URIString, body) - fmt.Println(URIString) - case "NewApplcmCreateCommand": //appLCM application Creation URI //ealtedge/mepm/app_lcm/v1/app_instances var body []byte - URIString = common.ApplcmUri - body, err := json.Marshal(map[string]string{ - "appDId": strings.TrimSpace(valueArgs[0]), - "appInstancename": strings.TrimSpace(valueArgs[1]), - "appInstanceDescriptor": strings.TrimSpace(valueArgs[2]), - }) + URIString = common.ApplcmUriCreate + //Assigning the AppLcm Create Command Line Flags to the Json Paylod. + payload := model.CreateApplicationReq{AppDID: strings.TrimSpace(valueArgs[0]), + AppInstancename: strings.TrimSpace(valueArgs[1]), + AppInstanceDescriptor: strings.TrimSpace(valueArgs[2])} + body, err := json.Marshal(payload) if err != nil { log.Fatalln(err) } - fmt.Println(URIString) HttpPostRequestBuilder(URIString, body) case "NewApplcmDeleteCommand": @@ -75,8 +71,9 @@ func BuilderRequest(valueArgs []string, command string) error { ///ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId} var body []byte URIString = common.ApplcmUri + strings.TrimSpace(valueArgs[0]) + + //Empty body for Delete Command. body = jsonEmptyBodyFormat() - fmt.Println(URIString) HttpDeleteRequestBuilder(URIString, body) case "NewApplcmStartCommand": @@ -85,8 +82,15 @@ func BuilderRequest(valueArgs []string, command string) error { var body []byte URIString = common.ApplcmUri + strings.TrimSpace(valueArgs[0]) + common.InstantiateUri - body = jsonEmptyBodyFormat() - fmt.Println(URIString) + + selectedMECHostInfo := model.SelectedMECHostInfo{HostName: strings.TrimSpace(valueArgs[1]), + HostId: strings.TrimSpace(valueArgs[2])} + //Payload + payload := model.InstantiateApplicationReq{SelectedMECHostInfo: selectedMECHostInfo} + body, err := json.Marshal(payload) + if err != nil { + fmt.Println(err) + } HttpPostRequestBuilder(URIString, body) case "NewApplcmTerminateCommand": @@ -95,11 +99,8 @@ func BuilderRequest(valueArgs []string, command string) error { var body []byte URIString = common.ApplcmUri + strings.TrimSpace(valueArgs[0]) + common.TerminateUri body = jsonEmptyBodyFormat() - fmt.Println(URIString) HttpPostRequestBuilder(URIString, body) - } - return nil } diff --git a/ocd/cli/ealt/cmd/adapter/httphelper.go b/ocd/cli/ealt/cmd/adapter/httphelper.go index 2f34d7c..9f91079 100644 --- a/ocd/cli/ealt/cmd/adapter/httphelper.go +++ b/ocd/cli/ealt/cmd/adapter/httphelper.go @@ -19,7 +19,6 @@ package adapter import ( "bytes" "ealt/cmd/common" - "encoding/json" "fmt" "io" "io/ioutil" @@ -37,20 +36,20 @@ var client = http.Client{} func httpEndPointBuider(uri string) string { - return "http://" + strings.TrimSpace(MECMClusterIP) + strings.TrimSpace(APPLCMPort) + uri + return "http://" + strings.TrimSpace(MECMClusterIP) + ":" + strings.TrimSpace(APPLCMPort) + uri } func HttpDeleteRequestBuilder(uri string, body []byte) { uri = httpEndPointBuider(uri) + fmt.Println("Request URL :\t" + uri) request, err := http.NewRequest(http.MethodDelete, uri, bytes.NewBuffer(body)) request.Header.Set("Content-Type", "application/json") if err != nil { log.Fatalln(err) } - response, err := client.Do(request) if err != nil { log.Fatalln(err) @@ -63,22 +62,23 @@ func HttpDeleteRequestBuilder(uri string, body []byte) { log.Fatalln(err) } - fmt.Println(string(output)) + fmt.Println("Response Data: \n" + string(output)) } func HttpPostRequestBuilder(uri string, body []byte) error { uri = httpEndPointBuider(uri) + fmt.Println("Request URL :\t" + uri) + fmt.Println("Request Body :\t" + string(body) + "\n") request, err := http.NewRequest(http.MethodPost, uri, bytes.NewBuffer(body)) request.Header.Set("Content-Type", "application/json") - fmt.Println(request) + //fmt.Println(request) if err != nil { log.Fatalln(err) } - response, err := client.Do(request) if err != nil { log.Fatalln(err) @@ -91,26 +91,40 @@ func HttpPostRequestBuilder(uri string, body []byte) error { log.Fatalln(err) } - fmt.Println(string(output)) + fmt.Println("Response Data: \n\n" + string(output)) return nil } func HttpMultiPartPostRequestBuilder(uri string, body []byte, file string) error { filepath := getFilePathWithName(file) + fmt.Println("File Path :" + filepath) uri = httpEndPointBuider(uri) - + fmt.Println("Request URL :\t" + uri) request, err := fileUploadRequest(uri, "file", filepath) + if err != nil { + log.Fatalln(err) + } response, err := client.Do(request) if err != nil { log.Fatalln(err) + } else { + body := &bytes.Buffer{} + _, err := body.ReadFrom(response.Body) + if err != nil { + log.Fatal(err) + } + response.Body.Close() + fmt.Println(response.StatusCode) + fmt.Println(response.Header) + + // fmt.Println(body) + // var result map[string]interface{} + // json.NewDecoder(response.Body).Decode(&result) + // fmt.Println(result) } - var result map[string]interface{} - json.NewDecoder(response.Body).Decode(&result) - fmt.Println(result) - return nil } @@ -151,6 +165,7 @@ func fileUploadRequest(uri string, paramName, filepath string) (*http.Request, e if err != nil { log.Fatalln(err) } - request.Header.Set("Content-Type", "multipart/form-data") + request.Header.Add("Content-Type", multiPartWriter.FormDataContentType()) + //request.Header.Set("Content-Type", "multipart/form-data") return request, err } diff --git a/ocd/cli/ealt/cmd/app.go b/ocd/cli/ealt/cmd/app.go index 49bec5d..96a80f7 100644 --- a/ocd/cli/ealt/cmd/app.go +++ b/ocd/cli/ealt/cmd/app.go @@ -16,8 +16,6 @@ limitations under the License. package cmd import ( - "fmt" - appCmds "ealt/cmd/appm" "github.com/spf13/cobra" @@ -28,10 +26,7 @@ var appCmd = &cobra.Command{ Use: "app", Short: "The command is used for Application Management in the EALT Edge System.", Long: `The command is used for Application Management in the EALT Edge System. - It has multiple options like create , delete`, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("app called") - }, + It has options: create , delete`, } func init() { diff --git a/ocd/cli/ealt/cmd/applcm.go b/ocd/cli/ealt/cmd/applcm.go index a5386cc..50fa33c 100644 --- a/ocd/cli/ealt/cmd/applcm.go +++ b/ocd/cli/ealt/cmd/applcm.go @@ -16,8 +16,6 @@ limitations under the License. package cmd import ( - "fmt" - applcmCmds "ealt/cmd/applcmpkg" "github.com/spf13/cobra" @@ -34,9 +32,6 @@ var applcmCmd = &cobra.Command{ 2. Start 3. Delete 4. Stop.`, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("applcm called") - }, } func init() { diff --git a/ocd/cli/ealt/cmd/applcmpkg/delete.go b/ocd/cli/ealt/cmd/applcmpkg/delete.go index d3104ac..44c4a94 100644 --- a/ocd/cli/ealt/cmd/applcmpkg/delete.go +++ b/ocd/cli/ealt/cmd/applcmpkg/delete.go @@ -25,12 +25,10 @@ import ( func NewApplcmDeleteCommand() *cobra.Command { var cmd = &cobra.Command{ Use: "delete", - Short: "Install Complete EALT Deployment Environment", - Long: `Install Complete EALT Deployment Environment`, + Short: "Delete the application from the MEP Node.", + Long: `Delete the application from the MEP Node.`, RunE: func(cmd *cobra.Command, args []string) error { - var theFlags []string - theFlags[0] = cmd.Flag("appid").Value.String() - + theFlags := []string{cmd.Flag("appid").Value.String()} err := adapter.BuilderRequest(theFlags, "NewApplcmDeleteCommand") if err != nil { return err diff --git a/ocd/cli/ealt/cmd/applcmpkg/start.go b/ocd/cli/ealt/cmd/applcmpkg/start.go index 2ae47dd..f555eac 100644 --- a/ocd/cli/ealt/cmd/applcmpkg/start.go +++ b/ocd/cli/ealt/cmd/applcmpkg/start.go @@ -17,7 +17,6 @@ package applcmpkg import ( "ealt/cmd/adapter" - "fmt" "github.com/spf13/cobra" ) @@ -29,9 +28,9 @@ func NewApplcmStartCommand() *cobra.Command { Short: "To start the application instance on MEP Node", Long: `To start the application instance on MEP Node`, RunE: func(cmd *cobra.Command, args []string) error { - theFlags := []string{cmd.Flag("appid").Value.String()} - //theFlags[0] = cmd.Flag("appid").Value.String() - fmt.Println(theFlags[0]) + theFlags := []string{cmd.Flag("appid").Value.String(), + cmd.Flag("hostname").Value.String(), + cmd.Flag("hostip").Value.String()} err := adapter.BuilderRequest(theFlags, "NewApplcmStartCommand") if err != nil { return err @@ -40,6 +39,10 @@ func NewApplcmStartCommand() *cobra.Command { }, } cmd.Flags().StringP("appid", "i", "", "Application Instance ID to be started") + cmd.Flags().StringP("hostname", "n", "", "Host Name where the App has to be instantiated") + cmd.Flags().StringP("hostip", "o", "", "Host IP / MEP Node IP") cmd.MarkFlagRequired("appid") + cmd.MarkFlagRequired("hostname") + cmd.MarkFlagRequired("hostip") return cmd } diff --git a/ocd/cli/ealt/cmd/applcmpkg/terminate.go b/ocd/cli/ealt/cmd/applcmpkg/terminate.go index 20c87b9..49b0109 100644 --- a/ocd/cli/ealt/cmd/applcmpkg/terminate.go +++ b/ocd/cli/ealt/cmd/applcmpkg/terminate.go @@ -25,11 +25,10 @@ import ( func NewApplcmTerminateCommand() *cobra.Command { var cmd = &cobra.Command{ Use: "kill", - Short: "To kill the application ", - Long: `Install Complete EALT Deployment Environment`, + Short: "To terminate the application instance id.", + Long: `To terminate the application instance id on MEP Node.`, RunE: func(cmd *cobra.Command, args []string) error { - var theFlags []string - theFlags[0] = cmd.Flag("appid").Value.String() + theFlags := []string{cmd.Flag("appid").Value.String()} err := adapter.BuilderRequest(theFlags, "NewApplcmTerminateCommand") if err != nil { return err @@ -37,9 +36,7 @@ func NewApplcmTerminateCommand() *cobra.Command { return nil }, } - cmd.Flags().StringP("appid", "i", "", "Application Instance ID to be terminated") cmd.MarkFlagRequired("appid") - return cmd } diff --git a/ocd/cli/ealt/cmd/appm/create.go b/ocd/cli/ealt/cmd/appm/create.go index 5ea257d..a691237 100644 --- a/ocd/cli/ealt/cmd/appm/create.go +++ b/ocd/cli/ealt/cmd/appm/create.go @@ -28,8 +28,7 @@ func NewAppCreateCommand() *cobra.Command { Short: "To create the application on MEP Node", Long: `To create the application on MEP Node`, RunE: func(cmd *cobra.Command, args []string) error { - var theFlags []string - theFlags[0] = cmd.Flag("packagefile").Value.String() + theFlags := []string{cmd.Flag("packagefile").Value.String()} err := adapter.BuilderRequest(theFlags, "NewAppCreateCommand") if err != nil { return err diff --git a/ocd/cli/ealt/cmd/appm/delete.go b/ocd/cli/ealt/cmd/appm/delete.go index cb79a52..e64e949 100644 --- a/ocd/cli/ealt/cmd/appm/delete.go +++ b/ocd/cli/ealt/cmd/appm/delete.go @@ -25,11 +25,10 @@ import ( func NewAppDeleteCommand() *cobra.Command { var cmd = &cobra.Command{ Use: "delete", - Short: "Install Complete EALT Deployment Environment", - Long: `Install Complete EALT Deployment Environment`, + Short: "Remove the Application Package from the MECM Host.", + Long: `Remove the Application Package from the MECM Host.`, RunE: func(cmd *cobra.Command, args []string) error { - var theFlags []string - theFlags[0] = cmd.Flag("pkgid").Value.String() + theFlags := []string{cmd.Flag("packageid").Value.String()} err := adapter.BuilderRequest(theFlags, "NewAppDeleteCommand") if err != nil { return err diff --git a/ocd/cli/ealt/cmd/common/constant.go b/ocd/cli/ealt/cmd/common/constant.go index 64922f0..6445c69 100644 --- a/ocd/cli/ealt/cmd/common/constant.go +++ b/ocd/cli/ealt/cmd/common/constant.go @@ -17,8 +17,10 @@ limitations under the License. package common const ( - AppmUri = "/ealtedge/mepm/app_pkgm/v1/app_packages/" - ApplcmUri = "/ealtedge/mepm/app_lcm/v1/app_instances/" + AppmUri = "/ealtedge/mepm/app_pkgm/v1/app_packages/" + AppmUriCreate = "/ealtedge/mepm/app_pkgm/v1/app_packages" + ApplcmUri = "/ealtedge/mepm/app_lcm/v1/app_instances/" + ApplcmUriCreate = "/ealtedge/mepm/app_lcm/v1/app_instances" InstantiateUri = "/instantiate" TerminateUri = "/terminate" diff --git a/ocd/cli/ealt/cmd/model/model.go b/ocd/cli/ealt/cmd/model/model.go new file mode 100644 index 0000000..4462c71 --- /dev/null +++ b/ocd/cli/ealt/cmd/model/model.go @@ -0,0 +1,30 @@ +/* +Copyright © 2020 NAME HERE + +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 model + +type InstantiateApplicationReq struct { + SelectedMECHostInfo SelectedMECHostInfo `json:"selectedMECHostInfo"` +} + +type SelectedMECHostInfo struct { + HostName string `json:"hostName"` + HostId string `json:"hostId"` +} +type CreateApplicationReq struct { + AppDID string `json:"appDId"` + AppInstancename string `json:"appInstancename"` + AppInstanceDescriptor string `json:"appInstanceDescriptor"` +}