Class: Fog::Volume::OpenStack::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/openstack/volume.rb,
lib/fog/openstack/requests/volume/set_tenant.rb,
lib/fog/openstack/requests/volume/list_volumes.rb,
lib/fog/openstack/requests/volume/create_volume.rb,
lib/fog/openstack/requests/volume/delete_volume.rb,
lib/fog/openstack/requests/volume/list_snapshots.rb,
lib/fog/openstack/requests/volume/delete_snapshot.rb,
lib/fog/openstack/requests/volume/get_volume_details.rb,
lib/fog/openstack/requests/volume/get_snapshot_details.rb,
lib/fog/openstack/requests/volume/create_volume_snapshot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fog/openstack/volume.rb', line 85

def initialize(options={})
  require 'multi_json'

  @openstack_auth_token = options[:openstack_auth_token]

  unless @openstack_auth_token
    missing_credentials = Array.new
    @openstack_api_key  = options[:openstack_api_key]
    @openstack_username = options[:openstack_username]

    missing_credentials << :openstack_api_key  unless @openstack_api_key
    missing_credentials << :openstack_username unless @openstack_username
    raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?
  end

  @openstack_tenant               = options[:openstack_tenant]
  @openstack_auth_uri             = URI.parse(options[:openstack_auth_url])
  @openstack_management_url       = options[:openstack_management_url]
  @openstack_must_reauthenticate  = false
  @openstack_service_name         = options[:openstack_service_name] || ['volume']

  @connection_options = options[:connection_options] || {}

  @current_user = options[:current_user]
  @current_tenant = options[:current_tenant]

  authenticate

  @persistent = options[:persistent] || false
  @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end

Instance Attribute Details

#current_tenantObject (readonly)

Returns the value of attribute current_tenant.



83
84
85
# File 'lib/fog/openstack/volume.rb', line 83

def current_tenant
  @current_tenant
end

#current_userObject (readonly)

Returns the value of attribute current_user.



82
83
84
# File 'lib/fog/openstack/volume.rb', line 82

def current_user
  @current_user
end

Instance Method Details

#create_volume(name, description, size, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/openstack/requests/volume/create_volume.rb', line 6

def create_volume(name, description, size, options={})
  data = {
    'volume' => {
      'display_name'        => name,
      'display_description' => description,
      'size'                => size
    }
  }

  vanilla_options = ['snapshot_id']
  vanilla_options.select{|o| options[o]}.each do |key|
    data['volume'][key] = options[key]
  end
  request(
    :body     => MultiJson.encode(data),
    :expects  => [200, 202],
    :method   => 'POST',
    :path     => "volumes"
  )
end

#create_volume_snapshot(volume_id, name, description, force = false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fog/openstack/requests/volume/create_volume_snapshot.rb', line 6

def create_volume_snapshot(volume_id, name, description, force=false)
  data = {
    'snapshot' => {
      'volume_id'           => volume_id,
      'display_name'        => name,
      'display_description' => description,
      'force'               => force
    }
  }

  request(
    :body     => MultiJson.encode(data),
    :expects  => [200, 202],
    :method   => 'POST',
    :path     => "snapshots"
  )
end

#credentialsObject



117
118
119
120
121
122
123
124
# File 'lib/fog/openstack/volume.rb', line 117

def credentials
  { :provider                 => 'openstack',
    :openstack_auth_url       => @openstack_auth_uri.to_s,
    :openstack_auth_token     => @auth_token,
    :openstack_management_url => @openstack_management_url,
    :current_user             => @current_user,
    :current_tenant           => @current_tenant }
end

#delete_snapshot(snapshot_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/volume/delete_snapshot.rb', line 6

def delete_snapshot(snapshot_id)
  request(
    :expects  => 202,
    :method   => 'DELETE',
    :path     => "snapshots/#{snapshot_id}"
  )
end

#delete_volume(volume_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/volume/delete_volume.rb', line 6

def delete_volume(volume_id)
  request(
    :expects  => 202,
    :method   => 'DELETE',
    :path     => "volumes/#{volume_id}"
  )
end

#get_snapshot_details(snapshot_id) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/fog/openstack/requests/volume/get_snapshot_details.rb', line 6

def get_snapshot_details(snapshot_id)

  request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "snapshots/#{snapshot_id}"
  )
end

#get_volume_details(volume_id) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/fog/openstack/requests/volume/get_volume_details.rb', line 6

def get_volume_details(volume_id)

  request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "volumes/#{volume_id}"
  )
end

#list_snapshots(detailed = true) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/fog/openstack/requests/volume/list_snapshots.rb', line 6

def list_snapshots(detailed=true)

  path = detailed ? 'snapshots/detail' : 'snapshots'
  request(
    :expects  => 200,
    :method   => 'GET',
    :path     => path
  )
end

#list_volumes(detailed = true) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/fog/openstack/requests/volume/list_volumes.rb', line 6

def list_volumes(detailed=true)

  path = detailed ? 'volumes/detail' : 'volumes'
  request(
    :expects  => 200,
    :method   => 'GET',
    :path     => path
  )
end

#reloadObject



126
127
128
# File 'lib/fog/openstack/volume.rb', line 126

def reload
  @connection.reset
end

#request(params) ⇒ Object



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
# File 'lib/fog/openstack/volume.rb', line 130

def request(params)
  begin
    response = @connection.request(params.merge({
      :headers  => {
        'Content-Type' => 'application/json',
        'X-Auth-Token' => @auth_token
      }.merge!(params[:headers] || {}),
      :host     => @host,
      :path     => "#{@path}/#{params[:path]}"#,
      # Causes errors for some requests like tenants?limit=1
      # :query    => ('ignore_awful_caching' << Time.now.to_i.to_s)
    }))
  rescue Excon::Errors::Unauthorized => error
    if error.response.body != 'Bad username or password' # token expiration
      @openstack_must_reauthenticate = true
      authenticate
      retry
    else # bad credentials
      raise error
    end
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
    when Excon::Errors::NotFound
      Fog::Compute::OpenStack::NotFound.slurp(error)
    else
      error
    end
  end
  unless response.body.empty?
    response.body = MultiJson.decode(response.body)
  end
  response
end

#set_tenant(tenant) ⇒ Object



6
7
8
9
10
# File 'lib/fog/openstack/requests/volume/set_tenant.rb', line 6

def set_tenant(tenant)
  @openstack_must_reauthenticate = true
  @openstack_tenant = tenant.to_s
  authenticate
end