class NightcrawlerSwift::Connection

Attributes

auth_response[W]
expires_at[R]
token_id[R]

Private Class Methods

connected_attr_reader(*args) click to toggle source
# File lib/nightcrawler_swift/connection.rb, line 3
def self.connected_attr_reader(*args)
  args.each do |arg|
    define_method(arg.to_sym) do
      connect! unless connected?
      instance_variable_get("@#{arg}")
    end
  end
end

Public Instance Methods

auth_response() click to toggle source
# File lib/nightcrawler_swift/connection.rb, line 18
def auth_response
  @auth_response ||= nil
  authenticate! if @auth_response.nil?
  @auth_response
end
configure() click to toggle source
# File lib/nightcrawler_swift/connection.rb, line 36
def configure
  select_token
  select_catalog
  select_endpoints
  configure_urls
end
connect!() click to toggle source
# File lib/nightcrawler_swift/connection.rb, line 24
def connect!
  authenticate!
  configure

  NightcrawlerSwift.logger.debug "[NightcrawlerSwift] Connected, token_id: #{token_id}"
  self
end
connected?() click to toggle source
# File lib/nightcrawler_swift/connection.rb, line 32
def connected?
  !self.token_id.nil? and self.expires_at > Time.now
end

Private Instance Methods

auth_options() click to toggle source
# File lib/nightcrawler_swift/connection.rb, line 58
def auth_options
  {
    auth: {
      identity: {
          methods: [
              "password"
          ],
          password: {
              user: {
                  domain: {
                      id: "default"
                  },
                  name: opts.username,
                  password: opts.password
              }
          }
      },
      scope: {
          project: {
              domain: {
                  id: "default"
              },
              name: opts.tenant_name
          }
      }
    }
  }
end
authenticate!() click to toggle source
# File lib/nightcrawler_swift/connection.rb, line 48
def authenticate!
  url = opts.auth_url
  headers = {content_type: :json, accept: :json}
  response = Gateway.new(url).request {|r| r.post(auth_options.to_json, headers)}

  @auth_response = OpenStruct.new(headers: response.headers, body: JSON.parse(response.body))
rescue StandardError => e
  raise Exceptions::ConnectionError.new(e)
end
configure_urls() click to toggle source
# File lib/nightcrawler_swift/connection.rb, line 103
def configure_urls
  @admin_url = opts.admin_url || @endpoints.find {|e| e["interface"] == "admin"}["url"]
  @public_url = opts.public_url || @endpoints.find {|e| e["interface"] == "public"}["url"]
  @internal_url = opts.internal_url || @endpoints.find {|e| e["interface"] == "internal"}["url"]
  @upload_url = "#{@admin_url}/#{opts.bucket}"
end
opts() click to toggle source
# File lib/nightcrawler_swift/connection.rb, line 44
def opts
  NightcrawlerSwift.options
end
select_catalog() click to toggle source
# File lib/nightcrawler_swift/connection.rb, line 93
def select_catalog
  catalogs = auth_response["body"]["token"]["catalog"]
  @catalog = catalogs.find {|catalog| catalog["type"] == "object-store"}
end
select_endpoints() click to toggle source
# File lib/nightcrawler_swift/connection.rb, line 98
def select_endpoints
  raise Exceptions::ConfigurationError.new "No catalog of type 'object-store' found" if @catalog.nil?
  @endpoints = @catalog["endpoints"]
end
select_token() click to toggle source
# File lib/nightcrawler_swift/connection.rb, line 87
def select_token
  @token_id = auth_response.headers[:x_subject_token]
  @expires_at = auth_response.body["token"]["expires_at"]
  @expires_at = DateTime.parse(@expires_at).to_time
end