Sha256: 44d664ed3244151006553a53eaaa932e3574bd5b17e9239fe26efde5784530fb

Contents?: true

Size: 1.93 KB

Versions: 25

Compression:

Stored size: 1.93 KB

Contents

module Fog
  module RiakCS
    class Usage
      module Utils
        TYPES_TO_STRING = { :access  => 'a', :storage => 'b' }
        DEFAULT_TYPES   = TYPES_TO_STRING.keys
        DEFAULT_FORMAT  = :json

        def sanitize_and_convert_time(time)
          time.utc.iso8601.gsub(/[:-]/, '')
        end

        def format_and_types_to_path(format, types)
          format_character = format.to_s.split('').first
          type_characters  = types.map { |t| TYPES_TO_STRING[t] }.compact

          [type_characters, format_character].flatten.compact.join
        end

        def request_uri(access_key_id, options)
          format        = DEFAULT_FORMAT
          types         = options[:types]       || DEFAULT_TYPES
          start_time    = options[:start_time]  || Time.now.utc - 86400
          end_time      = options[:end_time]    || Time.now.utc

          [access_key_id,
           format_and_types_to_path(format, types),
           sanitize_and_convert_time(start_time),
           sanitize_and_convert_time(end_time)].join('.')
        end
      end

      class Real
        include Utils

        def get_usage(access_key_id, options = {})
          response = @connection.get_object('riak-cs', ["usage", request_uri(access_key_id, options)].join("/"))

          if !response.body.empty?
            response.body = Fog::JSON.decode(response.body)
          end
          response
        end
      end

      class Mock
        include Utils

        def get_usage(access_key, options = {})
          Excon::Response.new.tap do |response|
            response.status = 200
            response.headers['Content-Type'] = 'application/json'
            response.body = {
              'Access' => {
                'Nodes'   => [],
                'Errors'  => []
              },
              'Storage' => {
                'Samples' => [],
                'Errors'  => []
              }
            }
          end
        end
      end
    end
  end
end

Version data entries

25 entries across 23 versions & 6 rubygems

Version Path
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/gems/fog-riakcs-0.1.0/lib/fog/riakcs/usage/get_usage.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/fog-riakcs-0.1.0/lib/fog/riakcs/usage/get_usage.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/gems/fog-riakcs-0.1.0/lib/fog/riakcs/usage/get_usage.rb
vagrant-cloudstack-1.2.0 vendor/bundle/gems/fog-riakcs-0.1.0/lib/fog/riakcs/usage/get_usage.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/fog-1.22.1/lib/fog/riakcs/requests/usage/get_usage.rb
fog-riakcs-0.1.0 lib/fog/riakcs/usage/get_usage.rb
fog-riakcs-0.0.1 lib/fog/riakcs/usage/get_usage.rb
fog-1.27.0 lib/fog/riakcs/requests/usage/get_usage.rb
fog-1.26.0 lib/fog/riakcs/requests/usage/get_usage.rb
fog-1.25.0 lib/fog/riakcs/requests/usage/get_usage.rb
nsidc-fog-1.24.1 lib/fog/riakcs/requests/usage/get_usage.rb
fog-1.24.0 lib/fog/riakcs/requests/usage/get_usage.rb
ns-fog-1.22.11 lib/fog/riakcs/requests/usage/get_usage.rb
ns-fog-1.22.10 lib/fog/riakcs/requests/usage/get_usage.rb
ns-fog-1.22.9 lib/fog/riakcs/requests/usage/get_usage.rb
ns-fog-1.22.8 lib/fog/riakcs/requests/usage/get_usage.rb
ns-fog-1.22.7 lib/fog/riakcs/requests/usage/get_usage.rb
ns-fog-1.22.6 lib/fog/riakcs/requests/usage/get_usage.rb
fog-1.23.0 lib/fog/riakcs/requests/usage/get_usage.rb
ns-fog-1.22.4 lib/fog/riakcs/requests/usage/get_usage.rb