forked from accuknox/accuknox-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(license): update license commands (accuknox#6)
- update license subcommands
- Loading branch information
1 parent
e3ef265
commit 81ef8a7
Showing
6 changed files
with
114 additions
and
30 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 @@ | ||
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) | ||
} | ||
} |
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,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() { | ||
|
||
} |
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
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