Skip to content

Commit

Permalink
Merge pull request #1212 from travis-ci/pricing_adjustments_master
Browse files Browse the repository at this point in the history
Pricing adjustments master
  • Loading branch information
travis-architect authored Dec 13, 2021
2 parents dc7fba9 + 5d2edf8 commit 99fd627
Show file tree
Hide file tree
Showing 36 changed files with 577 additions and 17 deletions.
23 changes: 23 additions & 0 deletions lib/travis/api/v3/billing_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ def executions(owner_type, owner_id, page, per_page, from, to)
executions
end

def calculate_credits(users, executions)
response = connection.post("/usage/credits_calculator", users: users, executions: executions)
response.body.map do |calculator_data|
Travis::API::V3::Models::CreditsResult.new(calculator_data)
end
end

def credits_calculator_default_config
response = connection.get('/usage/credits_calculator/default_config')

Travis::API::V3::Models::CreditsCalculatorConfig.new(response.body)
end

def all
data = connection.get('/subscriptions').body
subscriptions = data.fetch('subscriptions').map do |subscription_data|
Expand Down Expand Up @@ -210,11 +223,21 @@ def create_auto_refill(plan_id, is_enabled)
handle_errors_and_respond(response)
end

def update_auto_refill(addon_id, threshold, amount)
response = connection.patch('/auto_refill', {id: addon_id, threshold: threshold, amount: amount})
handle_errors_and_respond(response)
end

def get_auto_refill(plan_id)
response = connection.get("/auto_refill?plan_id=#{plan_id}")
handle_errors_and_respond(response) { |r| Travis::API::V3::Models::AutoRefill.new(r) }
end

def cancel_v2_subscription(id, reason_data)
response = connection.post("/v2/subscriptions/#{id}/cancel", reason_data)
handle_subscription_response(response)
end

private

def handle_subscription_response(response)
Expand Down
3 changes: 2 additions & 1 deletion lib/travis/api/v3/models/auto_refill.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module Travis::API::V3
class Models::AutoRefill

attr_reader :enabled, :threshold, :amount
attr_reader :addon_id, :enabled, :threshold, :amount
def initialize(attributes = {})
@addon_id = attributes.fetch('addon_id', nil) || attributes.fetch('id', nil)
@enabled = attributes.key?('enabled') ? attributes.fetch('enabled') : true
@threshold = attributes.key?('refill_threshold') ? attributes.fetch('refill_threshold') : 25000
@amount = attributes.key?('refill_amount') ? attributes.fetch('refill_amount'): 10000
Expand Down
11 changes: 11 additions & 0 deletions lib/travis/api/v3/models/credits_calculator_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Travis::API::V3
class Models::CreditsCalculatorConfig
ATTRS = %w[users minutes os instance_size]

attr_accessor *ATTRS

def initialize(attrs)
ATTRS.each { |key| send("#{key}=", attrs[key]) }
end
end
end
11 changes: 11 additions & 0 deletions lib/travis/api/v3/models/credits_result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Travis::API::V3
class Models::CreditsResult
ATTRS = %w[users minutes os instance_size credits price]

attr_accessor *ATTRS

def initialize(attrs)
ATTRS.each { |key| send("#{key}=", attrs[key]) }
end
end
end
5 changes: 3 additions & 2 deletions lib/travis/api/v3/models/executions.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Travis::API::V3
class Models::Execution
attr_reader :id, :os, :instance_size, :arch, :virtualization_type, :queue, :job_id, :repository_id, :owner_id,
:owner_type, :plan_id, :sender_id, :credits_consumed, :started_at, :finished_at, :created_at,
:updated_at
:owner_type, :plan_id, :sender_id, :credits_consumed, :user_license_credits_consumed, :started_at,
:finished_at, :created_at, :updated_at

def initialize(attributes = {})
@id = attributes.fetch('id')
Expand All @@ -18,6 +18,7 @@ def initialize(attributes = {})
@plan_id = attributes.fetch('plan_id')
@sender_id = attributes.fetch('sender_id')
@credits_consumed = attributes.fetch('credits_consumed')
@user_license_credits_consumed = attributes.fetch('user_license_credits_consumed')
@started_at = attributes.fetch('started_at')
@finished_at = attributes.fetch('finished_at')
@created_at = attributes.fetch('created_at')
Expand Down
3 changes: 2 additions & 1 deletion lib/travis/api/v3/models/v2_addon.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module Travis::API::V3
class Models::V2Addon
attr_reader :id, :name, :type, :current_usage
attr_reader :id, :name, :type, :current_usage, :recurring

def initialize(attrs)
@id = attrs.fetch('id')
@name = attrs.fetch('name')
@type = attrs.fetch('type')
@current_usage = attrs['current_usage'] && Models::V2AddonUsage.new(attrs['current_usage'])
@recurring = attrs['recurring']
end
end
end
6 changes: 5 additions & 1 deletion lib/travis/api/v3/models/v2_plan_config.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Travis::API::V3
class Models::V2PlanConfig
attr_reader :id, :name, :private_repos, :starting_price, :starting_users, :plan_type,
:private_credits, :public_credits, :addon_configs, :concurrency_limit, :available_standalone_addons, :auto_refill_enabled, :trial_plan
:private_credits, :public_credits, :addon_configs, :concurrency_limit, :available_standalone_addons, :auto_refill_enabled, :trial_plan,
:annual, :auto_refill_thresholds, :auto_refill_amounts

def initialize(attrs)
@id = attrs.fetch('id')
Expand All @@ -15,6 +16,9 @@ def initialize(attrs)
@plan_type = attrs.fetch('plan_type')
@concurrency_limit = attrs.fetch('concurrency_limit')
@available_standalone_addons = attrs.fetch('available_standalone_addons')
@annual = attrs.fetch('annual')
@auto_refill_thresholds = attrs.fetch('auto_refill_thresholds')
@auto_refill_amounts = attrs.fetch('auto_refill_amounts')
@trial_plan = attrs.fetch('trial_plan', false)
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/travis/api/v3/models/v2_subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Models::V2Subscription
include Models::Owner

attr_reader :id, :plan, :permissions, :source, :billing_info, :credit_card_info, :owner, :status, :valid_to, :canceled_at,
:client_secret, :payment_intent, :addons, :auto_refill, :available_standalone_addons, :created_at
:client_secret, :payment_intent, :addons, :auto_refill, :available_standalone_addons, :created_at, :scheduled_plan_name

def initialize(attributes = {})
@id = attributes.fetch('id')
Expand All @@ -30,6 +30,7 @@ def initialize(attributes = {})
@status = attributes.fetch('status')
@valid_to = attributes.fetch('valid_to')
@canceled_at = attributes.fetch('canceled_at')
@scheduled_plan_name = attributes.fetch('scheduled_plan')
end
end

Expand Down
15 changes: 15 additions & 0 deletions lib/travis/api/v3/queries/credits_calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Travis::API::V3
class Queries::CreditsCalculator < Query
params :users, :executions

def calculate(user_id)
client = BillingClient.new(user_id)
client.calculate_credits(params['users'], params['executions'])
end

def default_config(user_id)
client = BillingClient.new(user_id)
client.credits_calculator_default_config
end
end
end
13 changes: 12 additions & 1 deletion lib/travis/api/v3/queries/v2_subscription.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Travis::API::V3
class Queries::V2Subscription < Query
params :enabled
params :enabled, :threshold, :amount

def update_address(user_id)
address_data = params.dup.tap { |h| h.delete('subscription.id') }
Expand Down Expand Up @@ -49,5 +49,16 @@ def toggle_auto_refill(user_id, plan_id)
client = BillingClient.new(user_id)
client.create_auto_refill(plan_id, enabled)
end

def cancel(user_id)
reason_data = params.dup.tap { |h| h.delete('subscription.id') }
client = BillingClient.new(user_id)
client.cancel_v2_subscription(params['subscription.id'], reason_data)
end

def update_auto_refill(user_id, addon_id)
client = BillingClient.new(user_id)
client.update_auto_refill(addon_id, threshold, amount)
end
end
end
4 changes: 2 additions & 2 deletions lib/travis/api/v3/renderer/auto_refill.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Travis::API::V3
class Renderer::AutoRefill < ModelRenderer
representation(:standard, :enabled, :threshold, :amount)
representation(:minimal, :enabled, :threshold, :amount)
representation(:standard, :addon_id, :enabled, :threshold, :amount)
representation(:minimal, :addon_id, :enabled, :threshold, :amount)
end
end
5 changes: 5 additions & 0 deletions lib/travis/api/v3/renderer/credits_calculator_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Travis::API::V3
class Renderer::CreditsCalculatorConfig < ModelRenderer
representation(:standard, :users, :minutes, :os, :instance_size)
end
end
5 changes: 5 additions & 0 deletions lib/travis/api/v3/renderer/credits_result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Travis::API::V3
class Renderer::CreditsResult < ModelRenderer
representation(:standard, :users, :minutes, :os, :instance_size, :credits, :price)
end
end
6 changes: 6 additions & 0 deletions lib/travis/api/v3/renderer/credits_results.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Travis::API::V3
class Renderer::CreditsResults < CollectionRenderer
type :credits_results
collection_key :credits_results
end
end
4 changes: 2 additions & 2 deletions lib/travis/api/v3/renderer/execution.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Travis::API::V3
class Renderer::Execution < ModelRenderer
representation :minimal, :id, :os, :instance_size, :arch, :virtualization_type, :queue, :job_id,
:repository_id, :owner_id, :owner_type, :plan_id, :sender_id, :credits_consumed, :started_at,
:finished_at, :created_at, :updated_at
:repository_id, :owner_id, :owner_type, :plan_id, :sender_id, :credits_consumed,
:user_license_credits_consumed, :started_at, :finished_at, :created_at, :updated_at
representation :standard, *representations[:minimal]
end
end
4 changes: 2 additions & 2 deletions lib/travis/api/v3/renderer/v2_addon.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Travis::API::V3
class Renderer::V2Addon < ModelRenderer
representation(:standard, :id, :name, :type, :current_usage)
representation(:minimal, :id, :name, :type, :current_usage)
representation(:standard, :id, :name, :type, :current_usage, :recurring)
representation(:minimal, :id, :name, :type, :current_usage, :recurring)

def current_usage
Renderer.render_model(model.current_usage, mode: :standard) unless model.current_usage.nil?
Expand Down
4 changes: 2 additions & 2 deletions lib/travis/api/v3/renderer/v2_plan_config.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Travis::API::V3
class Renderer::V2PlanConfig < ModelRenderer
representation(:standard, :id, :name, :private_repos, :starting_price, :starting_users, :private_credits,
:public_credits, :addon_configs, :plan_type, :concurrency_limit, :available_standalone_addons, :trial_plan)
:public_credits, :addon_configs, :plan_type, :concurrency_limit, :available_standalone_addons, :trial_plan, :annual, :auto_refill_thresholds, :auto_refill_amounts)
representation(:minimal, :id, :name, :private_repos, :starting_price, :starting_users, :private_credits,
:public_credits, :addon_configs, :plan_type, :concurrency_limit, :trial_plan)
:public_credits, :addon_configs, :plan_type, :concurrency_limit, :trial_plan, :annual, :auto_refill_thresholds, :auto_refill_amounts)
end
end
2 changes: 1 addition & 1 deletion lib/travis/api/v3/renderer/v2_subscription.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Travis::API::V3
class Renderer::V2Subscription < ModelRenderer
representation(:standard, :id, :plan, :addons, :auto_refill, :status, :valid_to, :canceled_at, :source, :owner, :client_secret, :billing_info, :credit_card_info, :payment_intent, :created_at)
representation(:standard, :id, :plan, :addons, :auto_refill, :status, :valid_to, :canceled_at, :source, :owner, :client_secret, :billing_info, :credit_card_info, :payment_intent, :created_at, :scheduled_plan_name)

def billing_info
Renderer.render_model(model.billing_info, mode: :standard) unless model.billing_info.nil?
Expand Down
8 changes: 8 additions & 0 deletions lib/travis/api/v3/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ module Routes
end
end

resource :credits_calculator do
route '/credits_calculator'
post :calculator
get :default_config
end

resource :repositories do
route '/repos'
get :for_current_user
Expand Down Expand Up @@ -364,11 +370,13 @@ module Routes
patch :changetofree, '/changetofree'
patch :update_plan, '/plan'
post :pay, '/pay'
post :cancel, '/cancel'
post :buy_addon, '/addon/{addon.id}'
get :user_usages, '/user_usages'
get :invoices, '/invoices'
get :auto_refill, '/auto_refill'
patch :toggle_auto_refill, '/auto_refill'
patch :update_auto_refill, '/update_auto_refill'
end

hidden_resource :trials do
Expand Down
1 change: 1 addition & 0 deletions lib/travis/api/v3/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Services
BuildPermissions = Module.new { extend Services }
Caches = Module.new { extend Services }
Coupons = Module.new { extend Services }
CreditsCalculator = Module.new { extend Services }
Cron = Module.new { extend Services }
Crons = Module.new { extend Services }
EmailSubscription = Module.new { extend Services }
Expand Down
12 changes: 12 additions & 0 deletions lib/travis/api/v3/services/credits_calculator/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Travis::API::V3
class Services::CreditsCalculator::Calculator < Service
result_type :credits_results
params :users, :executions

def run!
raise LoginRequired unless access_control.logged_in?

result query(:credits_calculator).calculate(access_control.user.id)
end
end
end
11 changes: 11 additions & 0 deletions lib/travis/api/v3/services/credits_calculator/default_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Travis::API::V3
class Services::CreditsCalculator::DefaultConfig < Service
result_type :credits_calculator_config

def run!
raise LoginRequired unless access_control.logged_in?

result query(:credits_calculator).default_config(access_control.user.id)
end
end
end
11 changes: 11 additions & 0 deletions lib/travis/api/v3/services/v2_subscription/cancel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Travis::API::V3
class Services::V2Subscription::Cancel < Service
params :reason, :reason_details

def run!
raise LoginRequired unless access_control.full_access_or_logged_in?
query.cancel(access_control.user.id)
no_content
end
end
end
10 changes: 10 additions & 0 deletions lib/travis/api/v3/services/v2_subscription/update_auto_refill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Travis::API::V3
class Services::V2Subscription::UpdateAutoRefill < Service
params :addon_id, :threshold, :amount
def run!
raise LoginRequired unless access_control.full_access_or_logged_in?
query.update_auto_refill(access_control.user.id, params['addon_id'])
no_content
end
end
end
2 changes: 1 addition & 1 deletion lib/travis/api/v3/services/v2_subscription/update_plan.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Travis::API::V3
class Services::V2Subscription::UpdatePlan < Service
params :plan
params :plan, :coupon

def run!
raise LoginRequired unless access_control.full_access_or_logged_in?
Expand Down
Loading

0 comments on commit 99fd627

Please sign in to comment.