Skip to content

Commit

Permalink
logging: adds Google Cloud Pub/Sub logging endpoint support
Browse files Browse the repository at this point in the history
  • Loading branch information
mccurdyc committed May 28, 2020
1 parent b5021da commit 12e7485
Show file tree
Hide file tree
Showing 13 changed files with 1,238 additions and 5 deletions.
6 changes: 6 additions & 0 deletions pkg/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ type Interface interface {
UpdateKafka(*fastly.UpdateKafkaInput) (*fastly.Kafka, error)
DeleteKafka(*fastly.DeleteKafkaInput) error

CreatePubsub(*fastly.CreatePubsubInput) (*fastly.Pubsub, error)
ListPubsubs(*fastly.ListPubsubsInput) ([]*fastly.Pubsub, error)
GetPubsub(*fastly.GetPubsubInput) (*fastly.Pubsub, error)
UpdatePubsub(*fastly.UpdatePubsubInput) (*fastly.Pubsub, error)
DeletePubsub(*fastly.DeletePubsubInput) error

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

GetRegions() (*fastly.RegionsResponse, error)
Expand Down
25 changes: 20 additions & 5 deletions pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/fastly/cli/pkg/logging/elasticsearch"
"github.com/fastly/cli/pkg/logging/ftp"
"github.com/fastly/cli/pkg/logging/gcs"
"github.com/fastly/cli/pkg/logging/googlepubsub"
"github.com/fastly/cli/pkg/logging/heroku"
"github.com/fastly/cli/pkg/logging/honeycomb"
"github.com/fastly/cli/pkg/logging/https"
Expand Down Expand Up @@ -288,18 +289,25 @@ func Run(args []string, env config.Environment, file config.File, configFilePath
httpsUpdate := https.NewUpdateCommand(httpsRoot.CmdClause, &globals)
httpsDelete := https.NewDeleteCommand(httpsRoot.CmdClause, &globals)

statsRoot := stats.NewRootCommand(app, &globals)
statsRegions := stats.NewRegionsCommand(statsRoot.CmdClause, &globals)
statsHistorical := stats.NewHistoricalCommand(statsRoot.CmdClause, &globals)
statsRealtime := stats.NewRealtimeCommand(statsRoot.CmdClause, &globals)

kafkaRoot := kafka.NewRootCommand(loggingRoot.CmdClause, &globals)
kafkaCreate := kafka.NewCreateCommand(kafkaRoot.CmdClause, &globals)
kafkaList := kafka.NewListCommand(kafkaRoot.CmdClause, &globals)
kafkaDescribe := kafka.NewDescribeCommand(kafkaRoot.CmdClause, &globals)
kafkaUpdate := kafka.NewUpdateCommand(kafkaRoot.CmdClause, &globals)
kafkaDelete := kafka.NewDeleteCommand(kafkaRoot.CmdClause, &globals)

googlepubsubRoot := googlepubsub.NewRootCommand(loggingRoot.CmdClause, &globals)
googlepubsubCreate := googlepubsub.NewCreateCommand(googlepubsubRoot.CmdClause, &globals)
googlepubsubList := googlepubsub.NewListCommand(googlepubsubRoot.CmdClause, &globals)
googlepubsubDescribe := googlepubsub.NewDescribeCommand(googlepubsubRoot.CmdClause, &globals)
googlepubsubUpdate := googlepubsub.NewUpdateCommand(googlepubsubRoot.CmdClause, &globals)
googlepubsubDelete := googlepubsub.NewDeleteCommand(googlepubsubRoot.CmdClause, &globals)

statsRoot := stats.NewRootCommand(app, &globals)
statsRegions := stats.NewRegionsCommand(statsRoot.CmdClause, &globals)
statsHistorical := stats.NewHistoricalCommand(statsRoot.CmdClause, &globals)
statsRealtime := stats.NewRealtimeCommand(statsRoot.CmdClause, &globals)

commands := []common.Command{
configureRoot,
whoamiRoot,
Expand Down Expand Up @@ -505,6 +513,13 @@ func Run(args []string, env config.Environment, file config.File, configFilePath
kafkaUpdate,
kafkaDelete,

googlepubsubRoot,
googlepubsubCreate,
googlepubsubList,
googlepubsubDescribe,
googlepubsubUpdate,
googlepubsubDelete,

statsRoot,
statsRegions,
statsHistorical,
Expand Down
85 changes: 85 additions & 0 deletions pkg/app/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2594,6 +2594,91 @@ COMMANDS
--version=VERSION Number of service version
-n, --name=NAME The name of the Kafka logging object
logging googlepubsub create --name=NAME --version=VERSION --user=USER --secret-key=SECRET-KEY --topic=TOPIC --project-id=PROJECT-ID [<flags>]
Create a Google Cloud Pub/Sub logging endpoint on a Fastly service version
-n, --name=NAME The name of the Google Cloud Pub/Sub logging
object. Used as a primary key for API access
-s, --service-id=SERVICE-ID Service ID
--version=VERSION Number of service version
--user=USER Your Google Cloud Platform service account
email address. The client_email field in your
service account authentication JSON
--secret-key=SECRET-KEY Your Google Cloud Platform account secret key.
The private_key field in your service account
authentication JSON
--topic=TOPIC The Google Cloud Pub/Sub topic to which logs
will be published
--project-id=PROJECT-ID The ID of your Google Cloud Platform project
--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
--placement=PLACEMENT Where in the generated VCL the logging call
should be placed, overriding any format_version
default. Can be none or waf_debug. This field
is not required and has no default value
--response-condition=RESPONSE-CONDITION
The name of an existing condition in the
configured endpoint, or leave blank to always
execute
logging googlepubsub list --version=VERSION [<flags>]
List Google Cloud Pub/Sub endpoints on a Fastly service version
-s, --service-id=SERVICE-ID Service ID
--version=VERSION Number of service version
logging googlepubsub describe --version=VERSION --name=NAME [<flags>]
Show detailed information about a Google Cloud Pub/Sub 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 Google Cloud Pub/Sub logging
object
logging googlepubsub update --version=VERSION --name=NAME [<flags>]
Update a Google Cloud Pub/Sub 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 Google Cloud Pub/Sub logging
object
--new-name=NEW-NAME New name of the Google Cloud Pub/Sub logging
object
--user=USER Your Google Cloud Platform service account
email address. The client_email field in your
service account authentication JSON
--secret-key=SECRET-KEY Your Google Cloud Platform account secret key.
The private_key field in your service account
authentication JSON
--topic=TOPIC The Google Cloud Pub/Sub topic to which logs
will be published
--project-id=PROJECT-ID The ID of your Google Cloud Platform project
--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
--placement=PLACEMENT Where in the generated VCL the logging call
should be placed, overriding any format_version
default. Can be none or waf_debug. This field
is not required and has no default value
--response-condition=RESPONSE-CONDITION
The name of an existing condition in the
configured endpoint, or leave blank to always
execute
logging googlepubsub delete --version=VERSION --name=NAME [<flags>]
Delete a Google Cloud Pub/Sub 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 Google Cloud Pub/Sub logging
object
stats regions
List stats regions
Expand Down
108 changes: 108 additions & 0 deletions pkg/logging/googlepubsub/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package googlepubsub

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 Google Cloud Pub/Sub logging endpoints.
type CreateCommand struct {
common.Base
manifest manifest.Data

// required
EndpointName string // Can't shaddow common.Base method Name().
Version int
User string
SecretKey string
Topic string
ProjectID string

// optional
Format common.OptionalString
FormatVersion common.OptionalUint
Placement common.OptionalString
ResponseCondition common.OptionalString
}

// 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 a Google Cloud Pub/Sub logging endpoint on a Fastly service version").Alias("add")

c.CmdClause.Flag("name", "The name of the Google Cloud Pub/Sub logging object. Used as a primary key for API access").Short('n').Required().StringVar(&c.EndpointName)
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.Version)

c.CmdClause.Flag("user", "Your Google Cloud Platform service account email address. The client_email field in your service account authentication JSON").Required().StringVar(&c.User)
c.CmdClause.Flag("secret-key", "Your Google Cloud Platform account secret key. The private_key field in your service account authentication JSON").Required().StringVar(&c.SecretKey)
c.CmdClause.Flag("topic", "The Google Cloud Pub/Sub topic to which logs will be published").Required().StringVar(&c.Topic)
c.CmdClause.Flag("project-id", "The ID of your Google Cloud Platform project").Required().StringVar(&c.ProjectID)

c.CmdClause.Flag("format", "Apache style log formatting").Action(c.Format.Set).StringVar(&c.Format.Value)
c.CmdClause.Flag("format-version", "The version of the custom logging format used for the configured endpoint. Can be either 2 (default) or 1").Action(c.FormatVersion.Set).UintVar(&c.FormatVersion.Value)
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. This field is not required and has no default value").Action(c.Placement.Set).StringVar(&c.Placement.Value)
c.CmdClause.Flag("response-condition", "The name of an existing condition in the configured endpoint, or leave blank to always execute").Action(c.ResponseCondition.Set).StringVar(&c.ResponseCondition.Value)

return &c
}

// createInput transforms values parsed from CLI flags into an object to be used by the API client library.
func (c *CreateCommand) createInput() (*fastly.CreatePubsubInput, error) {
var input fastly.CreatePubsubInput

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return nil, errors.ErrNoServiceID
}

input.Service = serviceID
input.Version = c.Version
input.Name = fastly.String(c.EndpointName)
input.User = fastly.String(c.User)
input.SecretKey = fastly.String(c.SecretKey)
input.Topic = fastly.String(c.Topic)
input.ProjectID = fastly.String(c.ProjectID)

if c.Format.Valid {
input.Format = fastly.String(c.Format.Value)
}

if c.FormatVersion.Valid {
input.FormatVersion = fastly.Uint(c.FormatVersion.Value)
}

if c.ResponseCondition.Valid {
input.ResponseCondition = fastly.String(c.ResponseCondition.Value)
}

if c.Placement.Valid {
input.Placement = fastly.String(c.Placement.Value)
}

return &input, nil
}

// Exec invokes the application logic for the command.
func (c *CreateCommand) Exec(in io.Reader, out io.Writer) error {
input, err := c.createInput()
if err != nil {
return err
}

d, err := c.Globals.Client.CreatePubsub(input)
if err != nil {
return err
}

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

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 Google Cloud Pub/Sub logging endpoints.
type DeleteCommand struct {
common.Base
manifest manifest.Data
Input fastly.DeletePubsubInput
}

// 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 Google Cloud Pub/Sub 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 Google Cloud Pub/Sub 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.DeletePubsub(&c.Input); err != nil {
return err
}

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

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 a Google Cloud Pub/Sub logging endpoint.
type DescribeCommand struct {
common.Base
manifest manifest.Data
Input fastly.GetPubsubInput
}

// 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 Google Cloud Pub/Sub 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 Google Cloud Pub/Sub logging object").Short('n').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

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

fmt.Fprintf(out, "Service ID: %s\n", googlepubsub.ServiceID)
fmt.Fprintf(out, "Version: %d\n", googlepubsub.Version)
fmt.Fprintf(out, "Name: %s\n", googlepubsub.Name)
fmt.Fprintf(out, "User: %s\n", googlepubsub.User)
fmt.Fprintf(out, "Secret key: %s\n", googlepubsub.SecretKey)
fmt.Fprintf(out, "Project ID: %s\n", googlepubsub.ProjectID)
fmt.Fprintf(out, "Topic: %s\n", googlepubsub.Topic)
fmt.Fprintf(out, "Format: %s\n", googlepubsub.Format)
fmt.Fprintf(out, "Format version: %d\n", googlepubsub.FormatVersion)
fmt.Fprintf(out, "Response condition: %s\n", googlepubsub.ResponseCondition)
fmt.Fprintf(out, "Placement: %s\n", googlepubsub.Placement)

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

0 comments on commit 12e7485

Please sign in to comment.