Skip to content

Commit

Permalink
Merge pull request #371 from aziontech/cache-rules
Browse files Browse the repository at this point in the history
feat: add cache settings and rules engine for web applications
  • Loading branch information
PatrickMenoti authored May 19, 2023
2 parents 98a5fef + 1ca41b2 commit b2ca742
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/MaxwelMazur/tablecli v0.0.0-20230208145104-c9458b902b58
github.com/aziontech/azionapi-go-sdk v0.37.0
github.com/aziontech/azionapi-go-sdk v0.39.0
github.com/fatih/color v1.13.0
github.com/go-faker/faker/v4 v4.1.0
github.com/go-git/go-git/v5 v5.4.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/aziontech/azionapi-go-sdk v0.37.0 h1:MLY/LaAz8iumRqiosbv+AEXbo46vnwgtENaRY4wyn9Y=
github.com/aziontech/azionapi-go-sdk v0.37.0/go.mod h1:cA5DY/VP4X5Eu11LpQNzNn83ziKjja7QVMIl4J45feA=
github.com/aziontech/azionapi-go-sdk v0.39.0 h1:vBTOAlhFbubAIffQTVerpjy1g17+VWMwHdE0lSgX3TE=
github.com/aziontech/azionapi-go-sdk v0.39.0/go.mod h1:cA5DY/VP4X5Eu11LpQNzNn83ziKjja7QVMIl4J45feA=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
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=
Expand Down
5 changes: 4 additions & 1 deletion messages/edge_applications/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,17 @@ var (
EdgeApplicationsPublishSuccessful = "Your Edge Application was published successfully\n"
EdgeApplicationsCdnPublishSuccessful = "Your CDN Edge Application was published successfully\n"
EdgeApplicationsPublishOutputDomainSuccess = "\nTo visualize your application access the domain: %s\n"
EdgeApplicationPublishDomainHint = "You may now edit your domain and add your own cnames. To do this you may run 'azioncli domain update' command and also configure you DNS\n"
EdgeApplicationPublishDomainHint = "You may now edit your domain and add your own cnames. To do this you may run 'azioncli domain update' command and also configure your DNS\n"
EdgeApplicationsPublishOutputCachePurge = "Domain cache was purged\n"
EdgeApplicationsPublishOutputEdgeFunctionCreate = "Created Edge Function %s with ID %d\n"
EdgeApplicationsPublishOutputEdgeFunctionUpdate = "Updated Edge Function %s with ID %d\n"
EdgeApplicationsPublishOutputEdgeApplicationCreate = "Created Edge Application %s with ID %d\n"
EdgeApplicationsPublishOutputEdgeApplicationUpdate = "Updated Edge Application %s with ID %d\n"
EdgeApplicationsPublishOutputDomainCreate = "Created Domain %s with ID %d\n"
EdgeApplicationsPublishOutputDomainUpdate = "Updated Domain %s with ID %d\n"
EdgeApplicationsCacheSettingsSuccessful = "Created Cache Settings for web application"
EdgeApplicationsPublishInputAddress = "Please inform an address to be used in the origin of this application: "
EdgeApplicationsRulesEngineSuccessful = "Created Rules Engine for web application"
EdgeApplicationsPublishFlagHelp = "Displays more information about the publish subcommand"
EdgeApplicationsPublishPropagation = "Content is being propagated to all Azion POPs and it might take a few minutes for all edges to be up to date\n"

Expand Down
70 changes: 70 additions & 0 deletions pkg/api/edge_applications/edge_applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,73 @@ func (c *Client) CreateDeviceGroups(ctx context.Context, req *CreateDeviceGroups
}
return &resp.Results, nil
}

// this function creates the necessary cache settings for next applications to work correctly on the edge
func (c *Client) CreateCacheSettingsNextApplication(ctx context.Context, req *CreateCacheSettingsRequest, applicationId int64) (CacheSettingsResponse, error) {
req.SetBrowserCacheSettings("override")
req.SetBrowserCacheSettingsMaximumTtl(31536000)
req.SetCdnCacheSettings("override")
req.SetCdnCacheSettingsMaximumTtl(31536000)

request := c.apiClient.EdgeApplicationsCacheSettingsApi.EdgeApplicationsEdgeApplicationIdCacheSettingsPost(ctx, applicationId).ApplicationCacheCreateRequest(req.ApplicationCacheCreateRequest)

resp, httpResp, err := request.Execute()
if err != nil {
return nil, utils.ErrorPerStatusCode(httpResp, err)
}

return resp.Results, nil
}

func (c *Client) CreateRulesEngineNextApplication(ctx context.Context, applicationId int64, cacheId int64, typeLang string) error {
req := CreateRulesEngineRequest{}
req.SetName("cache policy")
behavior := make([]sdk.RulesEngineBehavior, 1)
behavior[0].SetName("set_cache_policy")
behavior[0].SetTarget(cacheId)
req.SetBehaviors(behavior)

criteria := make([][]sdk.RulesEngineCriteria, 1)
for i := 0; i < 1; i++ {
criteria[i] = make([]sdk.RulesEngineCriteria, 1)
}

criteria[0][0].SetConditional("if")
criteria[0][0].SetVariable("${uri}")
criteria[0][0].SetOperator("starts_with")
if typeLang == "nextjs" {
criteria[0][0].SetInputValue("/_next/static")
} else {
criteria[0][0].SetInputValue("/")
}

req.SetCriteria(criteria)

_, httpResp, err := c.apiClient.EdgeApplicationsRulesEngineApi.
EdgeApplicationsEdgeApplicationIdRulesEnginePhaseRulesPost(ctx, applicationId, "request").
CreateRulesEngineRequest(req.CreateRulesEngineRequest).Execute()
if err != nil {
return utils.ErrorPerStatusCode(httpResp, err)
}

req.SetName("enable gzip")

behavior[0].SetName("enable_gzip")
behavior[0].SetTarget("")
req.SetBehaviors(behavior)

criteria[0][0].SetConditional("if")
criteria[0][0].SetVariable("${request_uri}")
criteria[0][0].SetOperator("exists")
criteria[0][0].SetInputValue("")
req.SetCriteria(criteria)

_, httpResp, err = c.apiClient.EdgeApplicationsRulesEngineApi.
EdgeApplicationsEdgeApplicationIdRulesEnginePhaseRulesPost(ctx, applicationId, "response").
CreateRulesEngineRequest(req.CreateRulesEngineRequest).Execute()
if err != nil {
return utils.ErrorPerStatusCode(httpResp, err)
}

return nil
}
23 changes: 23 additions & 0 deletions pkg/cmd/edge_applications/.fixtures/cache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"results": {
"id": 1673635841,
"name": "ssass",
"delivery_protocol": "http",
"http_port": 80,
"https_port": 443,
"minimum_tls_version": "",
"active": true,
"debug_rules": false,
"application_acceleration": true,
"caching": true,
"device_detection": false,
"edge_firewall": false,
"edge_functions": false,
"image_optimization": false,
"l2_caching": false,
"load_balancer": false,
"raw_logs": false,
"web_application_firewall": false
},
"schema_version": 3
}
28 changes: 28 additions & 0 deletions pkg/cmd/edge_applications/.fixtures/origin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"results": {
"origin_id": 92779,
"origin_key": "176fa5e2-a895-4862-9657-e2e37d9125a7",
"name": "Create Or22i21231222n2",
"origin_type": "single_origin",
"addresses": [
{
"address": "httpbin.org",
"weight": null,
"server_role": "primary",
"is_active": true
}
],
"origin_protocol_policy": "http",
"is_origin_redirection_enabled": false,
"host_header": "asdf.asdfom",
"method": "",
"origin_path": "/requests",
"connection_timeout": 60,
"timeout_between_bytes": 120,
"hmac_authentication": false,
"hmac_region_name": "",
"hmac_access_key": "",
"hmac_secret_key": ""
},
"schema_version": 3
}
23 changes: 23 additions & 0 deletions pkg/cmd/edge_applications/edge_applications_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edge_applications

import (
"io"
"io/fs"
"log"
"os"
Expand Down Expand Up @@ -43,6 +44,22 @@ func Mock(mock *httpmock.Registry) {
httpmock.REST("PATCH", "edge_applications/777/rules_engine/request/rules/137056"),
httpmock.JSONFromFile(".fixtures/rule.json"),
)
mock.Register(
httpmock.REST("POST", "edge_applications/777/cache_settings"),
httpmock.JSONFromFile(".fixtures/cache.json"),
)
mock.Register(
httpmock.REST("POST", "edge_applications/777/origins"),
httpmock.JSONFromFile(".fixtures/origin.json"),
)
mock.Register(
httpmock.REST("POST", "edge_applications/777/rules_engine/request/rules"),
httpmock.JSONFromFile(".fixtures/rule.json"),
)
mock.Register(
httpmock.REST("POST", "edge_applications/777/rules_engine/response/rules"),
httpmock.JSONFromFile(".fixtures/rule.json"),
)
mock.Register(
httpmock.REST("POST", "domains"),
httpmock.JSONFromFile(".fixtures/domain.json"),
Expand Down Expand Up @@ -124,6 +141,9 @@ func TestNewCmd(t *testing.T) {
GetAzionJsonCdn: func() (*contracts.AzionApplicationCdn, error) {
return &contracts.AzionApplicationCdn{}, nil
},
AskInput: func(in io.ReadCloser, out io.Writer, message string) string {
return "www.test.com"
},
}
},
},
Expand Down Expand Up @@ -188,6 +208,9 @@ func TestNewCmd(t *testing.T) {
GetAzionJsonCdn: func() (*contracts.AzionApplicationCdn, error) {
return &contracts.AzionApplicationCdn{}, nil
},
AskInput: func(in io.ReadCloser, out io.Writer, message string) string {
return "www.test.com"
},
}
},
},
Expand Down
57 changes: 53 additions & 4 deletions pkg/cmd/edge_applications/publish/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/MakeNowJust/heredoc"
msg "github.com/aziontech/azion-cli/messages/edge_applications"
apidom "github.com/aziontech/azion-cli/pkg/api/domains"
sdk "github.com/aziontech/azionapi-go-sdk/edgeapplications"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"text/template"

"github.com/MakeNowJust/heredoc"
msg "github.com/aziontech/azion-cli/messages/edge_applications"
apidom "github.com/aziontech/azion-cli/pkg/api/domains"
sdk "github.com/aziontech/azionapi-go-sdk/edgeapplications"

apiapp "github.com/aziontech/azion-cli/pkg/api/edge_applications"
api "github.com/aziontech/azion-cli/pkg/api/edge_functions"
apipurge "github.com/aziontech/azion-cli/pkg/api/realtime_purge"
Expand All @@ -40,6 +42,7 @@ type PublishCmd struct {
BuildCmd func(f *cmdutil.Factory) *build.BuildCmd
Open func(name string) (*os.File, error)
FilepathWalk func(root string, fn filepath.WalkFunc) error
AskInput func(in io.ReadCloser, out io.Writer, message string) string
F *cmdutil.Factory
}

Expand All @@ -62,6 +65,7 @@ func NewPublishCmd(f *cmdutil.Factory) *PublishCmd {
Open: os.Open,
FilepathWalk: filepath.Walk,
F: f,
AskInput: utils.AskForInput,
}
}

Expand Down Expand Up @@ -257,6 +261,41 @@ func (cmd *PublishCmd) run(f *cmdutil.Factory) error {
}
conf.Domain.Id = domain.GetId()
newDomain = true

//after everything was create, we now create the cache and rules required
reqOrigin := apiapp.CreateOriginsRequest{}
var addresses []string
if len(conf.Origin.Address) > 0 {
address := prepareAddresses(conf.Origin.Address)
addresses = conf.Origin.Address
reqOrigin.SetAddresses(address)
} else {
response := cmd.AskInput(cmd.Io.In, cmd.Io.Out, msg.EdgeApplicationsPublishInputAddress)
addresses = strings.Split(response, ",")
address := prepareAddresses(addresses)
reqOrigin.SetAddresses(address)
}
reqOrigin.SetName(conf.Name)
reqOrigin.SetHostHeader("${host}")
origin, err := cliapp.CreateOrigins(ctx, conf.Application.Id, &reqOrigin)
if err != nil {
return err
}
conf.Origin.Id = origin.GetOriginId()
conf.Origin.Address = addresses
reqCache := apiapp.CreateCacheSettingsRequest{}
reqCache.SetName(conf.Name)
cache, err := cliapp.CreateCacheSettingsNextApplication(ctx, &reqCache, conf.Application.Id)
if err != nil {
return err
}
fmt.Fprintf(cmd.F.IOStreams.Out, "%s\n", msg.EdgeApplicationsCacheSettingsSuccessful)
err = cliapp.CreateRulesEngineNextApplication(ctx, conf.Application.Id, cache.GetId(), typeLang.String())
if err != nil {
return err
}
fmt.Fprintf(cmd.F.IOStreams.Out, "%s\n", msg.EdgeApplicationsRulesEngineSuccessful)

} else {
domain, err = cmd.updateDomain(clidom, ctx, conf, domaiName)
if err != nil {
Expand Down Expand Up @@ -401,6 +440,7 @@ func (cmd *PublishCmd) createApplication(client *apiapp.Client, ctx context.Cont
fmt.Fprintf(cmd.F.IOStreams.Out, msg.EdgeApplicationsPublishOutputEdgeApplicationCreate, application.GetName(), application.GetId())
reqUpApp := apiapp.UpdateRequest{}
reqUpApp.SetEdgeFunctions(true)
reqUpApp.SetApplicationAcceleration(true)
reqUpApp.Id = application.GetId()
application, err = client.Update(ctx, &reqUpApp)
if err != nil {
Expand Down Expand Up @@ -659,6 +699,15 @@ func publishCdn(cmd *PublishCmd, f *cmdutil.Factory) error {
return nil
}

func prepareAddresses(addrs []string) (addresses []sdk.CreateOriginsRequestAddresses) {
var addr sdk.CreateOriginsRequestAddresses
for _, v := range addrs {
addr.Address = v
addresses = append(addresses, addr)
}
return
}

func publishStatic(cmd *PublishCmd, f *cmdutil.Factory) error {
conf, err := cmd.GetAzionJsonContent()
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/contracts/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type AzionApplicationOptions struct {
Application AzionJsonDataApplication `json:"application"`
Domain AzionJsonDataDomain `json:"domain"`
RtPurge AzionJsonDataPurge `json:"rt-purge"`
Origin AzionJsonDataOrigin `json:"origin"`
}

type AzionApplicationCdn struct {
Expand Down Expand Up @@ -76,6 +77,12 @@ type AzionJsonDataApplication struct {
Name string `json:"name"`
}

type AzionJsonDataOrigin struct {
Id int64 `json:"id"`
Name string `json:"name"`
Address []string `json:"address"`
}

type AzionJsonDataDomain struct {
Id int64 `json:"id"`
Name string `json:"name"`
Expand Down
6 changes: 6 additions & 0 deletions utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,9 @@ func checkOrderField(body string) error {
func CreateVersionID() string {
return time.Now().Format("20060102150405")
}

func AskForInput(in io.ReadCloser, out io.Writer, message string) (response string) {
fmt.Fprintf(out, "%s: ", message)
fmt.Fscanln(in, &response)
return response
}

0 comments on commit b2ca742

Please sign in to comment.