Skip to content

Commit

Permalink
Add CRUD commands for S3 logging endpoints (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
phamann authored Mar 20, 2020
1 parent 48265b8 commit 3b6f710
Show file tree
Hide file tree
Showing 13 changed files with 1,123 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pkg/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ type Interface interface {
UpdateBigQuery(*fastly.UpdateBigQueryInput) (*fastly.BigQuery, error)
DeleteBigQuery(*fastly.DeleteBigQueryInput) error

CreateS3(*fastly.CreateS3Input) (*fastly.S3, error)
ListS3s(*fastly.ListS3sInput) ([]*fastly.S3, error)
GetS3(*fastly.GetS3Input) (*fastly.S3, error)
UpdateS3(*fastly.UpdateS3Input) (*fastly.S3, error)
DeleteS3(*fastly.DeleteS3Input) error

GetUser(*fastly.GetUserInput) (*fastly.User, error)
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/fastly/cli/pkg/healthcheck"
"github.com/fastly/cli/pkg/logging"
"github.com/fastly/cli/pkg/logging/bigquery"
"github.com/fastly/cli/pkg/logging/s3"
"github.com/fastly/cli/pkg/service"
"github.com/fastly/cli/pkg/serviceversion"
"github.com/fastly/cli/pkg/text"
Expand Down Expand Up @@ -126,6 +127,13 @@ func Run(args []string, env config.Environment, file config.File, configFilePath
bigQueryUpdate := bigquery.NewUpdateCommand(bigQueryRoot.CmdClause, &globals)
bigQueryDelete := bigquery.NewDeleteCommand(bigQueryRoot.CmdClause, &globals)

s3Root := s3.NewRootCommand(loggingRoot.CmdClause, &globals)
s3Create := s3.NewCreateCommand(s3Root.CmdClause, &globals)
s3List := s3.NewListCommand(s3Root.CmdClause, &globals)
s3Describe := s3.NewDescribeCommand(s3Root.CmdClause, &globals)
s3Update := s3.NewUpdateCommand(s3Root.CmdClause, &globals)
s3Delete := s3.NewDeleteCommand(s3Root.CmdClause, &globals)

commands := []common.Command{
configureRoot,
whoamiRoot,
Expand Down Expand Up @@ -183,6 +191,13 @@ func Run(args []string, env config.Environment, file config.File, configFilePath
bigQueryDescribe,
bigQueryUpdate,
bigQueryDelete,

s3Root,
s3Create,
s3List,
s3Describe,
s3Update,
s3Delete,
}

// Handle parse errors and display contextal usage if possible. Due to bugs
Expand Down
109 changes: 109 additions & 0 deletions pkg/app/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,115 @@ COMMANDS
--version=VERSION Number of service version
-n, --name=NAME The name of the BigQuery logging object
logging s3 create --name=NAME --version=VERSION --bucket=BUCKET --access-key=ACCESS-KEY --secret-key=SECRET-KEY [<flags>]
Create an Amazon S3 logging endpoint on a Fastly service version
-n, --name=NAME The name of the S3 logging object. Used as a
primary key for API access
-s, --service-id=SERVICE-ID Service ID
--version=VERSION Number of service version
--bucket=BUCKET Your S3 bucket name
--access-key=ACCESS-KEY Your S3 account access key
--secret-key=SECRET-KEY Your S3 account secret key
--domain=DOMAIN The domain of the S3 endpoint
--path=PATH The path to upload logs to
--period=PERIOD How frequently log files are finalized so they
can be available for reading (in seconds,
default 3600)
--gzip-level=GZIP-LEVEL What level of GZIP encoding to have when
dumping logs (default 0, no compression)
--format=FORMAT Apache style log formatting
--format-version=FORMAT-VERSION
The version of the custom logging format used
for the configured endpoint. Can be either 2
(default) or 1
--message-type=MESSAGE-TYPE
How the message should be formatted. One of:
classic (default), loggly, logplex or blank
--response-condition=RESPONSE-CONDITION
The name of an existing condition in the
configured endpoint, or leave blank to always
execute
--timestamp-format=TIMESTAMP-FORMAT
strftime specified timestamp formatting
(default "%Y-%m-%dT%H:%M:%S.000")
--redundancy=REDUNDANCY The S3 redundancy level. Can be either standard
or reduced_redundancy
--placement=PLACEMENT Where in the generated VCL the logging call
should be placed, overriding any format_version
default. Can be none or waf_debug
--server-side-encryption=SERVER-SIDE-ENCRYPTION
Set to enable S3 Server Side Encryption. Can be
either AES256 or aws:kms
--server-side-encryption-kms-key-id=SERVER-SIDE-ENCRYPTION-KMS-KEY-ID
Server-side KMS Key ID. Must be set if
server-side-encryption is set to aws:kms
logging s3 list --version=VERSION [<flags>]
List S3 endpoints on a Fastly service version
-s, --service-id=SERVICE-ID Service ID
--version=VERSION Number of service version
logging s3 describe --version=VERSION --name=NAME [<flags>]
Show detailed information about a S3 logging endpoint on a Fastly service
version
-s, --service-id=SERVICE-ID Service ID
--version=VERSION Number of service version
-d, --name=NAME The name of the S3 logging object
logging s3 update --version=VERSION --name=NAME [<flags>]
Update a S3 logging endpoint on a Fastly service version
-s, --service-id=SERVICE-ID Service ID
--version=VERSION Number of service version
-n, --name=NAME The name of the S3 logging object
--new-name=NEW-NAME New name of the S3 logging object
--bucket=BUCKET Your S3 bucket name
--access-key=ACCESS-KEY Your S3 account access key
--secret-key=SECRET-KEY Your S3 account secret key
--domain=DOMAIN The domain of the S3 endpoint
--path=PATH The path to upload logs to
--period=PERIOD How frequently log files are finalized so they
can be available for reading (in seconds,
default 3600)
--gzip-level=GZIP-LEVEL What level of GZIP encoding to have when
dumping logs (default 0, no compression)
--format=FORMAT Apache style log formatting
--format-version=FORMAT-VERSION
The version of the custom logging format used
for the configured endpoint. Can be either 2
(default) or 1
--message-type=MESSAGE-TYPE
How the message should be formatted. One of:
classic (default), loggly, logplex or blank
--response-condition=RESPONSE-CONDITION
The name of an existing condition in the
configured endpoint, or leave blank to always
execute
--timestamp-format=TIMESTAMP-FORMAT
strftime specified timestamp formatting
(default "%Y-%m-%dT%H:%M:%S.000")
--redundancy=REDUNDANCY The S3 redundancy level. Can be either standard
or reduced_redundancy
--placement=PLACEMENT Where in the generated VCL the logging call
should be placed, overriding any format_version
default. Can be none or waf_debug
--server-side-encryption=SERVER-SIDE-ENCRYPTION
Set to enable S3 Server Side Encryption. Can be
either AES256 or aws:kms
--server-side-encryption-kms-key-id=SERVER-SIDE-ENCRYPTION-KMS-KEY-ID
Server-side KMS Key ID. Must be set if
server-side-encryption is set to aws:kms
logging s3 delete --version=VERSION --name=NAME [<flags>]
Delete a S3 logging endpoint on a Fastly service version
-s, --service-id=SERVICE-ID Service ID
--version=VERSION Number of service version
-n, --name=NAME The name of the S3 logging object
For help on a specific command, try e.g.
fastly help configure
Expand Down
2 changes: 1 addition & 1 deletion pkg/logging/bigquery/bigquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestBigQueryList(t *testing.T) {
}
}

func TestBiogQueryDescribe(t *testing.T) {
func TestBigQueryDescribe(t *testing.T) {
for _, testcase := range []struct {
args []string
api mock.API
Expand Down
84 changes: 84 additions & 0 deletions pkg/logging/s3/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package s3

import (
"io"

"github.com/fastly/cli/pkg/common"
"github.com/fastly/cli/pkg/compute/manifest"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/fastly"
)

// CreateCommand calls the Fastly API to create Amazon S3 logging endpoints.
type CreateCommand struct {
common.Base
manifest manifest.Data
Input fastly.CreateS3Input

redundancy string
serverSideEncryption string
}

// NewCreateCommand returns a usable command registered under the parent.
func NewCreateCommand(parent common.Registerer, globals *config.Data) *CreateCommand {
var c CreateCommand
c.Globals = globals
c.manifest.File.Read(manifest.Filename)
c.CmdClause = parent.Command("create", "Create an Amazon S3 logging endpoint on a Fastly service version").Alias("add")

c.CmdClause.Flag("name", "The name of the S3 logging object. Used as a primary key for API access").Short('n').Required().StringVar(&c.Input.Name)
c.CmdClause.Flag("service-id", "Service ID").Short('s').StringVar(&c.manifest.Flag.ServiceID)
c.CmdClause.Flag("version", "Number of service version").Required().IntVar(&c.Input.Version)

c.CmdClause.Flag("bucket", "Your S3 bucket name").Required().StringVar(&c.Input.BucketName)
c.CmdClause.Flag("access-key", "Your S3 account access key").Required().StringVar(&c.Input.AccessKey)
c.CmdClause.Flag("secret-key", "Your S3 account secret key").Required().StringVar(&c.Input.SecretKey)

c.CmdClause.Flag("domain", "The domain of the S3 endpoint").StringVar(&c.Input.Domain)
c.CmdClause.Flag("path", "The path to upload logs to").StringVar(&c.Input.Path)
c.CmdClause.Flag("period", "How frequently log files are finalized so they can be available for reading (in seconds, default 3600)").UintVar(&c.Input.Period)
c.CmdClause.Flag("gzip-level", "What level of GZIP encoding to have when dumping logs (default 0, no compression)").UintVar(&c.Input.GzipLevel)
c.CmdClause.Flag("format", "Apache style log formatting").StringVar(&c.Input.Format)
c.CmdClause.Flag("format-version", "The version of the custom logging format used for the configured endpoint. Can be either 2 (default) or 1").UintVar(&c.Input.FormatVersion)
c.CmdClause.Flag("message-type", "How the message should be formatted. One of: classic (default), loggly, logplex or blank").StringVar(&c.Input.MessageType)
c.CmdClause.Flag("response-condition", "The name of an existing condition in the configured endpoint, or leave blank to always execute").StringVar(&c.Input.ResponseCondition)
c.CmdClause.Flag("timestamp-format", `strftime specified timestamp formatting (default "%Y-%m-%dT%H:%M:%S.000")`).StringVar(&c.Input.TimestampFormat)
c.CmdClause.Flag("redundancy", "The S3 redundancy level. Can be either standard or reduced_redundancy").EnumVar(&c.redundancy, string(fastly.S3RedundancyStandard), string(fastly.S3RedundancyReduced))
c.CmdClause.Flag("placement", "Where in the generated VCL the logging call should be placed, overriding any format_version default. Can be none or waf_debug").StringVar(&c.Input.Placement)
c.CmdClause.Flag("server-side-encryption", "Set to enable S3 Server Side Encryption. Can be either AES256 or aws:kms").EnumVar(&c.serverSideEncryption, string(fastly.S3ServerSideEncryptionAES), string(fastly.S3ServerSideEncryptionKMS))
c.CmdClause.Flag("server-side-encryption-kms-key-id", "Server-side KMS Key ID. Must be set if server-side-encryption is set to aws:kms").StringVar(&c.Input.ServerSideEncryptionKMSKeyID)
return &c
}

// Exec invokes the application logic for the command.
func (c *CreateCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
c.Input.Service = serviceID

switch c.redundancy {
case string(fastly.S3RedundancyStandard):
c.Input.Redundancy = fastly.S3RedundancyStandard
case string(fastly.S3RedundancyReduced):
c.Input.Redundancy = fastly.S3RedundancyReduced
}

switch c.serverSideEncryption {
case string(fastly.S3ServerSideEncryptionAES):
c.Input.ServerSideEncryption = fastly.S3ServerSideEncryptionAES
case string(fastly.S3ServerSideEncryptionKMS):
c.Input.ServerSideEncryption = fastly.S3ServerSideEncryptionKMS
}

d, err := c.Globals.Client.CreateS3(&c.Input)
if err != nil {
return err
}

text.Success(out, "Created S3 logging endpoint %s (service %s version %d)", d.Name, d.ServiceID, d.Version)
return nil
}
47 changes: 47 additions & 0 deletions pkg/logging/s3/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package s3

import (
"io"

"github.com/fastly/cli/pkg/common"
"github.com/fastly/cli/pkg/compute/manifest"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/fastly"
)

// DeleteCommand calls the Fastly API to delete Amazon S3 logging endpoints.
type DeleteCommand struct {
common.Base
manifest manifest.Data
Input fastly.DeleteS3Input
}

// NewDeleteCommand returns a usable command registered under the parent.
func NewDeleteCommand(parent common.Registerer, globals *config.Data) *DeleteCommand {
var c DeleteCommand
c.Globals = globals
c.manifest.File.Read(manifest.Filename)
c.CmdClause = parent.Command("delete", "Delete a S3 logging endpoint on a Fastly service version").Alias("remove")
c.CmdClause.Flag("service-id", "Service ID").Short('s').StringVar(&c.manifest.Flag.ServiceID)
c.CmdClause.Flag("version", "Number of service version").Required().IntVar(&c.Input.Version)
c.CmdClause.Flag("name", "The name of the S3 logging object").Short('n').Required().StringVar(&c.Input.Name)
return &c
}

// Exec invokes the application logic for the command.
func (c *DeleteCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
c.Input.Service = serviceID

if err := c.Globals.Client.DeleteS3(&c.Input); err != nil {
return err
}

text.Success(out, "Deleted S3 logging endpoint %s (service %s version %d)", c.Input.Name, c.Input.Service, c.Input.Version)
return nil
}
66 changes: 66 additions & 0 deletions pkg/logging/s3/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package s3

import (
"fmt"
"io"

"github.com/fastly/cli/pkg/common"
"github.com/fastly/cli/pkg/compute/manifest"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/errors"
"github.com/fastly/go-fastly/fastly"
)

// DescribeCommand calls the Fastly API to describe an Amazon S3 logging endpoint.
type DescribeCommand struct {
common.Base
manifest manifest.Data
Input fastly.GetS3Input
}

// NewDescribeCommand returns a usable command registered under the parent.
func NewDescribeCommand(parent common.Registerer, globals *config.Data) *DescribeCommand {
var c DescribeCommand
c.Globals = globals
c.manifest.File.Read(manifest.Filename)
c.CmdClause = parent.Command("describe", "Show detailed information about a S3 logging endpoint on a Fastly service version").Alias("get")
c.CmdClause.Flag("service-id", "Service ID").Short('s').StringVar(&c.manifest.Flag.ServiceID)
c.CmdClause.Flag("version", "Number of service version").Required().IntVar(&c.Input.Version)
c.CmdClause.Flag("name", "The name of the S3 logging object").Short('d').Required().StringVar(&c.Input.Name)
return &c
}

// Exec invokes the application logic for the command.
func (c *DescribeCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
c.Input.Service = serviceID

s3, err := c.Globals.Client.GetS3(&c.Input)
if err != nil {
return err
}

fmt.Fprintf(out, "Service ID: %s\n", s3.ServiceID)
fmt.Fprintf(out, "Version: %d\n", s3.Version)
fmt.Fprintf(out, "Name: %s\n", s3.Name)
fmt.Fprintf(out, "Bucket: %s\n", s3.BucketName)
fmt.Fprintf(out, "Access key: %s\n", s3.AccessKey)
fmt.Fprintf(out, "Secret key: %s\n", s3.SecretKey)
fmt.Fprintf(out, "Path: %s\n", s3.Path)
fmt.Fprintf(out, "Period: %d\n", s3.Period)
fmt.Fprintf(out, "GZip level: %d\n", s3.GzipLevel)
fmt.Fprintf(out, "Format: %s\n", s3.Format)
fmt.Fprintf(out, "Format version: %d\n", s3.FormatVersion)
fmt.Fprintf(out, "Response condition: %s\n", s3.ResponseCondition)
fmt.Fprintf(out, "Message type: %s\n", s3.MessageType)
fmt.Fprintf(out, "Timestamp format: %s\n", s3.TimestampFormat)
fmt.Fprintf(out, "Placement: %s\n", s3.Placement)
fmt.Fprintf(out, "Redundancy: %s\n", s3.Redundancy)
fmt.Fprintf(out, "Server-side encryption: %s\n", s3.ServerSideEncryption)
fmt.Fprintf(out, "Server-side encryption KMS key ID: %s\n", s3.ServerSideEncryption)

return nil
}
3 changes: 3 additions & 0 deletions pkg/logging/s3/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package s3 contains commands to inspect and manipulate Fastly service S3
// logging endpoints.
package s3
Loading

0 comments on commit 3b6f710

Please sign in to comment.