Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit 467611b

Browse files
committed
v3: Disallow ordering by updated_at for usage events
For app and service usage events, the underlying tables only keep track of an `updated_at` timestamp. In the presented JSON, we do show an `updated_at` field, but it's always derived from the `created_at` value (since these events can't be updated). When filtering lists of these events, our normal ordering queries won't work for `order_by=updated_at`, since that column doesn't exist. Rather than try to find a clever way to support this, we are just disallowing ordering by `updated_at`. Ordering by `created_at` should be exactly equivalent for these resources and is still supported. [finishes #173185328] [finishes #173185583] Authored-by: Reid Mitchell <rmitchell@pivotal.io>
1 parent 47e95ea commit 467611b

6 files changed

Lines changed: 50 additions & 2 deletions

File tree

app/messages/app_usage_events_list_message.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class AppUsageEventsListMessage < ListMessage
1616

1717
validates :guids, array: true, allow_nil: true
1818

19+
def valid_order_by_values
20+
[:created_at]
21+
end
22+
1923
def self.from_params(params)
2024
super(params, %w(after_guid guids))
2125
end

app/messages/service_usage_events_list_message.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class ServiceUsageEventsListMessage < ListMessage
2020
validates :service_instance_types, array: true, allow_nil: true
2121
validates :service_offering_guids, array: true, allow_nil: true
2222

23+
def valid_order_by_values
24+
[:created_at]
25+
end
26+
2327
def self.from_params(params)
2428
super(params, %w(after_guid guids service_instance_types service_offering_guids))
2529
end

docs/v3/source/includes/experimental_resources/app_usage_events/_list.md.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Name | Type | Description
3333
---- | ---- | ------------
3434
**page** | _integer_ | Page to display. Valid values are integers >= 1.
3535
**per_page** | _integer_ | Number of results per page. <br>Valid values are 1 through 5000.
36-
**order_by** | _string_ | Value to sort by. Defaults to ascending. Prepend with `-` to sort descending. <br>Valid values are `created_at`, and `updated_at`.
36+
**order_by** | _string_ | Value to sort by. Defaults to ascending. Prepend with `-` to sort descending. <br>Valid value is `created_at`.
3737
**after_guid** | _string_ | Filters out events before and including the event with the given guid.
3838
**guids** | _list of strings_ | Comma-delimited list of usage event guids to filter by.
3939

docs/v3/source/includes/experimental_resources/service_usage_events/_list.md.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Name | Type | Description
3333
---- | ---- | ------------
3434
**page** | _integer_ | Page to display. Valid values are integers >= 1.
3535
**per_page** | _integer_ | Number of results per page. <br>Valid values are 1 through 5000.
36-
**order_by** | _string_ | Value to sort by. Defaults to ascending. Prepend with `-` to sort descending. <br>Valid values are `created_at`, and `updated_at`.
36+
**order_by** | _string_ | Value to sort by. Defaults to ascending. Prepend with `-` to sort descending. <br>Valid value is `created_at`.
3737
**after_guid** | _string_ | Filters out events before and including the event with the given guid.
3838
**guids** | _list of strings_ | Comma-delimited list of usage event guids to filter by.
3939
**service_instance_types** | _list of strings_ | Comma-delimited list of service instance types to filter by. Valid values are `managed_service_instance` and `user_provided_service_instance`.

spec/unit/messages/app_usage_events_list_message_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@ module VCAP::CloudController
7777
expect(subject.guids).to eq(['some-guid'])
7878
end
7979
end
80+
81+
context 'when the order_by filter is provided' do
82+
context 'and the value is invalid' do
83+
let(:params) { { order_by: 'updated_at' } }
84+
85+
it 'validates and returns an error' do
86+
expect(subject).not_to be_valid
87+
expect(subject.errors[:order_by]).to include("can only be: 'created_at'")
88+
end
89+
end
90+
91+
context 'and the value is valid' do
92+
let(:params) { { order_by: 'created_at' } }
93+
94+
it 'sets the message order_by to the provided value' do
95+
expect(subject).to be_valid
96+
expect(subject.order_by).to eq('created_at')
97+
end
98+
end
99+
end
80100
end
81101
end
82102
end

spec/unit/messages/service_usage_events_list_message_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,26 @@ module VCAP::CloudController
114114
expect(subject.service_instance_types).to eq(['managed_service_instance'])
115115
end
116116
end
117+
118+
context 'when the order_by filter is provided' do
119+
context 'and the value is invalid' do
120+
let(:params) { { order_by: 'updated_at' } }
121+
122+
it 'validates and returns an error' do
123+
expect(subject).not_to be_valid
124+
expect(subject.errors[:order_by]).to include("can only be: 'created_at'")
125+
end
126+
end
127+
128+
context 'and the value is valid' do
129+
let(:params) { { order_by: 'created_at' } }
130+
131+
it 'sets the message order_by to the provided value' do
132+
expect(subject).to be_valid
133+
expect(subject.order_by).to eq('created_at')
134+
end
135+
end
136+
end
117137
end
118138
end
119139
end

0 commit comments

Comments
 (0)