Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add costs to client #12

Merged
merged 6 commits into from
Jul 20, 2015
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/ey-core/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Ey::Core::Client < Cistern::Service
collection :component_actions
collection :components
collection :connectors
collection :costs
collection :database_server_revisions
collection :database_server_snapshots
collection :database_servers
Expand Down Expand Up @@ -82,6 +83,7 @@ class Ey::Core::Client < Cistern::Service
model :component
model :component_action
model :connector
model :cost
model :database_server
model :database_server_snapshot
model :database_server_revision
Expand Down Expand Up @@ -220,6 +222,7 @@ class Ey::Core::Client < Cistern::Service
request :get_connector
request :get_connectors
request :get_contacts
request :get_costs
request :get_current_user
request :get_database_server
request :get_database_server_revisions
Expand Down Expand Up @@ -608,6 +611,7 @@ def self.data
:connectors => {},
:contacts => {},
:contact_assignments => [],
:costs => [],
:database_server_firewalls => [],
:database_server_revisions => {},
:database_server_snapshots => {},
Expand Down
8 changes: 8 additions & 0 deletions lib/ey-core/collections/costs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Ey::Core::Client::Costs < Ey::Core::Collection
model Ey::Core::Client::Cost

self.model_root = "cost"
self.model_request = :get_cost
self.collection_root = "costs"
self.collection_request = :get_costs
end
1 change: 1 addition & 0 deletions lib/ey-core/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Ey::Core::Client::Account < Ey::Core::Model
has_many :owners, key: :users
has_many :ssl_certificates
has_many :referrals, key: :account_referrals
has_many :costs

has_one :cancellation, assoc_name: 'account_cancellation', collection: :account_cancellations
has_one :account_trial
Expand Down
16 changes: 16 additions & 0 deletions lib/ey-core/models/cost.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Ey::Core::Client::Cost < Ey::Core::Model
extend Ey::Core::Associations

attribute :billing_month
attribute :data_type
attribute :level
attribute :finality
attribute :related_resource_type
attribute :category
attribute :units
attribute :description
attribute :value

has_one :account
has_one :environment
end
1 change: 1 addition & 0 deletions lib/ey-core/models/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Ey::Core::Client::Environment < Ey::Core::Model
has_one :project

has_many :clusters
has_many :costs
has_many :keypairs
has_many :servers
has_many :applications
Expand Down
21 changes: 21 additions & 0 deletions lib/ey-core/requests/get_costs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Ey::Core::Client
class Real
def get_costs(params={})
request(
"url" => params["url"],
"path" => "/accounts/#{params[:id]}/costs"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

params["id"]

)
end
end

class Mock
def get_costs(params={})
extract_url_params!(params)

response(
:body => {"costs" => self.data[:costs]},
:status => 200
)
end
end
end
22 changes: 22 additions & 0 deletions spec/costs_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'spec_helper'

describe 'Costs' do
let(:client) { create_client }
let!(:account) { create_account(client: client) }

it 'should return an empty array with no costs' do
expect(account.costs.all).to be_empty
end

it 'should get costs for an account' do
create_cost(client, account: account)
expect(account.costs.all).not_to be_empty
end

it 'should search costs' do
create_cost(client, account: account, level: 'total')
create_cost(client, account: account, level: 'summarized')
costs = account.costs.select { |c| c.level == 'total' }
expect(costs.first.level).to eq 'total'
end
end
25 changes: 25 additions & 0 deletions spec/support/resource_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ def create_server(client, options={})
)
end

def create_cost(client, options={})
account = options[:account] || create_account(client: client)
level = options[:level] || "summarized"
finality = options[:finality] || "estimated"
related_resource_type = options[:related_resource_type] || "account"
category = options[:category] || "non-server"
description = options[:description] || "AWS Other Services"
value = options[:value] || "1763"
environment = options[:environment] || nil

cost = client.data[:costs] << {
billing_month: "2015-07",
data_type: "cost",
level: level,
finality: finality,
related_resource_type: related_resource_type,
category: category,
units: "USD cents",
description: description,
value: value,
account: client.url_for("accounts/#{account.identity}"),
environment: environment
}
end

def create_account_referral(client, options={})
referred = options.delete(:referred) || create_account(client: client)
referrer = options.delete(:referrer) || create_account(client: client)
Expand Down