Skip to content

Commit

Permalink
feat: add cache settings update command
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickMenoti committed Feb 28, 2023
1 parent 9672bd1 commit 4a0d581
Show file tree
Hide file tree
Showing 14 changed files with 926 additions and 10 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.24.0
github.com/aziontech/azionapi-go-sdk v0.27.0
github.com/fatih/color v1.13.0
github.com/go-git/go-git/v5 v5.4.2
github.com/spf13/cobra v1.6.1
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.24.0 h1:45s3oTgpKukUuDnt55rLbNXoPOY1Y5ZGjYr9usmQm14=
github.com/aziontech/azionapi-go-sdk v0.24.0/go.mod h1:wL7eJYyE2qU853U2iKnQuFHoY69N5WwGnQJwySsdtX0=
github.com/aziontech/azionapi-go-sdk v0.27.0 h1:khueEI4CMPJ79IoUD9DsNGX5G2WjntbjZJFwm0J8IFg=
github.com/aziontech/azionapi-go-sdk v0.27.0/go.mod h1:wL7eJYyE2qU853U2iKnQuFHoY69N5WwGnQJwySsdtX0=
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
3 changes: 3 additions & 0 deletions messages/cache_settings/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ var (
ErrorCreateCacheSettings = errors.New("Failed to create the Cache Setting: %s. Check your settings and try again. If the error persists, contact Azion support.")
ErrorBrowserMaximumTtlNotSent = errors.New("When browser cache settings is 'override' you must send the --browser-cache-settings-maximum-ttl flag")
ErrorApplicationAccelerationNotEnabled = errors.New("When --enable-caching-string-sort, --enable-caching-for-post or --enable-caching-for-options is sent, application acceleration must be enabled")

ErrorMandatoryUpdateFlags = errors.New("Required flags are missing. You must provide the application-id and cache-settings-id flags when --in flag is not provided. Run the command 'azioncli <command> <subcommand> --help' to display more information and try again.")
ErrorMandatoryUpdateInFlags = errors.New("Required flags are missing. You must provide the application-id flag when --in flag is not provided. Run the command 'azioncli <command> <subcommand> --help' to display more information and try again.")
)
9 changes: 8 additions & 1 deletion messages/cache_settings/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var (
CacheSettingsShortDescription = "Cache Settings lets you check, remove or update existing settings, besides creating new ones"
CacheSettingsLongDescription = "Cache Settings lets you check, remove or update existing settings, besides creating new ones"
CacheSettingsFlagHelp = "Displays more information about the cache_settings command"
CacheSettingsId = "Unique identifier for a cache setting"

// [ list ]
CacheSettingsListUsage = "list [flags]"
Expand All @@ -15,7 +16,7 @@ var (

// [ create ]
CacheSettingsCreateUsage = "create [flags]"
CacheSettingsCreateShortDescription = "Creates a new Cache Settings"
CacheSettingsCreateShortDescription = "Creates a new Cache Setting"
CacheSettingsCreateLongDescription = "Creates a Cache Setting based on given attributes to be used in edge applications"
CacheSettingsCreateFlagEdgeApplicationId = "Unique identifier for an edge application"
CacheSettingsCreateFlagName = "The Cache Settings' name"
Expand All @@ -39,4 +40,10 @@ var (
CacheSettingsCreateFlagCdnCacheSettingsMaxTtl = "CDN cache settings' maximum TTL"
CacheSettingsCreateFlagBrowserCacheSettingsMaxTtl = "Browser cache settings' maximum TTL"
CacheSettingsCreateFlagAdaptiveDeliveryAction = "Cache Settings' adaptive delivery action"

// [ update ]
CacheSettingsUpdateUsage = "update [flags]"
CacheSettingsUpdateShortDescription = "Modifies a Cache Setting"
CacheSettingsUpdateLongDescription = "Modifies a Cache Setting based on given attributes to be used in edge applications"
CacheSettingsUpdateOutputSuccess = "Updated cache setting with ID %d\n"
)
29 changes: 24 additions & 5 deletions pkg/api/edge_applications/edge_applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package edgeapplications
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -290,6 +291,11 @@ type CreateCacheSettingsRequest struct {
sdk.ApplicationCacheCreateRequest
}

type UpdateCacheSettingsRequest struct {
sdk.ApplicationCachePatchRequest
Id int64
}

func (c *Client) CreateCacheSettings(ctx context.Context, req *CreateCacheSettingsRequest, applicationId int64) (CacheSettingsResponse, error) {

request := c.apiClient.EdgeApplicationsCacheSettingsApi.EdgeApplicationsEdgeApplicationIdCacheSettingsPost(ctx, applicationId).ApplicationCacheCreateRequest(req.ApplicationCacheCreateRequest)
Expand All @@ -302,16 +308,29 @@ func (c *Client) CreateCacheSettings(ctx context.Context, req *CreateCacheSettin
return cacheResponse.Results, nil
}

func (c *Client) UpdateCacheSettings(ctx context.Context, req *UpdateCacheSettingsRequest, applicationId int64) (CacheSettingsResponse, error) {

request := c.apiClient.EdgeApplicationsCacheSettingsApi.EdgeApplicationsEdgeApplicationIdCacheSettingsCacheSettingsPatch(ctx, applicationId, req.Id).ApplicationCachePatchRequest(req.ApplicationCachePatchRequest)

cacheResponse, httpResp, err := request.Execute()
if err != nil {
fmt.Println(err.Error())
return nil, utils.ErrorPerStatusCode(httpResp, err)
}

return cacheResponse.Results, nil
}

func (c *Client) ListCacheSettings(ctx context.Context, opts *contracts.ListOptions, edgeApplicationID int64) (*sdk.ApplicationCacheGetResponse, error) {
if opts.OrderBy == "" {
opts.OrderBy = "id"
}

resp, httpResp, err := c.apiClient.EdgeApplicationsCacheSettingsApi.EdgeApplicationsEdgeApplicationIdCacheSettingsGet(ctx, edgeApplicationID).
OrderBy(opts.OrderBy).
Page(opts.Page).
PageSize(opts.PageSize).
Sort(opts.Sort).Execute()
resp, httpResp, err := c.apiClient.EdgeApplicationsCacheSettingsApi.EdgeApplicationsEdgeApplicationIdCacheSettingsGet(ctx, edgeApplicationID).
OrderBy(opts.OrderBy).
Page(opts.Page).
PageSize(opts.PageSize).
Sort(opts.Sort).Execute()

if err != nil {
return &sdk.ApplicationCacheGetResponse{}, utils.ErrorPerStatusCode(httpResp, err)
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/cache_settings/cache_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
msg "github.com/aziontech/azion-cli/messages/cache_settings"
"github.com/aziontech/azion-cli/pkg/cmd/cache_settings/create"
"github.com/aziontech/azion-cli/pkg/cmd/cache_settings/list"
"github.com/aziontech/azion-cli/pkg/cmd/cache_settings/update"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/spf13/cobra"
)
Expand All @@ -24,6 +25,7 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
}

cacheCmd.AddCommand(create.NewCmd(f))
cacheCmd.AddCommand(update.NewCmd(f))
cacheCmd.AddCommand(list.NewCmd(f))
cacheCmd.Flags().BoolP("help", "h", false, msg.CacheSettingsFlagHelp)
return cacheCmd
Expand Down
23 changes: 23 additions & 0 deletions pkg/cmd/cache_settings/update/fixtures/app_result.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
}
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": false,
"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
}
1 change: 1 addition & 0 deletions pkg/cmd/cache_settings/update/fixtures/error
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file for unmarshalling error
27 changes: 27 additions & 0 deletions pkg/cmd/cache_settings/update/fixtures/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"results": {
"id": 112233,
"name": "cachesettingswithfields",
"browser_cache_settings": "honor",
"browser_cache_settings_maximum_ttl": 0,
"cdn_cache_settings": "honor",
"cdn_cache_settings_maximum_ttl": 60,
"cache_by_query_string": "ignore",
"query_string_fields": null,
"enable_query_string_sort": false,
"cache_by_cookies": "ignore",
"cookie_names": null,
"adaptive_delivery_action": "ignore",
"device_group": [],
"enable_caching_for_post": false,
"l2_caching_enabled": false,
"is_slice_configuration_enabled": false,
"is_slice_edge_caching_enabled": false,
"is_slice_l2_caching_enabled": false,
"slice_configuration_range": null,
"enable_caching_for_options": false,
"enable_stale_cache": true,
"l2_region": null
},
"schema_version": 3
}
24 changes: 24 additions & 0 deletions pkg/cmd/cache_settings/update/fixtures/update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "thename",
"id": 112233,
"browser_cache_settings": "honor",
"browser_cache_settings_maximum_ttl": 0,
"cdn_cache_settings": "honor",
"cdn_cache_settings_maximum_ttl": 60,
"cache_by_query_string": "ignore",
"query_string_fields": ["aa"],
"enable_query_string_sort": true,
"cache_by_cookies": "ignore",
"cookie_names": ["aa"],
"adaptive_delivery_action": "ignore",
"device_group": [],
"enable_caching_for_post": true,
"l2_caching_enabled": false,
"is_slice_configuration_enabled": false,
"is_slice_edge_caching_enabled": false,
"is_slice_l2_caching_enabled": false,
"slice_configuration_range": null,
"enable_caching_for_options": true,
"enable_stale_cache": true,
"l2_region": null
}
Loading

0 comments on commit 4a0d581

Please sign in to comment.