Skip to content

Commit

Permalink
Merge pull request #906 from aziontech/dev
Browse files Browse the repository at this point in the history
Deploy to Production - 2024/07/22
  • Loading branch information
PatrickMenoti authored Jul 22, 2024
2 parents 295f9fe + f1e9061 commit b93111e
Show file tree
Hide file tree
Showing 16 changed files with 1,868 additions and 164 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ changelog:
regexp: ^.*?docs(\([[:word:]]+\))??!?:.+$
order: 50
- title: "Tests"
regexp: ^.*?tests(\([[:word:]]+\))??!?:.+$
regexp: ^.*?(test(s)?(\([[:word:]]+\))?)!?:.+$
order: 60
- title: Other Work
order: 999
2 changes: 1 addition & 1 deletion pkg/cmd/create/edge_function/edge_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func createRequestFromFlags(cmd *cobra.Command, fields *Fields, request *api.Cre
}

if !cmd.Flags().Changed("active") {
answers, err := utils.Select(msg.AskActive, []string{"true", "false"})
answers, err := utils.Select(utils.NewSelectPrompter(msg.AskActive, []string{"true", "false"}))
if err != nil {
logger.Debug("Error while parsing answer", zap.Error(err))
return utils.ErrorParseResponse
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/create/edge_storage/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ func (fields *FieldsBucket) CreateRequestFromFlags(cmd *cobra.Command, request *
}
if !cmd.Flags().Changed("edge-access") {
answers, err := utils.Select(
msg.ASK_EDGE_ACCESSS_CREATE_BUCKET,
[]string{string(sdk.READ_ONLY), string(sdk.READ_WRITE), string(sdk.RESTRICTED)})
utils.NewSelectPrompter(
msg.ASK_EDGE_ACCESSS_CREATE_BUCKET,
[]string{string(sdk.READ_ONLY), string(sdk.READ_WRITE), string(sdk.RESTRICTED)}))
if err != nil {
logger.Debug("Error while parsing answer", zap.Error(err))
return utils.ErrorParseResponse
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/create/origin/origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func createRequestFromFlags(cmd *cobra.Command, fields *Fields, request *api.Cre
request.SetName(fields.Name)

if !cmd.Flags().Changed("origin-type") {
answer, err := utils.Select(msg.AskOriginType, []string{"single_origin", "object_storage"})
answer, err := utils.Select(utils.NewSelectPrompter(msg.AskOriginType, []string{"single_origin", "object_storage"}))
if err != nil {
return err
}
Expand Down
46 changes: 17 additions & 29 deletions pkg/cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"path/filepath"
"strconv"
"strings"

"github.com/MakeNowJust/heredoc"
msg "github.com/aziontech/azion-cli/messages/deploy"
Expand Down Expand Up @@ -199,40 +198,29 @@ func (cmd *DeployCmd) Run(f *cmdutil.Factory) error {
logger.Debug("Error while getting default rules engine", zap.Error(err))
return err
}
behaviors := make([]sdk.RulesEngineBehaviorEntry, 0)

if strings.ToLower(conf.Preset) == "javascript" || strings.ToLower(conf.Preset) == "typescript" {
reqRules := apiEdgeApplications.UpdateRulesEngineRequest{}
reqRules.IdApplication = conf.Application.ID
var behString sdk.RulesEngineBehaviorString
behString.SetName("set_origin")

_, err := clients.EdgeApplication.UpdateRulesEnginePublish(ctx, &reqRules, conf.Function.InstanceID)
if err != nil {
return err
}
} else {
behaviors := make([]sdk.RulesEngineBehaviorEntry, 0)
behString.SetTarget(strconv.Itoa(int(singleOriginId)))

var behString sdk.RulesEngineBehaviorString
behString.SetName("set_origin")
behaviors = append(behaviors, sdk.RulesEngineBehaviorEntry{
RulesEngineBehaviorString: &behString,
})

behString.SetTarget(strconv.Itoa(int(singleOriginId)))

behaviors = append(behaviors, sdk.RulesEngineBehaviorEntry{
RulesEngineBehaviorString: &behString,
})

reqUpdateRulesEngine := apiEdgeApplications.UpdateRulesEngineRequest{
IdApplication: conf.Application.ID,
Phase: "request",
Id: ruleDefaultID,
}
reqUpdateRulesEngine := apiEdgeApplications.UpdateRulesEngineRequest{
IdApplication: conf.Application.ID,
Phase: "request",
Id: ruleDefaultID,
}

reqUpdateRulesEngine.SetBehaviors(behaviors)
reqUpdateRulesEngine.SetBehaviors(behaviors)

_, err = clients.EdgeApplication.UpdateRulesEngine(ctx, &reqUpdateRulesEngine)
if err != nil {
logger.Debug("Error while updating default rules engine", zap.Error(err))
return err
}
_, err = clients.EdgeApplication.UpdateRulesEngine(ctx, &reqUpdateRulesEngine)
if err != nil {
logger.Debug("Error while updating default rules engine", zap.Error(err))
return err
}
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/deploy/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func (cmd *DeployCmd) PurgeUrls(domain []string, path string) error {
}

func PurgeForUpdatedFiles(cmd *DeployCmd, domain apidom.DomainResponse, confPath string, msgs *[]string) error {
if _, err := os.Stat(PathStatic); os.IsNotExist(err) {
return nil
}
listURLsDomains := domain.GetCnames()
if !domain.GetCnameAccessOnly() {
listURLsDomains = append(listURLsDomains, domain.GetDomainName())
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/deploy/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (cmd *DeployCmd) doOriginSingle(
ctx context.Context,
conf *contracts.AzionApplicationOptions,
msgs *[]string) (int64, error) {
var DefaultOrigin = [1]string{"httpbin.org"}
var DefaultOrigin = [1]string{"api.azion.net"}

if conf.NotFirstRun {
return 0, nil
Expand Down
11 changes: 3 additions & 8 deletions pkg/cmd/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
msg "github.com/aziontech/azion-cli/messages/dev"
"github.com/aziontech/azion-cli/pkg/cmd/build"
"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/pkg/output"
vulcanPkg "github.com/aziontech/azion-cli/pkg/vulcan"
"github.com/aziontech/azion-cli/utils"
"github.com/spf13/cobra"
)
Expand All @@ -25,6 +25,7 @@ type DevCmd struct {
CommandRunInteractive func(f *cmdutil.Factory, comm string) error
BuildCmd func(f *cmdutil.Factory) *build.BuildCmd
F *cmdutil.Factory
Vulcan func() *vulcanPkg.VulcanPkg
}

func NewDevCmd(f *cmdutil.Factory) *DevCmd {
Expand All @@ -35,6 +36,7 @@ func NewDevCmd(f *cmdutil.Factory) *DevCmd {
CommandRunInteractive: func(f *cmdutil.Factory, comm string) error {
return utils.CommandRunInteractive(f, comm)
},
Vulcan: vulcanPkg.NewVulcan,
}
}

Expand Down Expand Up @@ -74,13 +76,6 @@ func (cmd *DevCmd) Run(f *cmdutil.Factory) error {
return output.Print(&outGen)
}

contract := &contracts.BuildInfo{}

if isFirewall {
contract.IsFirewall = isFirewall
contract.OwnWorker = "true"
}

err := vulcan(cmd, isFirewall)
if err != nil {
return err
Expand Down
93 changes: 93 additions & 0 deletions pkg/cmd/dev/dev_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package dev

import (
"errors"
"testing"

"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/httpmock"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/pkg/testutils"
vulcanPkg "github.com/aziontech/azion-cli/pkg/vulcan"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
)

func TestDev(t *testing.T) {
logger.New(zapcore.DebugLevel)
tests := []struct {
name string
mockVulcan func() *vulcanPkg.VulcanPkg
mockCommandRun func(f *cmdutil.Factory, comm string) error
isFirewall bool
expectedError error
}{
{
name: "dev - successful execution without firewall",
mockVulcan: func() *vulcanPkg.VulcanPkg {
return &vulcanPkg.VulcanPkg{
Command: func(flags, params string, f *cmdutil.Factory) string {
return "echo 1"
},
}
},
mockCommandRun: func(f *cmdutil.Factory, comm string) error {
return nil
},
isFirewall: false,
expectedError: nil,
},
{
name: "dev - successful execution with firewall",
mockVulcan: func() *vulcanPkg.VulcanPkg {
return &vulcanPkg.VulcanPkg{
Command: func(flags, params string, f *cmdutil.Factory) string {
return "echo 1"
},
}
},
mockCommandRun: func(f *cmdutil.Factory, comm string) error {
return nil
},
isFirewall: true,
expectedError: nil,
},
{
name: "dev - failed command execution",
mockVulcan: func() *vulcanPkg.VulcanPkg {
return &vulcanPkg.VulcanPkg{
Command: func(flags, params string, f *cmdutil.Factory) string {
return "echo 1"
},
}
},
mockCommandRun: func(f *cmdutil.Factory, comm string) error {
return errors.New("failed to run command")
},
isFirewall: false,
expectedError: errors.New("Error executing Vulcan: Failed to run dev command. Verify if the command is correct and check the output above for more details. Run the 'azion dev' command again or contact Azion's support"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mock := &httpmock.Registry{}
f, _, _ := testutils.NewFactory(mock)

devCmd := NewDevCmd(f)
devCmd.Vulcan = tt.mockVulcan
devCmd.CommandRunInteractive = tt.mockCommandRun

isFirewall = tt.isFirewall

err := devCmd.Run(f)
if tt.expectedError != nil {
require.Error(t, err)
assert.Equal(t, tt.expectedError.Error(), err.Error())
} else {
require.NoError(t, err)
}
})
}
}
3 changes: 1 addition & 2 deletions pkg/cmd/dev/vulcan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (

msg "github.com/aziontech/azion-cli/messages/dev"
"github.com/aziontech/azion-cli/pkg/logger"
vulcanPkg "github.com/aziontech/azion-cli/pkg/vulcan"
"go.uber.org/zap"
)

func vulcan(cmd *DevCmd, isFirewall bool) error {

vul := vulcanPkg.NewVulcan()
vul := cmd.Vulcan()
command := vul.Command("", "dev", cmd.F)
if isFirewall {
command = vul.Command("", "dev --firewall", cmd.F)
Expand Down
1 change: 0 additions & 1 deletion pkg/cmd/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func TestSync(t *testing.T) {
syncCmd.GetAzionJsonContent = tt.mockGetContentFunc
syncCmd.WriteAzionJsonContent = tt.mockWriteFunc

// Replace syncResources function with mock
syncCmd.SyncResources = tt.mockSyncResources

err := Sync(syncCmd)
Expand Down
7 changes: 4 additions & 3 deletions pkg/cmd/sync/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ func (synch *SyncCmd) syncRules(info contracts.SyncOpts, f *cmdutil.Factory) err
continue
}
newRule := contracts.AzionJsonDataRules{
Id: rule.GetId(),
Name: rule.GetName(),
Id: rule.GetId(),
Name: rule.GetName(),
Phase: rule.GetPhase(),
}
info.Conf.RulesEngine.Rules = append(info.Conf.RulesEngine.Rules, newRule)
err := synch.WriteAzionJsonContent(info.Conf, ProjectConf)
Expand All @@ -145,7 +146,7 @@ func (synch *SyncCmd) syncEnv(f *cmdutil.Factory) error {
envs, err := godotenv.Read(synch.EnvPath)
if err != nil {
logger.Debug("Error while loading .env file", zap.Error(err))
return err
return nil // not every project has a .env file... this should not stop the execution
}

for _, variable := range resp {
Expand Down
6 changes: 4 additions & 2 deletions pkg/cmd/update/edge_storage/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ func (b *bucket) createRequestFromFlags(cmd *cobra.Command, request *api.Request
b.name = answers
}
if !cmd.Flags().Changed("edge-access") {

answers, err := utils.Select(
msg.ASK_EDGE_ACCESSS_CREATE_BUCKET,
[]string{string(sdk.READ_ONLY), string(sdk.READ_WRITE), string(sdk.RESTRICTED)})
utils.NewSelectPrompter(
msg.ASK_EDGE_ACCESSS_CREATE_BUCKET,
[]string{string(sdk.READ_ONLY), string(sdk.READ_WRITE), string(sdk.RESTRICTED)}))
if err != nil {
logger.Debug("Error while parsing answer", zap.Error(err))
return utils.ErrorParseResponse
Expand Down
12 changes: 6 additions & 6 deletions pkg/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (man *ManifestInterpreter) CreateResources(
}
updated, err := clientOrigin.Update(ctx, conf.Application.ID, OriginKeys[origin.Name], requestUpdate)
if err != nil {
return fmt.Errorf("%w: %s", msg.ErrorUpdateOrigin, err.Error())
return fmt.Errorf("%w - '%s': %s", msg.ErrorUpdateOrigin, updated.GetName(), err.Error())
}

newEntry := contracts.AzionJsonDataOrigin{
Expand All @@ -142,7 +142,7 @@ func (man *ManifestInterpreter) CreateResources(
}
created, err := clientOrigin.Create(ctx, conf.Application.ID, requestCreate)
if err != nil {
return fmt.Errorf("%w: %s", msg.ErrorCreateOrigin, err.Error())
return fmt.Errorf("%w - '%s': %s", msg.ErrorCreateOrigin, created.GetName(), err.Error())
}
newOrigin := contracts.AzionJsonDataOrigin{
OriginId: created.GetOriginId(),
Expand Down Expand Up @@ -177,7 +177,7 @@ func (man *ManifestInterpreter) CreateResources(
}
updated, err := clientCache.Update(ctx, requestUpdate, conf.Application.ID, id)
if err != nil {
return fmt.Errorf("%w: %s", msg.ErrorUpdateCache, err.Error())
return fmt.Errorf("%w - '%s': %s", msg.ErrorUpdateCache, updated.GetName(), err.Error())
}
newCache := contracts.AzionJsonDataCacheSettings{
Id: updated.GetId(),
Expand All @@ -196,7 +196,7 @@ func (man *ManifestInterpreter) CreateResources(
}
created, err := clientCache.Create(ctx, requestUpdate, conf.Application.ID)
if err != nil {
return fmt.Errorf("%w: %s", msg.ErrorCreateCache, err.Error())
return fmt.Errorf("%w - '%s': %s", msg.ErrorCreateCache, created.GetName(), err.Error())
}
newCache := contracts.AzionJsonDataCacheSettings{
Id: created.GetId(),
Expand Down Expand Up @@ -236,7 +236,7 @@ func (man *ManifestInterpreter) CreateResources(
requestUpdate.IdApplication = conf.Application.ID
updated, err := client.UpdateRulesEngine(ctx, requestUpdate)
if err != nil {
return fmt.Errorf("%w: %s", msg.ErrorUpdateRule, err.Error())
return fmt.Errorf("%w - '%s': %s", msg.ErrorUpdateRule, updated.GetName(), err.Error())
}
newRule := contracts.AzionJsonDataRules{
Id: updated.GetId(),
Expand All @@ -262,7 +262,7 @@ func (man *ManifestInterpreter) CreateResources(
requestCreate.Order = &rule.Order
created, err := client.CreateRulesEngine(ctx, conf.Application.ID, rule.Phase, requestCreate)
if err != nil {
return fmt.Errorf("%w: %s", msg.ErrorCreateRule, err.Error())
return fmt.Errorf("%w - '%s': %s", msg.ErrorCreateRule, created.GetName(), err.Error())
}
newRule := contracts.AzionJsonDataRules{
Id: created.GetId(),
Expand Down
Loading

0 comments on commit b93111e

Please sign in to comment.