Class: HP::Cloud::AuthCache

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

Constant Summary collapse

@@home =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuthCache

Returns a new instance of AuthCache.



30
31
32
33
34
35
36
# File 'lib/hpcloud/auth_cache.rb', line 30

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

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



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

def directory
  @directory
end

Class Method Details

.home_directory=(dir) ⇒ Object



38
39
40
# File 'lib/hpcloud/auth_cache.rb', line 38

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

Instance Method Details

#default_zone(opts, service) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/hpcloud/auth_cache.rb', line 116

def default_zone(opts, service)
  creds = read(opts)
  return nil if creds.nil?
  catalog = creds[:service_catalog]
  return nil if catalog.nil?
  return nil if catalog[service.to_sym].nil?
  keys = []
  catalog[service.to_sym].keys.each { |x| keys << x.to_s }
  keys.delete("name")
  return keys.sort.first.to_s
end

#get_file_name(opts) ⇒ Object



50
51
52
53
# File 'lib/hpcloud/auth_cache.rb', line 50

def get_file_name(opts)
  return "#{@directory}" if opts.nil?
  "#{@directory}#{get_name(opts)}"
end

#get_name(opts) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/hpcloud/auth_cache.rb', line 42

def get_name(opts)
  begin
    "#{opts[:hp_access_key]}:#{opts[:hp_tenant_id]}"
  rescue
    "default"
  end
end

#read(opts) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hpcloud/auth_cache.rb', line 83

def read(opts)
   = get_name(opts)
  return @aucas[] if @aucas[].nil? == false
  file_name = get_file_name(opts)
  if File.exists?(file_name)
    begin
      @aucas[] = YAML::load(File.open(file_name))
    rescue Exception => e
      raise Exception.new('Error reading cache file: ' + file_name)
    end
  end
  return @aucas[]
end

#remove(opts = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hpcloud/auth_cache.rb', line 55

def remove(opts = nil)
  if opts.nil?
    begin
      dirname = get_file_name(nil)
      dir = Dir.new(dirname)
      dir.entries.each { |x|
        file_name = dirname + x
        begin
          unless (x == "." || x == "..")
            File.delete(file_name)
          end
        rescue
          warn "Error deleting cache file: #{file_name}"
        end
      }
    rescue
    end
  else
    file_name = get_file_name(opts)
    begin
      File.delete(file_name)
    rescue
      return false
    end
  end
  return true
end

#write(opts, creds) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/hpcloud/auth_cache.rb', line 97

def write(opts, creds)
   = get_name(opts)
  @aucas[] = creds;
  config = @aucas[]
  if config.nil?
    remove(opts)
    return
  end
  file_name = get_file_name(opts)
  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