Skip to content

Commit

Permalink
feature. add api for importing cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
ktkfree committed Nov 14, 2022
1 parent 778cd19 commit 8ea9776
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 5 deletions.
117 changes: 117 additions & 0 deletions cmd/cluster-import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
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"
"errors"
"fmt"
"log"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

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

// clusterImportCmd represents the import command
var clusterImportCmd = &cobra.Command{
Use: "import",
Short: "Import a TKS Cluster.",
Long: `Import a TKS Cluster.
Example:
tks cluster import <CLUSTERNAME> [--contract-id CONTRACTID --kubeconfig KUBECONFIG]`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
fmt.Println("You must specify cluster name.")
return errors.New("Usage: tks cluster import <CLUSTERNAME> --contract-id <CONTRACTID>")
}
var conn *grpc.ClientConn
tksClusterLcmUrl = viper.GetString("tksClusterLcmUrl")
if tksClusterLcmUrl == "" {
return errors.New("You must specify tksClusterLcmUrl at config file")
}
conn, err := grpc.Dial(tksClusterLcmUrl, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("Could not connect to LCM server: %s", err)
}
defer conn.Close()

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

/* Parse command line arguments */
ClusterName := args[0]
ContractId, _ := cmd.Flags().GetString("contract-id")
creator, _ := cmd.Flags().GetString("creator")
description, _ := cmd.Flags().GetString("description")
kubeconfig, _ := cmd.Flags().GetString("kubeconfig")

/* Construct request map */
data := pb.ImportClusterRequest{
Name: ClusterName,
ContractId: ContractId,
Kubeconfig: kubeconfig,
Creator: creator,
Description: description,
}

m := protojson.MarshalOptions{
Indent: " ",
UseProtoNames: true,
}
jsonBytes, _ := m.Marshal(&data)
fmt.Println("Proto Json data: ")
fmt.Println(string(jsonBytes))

r, err := client.ImportCluster(ctx, &data)
fmt.Println("Response:\n", r)
if err != nil {
return fmt.Errorf("Error: %s", err)
} else {
fmt.Println("Success: The request to import cluster ", args[0], " was accepted.")
}

return nil
},
}

func init() {
clusterCmd.AddCommand(clusterImportCmd)

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

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

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// clusterCreateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
clusterImportCmd.Flags().String("contract-id", "", "Contract ID")
_ = clusterImportCmd.MarkFlagRequired("contract-id")
clusterImportCmd.Flags().String("kubeconfig", "", "Kubeconfig of the cluster")

clusterImportCmd.Flags().String("creator", "", "Uuid of creator")
clusterImportCmd.Flags().String("description", "", "Description of cluster")
}
2 changes: 2 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func TestClusterCmd(t *testing.T) {
{[]string{"cluster", "list", "-h"}, false, "", "A longer description that spans multiple lines and likely contains examples"},
{[]string{"cluster", "list", "--config", "xx"}, false, "", "A longer description that spans multiple lines and likely contains examples"},
{[]string{"cluster", "list", "--config"}, true, "flag needs an argument: --config", ""},

{[]string{"cluster", "import", "--contract-id"}, true, "flag needs an argument: --contract-id", ""},
}

doTest(t, testcases)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/openinfradev/tks-proto v0.0.6-0.20221018052004-85d1b297f865 // indirect
github.com/openinfradev/tks-proto v0.0.6-0.20221108083743-52cfd2c1ce73 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/rivo/uniseg v0.4.2 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,8 @@ github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJ
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/openinfradev/tks-proto v0.0.6-0.20220927101958-ec0ec0085191 h1:+9bYv05jGgzZ6s9amFLHl0CTDLf0mAzmhraquoVPX+A=
github.com/openinfradev/tks-proto v0.0.6-0.20220927101958-ec0ec0085191/go.mod h1:IIxiwEZVfqLXm/O4KGiD0q/ELlyKxXx44TCM5ZWhpJs=
github.com/openinfradev/tks-proto v0.0.6-0.20221018052004-85d1b297f865 h1:YGJloolf98NRDFeiwdm3qrk+8FmzDqROgoOg8D+20Cw=
github.com/openinfradev/tks-proto v0.0.6-0.20221018052004-85d1b297f865/go.mod h1:IIxiwEZVfqLXm/O4KGiD0q/ELlyKxXx44TCM5ZWhpJs=
github.com/openinfradev/tks-proto v0.0.6-0.20221108083743-52cfd2c1ce73 h1:IEy2rxUKHVBmkv9/UZk+2Rh6I9B2DaoabndkqtEF8Y8=
github.com/openinfradev/tks-proto v0.0.6-0.20221108083743-52cfd2c1ce73/go.mod h1:IIxiwEZVfqLXm/O4KGiD0q/ELlyKxXx44TCM5ZWhpJs=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.0.5 h1:ipoSadvV8oGUjnUbMub59IDPPwfxF694nG/jwbMiyQg=
Expand Down

0 comments on commit 8ea9776

Please sign in to comment.