Last active
August 29, 2015 13:57
-
-
Save ragaskar/9744867 to your computer and use it in GitHub Desktop.
CloudController Repository Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @ragaskar, did you continue with this work?