Added Get Operation and Bug Fixes 14/3514/1
authorabhijit_onap <abhijit.das.gupta@huawei.com>
Wed, 27 May 2020 06:04:54 +0000 (11:34 +0530)
committerabhijit_onap <abhijit.das.gupta@huawei.com>
Wed, 27 May 2020 06:04:54 +0000 (11:34 +0530)
Added Get Operation for Application Package Management
and Application Life Cycle

Fixed some help information and udpated comments.

Fixed bug in Upload Package Information.

Signed-off-by: abhijit_onap <abhijit.das.gupta@huawei.com>
Change-Id: I4f4d1b261894fa8740055f3b526be522c3452924

ocd/cli/ealt/cmd/adapter/converter.go
ocd/cli/ealt/cmd/adapter/httphelper.go
ocd/cli/ealt/cmd/app.go
ocd/cli/ealt/cmd/applcm.go
ocd/cli/ealt/cmd/applcmpkg/delete.go
ocd/cli/ealt/cmd/applcmpkg/getinstance.go [new file with mode: 0644]
ocd/cli/ealt/cmd/applcmpkg/terminate.go
ocd/cli/ealt/cmd/appm/appinfo.go [new file with mode: 0644]
ocd/cli/ealt/cmd/appm/create.go

index 8e64bc8..9ffefac 100644 (file)
@@ -41,6 +41,13 @@ func BuilderRequest(valueArgs []string, command string) error {
                packageName = strings.TrimSpace(valueArgs[0])
                HttpMultiPartPostRequestBuilder(URIString, body, packageName)
 
+       case "NewAppInfoCommand":
+               URIString = common.AppmUri
+               var body []byte
+               URIString = common.AppmUri + strings.TrimSpace(valueArgs[0])
+               body = jsonEmptyBodyFormat()
+               HttpGetRequestBuilder(URIString, body)
+
        case "NewAppDeleteCommand":
                //The Delete Application Package URI
                //ealtedge/mepm/app_pkgm/v1/app_packages/{{ID}}
@@ -66,6 +73,16 @@ func BuilderRequest(valueArgs []string, command string) error {
                }
                HttpPostRequestBuilder(URIString, body)
 
+       case "NewApplcmInfoCommand":
+               //appLCM Get Application URI
+               ///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()
+               HttpGetRequestBuilder(URIString, body)
+
        case "NewApplcmDeleteCommand":
                //appLCM Delete Application URI
                ///ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}
index 9f91079..b9cc9c3 100644 (file)
@@ -19,6 +19,7 @@ package adapter
 import (
        "bytes"
        "ealt/cmd/common"
+       "encoding/json"
        "fmt"
        "io"
        "io/ioutil"
@@ -40,11 +41,13 @@ func httpEndPointBuider(uri string) string {
 
 }
 
-func HttpDeleteRequestBuilder(uri string, body []byte) {
+//Function to build the Get Requests for Application Package
+//Management and Application Life Cycle Management.
+func HttpGetRequestBuilder(uri string, body []byte) {
 
        uri = httpEndPointBuider(uri)
        fmt.Println("Request URL :\t" + uri)
-       request, err := http.NewRequest(http.MethodDelete, uri, bytes.NewBuffer(body))
+       request, err := http.NewRequest(http.MethodGet, uri, bytes.NewBuffer(body))
        request.Header.Set("Content-Type", "application/json")
 
        if err != nil {
@@ -61,9 +64,31 @@ func HttpDeleteRequestBuilder(uri string, body []byte) {
        if err != nil {
                log.Fatalln(err)
        }
-
        fmt.Println("Response Data: \n" + string(output))
+}
+
+//HTTP DELETE Message Builder
+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)
+       }
+       defer response.Body.Close()
+       output, err := ioutil.ReadAll(response.Body)
 
+       if err != nil {
+               log.Fatalln(err)
+       }
+       fmt.Println("Response Data: \n" + string(output))
 }
 
 func HttpPostRequestBuilder(uri string, body []byte) error {
@@ -74,8 +99,6 @@ func HttpPostRequestBuilder(uri string, body []byte) error {
        request, err := http.NewRequest(http.MethodPost, uri, bytes.NewBuffer(body))
        request.Header.Set("Content-Type", "application/json")
 
-       //fmt.Println(request)
-
        if err != nil {
                log.Fatalln(err)
        }
@@ -101,7 +124,7 @@ func HttpMultiPartPostRequestBuilder(uri string, body []byte, file string) error
        fmt.Println("File Path :" + filepath)
        uri = httpEndPointBuider(uri)
        fmt.Println("Request URL :\t" + uri)
-       request, err := fileUploadRequest(uri, "file", filepath)
+       request, err := fileUploadRequest(uri, "file", filepath, file)
        if err != nil {
                log.Fatalln(err)
        }
@@ -116,15 +139,15 @@ func HttpMultiPartPostRequestBuilder(uri string, body []byte, file string) error
                        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)
-       }
+               fmt.Println("Response Body:")
 
+               fmt.Println(body)
+               var result map[string]interface{}
+               json.NewDecoder(response.Body).Decode(&result)
+
+               fmt.Println("ID has to be send in Create Application Instance Request")
+       }
        return nil
 }
 
@@ -133,7 +156,7 @@ func getFilePathWithName(file string) string {
        return ONBOARDPACKAGEPATH + common.PATHSLASH + file
 }
 
-func fileUploadRequest(uri string, paramName, filepath string) (*http.Request, error) {
+func fileUploadRequest(uri string, paramName, filepath, filename string) (*http.Request, error) {
 
        file, err := os.Open(filepath)
        if err != nil {
@@ -144,10 +167,11 @@ func fileUploadRequest(uri string, paramName, filepath string) (*http.Request, e
        defer file.Close()
 
        //Buffer to store the request body as bytes
-       var requestBody bytes.Buffer
-       multiPartWriter := multipart.NewWriter(&requestBody)
+       //var requestBody bytes.Buffer
+       requestBody := &bytes.Buffer{}
+       multiPartWriter := multipart.NewWriter(requestBody)
 
-       fileWriter, err := multiPartWriter.CreateFormFile("file_field", filepath)
+       fileWriter, err := multiPartWriter.CreateFormFile(paramName, filename)
        if err != nil {
                log.Fatalln(err)
        }
@@ -160,12 +184,17 @@ func fileUploadRequest(uri string, paramName, filepath string) (*http.Request, e
 
        //Close multiwriter
        multiPartWriter.Close()
+       if err != nil {
+               return nil, err
+       }
+
+       request, err := http.NewRequest(http.MethodPost, uri, requestBody)
+       request.Header.Set("Content-Type", multiPartWriter.FormDataContentType())
+       //request.Header.Set("Content-Type", "multipart/form-data")
 
-       request, err := http.NewRequest(http.MethodPost, uri, &requestBody)
        if err != nil {
                log.Fatalln(err)
        }
-       request.Header.Add("Content-Type", multiPartWriter.FormDataContentType())
-       //request.Header.Set("Content-Type", "multipart/form-data")
+
        return request, err
 }
index 96a80f7..7a1399e 100644 (file)
@@ -26,12 +26,13 @@ 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 options: create , delete`,
+       It has options: create , info and delete`,
 }
 
 func init() {
        appCmd.AddCommand(appCmds.NewAppCreateCommand())
        appCmd.AddCommand(appCmds.NewAppDeleteCommand())
+       appCmd.AddCommand(appCmds.NewAppInfoCommand())
 
        rootCmd.AddCommand(appCmd)
 }
index 50fa33c..2953041 100644 (file)
@@ -29,9 +29,10 @@ var applcmCmd = &cobra.Command{
        some API which can be used to manage the Applicaton running on the MEP Node
        The command have following options :
        1. Create
-       2. Start
-       3. Delete
-       4. Stop.`,
+       2. Info
+       3. Start
+       4. Delete
+       5. Stop.`,
 }
 
 func init() {
@@ -40,6 +41,7 @@ func init() {
        applcmCmd.AddCommand(applcmCmds.NewApplcmStartCommand())
        applcmCmd.AddCommand(applcmCmds.NewApplcmDeleteCommand())
        applcmCmd.AddCommand(applcmCmds.NewApplcmTerminateCommand())
+       applcmCmd.AddCommand(applcmCmds.NewApplcmInfoCommand())
 
        rootCmd.AddCommand(applcmCmd)
 
index 44c4a94..2175ec1 100644 (file)
@@ -37,7 +37,7 @@ func NewApplcmDeleteCommand() *cobra.Command {
                },
        }
 
-       cmd.Flags().StringP("appid", "i", "", "Application Instance ID to be started")
+       cmd.Flags().StringP("appid", "i", "", "Application Instance ID to be Deleted!!")
        cmd.MarkFlagRequired("appid")
        return cmd
 }
diff --git a/ocd/cli/ealt/cmd/applcmpkg/getinstance.go b/ocd/cli/ealt/cmd/applcmpkg/getinstance.go
new file mode 100644 (file)
index 0000000..96b47cd
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+Copyright 2020 Huawei Technologies Co., Ltd.
+
+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 applcmpkg
+
+import (
+       "ealt/cmd/adapter"
+
+       "github.com/spf13/cobra"
+)
+
+// allCmd represents the all command
+func NewApplcmInfoCommand() *cobra.Command {
+       var cmd = &cobra.Command{
+               Use:   "info",
+               Short: "Get the Application Instance Details..",
+               Long:  `The command display the details of the Application Instance running on the MEP Host.`,
+               RunE: func(cmd *cobra.Command, args []string) error {
+                       theFlags := []string{cmd.Flag("appid").Value.String()}
+                       err := adapter.BuilderRequest(theFlags, "NewApplcmInfoCommand")
+                       if err != nil {
+                               return err
+                       }
+                       return nil
+               },
+       }
+
+       cmd.Flags().StringP("appid", "i", "", "Application Instance ID for which details are to be fetched")
+       cmd.MarkFlagRequired("appid")
+       return cmd
+}
index 49b0109..ab78317 100644 (file)
@@ -25,8 +25,8 @@ import (
 func NewApplcmTerminateCommand() *cobra.Command {
        var cmd = &cobra.Command{
                Use:   "kill",
-               Short: "To terminate the application instance id.",
-               Long:  `To terminate the application instance id on MEP Node.`,
+               Short: "To terminate the application instance.",
+               Long:  `To terminate the application instance  on MEP Node.`,
                RunE: func(cmd *cobra.Command, args []string) error {
                        theFlags := []string{cmd.Flag("appid").Value.String()}
                        err := adapter.BuilderRequest(theFlags, "NewApplcmTerminateCommand")
diff --git a/ocd/cli/ealt/cmd/appm/appinfo.go b/ocd/cli/ealt/cmd/appm/appinfo.go
new file mode 100644 (file)
index 0000000..cc3536b
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+Copyright 2020 Huawei Technologies Co., Ltd.
+
+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 appm
+
+import (
+       "ealt/cmd/adapter"
+
+       "github.com/spf13/cobra"
+)
+
+// allCmd represents the all command
+func NewAppInfoCommand() *cobra.Command {
+       var cmd = &cobra.Command{
+               Use:   "info",
+               Short: "Get Information on the Onbarded Application.",
+               Long:  `Get Information on the Onbarded Application.`,
+               RunE: func(cmd *cobra.Command, args []string) error {
+                       theFlags := []string{cmd.Flag("packageid").Value.String()}
+                       err := adapter.BuilderRequest(theFlags, "NewAppInfoCommand")
+                       if err != nil {
+                               return err
+                       }
+                       return nil
+               },
+       }
+
+       cmd.Flags().StringP("packageid", "i", "", "Application Package ID which is onbarded")
+       cmd.MarkFlagRequired("packageid")
+
+       return cmd
+}
index a691237..4969db6 100644 (file)
@@ -36,7 +36,7 @@ func NewAppCreateCommand() *cobra.Command {
                        return nil
                },
        }
-       cmd.Flags().StringP("packagefile", "f", "", "Application Package File to be onboarded to MEP")
+       cmd.Flags().StringP("packagefile", "f", "", "Application Package File to be onboarded.")
        cmd.MarkFlagRequired("packagefile")
        return cmd
 }