Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

CloudController Repository Pattern

View aggregates_current_organization.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
module Aggregate
class CurrentOrganization
attr_reader :organization, :spaces, :account
 
def initialize(attrs)
@organization = attrs.fetch(:organization)
@spaces = attrs.fetch(:spaces)
@account = attrs.fetch(:account)
end
 
def to_param
organization.guid
end
 
def guid
organization.guid
end
 
def name
organization.name
end
 
def credit_card_on_file?
!!account.billing_info
end
end
end
View aggregates_current_organization.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
class CloudControllerClient
class Error < StandardError; end
class NotFound < StandardError; end
class Forbidden < StandardError; end
class BadRequest < Error
def self.from_body(body)
error_body = JSON.parse(body)
new(error_body['code'], error_body['description'], error_body['error_code'], error_body['types'], error_body['backtrace'])
end
 
def initialize(code, description, error_code, error_types, cloud_controller_backtrace)
@code, @description, @error_code, @error_types, @cloud_controller_backtrace = code, description, error_code, error_types, cloud_controller_backtrace
end
 
attr_reader :code, :description, :error_code, :error_types, :cloud_controller_backtrace
 
def message
"#{error_code}: #{description}"
end
end
 
def initialize(cloud_controller_url, user_auth_header, logger = Rails.logger, debug = false)
@user_auth_header = user_auth_header
@cloud_controller_url = cloud_controller_url
@logger = logger
@debug = debug
end
 
def post(path, params)
response = Typhoeus::Request.post(
@cloud_controller_url + path,
body: params.to_json,
ssl_verifypeer: Environment::Network.verify_ssl?,
headers: {"Authorization" => @user_auth_header},
verbose: @debug)
parse_response(response)
end
 
def put(path, params)
response = Typhoeus::Request.put(
@cloud_controller_url + path,
body: params.to_json,
ssl_verifypeer: Environment::Network.verify_ssl?,
headers: {"Authorization" => @user_auth_header},
verbose: @debug)
parse_response(response)
end
 
def get(path, params = {})
response = Typhoeus::Request.get(
@cloud_controller_url + path,
params: params,
ssl_verifypeer: Environment::Network.verify_ssl?,
headers: {"Authorization" => @user_auth_header},
verbose: @debug)
parse_response(response)
end
 
def all(path)
response = get(path)
resource_data = response['resources']
while response['next_url'].present?
response = get(response['next_url'])
resource_data += response['resources']
end
resource_data
end
 
def delete(path, params = {})
response = Typhoeus::Request.delete(
@cloud_controller_url + path,
ssl_verifypeer: Environment::Network.verify_ssl?,
params: params,
headers: {"Authorization" => @user_auth_header},
verbose: @debug)
parse_response(response)
end
 
def enable_debug!
@debug = true
end
 
def debug(&block)
current_debug_setting = @debug
@debug = true
yield
@debug = current_debug_setting
end
 
private
 
def parse_response(response)
handle_error(response) unless response.success?
body = JSON.parse(response.body) unless response.status_message == 'No Content'
 
if @debug
@logger.info(body)
end
body
end
 
def handle_error(response)
case response.code
when 404
raise NotFound.new(response.body)
when 400
raise BadRequest.from_body(response.body)
when 403
raise Forbidden.new(response.body)
else
raise Error.new("code: #{response.code}; body: #{response.body}")
end
end
end
View aggregates_current_organization.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
module Repositories
class CurrentOrganization
def initialize(cloud_controller_client)
@cloud_controller_client = cloud_controller_client
end
 
def find_by_guid(guid)
organization_repo = Experimental::Repositories::Organizations.new(@cloud_controller_client)
space_repo = Experimental::Repositories::Spaces.new(@cloud_controller_client)
organization = organization_repo.find_by_guid(guid)
spaces = space_repo.spaces_for_organization(guid)
account = extract_account(Billing::Account.find_or_create_by(organization_id: guid))
Experimental::Aggregate::CurrentOrganization.new(organization: organization, spaces: spaces, account: account)
end
 
private
 
def extract_account(account)
Experimental::Entity::Account.new({
grace_period: account.grace_period?,
trial_period: account.trial_period?,
trial_suspension_period: account.trial_suspension_period?,
state_expiration_date: account.state_expiration_date,
instantiated_state: account.instantiated_state?,
billing_info: account.billing_info,
trial_expired_grace_period: account.trial_expired_grace_period?
})
end
end
end

Hi @ragaskar, did you continue with this work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.