Skip to content

Commit

Permalink
Merge pull request #15 from Jaesang/cmd-for-service
Browse files Browse the repository at this point in the history
Cmd for service
  • Loading branch information
robertchoi80 authored Mar 22, 2022
2 parents 2ae29ef + 993c0fd commit 116398c
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 15 deletions.
25 changes: 21 additions & 4 deletions cmd/cluster_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

pb "github.com/openinfradev/tks-proto/tks_pb"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/protobuf/encoding/protojson"
)

Expand All @@ -36,14 +37,19 @@ var clusterCreateCmd = &cobra.Command{
Long: `Create a TACO Cluster to AWS.
Example:
tks cluster create <CLUSTERNAME> --contract-id <CONTRACTID> --csp-id <CSPID>`,
tks cluster create <CLUSTERNAME>`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("You must specify cluster name.")
fmt.Println("Usage: tks cluster create <CLUSTERNAME> --contract-id <CONTRACTID> --csp-id <CSPID>")
os.Exit(1)
}
var conn *grpc.ClientConn
tksClusterLcmUrl = viper.GetString("tksClusterLcmUrl")
if tksClusterLcmUrl == "" {
fmt.Println("You must specify tksClusterLcmUrl at config file")
os.Exit(1)
}
conn, err := grpc.Dial(tksClusterLcmUrl, grpc.WithInsecure())
if err != nil {
log.Fatalf("Could not connect to LCM server: %s", err)
Expand All @@ -57,8 +63,21 @@ tks cluster create <CLUSTERNAME> --contract-id <CONTRACTID> --csp-id <CSPID>`,
/* Parse command line arguments */
ClusterName := args[0]
ContractId, _ := cmd.Flags().GetString("contract-id")
if ContractId == "" {
ContractId = viper.GetString("contractId")
if ContractId == "" {
fmt.Println("You must specify contractId and cspId")
os.Exit(1)
}
}
CspId, _ := cmd.Flags().GetString("csp-id")

if CspId == "" {
CspId = viper.GetString("cspId")
if CspId == "" {
fmt.Println("You must specify contractId and cspId")
os.Exit(1)
}
}
conf := pb.ClusterRawConf{}
conf.SshKeyName, _ = cmd.Flags().GetString("ssh-key-name")
conf.Region, _ = cmd.Flags().GetString("region")
Expand Down Expand Up @@ -103,9 +122,7 @@ func init() {
// is called directly, e.g.:
// clusterCreateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
clusterCreateCmd.Flags().String("contract-id", "", "Contract ID")
clusterCreateCmd.MarkFlagRequired("contract-id")
clusterCreateCmd.Flags().String("csp-id", "", "CSP ID")
clusterCreateCmd.MarkFlagRequired("csp-id")
clusterCreateCmd.Flags().String("region", "", "AWS Region")
clusterCreateCmd.Flags().Int("num-of-az", 3, "Number of availability zones in selected region")
clusterCreateCmd.Flags().String("ssh-key-name", "", "SSH key name for EC2 instance connection")
Expand Down
8 changes: 7 additions & 1 deletion cmd/cluster_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

pb "github.com/openinfradev/tks-proto/tks_pb"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/protobuf/encoding/protojson"
)

Expand All @@ -45,7 +46,12 @@ tks cluster delete <CLUSTER_ID>`,
}

var conn *grpc.ClientConn
conn, err := grpc.Dial(address, grpc.WithInsecure())
tksClusterLcmUrl = viper.GetString("tksClusterLcmUrl")
if tksClusterLcmUrl == "" {
fmt.Println("You must specify tksClusterLcmUrl at config file")
os.Exit(1)
}
conn, err := grpc.Dial(tksClusterLcmUrl, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %s", err)
}
Expand Down
14 changes: 12 additions & 2 deletions cmd/cluster_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import (
"context"
"fmt"
"log"
"os"
"time"

"google.golang.org/grpc"

"github.com/jedib0t/go-pretty/table"
pb "github.com/openinfradev/tks-proto/tks_pb"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/timestamppb"
)
Expand All @@ -39,8 +41,12 @@ var clusterListCmd = &cobra.Command{
Example:
tks cluster list (--long)`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("list called")
var conn *grpc.ClientConn
tksInfoUrl = viper.GetString("tksInfoUrl")
if tksInfoUrl == "" {
fmt.Println("You must specify tksInfoUrl at config file")
os.Exit(1)
}
conn, err := grpc.Dial(tksInfoUrl, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %s", err)
Expand All @@ -51,7 +57,11 @@ tks cluster list (--long)`,
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()
data := pb.GetClustersRequest{}
data.ContractId = defaultContractId
data.ContractId = viper.GetString("contractId")
if data.ContractId == "" {
fmt.Println("You must specify contractId at config file")
os.Exit(1)
}

m := protojson.MarshalOptions{
Indent: " ",
Expand Down
6 changes: 6 additions & 0 deletions cmd/cluster_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/jedib0t/go-pretty/table"
pb "github.com/openinfradev/tks-proto/tks_pb"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/protobuf/encoding/protojson"
)
Expand All @@ -44,6 +45,11 @@ tks cluster show <CLUSTER_ID>`,
os.Exit(1)
}
var conn *grpc.ClientConn
tksInfoUrl = viper.GetString("tksInfoUrl")
if tksInfoUrl == "" {
fmt.Println("You must specify tksInfoUrl at config file")
os.Exit(1)
}
conn, err := grpc.Dial(tksInfoUrl, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %s", err)
Expand Down
12 changes: 6 additions & 6 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ limitations under the License.
*/
package cmd

const (
defaultContractId string = "f84df193-f6c8-45ad-867c-c0bcc48f09e5"
defaultCspId string = "c947d800-ac43-4bab-a8e1-4ceaa0ffec98"
tksInfoUrl = "tks-info.taco-cat.xyz:9110"
tksClusterLcmUrl = "tks-cluster-lcm.taco-cat.xyz:9110"
tksContractUrl = "tks-contract.taco-cat.xyz:9110"
var (
defaultContractId string
defaultCspId string
tksInfoUrl string
tksClusterLcmUrl string
tksContractUrl string
)
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ package cmd

import (
"fmt"
"github.com/spf13/cobra"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

Expand Down
6 changes: 6 additions & 0 deletions cmd/service_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

pb "github.com/openinfradev/tks-proto/tks_pb"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/timestamppb"
)
Expand Down Expand Up @@ -55,6 +56,11 @@ tks service create --cluster-id <CLUSTERID> --service-name <LMA,LMA_EFK,SERVICE_
}

var conn *grpc.ClientConn
tksClusterLcmUrl = viper.GetString("tksClusterLcmUrl")
if tksClusterLcmUrl == "" {
fmt.Println("You must specify tksClusterLcmUrl at config file")
os.Exit(1)
}
conn, err := grpc.Dial(tksClusterLcmUrl, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %s", err)
Expand Down
99 changes: 99 additions & 0 deletions cmd/service_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
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 (
"context"
"fmt"
"log"
"os"
"time"

"google.golang.org/grpc"

pb "github.com/openinfradev/tks-proto/tks_pb"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/protobuf/encoding/protojson"
)

// serviceDeleteCmd represents the create command
var serviceDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete a TACO Service.",
Long: `Delete a TACO Service.
Example:
tks service delete <SERVICE ID>`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("You must specify service ID.")
fmt.Println("Usage: tks service delete <SERVICE ID>")
os.Exit(1)
}
var conn *grpc.ClientConn
tksClusterLcmUrl = viper.GetString("tksClusterLcmUrl")
if tksClusterLcmUrl == "" {
fmt.Println("You must specify tksClusterLcmUrl at config file")
os.Exit(1)
}
conn, err := grpc.Dial(tksClusterLcmUrl, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %s", err)
}
defer conn.Close()

client := pb.NewClusterLcmServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()

serviceId := args[0]

data := pb.UninstallAppGroupsRequest{}
data.AppGroupIds = []string{serviceId}

m := protojson.MarshalOptions{
Indent: " ",
UseProtoNames: true,
}
jsonBytes, _ := m.Marshal(&data)
verbose, err := rootCmd.PersistentFlags().GetBool("verbose")
if verbose {
fmt.Println("Proto Json data...")
fmt.Println(string(jsonBytes))
}
r, err := client.UninstallAppGroups(ctx, &data)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(r)
}
},
}

func init() {
serviceCmd.AddCommand(serviceDeleteCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serviceDeleteCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serviceDeleteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
Loading

0 comments on commit 116398c

Please sign in to comment.