Class: HP::Cloud::Metadata

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mta = nil) ⇒ Metadata

Returns a new instance of Metadata.



33
34
35
36
37
38
39
40
41
# File 'lib/hpcloud/metadata.rb', line 33

def initialize(mta = nil)
  @cstatus = CliStatus.new
  @fog_metadata = mta
  @hsh = {}
  if mta.nil?
    return
  end
  mta.map { |m| @hsh["#{m.key}"] = "#{m.value}" }
end

Instance Attribute Details

#cstatusObject (readonly)

Returns the value of attribute cstatus.



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

def cstatus
  @cstatus
end

#hshObject (readonly)

Returns the value of attribute hsh.



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

def hsh
  @hsh
end

Class Method Details

.get_keysObject



29
30
31
# File 'lib/hpcloud/metadata.rb', line 29

def self.get_keys()
  return [ "key", "value" ]
end

Instance Method Details

#remove_metadata(key) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hpcloud/metadata.rb', line 85

def (key)
  pair = nil
  if ! @fog_metadata.nil?
    pair = @fog_metadata.get(key)
  end
  if pair.nil?
    @cstatus = CliStatus.new("Metadata key '#{key}' not found", :not_found)
  else
    begin
      pair.destroy
    rescue Exception => e
      @cstatus = CliStatus.new("Error deleting metadata '#{key}': " + e.message)
    end
  end
  if @cstatus.is_success?
    return true
  end
  return false
end

#set_metadata(value) ⇒ Object



50
51
52
53
54
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
82
83
# File 'lib/hpcloud/metadata.rb', line 50

def (value)
  if value.nil?
    return true
  end
  if value.empty?
    @hsh = {}
    return true
  end
  begin
    hshstr = '{"' + value.gsub('=', '"=>"').gsub(',', '","') + '"}'
    hsh = eval(hshstr)
    if (hsh.kind_of? Hash)
      @hsh = hsh
      unless @fog_metadata.nil?
        @hsh.each { |k,v|
          pair = @fog_metadata.get(k)
          if pair.nil?
            pair = @fog_metadata.new
            pair.key = k
            pair.value = v
          else
            pair.value = v
          end
          pair.save
        }
      end
      return true
    end
  rescue SyntaxError => se
  rescue NameError => ne
  end
  @cstatus = CliStatus.new("Invalid metadata '#{value}' should be in the form 'k1=v1,k2=v2,...'", :incorrect_usage)
  return false
end

#to_arrayObject



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

def to_array()
  ray = []
  @hsh.each { |k,v| ray << {"key"=>k,"value"=>v} }
  ray.sort!{|a, b| a["key"] <=> b["key"]}
  return ray
end

#to_sObject



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/hpcloud/metadata.rb', line 105

def to_s
  found=false
  =""
  ray = to_array()
  ray.each { |val|
     << "," unless found == false
     << val["key"].to_s + "=" + val["value"].to_s
    found = true
  }
  return 
end