Skip to content

Commit 8c50ef5

Browse files
committed
Merge pull request #12 from engineyard/costs
Add costs to client
2 parents 2e7d702 + 1bdaf11 commit 8c50ef5

File tree

8 files changed

+98
-0
lines changed

8 files changed

+98
-0
lines changed

lib/ey-core/client.rb

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Ey::Core::Client < Cistern::Service
2121
collection :component_actions
2222
collection :components
2323
collection :connectors
24+
collection :costs
2425
collection :database_server_revisions
2526
collection :database_server_snapshots
2627
collection :database_servers
@@ -82,6 +83,7 @@ class Ey::Core::Client < Cistern::Service
8283
model :component
8384
model :component_action
8485
model :connector
86+
model :cost
8587
model :database_server
8688
model :database_server_snapshot
8789
model :database_server_revision
@@ -220,6 +222,7 @@ class Ey::Core::Client < Cistern::Service
220222
request :get_connector
221223
request :get_connectors
222224
request :get_contacts
225+
request :get_costs
223226
request :get_current_user
224227
request :get_database_server
225228
request :get_database_server_revisions
@@ -608,6 +611,7 @@ def self.data
608611
:connectors => {},
609612
:contacts => {},
610613
:contact_assignments => [],
614+
:costs => [],
611615
:database_server_firewalls => [],
612616
:database_server_revisions => {},
613617
:database_server_snapshots => {},

lib/ey-core/collections/costs.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Ey::Core::Client::Costs < Ey::Core::Collection
2+
model Ey::Core::Client::Cost
3+
4+
self.model_root = "cost"
5+
self.model_request = :get_cost
6+
self.collection_root = "costs"
7+
self.collection_request = :get_costs
8+
end

lib/ey-core/models/account.rb

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Ey::Core::Client::Account < Ey::Core::Model
2121
has_many :owners, key: :users
2222
has_many :ssl_certificates
2323
has_many :referrals, key: :account_referrals
24+
has_many :costs
2425

2526
has_one :cancellation, assoc_name: 'account_cancellation', collection: :account_cancellations
2627
has_one :account_trial

lib/ey-core/models/cost.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Ey::Core::Client::Cost < Ey::Core::Model
2+
extend Ey::Core::Associations
3+
4+
attribute :billing_month
5+
attribute :data_type
6+
attribute :level
7+
attribute :finality
8+
attribute :related_resource_type
9+
attribute :category
10+
attribute :units
11+
attribute :description
12+
attribute :value
13+
14+
has_one :account
15+
has_one :environment
16+
end

lib/ey-core/models/environment.rb

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Ey::Core::Client::Environment < Ey::Core::Model
1212
has_one :project
1313

1414
has_many :clusters
15+
has_many :costs
1516
has_many :keypairs
1617
has_many :servers
1718
has_many :applications

lib/ey-core/requests/get_costs.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Ey::Core::Client
2+
class Real
3+
def get_costs(params={})
4+
request(
5+
"url" => params["url"],
6+
"path" => "/accounts/#{params["id"]}/costs"
7+
)
8+
end
9+
end
10+
11+
class Mock
12+
def get_costs(params={})
13+
extract_url_params!(params)
14+
15+
response(
16+
:body => {"costs" => self.data[:costs]},
17+
:status => 200
18+
)
19+
end
20+
end
21+
end

spec/costs_spec.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'spec_helper'
2+
3+
describe 'Costs' do
4+
let(:client) { create_client }
5+
let!(:account) { create_account(client: client) }
6+
7+
it 'should return an empty array with no costs' do
8+
expect(account.costs.all).to be_empty
9+
end
10+
11+
it 'should get costs for an account' do
12+
create_cost(client, account: account)
13+
expect(account.costs.all).not_to be_empty
14+
end
15+
16+
it 'should search costs' do
17+
create_cost(client, account: account, level: 'total')
18+
create_cost(client, account: account, level: 'summarized')
19+
costs = account.costs.select { |c| c.level == 'total' }
20+
expect(costs.first.level).to eq 'total'
21+
end
22+
end

spec/support/resource_helper.rb

+25
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,31 @@ def create_server(client, options={})
4040
)
4141
end
4242

43+
def create_cost(client, options={})
44+
account = options[:account] || create_account(client: client)
45+
level = options[:level] || "summarized"
46+
finality = options[:finality] || "estimated"
47+
related_resource_type = options[:related_resource_type] || "account"
48+
category = options[:category] || "non-server"
49+
description = options[:description] || "AWS Other Services"
50+
value = options[:value] || "1763"
51+
environment = options[:environment] || nil
52+
53+
cost = client.data[:costs] << {
54+
billing_month: "2015-07",
55+
data_type: "cost",
56+
level: level,
57+
finality: finality,
58+
related_resource_type: related_resource_type,
59+
category: category,
60+
units: "USD cents",
61+
description: description,
62+
value: value,
63+
account: client.url_for("accounts/#{account.identity}"),
64+
environment: environment
65+
}
66+
end
67+
4368
def create_account_referral(client, options={})
4469
referred = options.delete(:referred) || create_account(client: client)
4570
referrer = options.delete(:referrer) || create_account(client: client)

0 commit comments

Comments
 (0)