Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy - 2022/01/25 #46

Merged
merged 23 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f93255a
feat: add edge_functions module skeleton
fmiamoto Jan 21, 2022
c783914
chore: remove misleading comment
fmiamoto Jan 21, 2022
0619db5
fix: correct annotation name
fmiamoto Jan 21, 2022
a1cc6bf
chore: move annotations field in struct
fmiamoto Jan 21, 2022
893c1f5
chore: parameters before flags
fmiamoto Jan 21, 2022
da87029
chore: fix typos
fmiamoto Jan 21, 2022
79054b7
Merge pull request #42 from aziontech/eng18861_skeleton
fmiamoto Jan 21, 2022
3aa0d95
feat: organize help for commands
PatrickMenoti Jan 21, 2022
4a73acf
fix: remove unnecessary dependency
PatrickMenoti Jan 21, 2022
9395db6
fix: correct spelling
PatrickMenoti Jan 24, 2022
1ae0616
Merge pull request #43 from aziontech/ENG-18888
PatrickMenoti Jan 24, 2022
949c52d
fix: check length of args array
fmiamoto Jan 24, 2022
edad417
feat(functions): add flags for create and update cmds
fmiamoto Jan 24, 2022
a88792c
refactor: create constants package
fmiamoto Jan 24, 2022
d7c402e
fix: correct description for list command
PatrickMenoti Jan 24, 2022
5e0710c
feat(functions): add flags for describe and list
fmiamoto Jan 24, 2022
ccbf272
docs: add explanation for constants package
fmiamoto Jan 24, 2022
9283954
chore: capitalize edge services
fmiamoto Jan 24, 2022
228fe35
Merge pull request #44 from aziontech/fix-list-command
PatrickMenoti Jan 24, 2022
44c0ea0
Merge pull request #45 from aziontech/eng18861_create_update
fmiamoto Jan 25, 2022
ddf7904
feat(functions): add initial describe cmd
fmiamoto Jan 25, 2022
a2851b2
chore: update comment
fmiamoto Jan 25, 2022
9ae51ee
Merge pull request #47 from aziontech/eng18861_describe
fmiamoto Jan 25, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ 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/token.AuthEndpoint=$$AUTH_URL \
-X github.com/aziontech/azion-cli/pkg/cmd/edge_services/requests.ApiUrl=$$API_URL
-X github.com/aziontech/azion-cli/pkg/constants.AuthURL=$$AUTH_URL \
-X github.com/aziontech/azion-cli/pkg/constants.ApiURL=$$API_URL
LDFLAGS_STRIP=-s -w
NAME_WITH_VERSION=$(NAME)-$(BIN_VERSION)

Expand All @@ -40,6 +40,15 @@ clean: ## delete additional files
lint: get-lint-deps ## running GoLint
@ $(GOBIN)/golangci-lint run ./...


.PHONY: dev
dev: dev-deps
$(RELOAD) -build 'make build'

.PHONY: dev-deps
dev-deps:
$(GO) install github.com/githubnemo/CompileDaemon@v1.4.0

.PHONY: get-lint-deps
get-lint-deps:
@if [ ! -x $(GOBIN)/golangci-lint ]; then\
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ module github.com/aziontech/azion-cli
go 1.17

require (
github.com/aziontech/azionapi-go-sdk v0.3.0
github.com/aziontech/azionapi-go-sdk v0.4.0
github.com/spf13/cobra v1.3.0
github.com/spf13/viper v1.10.1
)

require (
github.com/MakeNowJust/heredoc v1.0.0
github.com/golang/protobuf v1.5.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/pflag v1.0.5
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand All @@ -60,8 +62,8 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV
github.com/armon/go-metrics v0.3.10/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aziontech/azionapi-go-sdk v0.3.0 h1:RM0FsT9nX2q9a4fc8VsDus1mbhzXzt1Z8ZIEp+kP/OM=
github.com/aziontech/azionapi-go-sdk v0.3.0/go.mod h1:2caFKH52viwZI346MWCGDZzU0jGll6udJZZMDJNrWgg=
github.com/aziontech/azionapi-go-sdk v0.4.0 h1:OC4Cjf8jVLLNxIoQ7lsq61hnyBmFmPUcxweZy0HLmbQ=
github.com/aziontech/azionapi-go-sdk v0.4.0/go.mod h1:2caFKH52viwZI346MWCGDZzU0jGll6udJZZMDJNrWgg=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
Expand Down
51 changes: 51 additions & 0 deletions pkg/api/edge_functions/edge_functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package edge_funtions

import (
"context"
"net/http"

sdk "github.com/aziontech/azionapi-go-sdk/edgefunctions"
)

type Client struct {
apiClient *sdk.APIClient
}

type EdgeFunction interface {
GetId() float32 // Should be uint64
GetName() string
GetLanguage() string
GetReferenceCount() float32 // Should be uint64
GetModified() string
GetInitiatorType() string
GetLastEditor() string
GetFunctionToRun() string
GetJsonArgs() map[string]interface{}
GetCode() string
}

func NewClient(c *http.Client, url string, token string) *Client {
conf := sdk.NewConfiguration()
conf.HTTPClient = c
conf.AddDefaultHeader("Authorization", "token "+token)
conf.AddDefaultHeader("Accept", "application/json;version=3")
conf.Servers = sdk.ServerConfigurations{
{URL: url},
}

return &Client{
apiClient: sdk.NewAPIClient(conf),
}
}

func (c *Client) Get(ctx context.Context, id string) (EdgeFunction, error) {
req := c.apiClient.EdgeFunctionsApi.EdgeFunctionsIdGet(ctx, id)

res, _, err := req.Execute()

if err != nil {
return nil, err
}

return res.Results, nil
}
3 changes: 3 additions & 0 deletions pkg/cmd/configure/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
Use: "configure",
Short: "Configure parameters and credentials",
Long: `This command configures cli parameters and credentials used for connecting to our services.`,
Annotations: map[string]string{
"IsAdditional": "true",
},
RunE: func(cmd *cobra.Command, args []string) error {
client, err := f.HttpClient()
if err != nil {
Expand Down
73 changes: 73 additions & 0 deletions pkg/cmd/edge_functions/create/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package create

import (
"fmt"
"io/ioutil"
"strconv"

"github.com/MakeNowJust/heredoc"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/spf13/cobra"
)

type Fields struct {
Name string
Language string
Code string
Active string
InitiatorType string
Args string
}

func NewCmd(f *cmdutil.Factory) *cobra.Command {
fields := &Fields{}

cmd := &cobra.Command{
Use: "create [flags]",
Short: "Create a new Edge Function",
Long: "Create a new Edge Function",
SilenceUsage: true,
SilenceErrors: true,
Example: heredoc.Doc(`
$ azioncli edge_functions create -–name myfunc -–language javascript –-code ./mycode/function.js -–state active --initiator-type edge-application
`),
RunE: func(cmd *cobra.Command, args []string) error {
_, err := strconv.ParseBool(fields.Active)
if err != nil {
return fmt.Errorf("invalid --active flag: %s", fields.Active)
}

_, err = ioutil.ReadFile(fields.Code)
if err != nil {
return fmt.Errorf("failed to read code file: %w", err)
}

if cmd.Flags().Changed("args") {
_, err = ioutil.ReadFile(fields.Args)
if err != nil {
return fmt.Errorf("failed to read args file: %w", err)
}
}

// TODO: Interact with SDK to create function

return nil
},
}

flags := cmd.Flags()

flags.StringVar(&fields.Name, "name", "", "Name of your Edge Function.")
flags.StringVar(&fields.Language, "language", "", "Programming language of your Edge Function <javascript|lua>")
flags.StringVar(&fields.Code, "code", "", "Path to the file containing your Edge Function code.")
flags.StringVar(&fields.InitiatorType, "initiator-type", "", "Initiator of your Edge Function: <edge-application|edge-firewall>")
flags.StringVar(&fields.Active, "active", "", "Whether or not your Edge Function should be active: <true|false>")

_ = cmd.MarkFlagRequired("name")
_ = cmd.MarkFlagRequired("language")
_ = cmd.MarkFlagRequired("code")
_ = cmd.MarkFlagRequired("initiator-type")
_ = cmd.MarkFlagRequired("active")

return cmd
}
25 changes: 25 additions & 0 deletions pkg/cmd/edge_functions/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package delete

import (
"github.com/MakeNowJust/heredoc"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/spf13/cobra"
)

func NewCmd(f *cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "delete <edge_function_id> [flags]",
Short: "Delete an Edge Function",
Long: "Delete an Edge Function",
SilenceUsage: true,
SilenceErrors: true,
Example: heredoc.Doc(`
$ azioncli edge_functions delete 1234
`),
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}

return cmd
}
71 changes: 71 additions & 0 deletions pkg/cmd/edge_functions/describe/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package describe

import (
"context"
"encoding/json"
"fmt"

"github.com/MakeNowJust/heredoc"
api "github.com/aziontech/azion-cli/pkg/api/edge_functions"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/spf13/cobra"
)

func NewCmd(f *cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "describe <edge_function_id> [flags]",
Short: "Describe a given Edge Function",
Long: "Describe a given Edge Function",
SilenceUsage: true,
SilenceErrors: true,
Example: heredoc.Doc(`
$ azioncli edge_functions describe 1337 [--with-code]
`),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("missing edge function id argument")
}

httpClient, err := f.HttpClient()
if err != nil {
return fmt.Errorf("failed to get http client: %w", err)
}

client := api.NewClient(httpClient, f.Config.GetString("api_url"), f.Config.GetString("token"))

ctx := context.Background()
function, err := client.Get(ctx, args[0])
if err != nil {
return err
}

out := f.IOStreams.Out

fmt.Fprintf(out, "ID: %d\n", uint64(function.GetId()))
fmt.Fprintf(out, "Name: %s\n", function.GetName())
fmt.Fprintf(out, "Language: %s\n", function.GetLanguage())
fmt.Fprintf(out, "Reference Count: %d\n", uint64(function.GetReferenceCount()))
fmt.Fprintf(out, "Modified at: %s\n", function.GetModified())
fmt.Fprintf(out, "Initiator Type: %s\n", function.GetInitiatorType())
fmt.Fprintf(out, "Last Editor: %s\n", function.GetLastEditor())
fmt.Fprintf(out, "Function to run: %s\n", function.GetFunctionToRun())
fmt.Fprintf(out, "JSON Args: %s\n", serializeToJson(function.GetJsonArgs())) // Show serialized JSON

if cmd.Flags().Changed("with-code") {
fmt.Fprintf(out, "Code:\n%s\n", function.GetCode())
}

return nil
},
}

cmd.Flags().Bool("with-code", false, "Display the Edge Function code, disabled dy default")

return cmd
}

func serializeToJson(data map[string]interface{}) string {
// ignoring errors on purpose
serialized, _ := json.Marshal(data)
return string(serialized)
}
33 changes: 33 additions & 0 deletions pkg/cmd/edge_functions/edge_functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package edge_functions

import (
"github.com/aziontech/azion-cli/pkg/cmd/edge_functions/create"
del "github.com/aziontech/azion-cli/pkg/cmd/edge_functions/delete"
"github.com/aziontech/azion-cli/pkg/cmd/edge_functions/describe"
"github.com/aziontech/azion-cli/pkg/cmd/edge_functions/list"
"github.com/aziontech/azion-cli/pkg/cmd/edge_functions/update"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/spf13/cobra"
)

func NewCmd(f *cmdutil.Factory) *cobra.Command {
edgeFunctionsCmd := &cobra.Command{
Use: "edge_functions",
Short: "Manages Edge Functions of your Azion account",
Long: "You may create, update, delete, list and describe Edge Functions of an Azion account",
Annotations: map[string]string{
"IsAPI": "true",
},
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}

edgeFunctionsCmd.AddCommand(create.NewCmd(f))
edgeFunctionsCmd.AddCommand(del.NewCmd(f))
edgeFunctionsCmd.AddCommand(update.NewCmd(f))
edgeFunctionsCmd.AddCommand(describe.NewCmd(f))
edgeFunctionsCmd.AddCommand(list.NewCmd(f))

return edgeFunctionsCmd
}
27 changes: 27 additions & 0 deletions pkg/cmd/edge_functions/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package list

import (
"github.com/MakeNowJust/heredoc"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/spf13/cobra"
)

func NewCmd(f *cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "list [flags]",
Short: "List the Edge Functions of your account",
Long: "List the Edge Functions of your account",
SilenceUsage: true,
SilenceErrors: true,
Example: heredoc.Doc(`
$ azioncli edge_functions list [--details]
`),
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}

cmd.Flags().Bool("details", false, "Show additional fields")

return cmd
}
Loading