Skip to content

Commit

Permalink
Merge pull request cloudflare#757 from tjstansell/750-account-logpush
Browse files Browse the repository at this point in the history
support account-level logpush jobs
  • Loading branch information
jacobbednarz authored Dec 21, 2021
2 parents 558eebe + ccb1ee9 commit 8d7b587
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 39 deletions.
248 changes: 233 additions & 15 deletions logpush.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,32 @@ type LogpushDestinationExistsRequest struct {
DestinationConf string `json:"destination_conf"`
}

// CreateLogpushJob creates a new LogpushJob for a zone.
// CreateAccountLogpushJob creates a new account-level Logpush Job.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-create-logpush-job
func (api *API) CreateAccountLogpushJob(ctx context.Context, accountID string, job LogpushJob) (*LogpushJob, error) {
return api.createLogpushJob(ctx, AccountRouteRoot, accountID, job)
}

// CreateZoneLogpushJob creates a new zone-level Logpush Job.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-create-logpush-job
func (api *API) CreateZoneLogpushJob(ctx context.Context, zoneID string, job LogpushJob) (*LogpushJob, error) {
return api.createLogpushJob(ctx, ZoneRouteRoot, zoneID, job)
}

// CreateLogpushJob creates a new zone-level Logpush Job.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-create-logpush-job
//
// Deprecated: Use `CreateZoneLogpushJob` or `CreateAccountLogpushJob` depending
// on the desired resource to target.
func (api *API) CreateLogpushJob(ctx context.Context, zoneID string, job LogpushJob) (*LogpushJob, error) {
uri := fmt.Sprintf("/zones/%s/logpush/jobs", zoneID)
return api.createLogpushJob(ctx, ZoneRouteRoot, zoneID, job)
}

func (api *API) createLogpushJob(ctx context.Context, identifierType RouteRoot, identifier string, job LogpushJob) (*LogpushJob, error) {
uri := fmt.Sprintf("/%s/%s/logpush/jobs", identifierType, identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, job)
if err != nil {
return nil, err
Expand All @@ -109,11 +130,32 @@ func (api *API) CreateLogpushJob(ctx context.Context, zoneID string, job Logpush
return &r.Result, nil
}

// LogpushJobs returns all Logpush Jobs for a zone.
// ListAccountLogpushJobs returns all account-level Logpush Jobs for all datasets.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-list-logpush-jobs
func (api *API) ListAccountLogpushJobs(ctx context.Context, accountID string) ([]LogpushJob, error) {
return api.listLogpushJobs(ctx, AccountRouteRoot, accountID)
}

// ListZoneLogpushJobs returns all zone-level Logpush Jobs for all datasets.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-list-logpush-jobs
func (api *API) ListZoneLogpushJobs(ctx context.Context, zoneID string) ([]LogpushJob, error) {
return api.listLogpushJobs(ctx, ZoneRouteRoot, zoneID)
}

// LogpushJobs returns all zone-level Logpush Jobs for all datasets.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-list-logpush-jobs
//
// Deprecated: Use `ListZoneLogpushJobs` or `ListAccountLogpushJobs`
// depending on the desired resource to target.
func (api *API) LogpushJobs(ctx context.Context, zoneID string) ([]LogpushJob, error) {
uri := fmt.Sprintf("/zones/%s/logpush/jobs", zoneID)
return api.listLogpushJobs(ctx, ZoneRouteRoot, zoneID)
}

func (api *API) listLogpushJobs(ctx context.Context, identifierType RouteRoot, identifier string) ([]LogpushJob, error) {
uri := fmt.Sprintf("/%s/%s/logpush/jobs", identifierType, identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []LogpushJob{}, err
Expand All @@ -126,11 +168,33 @@ func (api *API) LogpushJobs(ctx context.Context, zoneID string) ([]LogpushJob, e
return r.Result, nil
}

// LogpushJobsForDataset returns all Logpush Jobs for a dataset in a zone.
// ListAccountLogpushJobsForDataset returns all account-level Logpush Jobs for a dataset.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-list-logpush-jobs-for-a-dataset
func (api *API) ListAccountLogpushJobsForDataset(ctx context.Context, accountID, dataset string) ([]LogpushJob, error) {
return api.listLogpushJobsForDataset(ctx, AccountRouteRoot, accountID, dataset)
}

// ListZoneLogpushJobsForDataset returns all zone-level Logpush Jobs for a dataset.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-list-logpush-jobs-for-a-dataset
func (api *API) ListZoneLogpushJobsForDataset(ctx context.Context, zoneID, dataset string) ([]LogpushJob, error) {
return api.listLogpushJobsForDataset(ctx, ZoneRouteRoot, zoneID, dataset)
}

// LogpushJobsForDataset returns all zone-level Logpush Jobs for a dataset.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-list-logpush-jobs-for-a-dataset
//
// Deprecated: Use `ListZoneLogpushJobsForDataset` or
// `ListAccountLogpushJobsForDataset` depending on the desired resource
// to target.
func (api *API) LogpushJobsForDataset(ctx context.Context, zoneID, dataset string) ([]LogpushJob, error) {
uri := fmt.Sprintf("/zones/%s/logpush/datasets/%s/jobs", zoneID, dataset)
return api.listLogpushJobsForDataset(ctx, ZoneRouteRoot, zoneID, dataset)
}

func (api *API) listLogpushJobsForDataset(ctx context.Context, identifierType RouteRoot, identifier, dataset string) ([]LogpushJob, error) {
uri := fmt.Sprintf("/%s/%s/logpush/datasets/%s/jobs", identifierType, identifier, dataset)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []LogpushJob{}, err
Expand All @@ -143,11 +207,36 @@ func (api *API) LogpushJobsForDataset(ctx context.Context, zoneID, dataset strin
return r.Result, nil
}

// GetAccountLogpushFields returns fields for a given account-level dataset.
//
// Account fields documentation: https://developers.cloudflare.com/logs/reference/log-fields/account
//
// API reference: https://api.cloudflare.com/#logpush-jobs-list-logpush-jobs
func (api *API) GetAccountLogpushFields(ctx context.Context, accountID, dataset string) (LogpushFields, error) {
return api.getLogpushFields(ctx, AccountRouteRoot, accountID, dataset)
}

// GetZoneLogpushFields returns fields for a given zone-level dataset.
//
// Zone fields documentation: https://developers.cloudflare.com/logs/reference/log-fields/zone
//
// API reference: https://api.cloudflare.com/#logpush-jobs-list-logpush-jobs
func (api *API) GetZoneLogpushFields(ctx context.Context, zoneID, dataset string) (LogpushFields, error) {
return api.getLogpushFields(ctx, ZoneRouteRoot, zoneID, dataset)
}

// LogpushFields returns fields for a given dataset.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-list-logpush-jobs
//
// Deprecated: Use `GetZoneLogpushFields` or `GetAccountLogpushFields`
// depending on the desired resource to target.
func (api *API) LogpushFields(ctx context.Context, zoneID, dataset string) (LogpushFields, error) {
uri := fmt.Sprintf("/zones/%s/logpush/datasets/%s/fields", zoneID, dataset)
return api.getLogpushFields(ctx, ZoneRouteRoot, zoneID, dataset)
}

func (api *API) getLogpushFields(ctx context.Context, identifierType RouteRoot, identifier, dataset string) (LogpushFields, error) {
uri := fmt.Sprintf("/%s/%s/logpush/datasets/%s/fields", identifierType, identifier, dataset)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return LogpushFields{}, err
Expand All @@ -160,11 +249,32 @@ func (api *API) LogpushFields(ctx context.Context, zoneID, dataset string) (Logp
return r.Result, nil
}

// GetAccountLogpushJob fetches detail about one account-level Logpush Job.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-logpush-job-details
func (api *API) GetAccountLogpushJob(ctx context.Context, accountID string, jobID int) (LogpushJob, error) {
return api.getLogpushJob(ctx, AccountRouteRoot, accountID, jobID)
}

// GetZoneLogpushJob fetches detail about one Logpush Job for a zone.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-logpush-job-details
func (api *API) GetZoneLogpushJob(ctx context.Context, zoneID string, jobID int) (LogpushJob, error) {
return api.getLogpushJob(ctx, ZoneRouteRoot, zoneID, jobID)
}

// LogpushJob fetches detail about one Logpush Job for a zone.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-logpush-job-details
//
// Deprecated: Use `GetZoneLogpushJob` or `GetAccountLogpushJob`
// depending on the desired resource to target.
func (api *API) LogpushJob(ctx context.Context, zoneID string, jobID int) (LogpushJob, error) {
uri := fmt.Sprintf("/zones/%s/logpush/jobs/%d", zoneID, jobID)
return api.getLogpushJob(ctx, ZoneRouteRoot, zoneID, jobID)
}

func (api *API) getLogpushJob(ctx context.Context, identifierType RouteRoot, identifier string, jobID int) (LogpushJob, error) {
uri := fmt.Sprintf("/%s/%s/logpush/jobs/%d", identifierType, identifier, jobID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return LogpushJob{}, err
Expand All @@ -177,11 +287,32 @@ func (api *API) LogpushJob(ctx context.Context, zoneID string, jobID int) (Logpu
return r.Result, nil
}

// UpdateAccountLogpushJob lets you update an account-level Logpush Job.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-update-logpush-job
func (api *API) UpdateAccountLogpushJob(ctx context.Context, accountID string, jobID int, job LogpushJob) error {
return api.updateLogpushJob(ctx, AccountRouteRoot, accountID, jobID, job)
}

// UpdateZoneLogpushJob lets you update a Logpush Job for a zone.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-update-logpush-job
func (api *API) UpdateZoneLogpushJob(ctx context.Context, zoneID string, jobID int, job LogpushJob) error {
return api.updateLogpushJob(ctx, ZoneRouteRoot, zoneID, jobID, job)
}

// UpdateLogpushJob lets you update a Logpush Job.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-update-logpush-job
//
// Deprecated: Use `UpdateZoneLogpushJob` or `UpdateAccountLogpushJob`
// depending on the desired resource to target.
func (api *API) UpdateLogpushJob(ctx context.Context, zoneID string, jobID int, job LogpushJob) error {
uri := fmt.Sprintf("/zones/%s/logpush/jobs/%d", zoneID, jobID)
return api.updateLogpushJob(ctx, ZoneRouteRoot, zoneID, jobID, job)
}

func (api *API) updateLogpushJob(ctx context.Context, identifierType RouteRoot, identifier string, jobID int, job LogpushJob) error {
uri := fmt.Sprintf("/%s/%s/logpush/jobs/%d", identifierType, identifier, jobID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, job)
if err != nil {
return err
Expand All @@ -194,11 +325,32 @@ func (api *API) UpdateLogpushJob(ctx context.Context, zoneID string, jobID int,
return nil
}

// DeleteAccountLogpushJob deletes an account-level Logpush Job.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-delete-logpush-job
func (api *API) DeleteAccountLogpushJob(ctx context.Context, accountID string, jobID int) error {
return api.deleteLogpushJob(ctx, AccountRouteRoot, accountID, jobID)
}

// DeleteZoneLogpushJob deletes a Logpush Job for a zone.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-delete-logpush-job
func (api *API) DeleteZoneLogpushJob(ctx context.Context, zoneID string, jobID int) error {
return api.deleteLogpushJob(ctx, ZoneRouteRoot, zoneID, jobID)
}

// DeleteLogpushJob deletes a Logpush Job for a zone.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-delete-logpush-job
//
// Deprecated: Use `DeleteZoneLogpushJob` or `DeleteAccountLogpushJob`
// depending on the desired resource to target.
func (api *API) DeleteLogpushJob(ctx context.Context, zoneID string, jobID int) error {
uri := fmt.Sprintf("/zones/%s/logpush/jobs/%d", zoneID, jobID)
return api.deleteLogpushJob(ctx, ZoneRouteRoot, zoneID, jobID)
}

func (api *API) deleteLogpushJob(ctx context.Context, identifierType RouteRoot, identifier string, jobID int) error {
uri := fmt.Sprintf("/%s/%s/logpush/jobs/%d", identifierType, identifier, jobID)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
Expand All @@ -211,11 +363,33 @@ func (api *API) DeleteLogpushJob(ctx context.Context, zoneID string, jobID int)
return nil
}

// GetAccountLogpushOwnershipChallenge returns ownership challenge.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-get-ownership-challenge
func (api *API) GetAccountLogpushOwnershipChallenge(ctx context.Context, accountID, destinationConf string) (*LogpushGetOwnershipChallenge, error) {
return api.getLogpushOwnershipChallenge(ctx, AccountRouteRoot, accountID, destinationConf)
}

// GetZoneLogpushOwnershipChallenge returns ownership challenge.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-get-ownership-challenge
func (api *API) GetZoneLogpushOwnershipChallenge(ctx context.Context, zoneID, destinationConf string) (*LogpushGetOwnershipChallenge, error) {
return api.getLogpushOwnershipChallenge(ctx, ZoneRouteRoot, zoneID, destinationConf)
}

// GetLogpushOwnershipChallenge returns ownership challenge.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-get-ownership-challenge
//
// Deprecated: Use `GetZoneLogpushOwnershipChallenge` or
// `GetAccountLogpushOwnershipChallenge` depending on the
// desired resource to target.
func (api *API) GetLogpushOwnershipChallenge(ctx context.Context, zoneID, destinationConf string) (*LogpushGetOwnershipChallenge, error) {
uri := fmt.Sprintf("/zones/%s/logpush/ownership", zoneID)
return api.getLogpushOwnershipChallenge(ctx, ZoneRouteRoot, zoneID, destinationConf)
}

func (api *API) getLogpushOwnershipChallenge(ctx context.Context, identifierType RouteRoot, identifier, destinationConf string) (*LogpushGetOwnershipChallenge, error) {
uri := fmt.Sprintf("/%s/%s/logpush/ownership", identifierType, identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, LogpushGetOwnershipChallengeRequest{
DestinationConf: destinationConf,
})
Expand All @@ -235,11 +409,33 @@ func (api *API) GetLogpushOwnershipChallenge(ctx context.Context, zoneID, destin
return &r.Result, nil
}

// ValidateLogpushOwnershipChallenge returns ownership challenge validation result.
// ValidateAccountLogpushOwnershipChallenge returns account-level ownership challenge validation result.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-validate-ownership-challenge
func (api *API) ValidateAccountLogpushOwnershipChallenge(ctx context.Context, accountID, destinationConf, ownershipChallenge string) (bool, error) {
return api.validateLogpushOwnershipChallenge(ctx, AccountRouteRoot, accountID, destinationConf, ownershipChallenge)
}

// ValidateZoneLogpushOwnershipChallenge returns zone-level ownership challenge validation result.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-validate-ownership-challenge
func (api *API) ValidateZoneLogpushOwnershipChallenge(ctx context.Context, zoneID, destinationConf, ownershipChallenge string) (bool, error) {
return api.validateLogpushOwnershipChallenge(ctx, ZoneRouteRoot, zoneID, destinationConf, ownershipChallenge)
}

// ValidateLogpushOwnershipChallenge returns zone-level ownership challenge validation result.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-validate-ownership-challenge
//
// Deprecated: Use `ValidateZoneLogpushOwnershipChallenge` or
// `ValidateAccountLogpushOwnershipChallenge` depending on the
// desired resource to target.
func (api *API) ValidateLogpushOwnershipChallenge(ctx context.Context, zoneID, destinationConf, ownershipChallenge string) (bool, error) {
uri := fmt.Sprintf("/zones/%s/logpush/ownership/validate", zoneID)
return api.validateLogpushOwnershipChallenge(ctx, ZoneRouteRoot, zoneID, destinationConf, ownershipChallenge)
}

func (api *API) validateLogpushOwnershipChallenge(ctx context.Context, identifierType RouteRoot, identifier, destinationConf, ownershipChallenge string) (bool, error) {
uri := fmt.Sprintf("/%s/%s/logpush/ownership/validate", identifierType, identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, LogpushValidateOwnershipChallengeRequest{
DestinationConf: destinationConf,
OwnershipChallenge: ownershipChallenge,
Expand All @@ -255,11 +451,33 @@ func (api *API) ValidateLogpushOwnershipChallenge(ctx context.Context, zoneID, d
return r.Result.Valid, nil
}

// CheckLogpushDestinationExists returns destination exists check result.
// CheckAccountLogpushDestinationExists returns account-level destination exists check result.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-check-destination-exists
func (api *API) CheckAccountLogpushDestinationExists(ctx context.Context, accountID, destinationConf string) (bool, error) {
return api.checkLogpushDestinationExists(ctx, AccountRouteRoot, accountID, destinationConf)
}

// CheckZoneLogpushDestinationExists returns zone-level destination exists check result.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-check-destination-exists
func (api *API) CheckZoneLogpushDestinationExists(ctx context.Context, zoneID, destinationConf string) (bool, error) {
return api.checkLogpushDestinationExists(ctx, ZoneRouteRoot, zoneID, destinationConf)
}

// CheckLogpushDestinationExists returns zone-level destination exists check result.
//
// API reference: https://api.cloudflare.com/#logpush-jobs-check-destination-exists
//
// Deprecated: Use `CheckZoneLogpushDestinationExists` or
// `CheckAccountLogpushDestinationExists` depending
// on the desired resource to target.
func (api *API) CheckLogpushDestinationExists(ctx context.Context, zoneID, destinationConf string) (bool, error) {
uri := fmt.Sprintf("/zones/%s/logpush/validate/destination/exists", zoneID)
return api.checkLogpushDestinationExists(ctx, ZoneRouteRoot, zoneID, destinationConf)
}

func (api *API) checkLogpushDestinationExists(ctx context.Context, identifierType RouteRoot, identifier, destinationConf string) (bool, error) {
uri := fmt.Sprintf("/%s/%s/logpush/validate/destination/exists", identifierType, identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, LogpushDestinationExistsRequest{
DestinationConf: destinationConf,
})
Expand Down
Loading

0 comments on commit 8d7b587

Please sign in to comment.