Class: HP::Cloud::Accounts

Inherits:
Object
  • Object
show all
Defined in:
lib/hpcloud/accounts.rb

Constant Summary collapse

DEFAULT_ACCOUNT =
"default_account"
TOP_LEVEL =
[:username, :provider]
CREDENTIALS =
[:account_id,
:secret_key,
:auth_uri,
:tenant_id,
:userpass]
ZONES =
[:compute,
:"object storage",
:cdn,
:dns,
:lbaas,
:db,
:network,
:"block storage"]
OPTIONS =
[:connect_timeout,
:read_timeout,
:write_timeout,
:ssl_verify_peer,
:ssl_ca_path,
:ssl_ca_file,
:preferred_flavor,
:preferred_win_image,
:preferred_image]
@@home =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAccounts

Returns a new instance of Accounts.



55
56
57
58
59
60
61
# File 'lib/hpcloud/accounts.rb', line 55

def initialize
  if @@home.nil?
    @@home = ENV['HOME']
  end
  @directory = @@home + "/.hpcloud/accounts/"
  @accts = {}
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



27
28
29
# File 'lib/hpcloud/accounts.rb', line 27

def directory
  @directory
end

Class Method Details

.get_knownObject



67
68
69
70
71
72
73
# File 'lib/hpcloud/accounts.rb', line 67

def self.get_known
  ret = ""
  CREDENTIALS.each{|key| ret += "\n * " + key.to_s }
  ZONES.each{|key| ret += "\n * " + key.to_s }
  OPTIONS.each{|key| ret += "\n * " + key.to_s }
  return ret
end

.home_directory=(dir) ⇒ Object



63
64
65
# File 'lib/hpcloud/accounts.rb', line 63

def self.home_directory=(dir)
  @@home = dir
end

Instance Method Details

#copy(src, dest) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hpcloud/accounts.rb', line 101

def copy(src, dest)
  begin
    src_name = get_file_name(src)
    dest_name = get_file_name(dest)
    FileUtils.cp(src_name, dest_name)
    return true
  rescue Exception => e
    raise Exception.new('Error copying ' + src_name + ' to ' + dest_name)
  end
  return false
end

#create(account) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/hpcloud/accounts.rb', line 148

def create()
  if @accts[].nil?
    uri = Config.new.get(:default_auth_uri)
    @accts[] = {:credentials=>{:auth_uri=>uri},
                       :regions=>{},
                       :options=>{}}
  end
  return @accts[]
end

#create_options(account_name, zone, avl_zone = nil) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/hpcloud/accounts.rb', line 238

def create_options(, zone, avl_zone = nil)
  hsh = get()
  provider = hsh[:provider] || 'hp'
  provider = provider.downcase
  creds = hsh[:credentials]
  regions = hsh[:regions] || {}
  opts = hsh[:options]
  opts.delete(:preferred_flavor)
  opts.delete(:preferred_image)
  opts.delete(:preferred_win_image)
  opts.delete(:checker_url)
  opts.delete(:checker_deferment)
  unless zone.nil?
    avl_zone = avl_zone || regions[zone.to_s.downcase.to_sym]
  end

  options = {}
  if provider == 'hp'
    options[:hp_access_key] = creds[:account_id] || creds[:hp_access_key] || creds[:hp_account_id]
    options[:hp_secret_key] = creds[:secret_key] || creds[:hp_secret_key]
    options[:hp_use_upass_auth_style] = true if creds[:userpass] == true
    options[:hp_auth_uri] = creds[:auth_uri] || creds[:hp_auth_uri]
    options[:hp_tenant_id ] = creds[:tenant_id] || creds[:hp_tenant_id]
    options[:hp_avl_zone] = avl_zone
    options[:connection_options] = opts
    options[:user_agent] = "HPCloud-UnixCLI/#{HP::Cloud::VERSION}"
  else
    options = creds
  end
  options[:provider] = provider

  return options
end

#get(account) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/hpcloud/accounts.rb', line 195

def get()
  hsh = read().clone
  settings = Config.new.settings
  hsh[:provider] ||= 'hp'
  hsh[:options][:connect_timeout] ||= settings[:connect_timeout]
  hsh[:options][:read_timeout] ||= settings[:read_timeout]
  hsh[:options][:write_timeout] ||= settings[:write_timeout]
  hsh[:options][:preferred_flavor] ||= settings[:preferred_flavor]
  hsh[:options][:preferred_image] ||= settings[:preferred_image]
  hsh[:options][:connect_timeout] = hsh[:options][:connect_timeout].to_i
  hsh[:options][:read_timeout] = hsh[:options][:read_timeout].to_i
  hsh[:options][:write_timeout] = hsh[:options][:write_timeout].to_i
  if hsh[:options][:ssl_verify_peer].nil?
    hsh[:options][:ssl_verify_peer] = settings[:ssl_verify_peer]
  end
  if hsh[:options][:ssl_verify_peer].to_s == "false" || hsh[:options][:ssl_verify_peer].to_s == "no"
    hsh[:options][:ssl_verify_peer] = false
  else
    hsh[:options][:ssl_verify_peer] = true
  end
  hsh[:options][:ssl_ca_path] ||= settings[:ssl_ca_path]
  hsh[:options][:ssl_ca_path] ||= settings[:ssl_ca_path]
  hsh[:options][:ssl_ca_file] ||= settings[:ssl_ca_file]
  hsh[:options].delete_if{ |k,v| v.nil? }
  return hsh
end

#get_file_name(account) ⇒ Object



75
76
77
# File 'lib/hpcloud/accounts.rb', line 75

def get_file_name()
  "#{@directory}#{.to_s.downcase.gsub(' ', '_')}"
end

#listObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/hpcloud/accounts.rb', line 79

def list
  unless File.directory?("#{@directory}")
    return "No such file or directory - #{@directory}\n" +
           "Run account:setup to create accounts."
  end
  ray = Dir.entries("#{@directory}")
  ray.delete_if{|item| File.file?("#{@directory}/#{item}") == false }
  ray.sort!
  return ray.join("\n")
end

#migrateObject



272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/hpcloud/accounts.rb', line 272

def migrate
  ray = list
  return false if ray.index('default').nil?
  return false unless ray.index('hp').nil?
  warn "Renaming account 'default' to 'hp'..."
  return false unless copy('default', 'hp')
  remove('default')

  config = Config.new(true)
  config.set(:default_account, 'hp')
  config.write()
  warn "Account 'hp' is now the default"
  return true
end

#read(account, createIt = false) ⇒ Object



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
# File 'lib/hpcloud/accounts.rb', line 113

def read(, createIt=false)
  return @accts[] if @accts[].nil? == false
  file_name = get_file_name()
  if  == 'hp' && File.exists?(file_name) == false
    migrate
  end
  if File.exists?(file_name)
    begin
      hsh = YAML::load(File.open(file_name))
      hsh[:credentials] = {} if hsh[:credentials].nil?
      if ((hsh[:zones].nil? == false) && (hsh[:regions].nil? == true))
        regions = {}
        zones = hsh[:zones]
        regions[:compute] = zones[:compute_availability_zone] unless zones[:compute_availability_zone].nil?
        regions[:"object storage"] = zones[:storage_availability_zone] unless zones[:storage_availability_zone].nil?
        regions[:cdn] = zones[:cdn_availability_zone] unless zones[:cdn_availability_zone].nil?
        regions[:"block storage"] = zones[:block_availability_zone] unless zones[:block_availability_zone].nil?
        hsh[:zones] = nil
        hsh[:regions] = regions
      end
      hsh[:regions] = {} if hsh[:regions].nil?
      hsh[:options] = {} if hsh[:options].nil?
      @accts[] = hsh
    rescue Exception => e
      raise Exception.new('Error reading account file: ' + file_name)
    end
  else
    if createIt
      return create()
    end
    raise Exception.new('Could not find account file: ' + file_name)
  end
  return @accts[]
end

#remove(account) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/hpcloud/accounts.rb', line 90

def remove()
  begin
    file_name = get_file_name()
    File.delete(file_name)
    return true
  rescue Exception => e
    raise Exception.new('Error removing account file: ' + file_name)
  end
  return false
end

#set(account, key, value) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/hpcloud/accounts.rb', line 177

def set(, key, value)
  hsh = @accts[]
  return false if hsh.nil?
  key = key.to_sym
  if CREDENTIALS.include?(key)
    hsh[:credentials][key] = value
  elsif ZONES.include?(key)
    hsh[:regions][key] = value
  elsif OPTIONS.include?(key)
    hsh[:options][key] = value
  elsif TOP_LEVEL.include?(key)
    hsh[key] = value
  else
    return false
  end
  return true
end

#set_cred(account, cred) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/hpcloud/accounts.rb', line 158

def set_cred(, cred)
  if @accts[].nil?
    @accts[] = {:credentials=>{}, :regions=>{}, :options=>{}}
  end
  @accts[][:credentials] = cred
  unless cred[:hp_auth_uri].nil?
    if cred[:hp_auth_uri].match(/hpcloud.net/)
      @accts[][:options][:ssl_verify_peer] = false
    end
  end
end

#set_regions(account, regions) ⇒ Object



170
171
172
173
174
175
# File 'lib/hpcloud/accounts.rb', line 170

def set_regions(, regions)
  if @accts[].nil?
    @accts[] = {:credentials=>{}, :regions=>{}, :options=>{}}
  end
  @accts[][:regions] = regions
end

#write(account) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/hpcloud/accounts.rb', line 222

def write()
  config = @accts[]
  if config.nil?
    raise Exception.new("Cannot find account information for #{}")
  end
  file_name = get_file_name()
  begin
    FileUtils.mkpath(@directory)
    File.open(file_name, 'w') do |file|
      file.write config.to_yaml
    end
  rescue Exception => e
    raise Exception.new("Error writing file #{file_name}")
  end
end