Module: Fog::OpenStack

Extended by:
Provider
Defined in:
lib/fog/openstack.rb

Defined Under Namespace

Modules: Errors

Class Method Summary collapse

Methods included from Provider

[], extended, service, services

Class Method Details

.authenticate_v1(options, connection_options = {}) ⇒ Object

legacy v1.0 style auth



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fog/openstack.rb', line 49

def self.authenticate_v1(options, connection_options = {})
  uri = options[:openstack_auth_uri]
  connection = Fog::Connection.new(uri.to_s, false, connection_options)
  @openstack_api_key  = options[:openstack_api_key]
  @openstack_username = options[:openstack_username]

  response = connection.request({
    :expects  => [200, 204],
    :headers  => {
      'X-Auth-Key'  => @openstack_api_key,
      'X-Auth-User' => @openstack_username
    },
    :host     => uri.host,
    :method   => 'GET',
    :path     =>  (uri.path and not uri.path.empty?) ? uri.path : 'v1.0'
  })

  return {
    :token => response.headers['X-Auth-Token'],
    :server_management_url => response.headers['X-Server-Management-Url'],
    :identity_public_endpoint => response.headers['X-Keystone']
  }
end

.authenticate_v2(options, connection_options = {}) ⇒ Object

Keystone Style Auth



74
75
76
77
78
79
80
81
82
83
84
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
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
# File 'lib/fog/openstack.rb', line 74

def self.authenticate_v2(options, connection_options = {})
  uri                   = options[:openstack_auth_uri]
  tenant_name           = options[:openstack_tenant]
  service_name          = options[:openstack_service_name]
  identity_service_name = options[:openstack_identity_service_name]
  endpoint_type         = (options[:openstack_endpoint_type] || 'publicURL').to_s
  openstack_region      = options[:openstack_region]


  body = retrieve_tokens_v2(options, connection_options)
  service = body['access']['serviceCatalog'].
    detect {|s| service_name.include?(s['type']) }

  options[:unscoped_token] = body['access']['token']['id']

  unless service
    unless tenant_name
      response = Fog::Connection.new(
        "#{uri.scheme}://#{uri.host}:#{uri.port}/v2.0/tenants", false, connection_options).request({
        :expects => [200, 204],
        :headers => {'Content-Type' => 'application/json',
                     'Accept' => 'application/json',
                     'X-Auth-Token' => body['access']['token']['id']},
        :host    => uri.host,
        :method  => 'GET'
      })

      body = Fog::JSON.decode(response.body)
      if body['tenants'].empty?
        raise Errors::NotFound.new('No Tenant Found')
      else
        options[:openstack_tenant] = body['tenants'].first['name']
      end
    end

    body = retrieve_tokens_v2(options, connection_options)
    service = body['access']['serviceCatalog'].
      detect{|s| service_name.include?(s['type']) }
  end

  service['endpoints'] = service['endpoints'].select do |endpoint|
    endpoint['region'] == openstack_region
  end if openstack_region

  if service['endpoints'].count > 1
    regions = service["endpoints"].map{ |e| e['region'] }.uniq.join(',')
    raise Errors::NotFound.new("Multiple regions available choose one of these '#{regions}'")
  end

  identity_service = body['access']['serviceCatalog'].
    detect{|x| identity_service_name.include?(x['type']) } if identity_service_name
  tenant = body['access']['token']['tenant']
  user = body['access']['user']

  management_url = service['endpoints'].detect{|s| s[endpoint_type]}[endpoint_type]
  identity_url   = identity_service['endpoints'].detect{|s| s['publicURL']}['publicURL'] if identity_service

  {
    :user                     => user,
    :tenant                   => tenant,
    :identity_public_endpoint => identity_url,
    :server_management_url    => management_url,
    :token                    => body['access']['token']['id'],
    :expires                  => body['access']['token']['expires'],
    :current_user_id          => body['access']['user']['id'],
    :unscoped_token           => options[:unscoped_token]
  }
end

.retrieve_tokens_v2(options, connection_options = {}) ⇒ Object



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

def self.retrieve_tokens_v2(options, connection_options = {})
  api_key     = options[:openstack_api_key].to_s
  username    = options[:openstack_username].to_s
  tenant_name = options[:openstack_tenant].to_s
  auth_token  = options[:openstack_auth_token] || options[:unscoped_token]
  uri         = options[:openstack_auth_uri]

  connection = Fog::Connection.new(uri.to_s, false, connection_options)
  request_body = {:auth => Hash.new}

  if auth_token
    request_body[:auth][:token] = {
      :id => auth_token
    }
  else
    request_body[:auth][:passwordCredentials] = {
      :username => username,
      :password => api_key
    }
  end
  request_body[:auth][:tenantName] = tenant_name if tenant_name

  response = connection.request({
    :expects  => [200, 204],
    :headers  => {'Content-Type' => 'application/json'},
    :body     => Fog::JSON.encode(request_body),
    :host     => uri.host,
    :method   => 'POST',
    :path     => (uri.path and not uri.path.empty?) ? uri.path : 'v2.0'
  })

  Fog::JSON.decode(response.body)
end