class CloudAccountObserver < ActiveRecord::Observer
  def after_create(account)
    if key = account.generate_auth_key
      account.update_attribute(:instance_key, InstanceKey.create!(:pem => key.pem, :name => key.id, :instance_key_owner => account))
    end
  end
end

CloudAccountObserver.instance

class CloudAccountObserver < ActiveRecord::Observer
  def after_create(account)
    # FIXME: new boxgrinder doesn't create bucket for amis automatically,
    # for now we create bucket from conductor
    # remove this hotfix when fixed on boxgrinder side
    if account.provider.cloud_type == 'ec2'
      create_bucket(account)
    end
    if key = account.generate_auth_key
      account.update_attribute(:instance_key, InstanceKey.create!(:pem => key.pem, :name => key.id, :instance_key_owner => account))
    end
  end

  private

  def create_bucket(account)
    client = account.connect
    bucket_name = "#{account.account_number}-imagefactory-amis"
    # TODO (jprovazn): getting particular bucket takes long time (core fetches all
    # buckets from provider), so we call directly create_bucket, if bucket exists,
    # exception should be thrown (actually existing bucket is returned - this
    # bug should be fixed soon)
    #client.create_bucket(:name => bucket_name) unless client.bucket(bucket_name)
    begin
      client.create_bucket(:name => bucket_name)
    rescue Exception => e
      Rails.logger.error e.message
      Rails.logger.error e.backtrace.join("\n  ")
    end
  end
end

CloudAccountObserver.instance

Create bucket for images on cloud provider side
Create bucket for images on cloud provider side

The newer boxgrinder uses euca2ools instead of ec2-ami-tools to build and upload
ec2 images. Ec2-ami-tools used to automatically create a amazon s3 bucket where
images are uploaded, euca2ools doesn't. In future boxgrinder should take care of
creation bucket but this will take time, so hotfix is to create bucket from
conductor when cloudaccount is created.
