Skip to content

Instantly share code, notes, and snippets.

@pyrabbit
Created September 25, 2019 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyrabbit/54de2f016715cff3f98ec2dd40c20419 to your computer and use it in GitHub Desktop.
Save pyrabbit/54de2f016715cff3f98ec2dd40c20419 to your computer and use it in GitHub Desktop.
API Gateway Rails Controller Implementation
module Enterprises
class AccessKeysController < DashboardsController
def create
gateway = Aws::APIGateway::Client.new
begin
api_key = create_api_key!(gateway, current_enterprise, params[:stage])
current_enterprise.update_stage_key!(api_key: api_key, stage: params[:stage])
redirect_to enterprises_access_keys_path
rescue Aws::APIGateway::Errors::ServiceError => e
redirect_to enterprises_access_keys_path, alert: e.message
end
end
def update
gateway = Aws::APIGateway::Client.new
begin
api_key_id = current_enterprise.stage_key_id(params[:stage])
gateway.delete_api_key(api_key: api_key_id)
api_key = create_api_key!(gateway, current_enterprise, params[:stage])
current_enterprise.update_stage_key!(api_key: api_key, stage: params[:stage])
redirect_to enterprises_access_keys_path
rescue Aws::APIGateway::Errors::ServiceError => e
redirect_to enterprises_access_keys_path, alert: e.message
end
end
private
def create_api_key!(gateway, enterprise, stage)
api_key = gateway.create_api_key(
value: SecureRandom.base58(40),
name: "#{enterprise.name} [#{stage}]"
)
gateway.create_usage_plan_key(
usage_plan_id: enterprise.stage_plan_id(stage),
key_type: 'API_KEY',
key_id: api_key.id
)
api_key
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment