Skip to content

Commit

Permalink
2515 Fixed verbose flag. Added output when importing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jensalm committed Dec 4, 2024
1 parent 1a895a2 commit 8f3c4e5
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 26 deletions.
30 changes: 19 additions & 11 deletions ziti/cmd/ascode/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ type Download struct {
configTypeCache *cache.Cache
authPolicyCache *cache.Cache
externalJwtCache *cache.Cache

Out io.Writer
Err io.Writer
}

var output Output
Expand All @@ -73,7 +76,11 @@ func NewDownload(loginOpts edge.LoginOptions, client *rest_management_api_client
func NewDownloadCmd(out io.Writer, errOut io.Writer) *cobra.Command {

d := &Download{}
downloadCmd := &cobra.Command{
d.Out = out
d.Err = errOut
d.loginOpts = edge.LoginOptions{}

cmd := &cobra.Command{
Use: "export [entity]",
Short: "Export Ziti entities",
Long: "Export all or selected Ziti entities.\n" +
Expand Down Expand Up @@ -108,22 +115,23 @@ func NewDownloadCmd(out io.Writer, errOut io.Writer) *cobra.Command {

v.AutomaticEnv()

downloadCmd.Flags().BoolVar(&d.ofJson, "json", true, "Output in JSON")
downloadCmd.Flags().BoolVar(&d.ofYaml, "yaml", false, "Output in YAML")
downloadCmd.MarkFlagsMutuallyExclusive("json", "yaml")
cmd.Flags().SetInterspersed(true)
cmd.Flags().BoolVar(&d.ofJson, "json", true, "Output in JSON")
cmd.Flags().BoolVar(&d.ofYaml, "yaml", false, "Output in YAML")
cmd.MarkFlagsMutuallyExclusive("json", "yaml")

downloadCmd.PersistentFlags().StringVarP(&d.filename, "output-file", "o", "", "Write output to local file")
cmd.Flags().StringVarP(&d.filename, "output-file", "o", "", "Write output to local file")

downloadCmd.PersistentFlags().BoolVarP(&d.verbose, "verbose", "v", false, "Enable verbose logging")

edge.AddLoginFlags(downloadCmd, &d.loginOpts)
edge.AddLoginFlags(cmd, &d.loginOpts)
d.loginOpts.Out = out
d.loginOpts.Err = errOut

return downloadCmd
return cmd
}

func (d *Download) Init(out io.Writer) error {
d.verbose = d.loginOpts.Verbose

logLvl := logrus.InfoLevel
if d.verbose {
logLvl = logrus.DebugLevel
Expand Down Expand Up @@ -346,7 +354,7 @@ func (d *Download) getEntities(entityName string, count ClientCount, list Client
more := true
for more {
resp, err := list(&offset, &limit)
_, _ = fmt.Fprintf(os.Stderr, "\u001B[2KReading %d/%d %s\r", offset, totalCount, entityName)
_, _ = fmt.Fprintf(d.Err, "\u001B[2KReading %d/%d %s\r", offset, totalCount, entityName)
if err != nil {
return nil, errors.Join(errors.New("error reading "+entityName), err)
}
Expand All @@ -365,7 +373,7 @@ func (d *Download) getEntities(entityName string, count ClientCount, list Client
offset += limit
}

_, _ = fmt.Fprintf(os.Stderr, "\u001B[2KRead %d %s\r\n", len(result), entityName)
_, _ = fmt.Fprintf(d.Err, "\u001B[2KRead %d %s\r\n", len(result), entityName)

return result, nil

Expand Down
4 changes: 2 additions & 2 deletions ziti/cmd/ascode/download/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ func (output Output) Write(data any) error {
"bytes": bytes,
"filename": output.filename,
}).
Debug("Wrote json data")
Debug("Wrote data")
} else {
log.
WithField("bytes", bytes).
Debug("Wrote json data")
Debug("Wrote data")
}
}

Expand Down
37 changes: 29 additions & 8 deletions ziti/cmd/ascode/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package upload
import (
"encoding/json"
"errors"
"fmt"
"github.com/judedaryl/go-arrayutils"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/edge-api/rest_management_api_client"
Expand Down Expand Up @@ -54,14 +55,21 @@ type Upload struct {
authPolicyCache *cache.Cache
extJwtSignersCache *cache.Cache
identityCache *cache.Cache

Out io.Writer
Err io.Writer
}

var log = pfxlog.Logger()

func NewUploadCmd(out io.Writer, errOut io.Writer) *cobra.Command {

u := &Upload{}
uploadCmd := &cobra.Command{
u.Out = out
u.Err = errOut
u.loginOpts = edge.LoginOptions{}

cmd := &cobra.Command{
Use: "import filename [entity]",
Short: "Import ziti entities",
Long: "Import all or selected ziti entities from the specified file.\n" +
Expand Down Expand Up @@ -108,20 +116,21 @@ func NewUploadCmd(out io.Writer, errOut io.Writer) *cobra.Command {
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
v.AutomaticEnv()

uploadCmd.Flags().BoolVar(&u.ofJson, "json", true, "Input parsed as JSON")
uploadCmd.Flags().BoolVar(&u.ofYaml, "yaml", false, "Input parsed as YAML")
uploadCmd.MarkFlagsMutuallyExclusive("json", "yaml")
cmd.Flags().SetInterspersed(true)
cmd.Flags().BoolVar(&u.ofJson, "json", true, "Input parsed as JSON")
cmd.Flags().BoolVar(&u.ofYaml, "yaml", false, "Input parsed as YAML")
cmd.MarkFlagsMutuallyExclusive("json", "yaml")

uploadCmd.PersistentFlags().BoolVarP(&u.verbose, "verbose", "v", false, "Enable verbose logging")

edge.AddLoginFlags(uploadCmd, &u.loginOpts)
edge.AddLoginFlags(cmd, &u.loginOpts)
u.loginOpts.Out = out
u.loginOpts.Err = errOut

return uploadCmd
return cmd
}

func (u *Upload) Init() {
u.verbose = u.loginOpts.Verbose

logLvl := logrus.InfoLevel
if u.verbose {
logLvl = logrus.DebugLevel
Expand Down Expand Up @@ -179,6 +188,7 @@ func (u *Upload) Execute(data map[string][]interface{}, inputArgs []string) (map
}
}
result["certificateAuthorities"] = cas
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreated %d CertificateAuthorities\r\n", len(cas))

externalJwtSigners := map[string]string{}
if all ||
Expand All @@ -200,6 +210,7 @@ func (u *Upload) Execute(data map[string][]interface{}, inputArgs []string) (map
Debug("ExtJWTSigners created")
}
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreated %d ExtJWTSigners\r\n", len(externalJwtSigners))
result["externalJwtSigners"] = externalJwtSigners

authPolicies := map[string]string{}
Expand All @@ -221,6 +232,7 @@ func (u *Upload) Execute(data map[string][]interface{}, inputArgs []string) (map
Debug("AuthPolicies created")
}
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreated %d AuthPolicies\r\n", len(authPolicies))
result["authPolicies"] = authPolicies

identities := map[string]string{}
Expand All @@ -241,6 +253,7 @@ func (u *Upload) Execute(data map[string][]interface{}, inputArgs []string) (map
Debug("Identities created")
}
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreated %d Identities\r\n", len(identities))
result["identities"] = identities

configTypes := map[string]string{}
Expand All @@ -263,6 +276,7 @@ func (u *Upload) Execute(data map[string][]interface{}, inputArgs []string) (map
Debug("ConfigTypes created")
}
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreated %d ConfigTypes\r\n", len(configTypes))
result["configTypes"] = configTypes

configs := map[string]string{}
Expand All @@ -284,6 +298,7 @@ func (u *Upload) Execute(data map[string][]interface{}, inputArgs []string) (map
Debug("Configs created")
}
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreated %d Configs\r\n", len(configs))
result["configs"] = configs

services := map[string]string{}
Expand All @@ -304,6 +319,7 @@ func (u *Upload) Execute(data map[string][]interface{}, inputArgs []string) (map
Debug("Services created")
}
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreated %d Services\r\n", len(services))
result["services"] = services

postureChecks := map[string]string{}
Expand All @@ -324,6 +340,7 @@ func (u *Upload) Execute(data map[string][]interface{}, inputArgs []string) (map
Debug("PostureChecks created")
}
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreated %d PostureChecks\r\n", len(postureChecks))
result["postureChecks"] = postureChecks

routers := map[string]string{}
Expand All @@ -345,6 +362,7 @@ func (u *Upload) Execute(data map[string][]interface{}, inputArgs []string) (map
Debug("EdgeRouters created")
}
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreated %d EdgeRouters\r\n", len(routers))
result["edgeRouters"] = routers

serviceEdgeRouterPolicies := map[string]string{}
Expand All @@ -365,6 +383,7 @@ func (u *Upload) Execute(data map[string][]interface{}, inputArgs []string) (map
Debug("ServiceEdgeRouterPolicies created")
}
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreated %d ServiceEdgeRouterPolicies\r\n", len(serviceEdgeRouterPolicies))
result["serviceEdgeRouterPolicies"] = serviceEdgeRouterPolicies

servicePolicies := map[string]string{}
Expand All @@ -385,6 +404,7 @@ func (u *Upload) Execute(data map[string][]interface{}, inputArgs []string) (map
Debug("ServicePolicies created")
}
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreated %d ServicePolicies\r\n", len(servicePolicies))
result["servicePolicies"] = servicePolicies

routerPolicies := map[string]string{}
Expand All @@ -405,6 +425,7 @@ func (u *Upload) Execute(data map[string][]interface{}, inputArgs []string) (map
Debug("EdgeRouterPolicies created")
}
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreated %d EdgeRouterPolicies\r\n", len(routerPolicies))
result["edgeRouterPolicies"] = routerPolicies

if u.verbose {
Expand Down
3 changes: 3 additions & 0 deletions ziti/cmd/ascode/upload/upload_auth_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package upload

import (
"encoding/json"
"fmt"
"github.com/antchfx/jsonquery"
"github.com/openziti/edge-api/rest_management_api_client/auth_policy"
"github.com/openziti/edge-api/rest_model"
Expand Down Expand Up @@ -47,6 +48,7 @@ func (u *Upload) ProcessAuthPolicies(input map[string][]interface{}) (map[string
"authPolicyId": *existing.ID,
}).Info("Found existing Auth Policy, skipping create")
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KSkipping AuthPolicy %s\r", *create.Name)
continue
}

Expand Down Expand Up @@ -75,6 +77,7 @@ func (u *Upload) ProcessAuthPolicies(input map[string][]interface{}) (map[string
create.Primary.ExtJWT.AllowedSigners = allowedSignerIds

// do the actual create since it doesn't exist
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreating AuthPolicy %s\r", *create.Name)
if u.verbose {
log.WithField("name", *create.Name).
Debug("Creating AuthPolicy")
Expand Down
3 changes: 3 additions & 0 deletions ziti/cmd/ascode/upload/upload_certificate_authorities.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package upload

import (
"fmt"
"github.com/openziti/edge-api/rest_management_api_client/certificate_authority"
"github.com/openziti/edge-api/rest_model"
"github.com/openziti/edge-api/rest_util"
Expand All @@ -39,10 +40,12 @@ func (u *Upload) ProcessCertificateAuthorities(input map[string][]interface{}) (
}).
Info("Found existing CertificateAuthority, skipping create")
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KSkipping CertificateAuthority %s\r", *create.Name)
continue
}

// do the actual create since it doesn't exist
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreating CertificateAuthority %s\r", *create.Name)
created, createErr := u.client.CertificateAuthority.CreateCa(&certificate_authority.CreateCaParams{Ca: create}, nil)
if createErr != nil {
if payloadErr, ok := createErr.(rest_util.ApiErrorPayload); ok {
Expand Down
3 changes: 3 additions & 0 deletions ziti/cmd/ascode/upload/upload_config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package upload

import (
"fmt"
"github.com/openziti/edge-api/rest_management_api_client/config"
"github.com/openziti/edge-api/rest_model"
"github.com/openziti/edge-api/rest_util"
Expand All @@ -39,10 +40,12 @@ func (u *Upload) ProcessConfigTypes(input map[string][]interface{}) (map[string]
}).
Info("Found existing ConfigType, skipping create")
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KSkipping ConfigType %s\r", *create.Name)
continue
}

// do the actual create since it doesn't exist
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreating ConfigType %s\r", *create.Name)
if u.verbose {
log.WithField("name", *create.Name).
Debug("Creating ConfigType")
Expand Down
3 changes: 3 additions & 0 deletions ziti/cmd/ascode/upload/upload_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package upload
import (
"encoding/json"
"errors"
"fmt"
"github.com/antchfx/jsonquery"
"github.com/openziti/edge-api/rest_management_api_client/config"
"github.com/openziti/edge-api/rest_model"
Expand All @@ -45,6 +46,7 @@ func (u *Upload) ProcessConfigs(input map[string][]interface{}) (map[string]stri
}).
Info("Found existing Config, skipping create")
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KSkipping Config %s\r", *create.Name)
continue
}

Expand All @@ -67,6 +69,7 @@ func (u *Upload) ProcessConfigs(input map[string][]interface{}) (map[string]stri
create.ConfigTypeID = configType.(*rest_model.ConfigTypeDetail).ID

// do the actual create since it doesn't exist
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreating Config %s\r", *create.Name)
if u.verbose {
log.WithField("name", *create.Name).Debug("Creating Config")
}
Expand Down
3 changes: 3 additions & 0 deletions ziti/cmd/ascode/upload/upload_edgerouter_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package upload

import (
"fmt"
"github.com/openziti/edge-api/rest_management_api_client/edge_router_policy"
"github.com/openziti/edge-api/rest_model"
"github.com/openziti/edge-api/rest_util"
Expand All @@ -37,6 +38,7 @@ func (u *Upload) ProcessEdgeRouterPolicies(input map[string][]interface{}) (map[
"edgeRouterPolicyId": *existing.ID,
}).
Info("Found existing EdgeRouterPolicy, skipping create")
_, _ = fmt.Fprintf(u.Err, "\u001B[2KSkipping EdgeRouterPolicy %s\r", *create.Name)
continue
}

Expand All @@ -55,6 +57,7 @@ func (u *Upload) ProcessEdgeRouterPolicies(input map[string][]interface{}) (map[
create.IdentityRoles = identityRoles

// do the actual create since it doesn't exist
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreating EdgeRouterPolicy %s\r", *create.Name)
if u.verbose {
log.WithField("name", *create.Name).Debug("Creating EdgeRouterPolicy")
}
Expand Down
3 changes: 3 additions & 0 deletions ziti/cmd/ascode/upload/upload_edgerouters.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package upload

import (
"errors"
"fmt"
"github.com/openziti/edge-api/rest_management_api_client/edge_router"
"github.com/openziti/edge-api/rest_model"
"github.com/openziti/edge-api/rest_util"
Expand All @@ -39,10 +40,12 @@ func (u *Upload) ProcessEdgeRouters(input map[string][]interface{}) (map[string]
"edgeRouterId": *existing.ID,
}).
Info("Found existing EdgeRouter, skipping create")
_, _ = fmt.Fprintf(u.Err, "\u001B[2KSkipping EdgeRouter %s\r", *create.Name)
continue
}

// do the actual create since it doesn't exist
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreating EdgeRouterPolicy %s\r", *create.Name)
if u.verbose {
log.WithField("name", *create.Name).Debug("Creating EdgeRouter")
}
Expand Down
3 changes: 3 additions & 0 deletions ziti/cmd/ascode/upload/upload_external_jwt_signers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package upload

import (
"fmt"
"github.com/openziti/edge-api/rest_management_api_client/external_jwt_signer"
"github.com/openziti/edge-api/rest_model"
"github.com/openziti/edge-api/rest_util"
Expand All @@ -39,10 +40,12 @@ func (u *Upload) ProcessExternalJwtSigners(input map[string][]interface{}) (map[
}).
Info("Found existing ExtJWTSigner, skipping create")
}
_, _ = fmt.Fprintf(u.Err, "\u001B[2KSkipping ExtJWTSigner %s\r", *create.Name)
continue
}

// do the actual create since it doesn't exist
_, _ = fmt.Fprintf(u.Err, "\u001B[2KCreating ExtJWTSigner %s\r", *create.Name)
if u.verbose {
log.WithField("name", *create.Name).Debug("Creating ExtJWTSigner")
}
Expand Down
Loading

0 comments on commit 8f3c4e5

Please sign in to comment.