Skip to content

Commit

Permalink
Merge pull request #12 from aserto-dev/gertd/remote
Browse files Browse the repository at this point in the history
topaz cli remote
  • Loading branch information
gertd committed Oct 25, 2022
2 parents 6693a4c + a66b998 commit ca15a4b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 23 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module github.com/aserto-dev/topaz

go 1.17

// replace github.com/slok/go-http-metrics => github.com/aserto-dev/go-http-metrics v0.10.1-0.20220201161800-936358c78a53

require (
github.com/alecthomas/kong v0.6.1
github.com/aserto-dev/aserto-go v0.8.14-0.20221018123009-a56db85a7ed3
Expand All @@ -12,7 +10,7 @@ require (
github.com/aserto-dev/errors v0.0.2
github.com/aserto-dev/go-authorizer v0.20.1
github.com/aserto-dev/go-directory v0.20.1
github.com/aserto-dev/go-directory-cli v0.20.1
github.com/aserto-dev/go-directory-cli v0.20.3
github.com/aserto-dev/go-edge-ds v0.20.1
github.com/aserto-dev/go-http-metrics v0.10.1-20221024-1
github.com/aserto-dev/logger v0.0.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ github.com/aserto-dev/go-authorizer v0.20.1 h1:01ry1AmhOhhGVeWyyvTVTnoCG6lYjmY5u
github.com/aserto-dev/go-authorizer v0.20.1/go.mod h1:RTpBixDT2WIPOkXcewCXG3NxOWDt22yiXMb+qvdxucM=
github.com/aserto-dev/go-directory v0.20.1 h1:tOoZPUeNqtiuy/kd/24oju0aTTug2tt6dx4qnLFxbJA=
github.com/aserto-dev/go-directory v0.20.1/go.mod h1:gjg6wZezLGXJj1LBEXaJUS9kpOnaDWeFUYhDG1TAkTY=
github.com/aserto-dev/go-directory-cli v0.20.1 h1:okRavhuGxkBmQmoizkX8EyqIJxunXSbiaqN0YyFWmNg=
github.com/aserto-dev/go-directory-cli v0.20.1/go.mod h1:g2n0U4JN/q51T6dmninbDT0Q7/4JLiaJns+lor//7dc=
github.com/aserto-dev/go-directory-cli v0.20.3 h1:bQR0H0UEzLt9ahr7VcGorZ+q8le8tr2OYPzkUWK8sC0=
github.com/aserto-dev/go-directory-cli v0.20.3/go.mod h1:g2n0U4JN/q51T6dmninbDT0Q7/4JLiaJns+lor//7dc=
github.com/aserto-dev/go-edge-ds v0.20.1 h1:lc0E5tBb2Rm/3pa6PQc7RBmBhQktrdyo8ZTLxD2mi0Y=
github.com/aserto-dev/go-edge-ds v0.20.1/go.mod h1:AFn7Tt4ShTV+RDPPGGB3LLtUZwB5MVR8IPCFxXYj4+k=
github.com/aserto-dev/go-grpc v0.8.0/go.mod h1:hog+zu5jE3+Y6t7m5PIBlavosmbd2qdF0BGZ9ueWK74=
Expand Down
39 changes: 29 additions & 10 deletions pkg/cli/clients/directory_client.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
package clients

import (
asertogoClient "github.com/aserto-dev/aserto-go/client"
grpcClient "github.com/aserto-dev/aserto-go/client"
"github.com/aserto-dev/go-directory-cli/client"
"github.com/aserto-dev/topaz/pkg/cli/cc"
"github.com/google/uuid"
)

const localhostDirectory = "localhost:9292"

func NewDirectoryClient(c *cc.CommonCtx, address string) (*client.Client, error) {
func NewDirectoryClient(c *cc.CommonCtx, cfg *Config) (*client.Client, error) {

if address == "" {
address = localhostDirectory
if cfg.Host == "" {
cfg.Host = localhostDirectory
}

conn, err := asertogoClient.NewConnection(
c.Context, asertogoClient.WithInsecure(true),
asertogoClient.WithAddr(address),
asertogoClient.WithSessionID(uuid.NewString()),
)
opts := []grpcClient.ConnectionOption{
grpcClient.WithAddr(cfg.Host),
grpcClient.WithInsecure(cfg.Insecure),
}

if cfg.APIKey != "" {
opts = append(opts, grpcClient.WithAPIKeyAuth(cfg.APIKey))
}

if cfg.SessionID != "" {
opts = append(opts, grpcClient.WithSessionID(cfg.SessionID))
}

if cfg.TenantID != "" {
opts = append(opts, grpcClient.WithTenantID(cfg.TenantID))
}

conn, err := grpcClient.NewConnection(c.Context, opts...)
if err != nil {
return nil, err
}

return client.New(conn, c.UI)
}

type Config struct {
Host string `flag:"host" short:"H" help:"" env:"TOPAZ_DIRECTORY_SVC" default:"localhost:9292"`
APIKey string `flag:"api-key" short:"k" help:"" env:"TOPAZ_DIRECTORY_KEY"`
Insecure bool `flag:"insecure" short:"i" help:""`
SessionID string `flag:"session-id" help:""`
TenantID string `flag:"tenant-id" help:""`
}
5 changes: 3 additions & 2 deletions pkg/cli/cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (

type BackupCmd struct {
File string `arg:"" default:"backup.tar.gz" help:"absolute file path to make backup to"`
clients.Config
}

const defaultFileName = "backup.tar.gz"

func (cmd BackupCmd) Run(c *cc.CommonCtx) error {
func (cmd *BackupCmd) Run(c *cc.CommonCtx) error {
if running, err := dockerx.IsRunning(dockerx.Topaz); !running || err != nil {
if err != nil {
return err
Expand All @@ -25,7 +26,7 @@ func (cmd BackupCmd) Run(c *cc.CommonCtx) error {
return nil
}

dirClient, err := clients.NewDirectoryClient(c, "")
dirClient, err := clients.NewDirectoryClient(c, &cmd.Config)
if err != nil {
return err
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/cli/cmd/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"github.com/aserto-dev/topaz/pkg/cli/clients"
"github.com/aserto-dev/topaz/pkg/cli/dockerx"
"github.com/fatih/color"
"github.com/google/uuid"
)

type ImportCmd struct {
Directory string `short:"d" required:"" help:"directory containing .json data"`
clients.Config
}

func (cmd ImportCmd) Run(c *cc.CommonCtx) error {
func (cmd *ImportCmd) Run(c *cc.CommonCtx) error {
if running, err := dockerx.IsRunning(dockerx.Topaz); !running || err != nil {
if err != nil {
return err
Expand All @@ -29,7 +31,8 @@ func (cmd ImportCmd) Run(c *cc.CommonCtx) error {
return err
}

dirClient, err := clients.NewDirectoryClient(c, "")
cmd.Config.SessionID = uuid.NewString()
dirClient, err := clients.NewDirectoryClient(c, &cmd.Config)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/cli/cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (

type LoadCmd struct {
File string `arg:"" default:"manifest.yaml" help:"absolute path to manifest file"`
clients.Config
}

var defaultManifestName = "manifest.yaml"

func (cmd LoadCmd) Run(c *cc.CommonCtx) error {
func (cmd *LoadCmd) Run(c *cc.CommonCtx) error {
if running, err := dockerx.IsRunning(dockerx.Topaz); !running || err != nil {
if err != nil {
return err
Expand All @@ -25,7 +26,7 @@ func (cmd LoadCmd) Run(c *cc.CommonCtx) error {
return nil
}

dirClient, err := clients.NewDirectoryClient(c, "")
dirClient, err := clients.NewDirectoryClient(c, &cmd.Config)
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/cli/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"github.com/aserto-dev/topaz/pkg/cli/clients"
"github.com/aserto-dev/topaz/pkg/cli/dockerx"
"github.com/fatih/color"
"github.com/google/uuid"
)

type RestoreCmd struct {
File string `arg:"" default:"backup.tar.gz" help:"absolute file path to local backup tarball"`
clients.Config
}

func (cmd RestoreCmd) Run(c *cc.CommonCtx) error {
func (cmd *RestoreCmd) Run(c *cc.CommonCtx) error {
if running, err := dockerx.IsRunning(dockerx.Topaz); !running || err != nil {
if err != nil {
return err
Expand All @@ -23,7 +25,9 @@ func (cmd RestoreCmd) Run(c *cc.CommonCtx) error {
return nil
}

dirClient, err := clients.NewDirectoryClient(c, "")
cmd.Config.SessionID = uuid.NewString()

dirClient, err := clients.NewDirectoryClient(c, &cmd.Config)
if err != nil {
return err
}
Expand Down

0 comments on commit ca15a4b

Please sign in to comment.