diff --git a/fastly/bigquery.go b/fastly/bigquery.go new file mode 100644 index 000000000..685c3a41b --- /dev/null +++ b/fastly/bigquery.go @@ -0,0 +1,234 @@ +package fastly + +import ( + "fmt" +) + +// BigQuery represents a BigQuery logging 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"` + 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"` +} + +// GetBigQueryInput is used as input to the GetBQs 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 +} + +// GetBigQuery lists all BigQuerys associated with a service version. +func (c *Client) GetBigQuery(i *GetBigQueryInput) ([]*BigQuery, error) { + if i.Service == "" { + return nil, ErrMissingService + } + + if i.Version == 0 { + return nil, ErrMissingVersion + } + + path := fmt.Sprintf("/service/%s/version/%d/logging/bigquery", i.Service, i.Version) + resp, err := c.Get(path, nil) + if err != nil { + return nil, err + } + + var bs []*BigQuery + if err := decodeJSON(&bs, resp.Body); err != nil { + return nil, err + } + return bs, nil +} + +// CreateBigQueryInput is used as input to the CreateBigQuery function. +// All fields are required. +type CreateBigQueryInput struct { + // Service is the ID of the service. + 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 + + // User is the user with access to write to your BigQuery dataset. + User string + + // Secret key is the user's secret key. + SecretKey string +} + +// CreateBigQuery creates a new Fastly BigQuery logging endpoint. +func (c *Client) CreateBigQuery(i *CreateBigQueryInput) (*BigQuery, error) { + if i.Service == "" { + return nil, ErrMissingService + } + + if i.Version == 0 { + return nil, ErrMissingVersion + } + + if i.Name == "" { + return nil, ErrMissingName + } + + if i.ProjectID == "" { + return nil, ErrMissingProjectID + } + + if i.Dataset == "" { + return nil, ErrMissingDataset + } + + if i.Table == "" { + return nil, ErrMissingTable + } + + if i.User == "" { + return nil, ErrMissingUser + } + + if i.SecretKey == "" { + return nil, ErrMissingSecretKey + } + + 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 + + path := fmt.Sprintf("/service/%s/version/%d/logging/gcs", i.Service, i.Version) + resp, err := c.PostForm(path, i, &RequestOptions{ + Params: params, + }) + if err != nil { + return nil, err + } + + var b *BigQuery + if err := decodeJSON(&b, resp.Body); err != nil { + return nil, err + } + return b, nil +} + +// UpdateBigQueryInput is used as input to the UpdateBigQuery function. +// All fields are required. +type UpdateBigQueryInput struct { + // Service is the ID of the service. + Service string + + //Version is the specific configuration version. + 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. + Name string + + // NewName is the new name of your BigQuery logging endpoint. + NewName string +} + +// UpdateBigQuery updates a BigQuery logging endpoint. +func (c *Client) UpdateBigQuery(i *UpdateBigQueryInput) (*BigQuery, error) { + if i.Service == "" { + return nil, ErrMissingService + } + + if i.Version == 0 { + return nil, ErrMissingVersion + } + + if i.Name == "" { + return nil, ErrMissingName + } + + if i.NewName == "" { + return nil, ErrMissingNewName + } + + params := make(map[string]string) + params["name"] = i.NewName + + path := fmt.Sprintf("/service/%s/version/%d/logging/bigquery/%s", i.Service, i.Version, i.Name) + resp, err := c.PutForm(path, i, &RequestOptions{ + Params: params, + }) + if err != nil { + return nil, err + } + + var b *BigQuery + if err := decodeJSON(&b, resp.Body); err != nil { + return nil, err + } + return b, nil +} + +// DeleteBigQueryInput is the input parameter to DeleteBigQuery. +// All fields are required. +type DeleteBigQueryInput struct { + // Service is the ID of the service. + Service string + + // Version is the specific configuration. + Version int + + // Name is the name of the BigQuery logging endpoint to delete. + Name string +} + +// DeleteBigQuery deletes the given BigQuery logging endpoint. +func (c *Client) DeleteBigQuery(i *DeleteBigQueryInput) error { + if i.Service == "" { + return ErrMissingService + } + + if i.Version == 0 { + return ErrMissingVersion + } + + if i.Name == "" { + return ErrMissingName + } + + path := fmt.Sprintf("/service/%s/version/%d/logging/bigquery/%s", i.Service, i.Version, i.Name) + resp, err := c.Delete(path, nil) + if err != nil { + return err + } + + var r *statusResp + if err := decodeJSON(&r, resp.Body); err != nil { + return err + } + if !r.Ok() { + return fmt.Errorf("Not Ok") + } + return nil +} diff --git a/fastly/errors.go b/fastly/errors.go index 157a67fb3..e273f1f66 100644 --- a/fastly/errors.go +++ b/fastly/errors.go @@ -105,6 +105,26 @@ var ErrMissingConfigSetID = errors.New("Missing required field 'ConfigSetID'") // requires a list of WAF id's, but it is empty var ErrMissingWAFList = errors.New("WAF slice is empty") +// ErrMissingProjectID is an error that is returned was an input struct +// requires a Project ID, but one was not set +var ErrMissingProjectID = errors.New("Missing required field 'ProjectID'") + +// ErrMissingDataset is an error that is returned was an input struct +// requires a Dataset, but one was not set +var ErrMissingDataset = errors.New("Missing required field 'Dataset'") + +// ErrMissingTable is an error that is returned was an input struct +// requires a Table, but one was not set +var ErrMissingTable = errors.New("Missing required field 'Table'") + +// ErrMissingUser is an error that is returned was an input struct +// requires a User, but one was not set +var ErrMissingUser = errors.New("Missing required field 'User'") + +// ErrMissingSecretKey is an error that is returned was an input struct +// requires a Secret Key, but one was not set +var ErrMissingSecretKey = errors.New("Missing required field 'SecretKey'") + // Ensure HTTPError is, in fact, an error. var _ error = (*HTTPError)(nil)