Class: NCC::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/ncc/instance.rb

Constant Summary collapse

@@valid_statuses =
%w(active build terminated error hard-reboot
reboot provider-operation shutting-down
suspending suspend unknown needs-verify)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfg, opt = {}) ⇒ Instance

Returns a new instance of Instance.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ncc/instance.rb', line 73

def initialize(cfg, opt={})
    @cfg = cfg
    @logger = opt[:logger] if opt.has_key? :logger
    self.id = opt['id']
    self.name = opt['name']
    self.size = opt['size']
    self.image = opt['image']
    self.environment = opt['environment']
    self.role = opt['role']
    self.host = opt['host']
    self.ip_address = opt['ip_address']
    self.console_log = opt['console_log']
    self.extra = opt['extra']
end

Instance Attribute Details

#console_logObject

Returns the value of attribute console_log.



54
55
56
# File 'lib/ncc/instance.rb', line 54

def console_log
  @console_log
end

#environmentObject

Returns the value of attribute environment.



54
55
56
# File 'lib/ncc/instance.rb', line 54

def environment
  @environment
end

#hostObject

Returns the value of attribute host.



54
55
56
# File 'lib/ncc/instance.rb', line 54

def host
  @host
end

#idObject

Returns the value of attribute id.



54
55
56
# File 'lib/ncc/instance.rb', line 54

def id
  @id
end

#imageObject

Returns the value of attribute image.



56
57
58
# File 'lib/ncc/instance.rb', line 56

def image
  @image
end

#ip_addressObject

Returns the value of attribute ip_address.



54
55
56
# File 'lib/ncc/instance.rb', line 54

def ip_address
  @ip_address
end

#nameObject

Returns the value of attribute name.



54
55
56
# File 'lib/ncc/instance.rb', line 54

def name
  @name
end

#sizeObject

Returns the value of attribute size.



56
57
58
# File 'lib/ncc/instance.rb', line 56

def size
  @size
end

#statusObject

Returns the value of attribute status.



56
57
58
# File 'lib/ncc/instance.rb', line 56

def status
  @status
end

Instance Method Details

#clear_extraObject



159
160
161
# File 'lib/ncc/instance.rb', line 159

def clear_extra
    @extra = nil
end

#debug(msg = nil) ⇒ Object

Should be mixed in



59
60
61
62
# File 'lib/ncc/instance.rb', line 59

def debug(msg=nil)
    msg ||= yield
    log 'debug', msg
end

#extra(param = nil) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ncc/instance.rb', line 163

def extra(param=nil)
    if param.nil?
        @extra
    else
        if !@extra.nil? and @extra.has_key? param
            @extra[param]
        else
            { }
        end
    end
end

#extra=(newextra) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/ncc/instance.rb', line 175

def extra=(newextra)
    if !newextra.nil?
        raise NCC::Error, "Invalid extra parameter of type " +
            "#{newextra.class} (must be Hash)" unless
            newextra.respond_to? :to_hash
        if @extra.nil?
            @extra = newextra.to_hash
        else
            @extra = @extra.deep_soft_merge(newextra.to_hash)
        end
    end
end

#log(level, msg) ⇒ Object



68
69
70
71
# File 'lib/ncc/instance.rb', line 68

def log(level, msg)
    @logger.send(level.intern, "#<#{me}>: #{msg}") if
        @logger.respond_to? level.intern
end

#roleObject



102
103
104
# File 'lib/ncc/instance.rb', line 102

def role
    @role
end

#role=(value) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/ncc/instance.rb', line 106

def role=(value)
    if value.nil?
        @role = []
    elsif value.respond_to? :join
        @role = value
    else
        @role = value.split(/, */)
    end
end

#set_without_validation(fields) ⇒ Object



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
# File 'lib/ncc/instance.rb', line 116

def set_without_validation(fields)
    fields.each_pair do |field, value|
        case field
        when :id
            @id = value
        when :name
            @name = value
        when :size
            @size = value
        when :image
            @image = value
        when :environment
            @environment = value
        when :role
            self.role = value
        when :extra
            @extra = value
        when :status
            self.status = value
        when :ip_address
            @ip_address = value
        when :host
            @host = value
        when :console_log
            @console_log = value
        else
            raise NCC::Error, "Invalid field #{field.inspect}"
        end
    end
end

#to_hashObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/ncc/instance.rb', line 194

def to_hash
    {
        'name' => name,
        'id' => id,
        'extra' => extra,
        'environment' => environment,
        'role' => role,
        'size' => size,
        'image' => image,
        'ip_address' => ip_address,
        'host' => host,
        'status' => status
    }.delete_nil_values
end

#to_jsonObject



209
210
211
# File 'lib/ncc/instance.rb', line 209

def to_json
    to_hash.to_json
end

#warn(msg) ⇒ Object



64
65
66
# File 'lib/ncc/instance.rb', line 64

def warn(msg)
    log 'warn', msg
end

#with_defaults(*defaults) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ncc/instance.rb', line 88

def with_defaults(*defaults)
    obj = self.dup
    defaults.each do |d|
        next if d.nil?
        obj.name ||= d['name']
        obj.size ||= d['size']
        obj.image ||= d['image']
        obj.environment ||= d['environment']
        obj.role ||= d['role']
        obj.extra = d['extra']
    end
    obj
end