Sha256: 8ca7ee5364db4e6386b95c6da120ed9040705e0825e92ebf572ca885caa9879a

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module OpenstackBridge
  class Swift < Struct.new(:host, :user, :password, :tenant, :container)
    attr_accessor :authentication

    def initialize(*)
      super
      self.authentication = OpenstackBridge::Authentication.new(host, user, password, tenant)
    end

    def end_point
      self.authentication.response['access']['serviceCatalog'].detect {|s| s['name'] == 'swift'}['endpoints'].first['publicURL']
    end

    def containers
      request(:get, "#{end_point}").raw_body.split("\n")
    end

    def create(name)
      request(:put, "#{end_point}/#{name}")
    end

    def delete(name)
      request(:delete, "#{end_point}/#{name}")
    end

    def container(name)
      OpenstackBridge::Container.new(self, name)
    end

    def request(method, path, params={}.to_json, httpclient = false)
      request = HTTPI::Request.new
      request.url = path
      request.body = params
      request.headers['Content-Type'] = 'application/json'
      request.headers['X-Auth-Token'] = authentication.token
      response = HTTPI.send(method, request, (httpclient ? :httpclient : :curb))
      puts response.inspect
      raise OpenstackBridge::Error.new(self), response.raw_body unless [200, 201, 202, 204].include?(response.code.to_i)
      response
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
openstack_bridge-0.0.2 lib/openstack_bridge/swift.rb