From d3fb7b775154e144b2dea2798a18b800e950d27f Mon Sep 17 00:00:00 2001 From: PatrickMenoti <82882574+PatrickMenoti@users.noreply.github.com> Date: Tue, 11 Jun 2024 14:09:25 -0300 Subject: [PATCH] feat: add sync command [rules engine only] --- messages/sync/errors.go | 5 +++ messages/sync/messages.go | 9 +++++ pkg/cmd/root/root.go | 2 + pkg/cmd/sync/sync.go | 83 ++++++++++++++++++++++++++++++++++++++ pkg/cmd/sync/tasks.go | 59 +++++++++++++++++++++++++++ pkg/contracts/contracts.go | 5 +++ 6 files changed, 163 insertions(+) create mode 100644 messages/sync/errors.go create mode 100644 messages/sync/messages.go create mode 100644 pkg/cmd/sync/sync.go create mode 100644 pkg/cmd/sync/tasks.go diff --git a/messages/sync/errors.go b/messages/sync/errors.go new file mode 100644 index 000000000..a9777a095 --- /dev/null +++ b/messages/sync/errors.go @@ -0,0 +1,5 @@ +package sync + +var ( + ERRORSYNC = "Failed to synchronize local resources with remote resources: %s" +) diff --git a/messages/sync/messages.go b/messages/sync/messages.go new file mode 100644 index 000000000..1bb03999b --- /dev/null +++ b/messages/sync/messages.go @@ -0,0 +1,9 @@ +package sync + +const ( + USAGE = "sync" + SHORTDESCRIPTION = "Synchronizes local azion.json file with remote resources" + LONGDESCRIPTION = "Synchronizes your local file containing your created resources with remote resources" + SYNCMESSAGERULE = "Adding out of sync rule '%s' to your azion.json file\n" + HELPFLAG = "Displays more information about the sync command" +) diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index 52f0e33f4..da1fced79 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -19,6 +19,7 @@ import ( logcmd "github.com/aziontech/azion-cli/pkg/cmd/logs" "github.com/aziontech/azion-cli/pkg/cmd/purge" "github.com/aziontech/azion-cli/pkg/cmd/reset" + "github.com/aziontech/azion-cli/pkg/cmd/sync" "github.com/aziontech/azion-cli/pkg/cmd/unlink" "github.com/aziontech/azion-cli/pkg/cmd/update" "github.com/aziontech/azion-cli/pkg/cmd/whoami" @@ -151,6 +152,7 @@ func NewCobraCmd(rootCmd *RootCmd, f *cmdutil.Factory) *cobra.Command { cobraCmd.AddCommand(whoami.NewCmd(f)) cobraCmd.AddCommand(purge.NewCmd(f)) cobraCmd.AddCommand(reset.NewCmd(f)) + cobraCmd.AddCommand(sync.NewCmd(f)) return cobraCmd } diff --git a/pkg/cmd/sync/sync.go b/pkg/cmd/sync/sync.go new file mode 100644 index 000000000..79f90ea8c --- /dev/null +++ b/pkg/cmd/sync/sync.go @@ -0,0 +1,83 @@ +package sync + +import ( + "github.com/MakeNowJust/heredoc" + msg "github.com/aziontech/azion-cli/messages/sync" + "github.com/aziontech/azion-cli/pkg/cmdutil" + "github.com/aziontech/azion-cli/pkg/contracts" + "github.com/aziontech/azion-cli/pkg/iostreams" + "github.com/aziontech/azion-cli/pkg/logger" + "github.com/aziontech/azion-cli/utils" + "github.com/spf13/cobra" + "go.uber.org/zap" +) + +var ( + isFirewall bool +) + +type SyncCmd struct { + Io *iostreams.IOStreams + GetAzionJsonContent func() (*contracts.AzionApplicationOptions, error) + WriteAzionJsonContent func(conf *contracts.AzionApplicationOptions) error + F *cmdutil.Factory +} + +func NewDevCmd(f *cmdutil.Factory) *SyncCmd { + return &SyncCmd{ + F: f, + Io: f.IOStreams, + GetAzionJsonContent: utils.GetAzionJsonContent, + WriteAzionJsonContent: utils.WriteAzionJsonContent, + } +} + +func NewCobraCmd(sync *SyncCmd) *cobra.Command { + syncCmd := &cobra.Command{ + Use: msg.USAGE, + Short: msg.SHORTDESCRIPTION, + Long: msg.LONGDESCRIPTION, + SilenceUsage: true, + SilenceErrors: true, + Example: heredoc.Doc(` + $ azion sync + $ azion sync --help + `), + RunE: func(cmd *cobra.Command, args []string) error { + return sync.Run(sync.F) + }, + } + syncCmd.Flags().BoolP("help", "h", false, msg.HELPFLAG) + return syncCmd +} + +func NewCmd(f *cmdutil.Factory) *cobra.Command { + return NewCobraCmd(NewDevCmd(f)) +} + +func (cmd *SyncCmd) Run(f *cmdutil.Factory) error { + logger.Debug("Running sync command") + + conf, err := cmd.GetAzionJsonContent() + if err != nil { + logger.Debug("Failed to get Azion JSON content", zap.Error(err)) + return err + } + + ruleIds := make(map[string]int64) + for _, ruleConf := range conf.RulesEngine.Rules { + ruleIds[ruleConf.Name] = ruleConf.Id + } + + info := contracts.SyncOpts{ + RuleIds: ruleIds, + Conf: conf, + } + + err = cmd.SyncResources(f, info) + if err != nil { + return err + } + + return nil +} diff --git a/pkg/cmd/sync/tasks.go b/pkg/cmd/sync/tasks.go new file mode 100644 index 000000000..12514aead --- /dev/null +++ b/pkg/cmd/sync/tasks.go @@ -0,0 +1,59 @@ +package sync + +import ( + "context" + "fmt" + + msg "github.com/aziontech/azion-cli/messages/sync" + api "github.com/aziontech/azion-cli/pkg/api/edge_applications" + "github.com/aziontech/azion-cli/pkg/cmdutil" + "github.com/aziontech/azion-cli/pkg/contracts" + "github.com/aziontech/azion-cli/pkg/logger" + "go.uber.org/zap" +) + +var ( + C context.Context + Opts *contracts.ListOptions +) + +func (synch *SyncCmd) SyncResources(f *cmdutil.Factory, info contracts.SyncOpts) error { + C = context.Background() + Opts = &contracts.ListOptions{ + PageSize: 1000, + Page: 1, + } + err := synch.syncRules(info, f) + if err != nil { + return fmt.Errorf(msg.ERRORSYNC, err.Error()) + } + return nil +} + +func (synch *SyncCmd) syncRules(info contracts.SyncOpts, f *cmdutil.Factory) error { + + client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token")) + resp, err := client.ListRulesEngine(C, Opts, info.Conf.Application.ID, "request") + if err != nil { + return err + } + + for _, rule := range resp.Results { + if id := info.RuleIds[rule.Name]; id > 0 || rule.Name == "Default Rule" { + // if remote rule is also on local environment, no action is needed + continue + } + newRule := contracts.AzionJsonDataRules{ + Id: rule.GetId(), + Name: rule.GetName(), + } + info.Conf.RulesEngine.Rules = append(info.Conf.RulesEngine.Rules, newRule) + err := synch.WriteAzionJsonContent(info.Conf) + if err != nil { + logger.Debug("Error while writing azion.json file", zap.Error(err)) + return err + } + logger.FInfo(synch.Io.Out, fmt.Sprintf(msg.SYNCMESSAGERULE, rule.Name)) + } + return nil +} diff --git a/pkg/contracts/contracts.go b/pkg/contracts/contracts.go index 8c490c622..1a18087e2 100644 --- a/pkg/contracts/contracts.go +++ b/pkg/contracts/contracts.go @@ -196,3 +196,8 @@ type RulesEngineBehaviorEntry struct { RulesEngineBehaviorObject *sdk.RulesEngineBehaviorObject RulesEngineBehaviorString *sdk.RulesEngineBehaviorString } + +type SyncOpts struct { + RuleIds map[string]int64 + Conf *AzionApplicationOptions +}