Skip to content

Commit

Permalink
Add CreatedBefore and CreatedAfter filters for admin runs list
Browse files Browse the repository at this point in the history
  • Loading branch information
Maed223 committed Jun 18, 2024
1 parent d981486 commit e443528
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions admin_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ const (
type AdminRunsListOptions struct {
ListOptions

RunStatus string `url:"filter[status],omitempty"`
Query string `url:"q,omitempty"`
RunStatus string `url:"filter[status],omitempty"`
CreatedBefore string `url:"filter[to],omitempty"`
CreatedAfter string `url:"filter[from],omitempty"`
Query string `url:"q,omitempty"`
// Optional: A list of relations to include. See available resources
// https://developer.hashicorp.com/terraform/enterprise/api-docs/admin/runs#available-related-resources
Include []AdminRunIncludeOpt `url:"include,omitempty"`
Expand Down Expand Up @@ -123,13 +125,35 @@ func (o *AdminRunsListOptions) valid() error {
return nil
}

if err := validateAdminRunDateRanges(o.CreatedBefore, o.CreatedAfter); err != nil {
return err
}

if err := validateAdminRunFilterParams(o.RunStatus); err != nil {
return err
}

return nil
}

func validateAdminRunDateRanges(before, after string) error {
if validString(&before) {
_, err := time.Parse(time.RFC3339, before)
if err != nil {
return fmt.Errorf("invalid date format for CreatedBefore: '%s', must be in RFC3339 format", before)
}
}

if validString(&after) {
_, err := time.Parse(time.RFC3339, after)
if err != nil {
return fmt.Errorf("invalid date format for CreatedAfter: '%s', must be in RFC3339 format", after)
}
}

return nil
}

func validateAdminRunFilterParams(runStatus string) error {
// For the platform, an invalid filter value is a semantically understood query that returns an empty set, no error, no warning. But for go-tfe, an invalid value is good enough reason to error prior to a network call to the platform:
if validString(&runStatus) {
Expand Down

0 comments on commit e443528

Please sign in to comment.