Sha256: 0af25ee9a7b5455c0ca7974d1df606ab0204fe378ecfe3ddd53d63b244248921

Contents?: true

Size: 1.52 KB

Versions: 5

Compression:

Stored size: 1.52 KB

Contents

require 'bcrypt'

module Jellyfish
  module Fog
    module AWS
      class Databases < Jellyfish::Provisioner
        def provision
          @password = SecureRandom.hex(5)
          db_instance_id = "id-#{@order_item.uuid[0..9]}"
          handle_errors do
            db = connection.create_db_instance(db_instance_id, details)
            @order_item.payload_response = db.to_json
            @order_item.provision_status = 'ok'
          end
        end

        def retire
          connection.delete_db_instance(identifier, snapshot, false)
          @order_item.provision_status = 'retired'
        rescue Excon::Errors::BadRequest, Excon::Errors::Forbidden => e
          raise e, 'Bad request. Check for valid credentials and proper permissions.', e.backtrace
        end

        private

        def details
          @order_item.answers.merge(
            'MasterUserPassword' => @password,
            'MasterUsername' => 'admin'
          )
        end

        def connection
          ::Fog::AWS::RDS.new(Jellyfish::Fog::AWS.aws_settings)
        end

        def identifier
          @order_item.payload_response['data']['body']['CreateDBInstanceResult']['DBInstance']['DBInstanceIdentifier']
        end

        def snapshot
          "snapshot-#{@order_item.uuid[0..5]}"
        end

        def handle_errors
          yield
        rescue Excon::Errors::BadRequest, Excon::Errors::Forbidden => e
          raise e, 'Bad request. Check for valid credentials and proper permissions.', e.backtrace
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jellyfish_fog_aws-0.3.0 lib/jellyfish_fog_aws/databases.rb
jellyfish_fog_aws-0.2.2 lib/jellyfish_fog_aws/databases.rb
jellyfish_fog_aws-0.2.1 lib/jellyfish_fog_aws/databases.rb
jellyfish_fog_aws-0.2.0 lib/jellyfish_fog_aws/databases.rb
jellyfish_fog_aws-0.1.1 lib/jellyfish_fog_aws/databases.rb