Skip to content

Commit

Permalink
fix(license): update license commands (accuknox#6)
Browse files Browse the repository at this point in the history
- update license subcommands
  • Loading branch information
sharmayajush authored and rajaSahil committed May 25, 2023
1 parent e3ef265 commit 81ef8a7
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 30 deletions.
19 changes: 0 additions & 19 deletions cmd/get/get.go

This file was deleted.

82 changes: 82 additions & 0 deletions cmd/license/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package license

import (
"context"
"fmt"
"os"
"strconv"

"github.com/accuknox/accuknox-cli/k8s"
"github.com/accuknox/accuknox-cli/utils"
pb "github.com/accuknox/auto-policy-discovery/src/protobuf/v1/license"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

var (
key string
user string
)

var installCmd = &cobra.Command{
Use: "install",
Short: "Install License",
Long: `Install license for discovery engine`,
RunE: func(cmd *cobra.Command, args []string) error {
client, err := k8s.ConnectK8sClient()
if err != nil {
fmt.Printf("unable to create Kubernetes clients: %s\n", err.Error())
return err
}

gRPC := ""
targetSvc := "discovery-engine"

if val, ok := os.LookupEnv("DISCOVERY_SERVICE"); ok {
gRPC = val
} else {
pf, err := utils.InitiatePortForward(client, port, port, matchLabels, targetSvc)
if err != nil {
return err
}
gRPC = "localhost:" + strconv.FormatInt(pf.LocalPort, 10)
}

conn, err := grpc.Dial(gRPC, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return err
}
defer conn.Close()

licenseClient := pb.NewLicenseClient(conn)

req := &pb.LicenseInstallRequest{
Key: key,
UserId: user,
}
_, err = licenseClient.InstallLicense(context.Background(), req)
if err != nil {
return err
}
fmt.Printf("License installed successfully for discovery engine.\n")

return nil
},
}

func init() {
LicenseCmd.AddCommand(installCmd)

installCmd.Flags().StringVar(&key, "key", "", "license key for installing license (required)")
installCmd.Flags().StringVar(&user, "user", "", "user id for installing license")

err := installCmd.MarkFlagRequired("key")
if err != nil {
fmt.Printf("key flag is required : %s\n", err)
}
err = installCmd.MarkFlagRequired("user")
if err != nil {
fmt.Printf("user flag is required : %s\n", err)
}
}
22 changes: 22 additions & 0 deletions cmd/license/license.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package license

import (
"github.com/spf13/cobra"
)

var matchLabels = map[string]string{"app": "discovery-engine"}
var port int64 = 9089

var LicenseCmd = &cobra.Command{
Use: "license",
Short: "license is a pallete which contains license commands",
Long: `license is a pallete which contains license commands`,
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Help()
return err
},
}

func init() {

}
13 changes: 6 additions & 7 deletions cmd/get/licenseStatus.go → cmd/license/status.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package get
package license

import (
"context"
Expand All @@ -12,13 +12,11 @@ import (
pb "github.com/accuknox/auto-policy-discovery/src/protobuf/v1/license"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

var matchLabels = map[string]string{"app": "discovery-engine"}
var port int64 = 9089

var licenseStatusCmd = &cobra.Command{
Use: "license-status",
Use: "status",
Short: "get license status",
Long: `get license status`,

Expand All @@ -43,12 +41,13 @@ var licenseStatusCmd = &cobra.Command{
gRPC = "localhost:" + strconv.FormatInt(pf.LocalPort, 10)
}

conn, err := grpc.Dial(gRPC, grpc.WithInsecure())
conn, err := grpc.Dial(gRPC, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
fmt.Printf("unable to dial to the target grpc: %s\n", err.Error())
return err
}
defer conn.Close()

licenseClient := pb.NewLicenseClient(conn)
req := &pb.LicenseStatusRequest{}
resp, err := licenseClient.GetLicenseStatus(context.Background(), req)
Expand All @@ -69,5 +68,5 @@ var licenseStatusCmd = &cobra.Command{
}

func init() {
GetCmd.AddCommand(licenseStatusCmd)
LicenseCmd.AddCommand(licenseStatusCmd)
}
4 changes: 2 additions & 2 deletions cmd/get/uuid.go → cmd/license/uuid.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package get
package license

import (
"fmt"
Expand Down Expand Up @@ -32,6 +32,6 @@ var uuidCmd = &cobra.Command{
}

func init() {
GetCmd.AddCommand(uuidCmd)
LicenseCmd.AddCommand(uuidCmd)

}
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package cmd

import (
"github.com/accuknox/accuknox-cli/cmd/get"
"github.com/accuknox/accuknox-cli/cmd/license"
"github.com/accuknox/accuknox-cli/k8s"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -41,7 +41,7 @@ operation) of containers at the system level.

// adding all the commands with sub commands
func addSubCommandPalettes() {
rootCmd.AddCommand(get.GetCmd)
rootCmd.AddCommand(license.LicenseCmd)
}

func init() {
Expand Down

0 comments on commit 81ef8a7

Please sign in to comment.