-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from openinfradev/tks-issues-486
app_serving: add Read & Update cmds
- Loading branch information
Showing
11 changed files
with
705 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.