Class: Fog::Storage::Atmos::Real

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/fog/storage/atmos.rb,
lib/fog/storage/atmos/requests/get_namespace.rb,
lib/fog/storage/atmos/requests/put_namespace.rb,
lib/fog/storage/atmos/requests/head_namespace.rb,
lib/fog/storage/atmos/requests/post_namespace.rb,
lib/fog/storage/atmos/requests/delete_namespace.rb

Constant Summary

Constants included from Utils

Utils::ENDPOINT_REGEX

Instance Method Summary collapse

Methods included from Utils

#api_path, #host, #port, #setup_credentials, #ssl?

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



82
83
84
85
86
87
88
89
90
# File 'lib/fog/storage/atmos.rb', line 82

def initialize(options={})
  setup_credentials(options)
  @connection_options = options[:connection_options] || {}
  @hmac               = Fog::HMAC.new('sha1', @storage_secret_decoded)
  @persistent = options.fetch(:persistent, false)

  @connection = Fog::XML::Connection.new("#{@prefix}://#{@storage_host}:#{@storage_port}",
      @persistent, @connection_options)
end

Instance Method Details

#delete_namespace(namespace = '', options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/fog/storage/atmos/requests/delete_namespace.rb', line 5

def delete_namespace(namespace = '', options = {})
  options = options.reject {|key, value| value.nil?}
  request({
            :expects  => 204,
            :method   => 'DELETE',
            :path     => "namespace/" + namespace,
            :query    => options
          }.merge(options))
end

#get_namespace(namespace = '', options = {}, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/storage/atmos/requests/get_namespace.rb', line 5

def get_namespace(namespace = '', options = {}, &block)
  options = options.reject {|key, value| value.nil?}

  if block_given?
    options[:response_block] = Proc.new
  end

  request({
            :expects  => 200,
            :method   => 'GET',
            :path     => "namespace/" + URI.escape(namespace),
            :query    => {},
            :parse => true
          }.merge(options))
end

#head_namespace(namespace = '', options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/storage/atmos/requests/head_namespace.rb', line 5

def head_namespace(namespace = '', options = {})
  options = options.reject {|key, value| value.nil?}
  request({
            :expects  => 200,
            :method   => 'HEAD',
            :path     => "namespace/" + URI.escape(namespace),
            :query    => {},
            :parse => true
          }.merge(options))
end

#post_namespace(namespace = '', options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/storage/atmos/requests/post_namespace.rb', line 5

def post_namespace(namespace = '', options = {})
  options = options.reject {|key, value| value.nil?}
  request({
            :expects  => 201,
            :method   => 'POST',
            :path     => "namespace/" + namespace,
            :query    => {},
            :parse => true
          }.merge(options))
end

#put_namespace(namespace = '', options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/storage/atmos/requests/put_namespace.rb', line 5

def put_namespace(namespace = '', options = {})
  options = options.reject {|key, value| value.nil?}
  request({
            :expects  => 200,
            :method   => 'PUT',
            :path     => "namespace/" + namespace,
            :query    => {},
            :parse => true
          }.merge(options))
end

#reloadObject



101
102
103
# File 'lib/fog/storage/atmos.rb', line 101

def reload
  @connection.reset
end

#request(params, &block) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/fog/storage/atmos.rb', line 105

def request(params, &block)
  req_path = params[:path]
  # Force set host and port
  params.merge!({
                  :host     => @storage_host,
                  :path     => "#{@api_path}/rest/#{params[:path]}",
                })
  # Set default method and headers
  params = {:method => 'GET', :headers => {}}.merge params

  params[:headers]["Content-Type"] ||= "application/octet-stream"

  # Add request date
  params[:headers]["date"] = Time.now().httpdate()
  params[:headers]["x-emc-uid"] = @storage_token

  # Build signature string
  signstring = ""
  signstring += params[:method]
  signstring += "\n"
  signstring += params[:headers]["Content-Type"]
  signstring += "\n"
  if( params[:headers]["range"] )
    signstring += params[:headers]["range"]
  end
  signstring += "\n"
  signstring += params[:headers]["date"]
  signstring += "\n"

  signstring += "/rest/" + URI.unescape( req_path ).downcase
  query_str = params[:query].map{|k,v| "#{k}=#{v}"}.join('&')
  signstring += '?' + query_str unless query_str.empty?
  signstring += "\n"

  customheaders = {}
  params[:headers].each { |key,value|
    case key
    when 'x-emc-date', 'x-emc-signature'
      #skip
    when /^x-emc-/
      customheaders[ key.downcase ] = value
    end
  }
  header_arr = customheaders.sort()

  header_arr.each { |key,value|
    # Values are lowercase and whitespace-normalized
    signstring += key + ":" + value.strip.chomp.squeeze( " " ) + "\n"
  }

  digest = @hmac.sign(signstring.chomp())
  signature = Base64.encode64( digest ).chomp()
  params[:headers]["x-emc-signature"] = signature

  params.delete(:host) #invalid excon request parameter

  parse = params.delete(:parse)

  begin
    response = @connection.request(params, &block)
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
    when Excon::Errors::NotFound
      Fog::Storage::Atmos::NotFound.slurp(error)
    else
      error
    end
  end
  unless response.body.empty?
    if parse
      document = Fog::ToHashDocument.new
      parser = Nokogiri::XML::SAX::PushParser.new(document)
      parser << response.body
      parser.finish
      response.body = document.body
    end
  end
  response
end

#sign(string) ⇒ Object



96
97
98
99
# File 'lib/fog/storage/atmos.rb', line 96

def sign(string)
  value = @hmac.sign(string)
  Base64.encode64( value ).chomp()
end

#uidObject



92
93
94
# File 'lib/fog/storage/atmos.rb', line 92

def uid
  @storage_token#.split('/')[-1]
end