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

Add format version to all missing logging providers #97

Merged
merged 3 commits into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
300 changes: 109 additions & 191 deletions fastly/bigquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,52 @@ package fastly
import (
"fmt"
"net/url"
"sort"
"time"
)

// BigQuery represents a BigQuery logging response from the Fastly API.
// BigQuery represents a BigQuery response from the Fastly API.
type BigQuery struct {
ServiceID string `mapstructure:"service_id"`
Name string `mapstructure:"name"`
Format string `mapstructure:"format"`
User string `mapstructure:"user"`
ProjectID string `mapstructure:"project_id"`
Dataset string `mapstructure:"dataset"`
Table string `mapstructure:"table"`
Template string `mapstructure:"template_suffix"`
SecretKey string `mapstructure:"secret_key"`
CreatedAt string `mapstructure:"created_at"`
UpdatedAt string `mapstructure:"updated_at"`
DeletedAt string `mapstructure:"deleted_at"`
ResponseCondition string `mapstructure:"response_condition"`
Placement string `mapstructure:"placement"`
ServiceID string `mapstructure:"service_id"`
Version int `mapstructure:"version"`

Name string `mapstructure:"name"`
Format string `mapstructure:"format"`
User string `mapstructure:"user"`
ProjectID string `mapstructure:"project_id"`
Dataset string `mapstructure:"dataset"`
Table string `mapstructure:"table"`
Template string `mapstructure:"template_suffix"`
SecretKey string `mapstructure:"secret_key"`
ResponseCondition string `mapstructure:"response_condition"`
Placement string `mapstructure:"placement"`
FormatVersion uint `mapstructure:"format_version"`
CreatedAt *time.Time `mapstructure:"created_at"`
philippschulte marked this conversation as resolved.
Show resolved Hide resolved
UpdatedAt *time.Time `mapstructure:"updated_at"`
DeletedAt *time.Time `mapstructure:"deleted_at"`
}

// GetBigQueryInput is used as input to the GetBigQuery function.
type GetBigQueryInput struct {
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
// bigQueriesByName is a sortable list of BigQueries.
type bigQueriesByName []*BigQuery

// Len, Swap, and Less implement the sortable interface.
func (s bigQueriesByName) Len() int { return len(s) }
func (s bigQueriesByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s bigQueriesByName) Less(i, j int) bool {
return s[i].Name < s[j].Name
}

// ListBigQueriesInput is used as input to the ListBigQueries function.
type ListBigQueriesInput struct {
// Service is the ID of the service (required).
Service string

// Version is the specific configuration version (required).
Version int
}

// GetBigQuery lists all BigQuerys associated with a service version.
func (c *Client) GetBigQuery(i *GetBigQueryInput) ([]*BigQuery, error) {
// ListBigQueries returns the list of BigQueries for the configuration version.
func (c *Client) ListBigQueries(i *ListBigQueriesInput) ([]*BigQuery, error) {
if i.Service == "" {
return nil, ErrMissingService
}
Expand All @@ -47,57 +63,35 @@ func (c *Client) GetBigQuery(i *GetBigQueryInput) ([]*BigQuery, error) {
return nil, err
}

var bs []*BigQuery
if err := decodeJSON(&bs, resp.Body); err != nil {
var bigQueries []*BigQuery
if err := decodeJSON(&bigQueries, resp.Body); err != nil {
return nil, err
}
return bs, nil
sort.Stable(bigQueriesByName(bigQueries))
return bigQueries, nil
}

// CreateBigQueryInput is used as input to the CreateBigQuery function.
type CreateBigQueryInput struct {
// All fields other than format are required.
// Service is the ID of the service.
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string

//Version is the specific configuration version.
Version int

// Name is the name if your bigquery logging endpoint.
Name string

// Project ID your GCP project ID.
ProjectID string

// Dataset is your BigQuery dataset.
Dataset string

// Table is your BigQuery table.
Table string

// Template is your BigQuery template suffix.
Template string

// User is the user with access to write to your BigQuery dataset.
User string

// Secret key is the user's secret key.
SecretKey string

// Format is the log formatting desired for your BigQuery dataset.
// Optional.
Format string

// ResponseCondition allows you to attach a response condition to your BigQuery logging endpoint.
// Optional.
ResponseCondition string

// Placement is the log placement desired for your BigQuery logging endpoint.
// Optional.
Placement string
Name string `form:"name,omitempty"`
ProjectID string `form:"project_id,omitempty"`
Dataset string `form:"dataset,omitempty"`
Table string `form:"table,omitempty"`
Template string `form:"template_suffix,omitempty"`
User string `form:"user,omitempty"`
SecretKey string `form:"secret_key,omitempty"`
Format string `form:"format,omitempty"`
ResponseCondition string `form:"response_condition,omitempty"`
Placement string `form:"placement,omitempty"`
FormatVersion uint `form:"format_version,omitempty"`
}

// CreateBigQuery creates a new Fastly BigQuery logging endpoint.
// CreateBigQuery creates a new Fastly BigQuery.
func (c *Client) CreateBigQuery(i *CreateBigQueryInput) (*BigQuery, error) {
if i.Service == "" {
return nil, ErrMissingService
Expand All @@ -107,118 +101,81 @@ func (c *Client) CreateBigQuery(i *CreateBigQueryInput) (*BigQuery, error) {
return nil, ErrMissingVersion
}

if i.Name == "" {
return nil, ErrMissingName
}

if i.ProjectID == "" {
return nil, ErrMissingProjectID
path := fmt.Sprintf("/service/%s/version/%d/logging/bigquery", i.Service, i.Version)
resp, err := c.PostForm(path, i, nil)
if err != nil {
return nil, err
}

if i.Dataset == "" {
return nil, ErrMissingDataset
var bigQuery *BigQuery
if err := decodeJSON(&bigQuery, resp.Body); err != nil {
return nil, err
}
return bigQuery, nil
}

if i.Table == "" {
return nil, ErrMissingTable
}
// GetBigQueryInput is used as input to the GetBigQuery function.
type GetBigQueryInput struct {
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string
Version int

if i.User == "" {
return nil, ErrMissingUser
}
// Name is the name of the BigQuery to fetch.
Name string
}

if i.SecretKey == "" {
return nil, ErrMissingSecretKey
// GetBigQuery gets the BigQuery configuration with the given parameters.
func (c *Client) GetBigQuery(i *GetBigQueryInput) (*BigQuery, error) {
if i.Service == "" {
return nil, ErrMissingService
}

params := make(map[string]string)
params["name"] = i.Name
params["project_id"] = i.ProjectID
params["dataset"] = i.Dataset
params["table"] = i.Table
params["user"] = i.User
params["secret_key"] = i.SecretKey
if i.Format != "" {
params["format"] = i.Format
}
if i.ResponseCondition != "" {
params["response_condition"] = i.ResponseCondition
}
if i.Template != "" {
params["template_suffix"] = i.Template
if i.Version == 0 {
return nil, ErrMissingVersion
}

if i.Placement != "" {
params["placement"] = i.Placement
if i.Name == "" {
return nil, ErrMissingName
}

path := fmt.Sprintf("/service/%s/version/%d/logging/bigquery", i.Service, i.Version)
resp, err := c.PostForm(path, i, &RequestOptions{
Params: params,
})
path := fmt.Sprintf("/service/%s/version/%d/logging/bigquery/%s", i.Service, i.Version, url.PathEscape(i.Name))
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}

var b *BigQuery
if err := decodeJSON(&b, resp.Body); err != nil {
var bigQuery *BigQuery
if err := decodeJSON(&bigQuery, resp.Body); err != nil {
return nil, err
}
return b, nil
return bigQuery, nil
}

// UpdateBigQueryInput is used as input to the UpdateBigQuery function.
type UpdateBigQueryInput struct {
// Service is the ID of the service.
// This field is required.
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string

//Version is the specific configuration version.
// This field is required.
Version int

// Name is the old name if your bigquery logging endpoint.
// Used to identify the correct BigQuery logging endpoint if there
// is a name change.
// This field is required.
// Name is the name of the BigQuery to update.
Name string

// NewName is the new name of your BigQuery logging endpoint.
// This field is required.
NewName string

// Project ID your GCP project ID.
ProjectID string

// Dataset is your BigQuery dataset.
Dataset string

// Table is your BigQuery table.
Table string

// Template is your BigQuery template suffix.
Template string

// User is the user with access to write to your BigQuery dataset.
User string

// Secret key is the user's secret key.
SecretKey string

// Format is the log formatting desired for your BigQuery dataset.
// Optional.
Format string

// ResponseCondition allows you to attach a response condition to your BigQuery logging endpoint.
// Optional.
ResponseCondition string

// Placement is the log placement desired for your BigQuery logging endpoint.
// Optional.
Placement string
NewName string `form:"name,omitempty"`
ProjectID string `form:"project_id,omitempty"`
Dataset string `form:"dataset,omitempty"`
Table string `form:"table,omitempty"`
Template string `form:"template_suffix,omitempty"`
User string `form:"user,omitempty"`
SecretKey string `form:"secret_key,omitempty"`
Format string `form:"format,omitempty"`
ResponseCondition string `form:"response_condition,omitempty"`
Placement string `form:"placement,omitempty"`
FormatVersion uint `form:"format_version,omitempty"`
}

// UpdateBigQuery updates a BigQuery logging endpoint.
// UpdateBigQuery updates a specific BigQuery.
func (c *Client) UpdateBigQuery(i *UpdateBigQueryInput) (*BigQuery, error) {
if i.Service == "" {
return nil, ErrMissingService
Expand All @@ -232,70 +189,31 @@ func (c *Client) UpdateBigQuery(i *UpdateBigQueryInput) (*BigQuery, error) {
return nil, ErrMissingName
}

if i.NewName == "" {
return nil, ErrMissingNewName
}

params := make(map[string]string)
params["name"] = i.NewName
if i.ProjectID != "" {
params["project_id"] = i.ProjectID
}
if i.Dataset != "" {
params["dataset"] = i.Dataset
}
if i.Table != "" {
params["table"] = i.Table
}
if i.Template != "" {
params["template_suffix"] = i.Template
}
if i.User != "" {
params["user"] = i.User
}
if i.SecretKey != "" {
params["secret_key"] = i.SecretKey
}
if i.Format != "" {
params["format"] = i.Format
}
if i.ResponseCondition != "" {
params["response_condition"] = i.ResponseCondition
}

if i.Placement != "" {
params["placement"] = i.Placement
}

path := fmt.Sprintf("/service/%s/version/%d/logging/bigquery/%s", i.Service, i.Version, url.PathEscape(i.Name))
resp, err := c.PutForm(path, i, &RequestOptions{
Params: params,
})
resp, err := c.PutForm(path, i, nil)
if err != nil {
return nil, err
}

var b *BigQuery
if err := decodeJSON(&b, resp.Body); err != nil {
var bigQuery *BigQuery
if err := decodeJSON(&bigQuery, resp.Body); err != nil {
return nil, err
}
return b, nil
return bigQuery, nil
}

// DeleteBigQueryInput is the input parameter to DeleteBigQuery.
// All fields are required.
type DeleteBigQueryInput struct {
// Service is the ID of the service.
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string

// Version is the specific configuration.
Version int

// Name is the name of the BigQuery logging endpoint to delete.
// Name is the name of the BigQuery to delete (required).
Name string
}

// DeleteBigQuery deletes the given BigQuery logging endpoint.
// DeleteBigQuery deletes the given BigQuery version.
func (c *Client) DeleteBigQuery(i *DeleteBigQueryInput) error {
if i.Service == "" {
return ErrMissingService
Expand Down
Loading