Skip to content

Commit

Permalink
feat: add sync command [rules engine only]
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickMenoti committed Jun 11, 2024
1 parent 63fc865 commit d3fb7b7
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
5 changes: 5 additions & 0 deletions messages/sync/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sync

var (
ERRORSYNC = "Failed to synchronize local resources with remote resources: %s"
)
9 changes: 9 additions & 0 deletions messages/sync/messages.go
Original file line number Diff line number Diff line change
@@ -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"
)
2 changes: 2 additions & 0 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
83 changes: 83 additions & 0 deletions pkg/cmd/sync/sync.go
Original file line number Diff line number Diff line change
@@ -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
}
59 changes: 59 additions & 0 deletions pkg/cmd/sync/tasks.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions pkg/contracts/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,8 @@ type RulesEngineBehaviorEntry struct {
RulesEngineBehaviorObject *sdk.RulesEngineBehaviorObject
RulesEngineBehaviorString *sdk.RulesEngineBehaviorString
}

type SyncOpts struct {
RuleIds map[string]int64
Conf *AzionApplicationOptions
}

0 comments on commit d3fb7b7

Please sign in to comment.