Skip to content

Commit

Permalink
Merge branch 'dev' into feat-origins-updated
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwelbm authored Feb 15, 2023
2 parents 9415ea6 + 7a4bf3a commit fcc8598
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 42 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deploy_prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ jobs:

- name: Build
env:
STORAGE_URL: ${{ secrets.STORAGE_API_STAGE }}
STORAGE_API: ${{ secrets.STORAGE_API_STAGE }}
run: make build ENVFILE=./env/prod

- name: Cross-build
env:
STORAGE_URL: ${{ secrets.STORAGE_API_STAGE }}
STORAGE_API: ${{ secrets.STORAGE_API_STAGE }}
run: make cross-build ENVFILE=./env/prod

- name: Install AWS CLI
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/deploy_stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ jobs:
env:
AZION_API: ${{ secrets.AZION_API_STAGE }}
AZION_SSO: ${{ secrets.AZION_SSO_STAGE }}
STORAGE_URL: ${{ secrets.STORAGE_API_STAGE }}
STORAGE_API: ${{ secrets.STORAGE_API_STAGE }}
run: make build ENVFILE=./env/stage

- name: Cross-build
env:
AZION_API: ${{ secrets.AZION_API_STAGE }}
AZION_SSO: ${{ secrets.AZION_SSO_STAGE }}
STORAGE_URL: ${{ secrets.STORAGE_API_STAGE }}
STORAGE_API: ${{ secrets.STORAGE_API_STAGE }}
run: make cross-build ENVFILE=./env/stage

- name: Install AWS CLI
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ BIN := azioncli
BIN_VERSION=$(shell git describe --tags)
# The variables with $$ should be sourced from an envfile
LDFLAGS=-X github.com/aziontech/azion-cli/pkg/cmd/version.BinVersion=$(BIN_VERSION) \
-X github.com/aziontech/azion-cli/pkg/constants.StorageApiURL=$$STORAGE_URL \
-X github.com/aziontech/azion-cli/pkg/constants.AuthURL=$$AUTH_URL \
-X github.com/aziontech/azion-cli/pkg/constants.ApiURL=$$API_URL \
-X github.com/aziontech/azion-cli/pkg/constants.StorageApiURL=$$STORAGE_URL \
-X github.com/aziontech/azion-cli/pkg/cmd/edge_applications/init.TemplateBranch=$$TEMPLATE_BRANCH \
-X github.com/aziontech/azion-cli/pkg/cmd/edge_applications/init.TemplateMajor=$$TEMPLATE_MAJOR
LDFLAGS_STRIP=-s -w
Expand Down
4 changes: 2 additions & 2 deletions env/stage
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
API_URL=https://${AZION_API}
AUTH_URL=https://${AZION_SSO}/api/user/me
STORAGE_URL=https://${STORAGE_API}
TEMPLATE_BRANCH=dev
TEMPLATE_MAJOR=0
STORAGE_URL=${STORAGE_API}
TEMPLATE_MAJOR=0
2 changes: 2 additions & 0 deletions messages/origins/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ var (
ErrorHmacAuthenticationFlag = errors.New("Invalid --hmac-authentication flag provided. The flag must have 'true' or 'false' values. Run the command 'azioncli domains <subcommand> --help' to display more information and try again.")
ErrorCreateOrigin = errors.New("Failed to create the Origin: %s. Check your settings and try again. If the error persists, contact Azion support.")
ErrorUpdateOrigin = errors.New("Failed to update the Origin: %s. Check your settings and try again. If the error persists, contact Azion support.")
ErrorFailToDelete = errors.New("Failed to delete the Origin: %s. Check your settings and try again. If the error persists, contact Azion support")
)

9 changes: 9 additions & 0 deletions messages/origins/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ var (
OriginsUpdateOutputSuccess = "Update origin with ID %s\n"
OriginsUpdateHelpFlag = "Displays more information about the create subcommand"

// [ delete ]
OriginsDeleteUsage = "delete [flags]"
OriginsDeleteShortDescription = "Removes an Origin"
OriginsDeleteLongDescription = "Removes an Origin from the Edge Applications library based on its given ID"
OriginsDeleteOutputSuccess = "Origin %s was successfully deleted\n"
OriginsDeleteFlagApplicationID = "The Edge Application's unique identifier"
OriginsDeleteFlagOriginKey = "The Origin key unique identifier"
OriginsDeleteHelpFlag = "Displays more information about the delete subcommand"

// [ general ]
OriginsFileWritten = "File successfully written to: %s\n"
)
10 changes: 10 additions & 0 deletions pkg/api/edge_applications/edge_applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,13 @@ func (c *Client) UpdateOrigins(ctx context.Context, edgeApplicationID int64, ori
}
return &resp.Results, nil
}

func (c *Client) DeleteOrigins(ctx context.Context, edgeApplicationID int64, originKey string) error {
httpResp, err:= c.apiClient.EdgeApplicationsOriginsApi.EdgeApplicationsEdgeApplicationIdOriginsOriginKeyDelete(ctx, edgeApplicationID, originKey).Execute()
if err != nil {
return utils.ErrorPerStatusCode(httpResp, err)
}
return nil
}


3 changes: 0 additions & 3 deletions pkg/api/storage_api/storage_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package storageapi

import (
"context"
"fmt"
"net/http"
"time"

Expand Down Expand Up @@ -44,8 +43,6 @@ func (c *Client) CreateVersionId(ctx context.Context, id string) (VersionIdRespo

res, httpResp, err := req.Execute()
if err != nil {
fmt.Println(err)
fmt.Println(httpResp.Body)
return nil, utils.ErrorPerStatusCode(httpResp, err)
}

Expand Down
17 changes: 7 additions & 10 deletions pkg/cmd/edge_applications/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,13 @@ func RunBuildCmdLine(cmd *BuildCmd, path string) error {
switch typeLang.String() {
case "nextjs", "flareact":

verId, err := cmd.GetVerId(cmd, applicationId.String())
if err != nil {
return err
var verId string = ""

if applicationId.String() != "" && applicationId.String() != "0" {
verId, err = cmd.GetVerId(cmd, applicationId.String())
if err != nil {
return err
}
}

jsonReplaceFunc, err := sjson.Set(string(file), "version-id", verId)
Expand Down Expand Up @@ -311,19 +315,12 @@ func writeWebdevEnvFile(cmd *BuildCmd, path string, envs []string) error {

func GetVersionID(cmd *BuildCmd, appID string) (string, error) {

fmt.Println("STAGE URL")
fmt.Println(cmd.f.Config.GetString("storage_url"))

fmt.Println("API URL")
fmt.Println(cmd.f.Config.GetString("api_url"))

client := api.NewClient(cmd.f.HttpClient, cmd.f.Config.GetString("storage_url"), cmd.f.Config.GetString("token"))

ctx := context.Background()

verId, err := client.CreateVersionId(ctx, appID)
if err != nil {
fmt.Println(err)
return "", fmt.Errorf(msg.ErrorGetVersionId.Error(), err)
}

Expand Down
3 changes: 0 additions & 3 deletions pkg/cmd/edge_applications/edge_applications_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edge_applications

import (
"fmt"
"io/fs"
"os"
"testing"
Expand Down Expand Up @@ -299,7 +298,6 @@ func TestEdgeApplicationsCmd(t *testing.T) {
cmdPublish := publishcmd.NewCobraCmd(publishCmd)

errPublish := cmdPublish.Execute()
fmt.Println(errPublish)

require.NoError(t, err)

Expand Down Expand Up @@ -465,7 +463,6 @@ func TestEdgeApplicationsCmd(t *testing.T) {
cmdPublish := publishcmd.NewCobraCmd(publishCmd)

errPublish := cmdPublish.Execute()
fmt.Println(errPublish)

require.NoError(t, err)

Expand Down
6 changes: 2 additions & 4 deletions pkg/cmd/edge_applications/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ func sortTag(tags ReferenceIter, major, branch string) (string, error) {
var tagWithMajorOKStr string
var err error
err = tags.ForEach(func(t *plumbing.Reference) error {
tagFormat := formatTag(string(t.Name()))
tagFormat = checkBranch(tagFormat, branch)
tagFormat := formatTag(string(t.Name()))
tagFormat = checkBranch(tagFormat, branch)
if tagFormat != "" {
if strings.Split(tagFormat, "")[0] == major {
var numberTag int
Expand Down Expand Up @@ -596,7 +596,6 @@ func initCdn(cmd *InitCmd, path string, info *InitInfo) error {
if shouldFetchTemplates {
pathWorker := path + "/azion"
if err := cmd.Mkdir(pathWorker, os.ModePerm); err != nil {
fmt.Println(err)
return msg.ErrorFailedCreatingAzionDirectory
}

Expand All @@ -612,7 +611,6 @@ func initCdn(cmd *InitCmd, path string, info *InitInfo) error {

err = cmd.WriteFile(path+"/azion/azion.json", data, 0644)
if err != nil {
fmt.Println(err)
return utils.ErrorInternalServerError
}

Expand Down
26 changes: 12 additions & 14 deletions pkg/cmd/edge_applications/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
"github.com/aziontech/azion-cli/utils"
"github.com/go-git/go-git/v5"

"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/mock"
)

func TestCobraCmd(t *testing.T) {
Expand Down Expand Up @@ -159,7 +159,6 @@ func TestCobraCmd(t *testing.T) {
f.IOStreams.In = io.NopCloser(in)

err := cmd.Execute()
// fmt.Println(err.Error())

require.NoError(t, err)
require.Contains(t, stdout.String(), `Template successfully fetched and configured`)
Expand Down Expand Up @@ -598,8 +597,8 @@ func Test_formatTag(t *testing.T) {
t.Errorf("formatTag() = %v, want %v", got, tt.want)
}
})
}
}
}

func Test_checkBranch(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -642,10 +641,9 @@ func Test_checkBranch(t *testing.T) {
t.Errorf("checkBranch() = %v, want %v", got, tt.want)
}
})
}
}
}


type mockReferenceIter struct {
mock.Mock
}
Expand All @@ -660,22 +658,22 @@ func Test_SortTag(t *testing.T) {

// defines the mock action
mockIter.On("ForEach", mock.Anything).Run(func(args mock.Arguments) {
f := args.Get(0).(func(*plumbing.Reference) error)
f := args.Get(0).(func(*plumbing.Reference) error)
refs := []*plumbing.Reference{
plumbing.NewReferenceFromStrings("refs/tags/v2.0.0", "beefdead"),
plumbing.NewReferenceFromStrings("refs/tags/v1.0.0", "deadbeef"),
plumbing.NewReferenceFromStrings("refs/tags/v1.0.0", "deadbeef"),
plumbing.NewReferenceFromStrings("refs/tags/v3.0.0", "deafbeef"),
}

for _, ref := range refs {
if err := f(ref); err != nil {
panic(err)
}
if err := f(ref); err != nil {
panic(err)
}
}
}).Return(nil)
}).Return(nil)

// call func sortTag with the mock
result, err := sortTag(mockIter, "2", "main")
result, err := sortTag(mockIter, "2", "main")
require.NoError(t, err)

expected := "refs/tags/v2.0.0"
Expand Down
46 changes: 46 additions & 0 deletions pkg/cmd/origins/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package delete

import (
"context"
"fmt"

"github.com/MakeNowJust/heredoc"
msg "github.com/aziontech/azion-cli/messages/origins"
api "github.com/aziontech/azion-cli/pkg/api/edge_applications"
"github.com/spf13/cobra"
"github.com/aziontech/azion-cli/pkg/cmdutil"
)



func NewCmd(f *cmdutil.Factory) *cobra.Command {
var applicationID int64
var originKey string
cmd := &cobra.Command{
Use: msg.OriginsDeleteUsage,
Short: msg.OriginsDeleteShortDescription,
Long: msg.OriginsDeleteLongDescription,
SilenceUsage: true,
SilenceErrors: true,
Example: heredoc.Doc(`
$ azioncli origins delete --application-id 1234 --origin-key 03a6e7bf-8e26-49c7-a66e-ab8eaa425086
$ azioncli origins delete -a 1234 -o 03a6e7bf-8e26-49c7-a66e-ab8eaa425086
`),
RunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Flags().Changed("application-id") || !cmd.Flags().Changed("origin-key") {
return msg.ErrorMissingArgumentsDelete
}
if err := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token")).
DeleteOrigins(context.Background(), applicationID, originKey); err != nil {
return fmt.Errorf(msg.ErrorFailToDelete.Error(), err)
}
fmt.Fprintf(f.IOStreams.Out, msg.OriginsDeleteOutputSuccess, originKey)
return nil
},
}

cmd.Flags().Int64VarP(&applicationID, "application-id", "a", 0, msg.OriginsDeleteFlagApplicationID)
cmd.Flags().StringVarP(&originKey, "origin-key", "o", "", msg.OriginsDeleteFlagOriginKey)
cmd.Flags().BoolP("help", "h", false, msg.OriginsDeleteHelpFlag)
return cmd
}
48 changes: 48 additions & 0 deletions pkg/cmd/origins/delete/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package delete

import (
"fmt"
"testing"

"github.com/aziontech/azion-cli/pkg/httpmock"
"github.com/aziontech/azion-cli/pkg/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
msg "github.com/aziontech/azion-cli/messages/origins"
)

func TestCreate(t *testing.T) {

t.Run("delete domain by id", func(t *testing.T) {
mock := &httpmock.Registry{}

mock.Register(
httpmock.REST("DELETE", "edge_applications/1673635839/origins/58755fef-e830-4ea4-b9e0-6481f1ef496d"),
httpmock.StatusStringResponse(204, ""),
)

f, stdout, _ := testutils.NewFactory(mock)
cmd := NewCmd(f)
cmd.SetArgs([]string{"--application-id", "1673635839", "--origin-key", "58755fef-e830-4ea4-b9e0-6481f1ef496d"})

_, err := cmd.ExecuteC()
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf(msg.OriginsDeleteOutputSuccess, "58755fef-e830-4ea4-b9e0-6481f1ef496d"), stdout.String())
})

t.Run("delete domain that is not found", func(t *testing.T) {
mock := &httpmock.Registry{}

mock.Register(
httpmock.REST("DELETE", "edge_applications/1673635839/origins/58755fef-e830-4ea4-b9e0-6481f1ef497a"),
httpmock.StatusStringResponse(404, "Not Found"),
)

f, _, _ := testutils.NewFactory(mock)
cmd := NewCmd(f)
cmd.SetArgs([]string{"-d", "1234"})

_, err := cmd.ExecuteC()
require.Error(t, err)
})
}
3 changes: 2 additions & 1 deletion pkg/cmd/origins/origins.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/aziontech/azion-cli/pkg/cmd/origins/list"
"github.com/aziontech/azion-cli/pkg/cmd/origins/create"
"github.com/aziontech/azion-cli/pkg/cmd/origins/update"
"github.com/aziontech/azion-cli/pkg/cmd/origins/delete"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/spf13/cobra"
)
Expand All @@ -27,7 +28,7 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
originsCmd.AddCommand(describe.NewCmd(f))
originsCmd.AddCommand(create.NewCmd(f))
originsCmd.AddCommand(update.NewCmd(f))
originsCmd.AddCommand(delete.NewCmd(f))
originsCmd.Flags().BoolP("help", "h", false, msg.OriginsFlagHelp)

return originsCmd
}

0 comments on commit fcc8598

Please sign in to comment.