Skip to content

Commit

Permalink
Merge pull request #40 from openinfradev/tks-issues-486
Browse files Browse the repository at this point in the history
app_serving: add Read & Update cmds
  • Loading branch information
robertchoi80 authored Feb 17, 2023
2 parents 8ff35e6 + bbf737e commit e81552b
Show file tree
Hide file tree
Showing 11 changed files with 705 additions and 4 deletions.
85 changes: 85 additions & 0 deletions cmd/appserve_abort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright © 2021 SK Telecom <https://github.com/openinfradev>
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 cmd

import (
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io"
"net/http"
"net/url"
"strings"
)

var appserveAbortCmd = &cobra.Command{
Use: "abort",
Short: "Abort an app paused in blue-green state",
Long: `Abort an app paused in blue-green state.
Example:
tks appserve abort <APP_ID>`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("APP_ID is mandatory! Run 'tks appserve abort --help'.")
}

// Get API Url
appserveApiUrl := viper.GetString("tksAppServeLcmUrl")
if appserveApiUrl == "" {
return errors.New("tks_appserve_api_url is mandatory.")
}

/* Parse command line params */
appId := args[0]

// Prepare request body
data := url.Values{}
data.Set("abort", "true")

// Initialize http client
client := &http.Client{}

// set the HTTP method, url, and request body
req, err := http.NewRequest(http.MethodPut, appserveApiUrl+"/apps/"+appId, strings.NewReader(data.Encode()))
if err != nil {
return fmt.Errorf("Error while constructing req: %s", err)
}

req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("Error from update req: %s", err)
}

defer resp.Body.Close()

// Check response
respBody, err := io.ReadAll(resp.Body)
if err == nil {
str := string(respBody)
fmt.Println("Response:\n", str)
}

return nil
},
}

func init() {
appserveCmd.AddCommand(appserveAbortCmd)
}
2 changes: 1 addition & 1 deletion cmd/appserve_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type conf struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
Type string `yaml:"type"`
Strategy string `yaml:"strategy"`
App_type string `yaml:"app_type"`
Artifact_url string `yaml:"artifact_url"`
Image_url string `yaml:"image_url"`
Expand Down Expand Up @@ -135,7 +136,6 @@ tks appserve create --appserve-config CONFIGFILE`,
respBody, err := io.ReadAll(resp.Body)
if err == nil {
str := string(respBody)
// TODO: after test run, fix this output msg.
fmt.Println("Response:\n", str)
}

Expand Down
82 changes: 82 additions & 0 deletions cmd/appserve_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright © 2021 SK Telecom <https://github.com/openinfradev>
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 cmd

import (
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io"
"net/http"
)

var appserveDeleteCmd = &cobra.Command{
Use: "delete",
Short: "delete app deployed by AppServing service",
Long: `delete app deployed by AppServing service.
Example:
tks appserve delete <APP_UUID>`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("App UUID is mandatory. Run 'tks appserve delete --help'")
}

// Get API Url
appserveApiUrl := viper.GetString("tksAppServeLcmUrl")
if appserveApiUrl == "" {
return errors.New("tks_appserve_api_url is mandatory.")
}

appId := args[0]
//buff := bytes.NewBuffer(cBytes)

// Initialize http client
client := &http.Client{}

// set the HTTP method, url, and request body
req, err := http.NewRequest(http.MethodDelete, appserveApiUrl+"/apps/"+appId, nil)
if err != nil {
return fmt.Errorf("Error while constructing req: %s", err)
}

// set the request header Content-Type for json
//req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("Error from delete req: %s", err)
}

defer resp.Body.Close()

// Check response
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Error while getting response: %s", err)
} else {
str := string(respBody)
fmt.Println("Response:\n", str)
}

return nil
},
}

func init() {
appserveCmd.AddCommand(appserveDeleteCmd)
}
113 changes: 113 additions & 0 deletions cmd/appserve_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
Copyright © 2021 SK Telecom <https://github.com/openinfradev>
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 cmd

import (
"encoding/json"
"errors"
"fmt"
"github.com/jedib0t/go-pretty/table"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/protobuf/types/known/timestamppb"
"io"
"net/http"
)

var contractId string

type app struct {
Id string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
App_type string `json:"app_type"`
Status string `json:"status"`
Image_url string `json:"image_url"`
Updated_at timestamppb.Timestamp
Created_at timestamppb.Timestamp
}

var appserveListCmd = &cobra.Command{
Use: "list",
Short: "list apps deployed by AppServing service",
Long: `list apps deployed by AppServing service.
Example:
tks appserve list --contract_id <CONTRACT_ID>`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
// Get API Url
appserveApiUrl := viper.GetString("tksAppServeLcmUrl")
if appserveApiUrl == "" {
return errors.New("tks_appserve_api_url is mandatory.")
}

if contractId == "" {
return errors.New("contract-id is mandatory param.")
}

resp, err := http.Get(appserveApiUrl + "/apps?contract_id=" + contractId)
if err != nil {
return fmt.Errorf("Error from GET req: %s", err)
}

defer resp.Body.Close()

// Check response
respBody, err := io.ReadAll(resp.Body)
if err == nil {
var body []app
json.Unmarshal(respBody, &body)

if len(body) == 0 {
fmt.Println("No app exists for specified contract!")
} else {
printApps(body, true)
}
}

return nil
},
}

func init() {
appserveCmd.AddCommand(appserveListCmd)

appserveListCmd.Flags().StringVar(&contractId, "contract-id", "", "contract ID")
}

func printApps(apps []app, long bool) {
t := table.NewWriter()
tTemp := table.Table{}
tTemp.Render()
t.Style().Options.DrawBorder = false
t.Style().Options.SeparateColumns = true
t.Style().Options.SeparateFooter = false
t.Style().Options.SeparateHeader = true
t.Style().Options.SeparateRows = false

if long {
t.AppendHeader(table.Row{"ID", "Name", "Status", "TYPE", "APP_TYPE", "CREATED_AT", "UPDATED_AT"})
for _, s := range apps {
tCreatedAt := parseTime(&s.Created_at)
tUpdatedAt := parseTime(&s.Updated_at)
t.AppendRow(table.Row{s.Id, s.Name, s.Status, s.Type, s.App_type, tCreatedAt, tUpdatedAt})
}
} else {
fmt.Println("Not implemented yet.")
}
fmt.Println(t.Render())
}
85 changes: 85 additions & 0 deletions cmd/appserve_promote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright © 2021 SK Telecom <https://github.com/openinfradev>
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 cmd

import (
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io"
"net/http"
"net/url"
"strings"
)

var appservePromoteCmd = &cobra.Command{
Use: "promote",
Short: "Promote an app paused in blue-green state",
Long: `Promote an app paused in blue-green state.
Example:
tks appserve promote <APP_ID>`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("APP_ID is mandatory! Run 'tks appserve promote --help'.")
}

// Get API Url
appserveApiUrl := viper.GetString("tksAppServeLcmUrl")
if appserveApiUrl == "" {
return errors.New("tks_appserve_api_url is mandatory.")
}

/* Parse command line params */
appId := args[0]

// Prepare request body
data := url.Values{}
data.Set("promote", "true")

// Initialize http client
client := &http.Client{}

// set the HTTP method, url, and request body
req, err := http.NewRequest(http.MethodPut, appserveApiUrl+"/apps/"+appId, strings.NewReader(data.Encode()))
if err != nil {
return fmt.Errorf("Error while constructing req: %s", err)
}

req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("Error from update req: %s", err)
}

defer resp.Body.Close()

// Check response
respBody, err := io.ReadAll(resp.Body)
if err == nil {
str := string(respBody)
fmt.Println("Response:\n", str)
}

return nil
},
}

func init() {
appserveCmd.AddCommand(appservePromoteCmd)
}
Loading

0 comments on commit e81552b

Please sign in to comment.