Class: Collins::Asset

Inherits:
Object
  • Object
show all
Includes:
Find, Update, Util
Defined in:
lib/collins/asset.rb,
lib/collins/asset_find.rb,
lib/collins/asset_update.rb

Overview

Represents the basic notion of a collins asset

Defined Under Namespace

Modules: Find, Update

Constant Summary collapse

DATETIME_FORMAT =

Default time format when displaying dates associated with an asset

"%F %T"

Constants included from Util::Logging

Util::Logging::DEFAULT_LOG_FORMAT

Constants included from Update

Update::ALL_PARAMS, Update::FILE_PARAMS, Update::NON_ATTRIBUTE_PARAMS

Constants included from Find

Find::ALL_PARAMS, Find::DATE_PARAMS, Find::GENERAL_PARAMS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#deep_copy_hash, #get_asset_or_tag, included, #require_non_empty, #require_that, #stringify_hash, #symbolize_hash

Methods included from Util::Logging

#get_logger

Methods included from Update

get_param, get_param_value, is_attribute?, is_file_param?, to_a

Methods included from Find

to_a, valid?

Constructor Details

#initialize(opts = {}) ⇒ Asset

Create an Asset

Parameters:

  • opts (Hash) (defaults to: {})

    Asset parameters

Options Hash (opts):

  • :tag (String)

    The asset tag

  • :created (String)

    The creation DateTime

  • :id (Fixnum)

    The ID of the asset

  • :status (String)

    The asset status

  • :type (String)

    The asset type

  • :updated (String)

    The update DateTime

  • :deleted (String)

    The delete DateTime



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/collins/asset.rb', line 87

def initialize opts = {}
  @extras = {}
  @addresses = []
  if opts.is_a?(String) then
    model = {:tag => opts}
  else
    model = opts
  end
  hash = symbolize_hash(model).inject({}) do |result, (k,v)|
    result[k.downcase] = v
    result
  end
  @created = parse_datetime hash.delete(:created).to_s
  @id = hash.delete(:id).to_s.to_i
  @status = hash.delete(:status).to_s
  @tag = hash.delete(:tag).to_s
  @type = hash.delete(:type).to_s
  @state = Collins::AssetState.from_json(hash.delete(:state))
  @updated = parse_datetime hash.delete(:updated).to_s
  @deleted = parse_datetime hash.delete(:deleted).to_s
  hash.each {|k,v| @extras[k] = v}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ NilClass, Object (protected)

Note:

This is never called directly

Convenience method for #get_attribute

This ‘magic’ method allows you to retrieve attributes on an asset, or check if an attribute exists via a predicate method.

Examples:

real_asset.hostname # => "foo"
bare_asset.hostname # => nil
real_asset.hostname? # => true
bare_asset.hostname? # => false

Returns:

  • (NilClass, Object)

    Nil if attribute not found, otherwise the attribute value



260
261
262
263
264
265
266
267
268
269
# File 'lib/collins/asset.rb', line 260

def method_missing(m, *args, &block)
  name = m.to_s.upcase
  is_bool = name.end_with?('?')
  if is_bool then
    name = name.sub('?', '')
    respond_to?(name)
  else
    extract(extras, "ATTRIBS", "0", name)
  end
end

Instance Attribute Details

#addressesArray<CollinsAddress>

Returns Addresses associated with asset.

Returns:

  • (Array<CollinsAddress>)

    Addresses associated with asset



24
25
26
# File 'lib/collins/asset.rb', line 24

def addresses
  @addresses
end

#createdDateTime, NilClass

Returns Timestamp. Can be nil.

Returns:

  • (DateTime, NilClass)

    Timestamp. Can be nil



26
27
28
# File 'lib/collins/asset.rb', line 26

def created
  @created
end

#deletedDateTime, NilClass (readonly)

Returns Timestamp. Can be nil.

Returns:

  • (DateTime, NilClass)

    Timestamp. Can be nil



26
27
28
# File 'lib/collins/asset.rb', line 26

def deleted
  @deleted
end

#extrasHash

Returns All additional asset metadata.

Returns:

  • (Hash)

    All additional asset metadata



44
45
46
# File 'lib/collins/asset.rb', line 44

def extras
  @extras
end

#idFixnum

Returns Asset ID or 0.

Returns:

  • (Fixnum)

    Asset ID or 0



28
29
30
# File 'lib/collins/asset.rb', line 28

def id
  @id
end

#ipmiCollins::Ipmi

Returns IPMI information.

Returns:



30
31
32
# File 'lib/collins/asset.rb', line 30

def ipmi
  @ipmi
end

#locationString

Returns multi-collins location.

Returns:

  • (String)

    multi-collins location



32
33
34
# File 'lib/collins/asset.rb', line 32

def location
  @location
end

#powerCollins::Power

Returns Power configuration information.

Returns:



34
35
36
# File 'lib/collins/asset.rb', line 34

def power
  @power
end

#stateCollins::AssetState

Returns Asset state, or nil.

Returns:



36
37
38
# File 'lib/collins/asset.rb', line 36

def state
  @state
end

#statusString

Returns Asset status, or empty string.

Returns:

  • (String)

    Asset status, or empty string



38
39
40
# File 'lib/collins/asset.rb', line 38

def status
  @status
end

#tagString

Returns Asset tag, or empty string.

Returns:

  • (String)

    Asset tag, or empty string



40
41
42
# File 'lib/collins/asset.rb', line 40

def tag
  @tag
end

#typeString

Returns Asset type, or empty string.

Returns:

  • (String)

    Asset type, or empty string



42
43
44
# File 'lib/collins/asset.rb', line 42

def type
  @type
end

#updatedDateTime, NilClass

Returns Timestamp. Can be nil.

Returns:

  • (DateTime, NilClass)

    Timestamp. Can be nil



26
27
28
# File 'lib/collins/asset.rb', line 26

def updated
  @updated
end

Class Method Details

.format_date_string(s) ⇒ DateTime

Convenience method for parsing asset ISO8601 date times

Parameters:

  • s (String)

    the ISO8601 datetime

Returns:

  • (DateTime)


72
73
74
75
# File 'lib/collins/asset.rb', line 72

def format_date_string s
  parsed = DateTime.parse(s)
  parsed.strftime("%FT%T")
end

.from_json(json_hash, bare_asset = false) ⇒ Collins::Asset

Given a Hash deserialized from JSON, convert to an Asset

Parameters:

  • json_hash (Hash)

    Asset representation

  • bare_asset (Boolean) (defaults to: false)

    Exists for API compatability, largely not needed

Returns:

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/collins/asset.rb', line 52

def from_json json_hash, bare_asset = false
  (raise Collins::CollinsError.new("Invalid JSON specified for Asset.from_json")) if (json_hash.nil? || !json_hash.is_a?(Hash))
  json = deep_copy_hash json_hash
  json = if json["data"] then json["data"] else json end
  if bare_asset or !json.include?("ASSET") then
    asset = Collins::Asset.new json
  else
    asset = Collins::Asset.new json.delete("ASSET")
  end
  asset.send('ipmi='.to_sym, Collins::Ipmi.from_json(json.delete("IPMI")))
  asset.send('addresses='.to_sym, Collins::Address.from_json(json.delete("ADDRESSES")))
  asset.send('power='.to_sym, Collins::Power.from_json(json.delete("POWER")))
  asset.send('location=', json.delete("LOCATION"))
  asset.send('extras=', json)
  asset
end

Instance Method Details

#backend_addressCollins::Address, NilClass

Returns First available backend address.

Returns:



111
112
113
# File 'lib/collins/asset.rb', line 111

def backend_address
  backend_addresses.first if backend_address?
end

#backend_address?Boolean

Returns True if asset has a backend address.

Returns:

  • (Boolean)

    True if asset has a backend address



115
116
117
# File 'lib/collins/asset.rb', line 115

def backend_address?
  backend_addresses.length > 0
end

#backend_addressesArray<Collins::Address>

Returns Array of backend addresses.

Returns:



119
120
121
# File 'lib/collins/asset.rb', line 119

def backend_addresses
  addresses.select{|a| a.is_private?}
end

#backend_ip_addressString, NilClass

Deprecated.

Users are encouraged to use #backend_address

Returns Address of first available backend address.

Returns:

  • (String, NilClass)

    Address of first available backend address



125
126
127
# File 'lib/collins/asset.rb', line 125

def backend_ip_address
  backend_address.address if backend_address?
end

#backend_ip_addressesArray<String>

Deprecated.

Users are encouraged to uses #backend_addresses

Returns Backend IP addresses.

Returns:

  • (Array<String>)

    Backend IP addresses



130
131
132
# File 'lib/collins/asset.rb', line 130

def backend_ip_addresses
  backend_addresses.map{|a| a.address}
end

#backend_netmaskString, NilClass

Returns Netmask of first available backend address.

Returns:

  • (String, NilClass)

    Netmask of first available backend address



159
160
161
# File 'lib/collins/asset.rb', line 159

def backend_netmask
  backend_address.netmask if backend_address?
end

#backend_netmasksArray<String>

Returns Array of backend netmasks.

Returns:

  • (Array<String>)

    Array of backend netmasks



163
164
165
# File 'lib/collins/asset.rb', line 163

def backend_netmasks
  backend_addresses.map{|i| i.netmask}
end

#cpu_countFixnum

Returns Number of CPU’s found.

Returns:

  • (Fixnum)

    Number of CPU’s found



188
189
190
# File 'lib/collins/asset.rb', line 188

def cpu_count
  (extract(extras, "HARDWARE", "CPU") || []).length
end

#cpusArray<Hash>

Returns CPU information.

Returns:

  • (Array<Hash>)

    CPU information



192
193
194
# File 'lib/collins/asset.rb', line 192

def cpus
  extract(extras, "HARDWARE", "CPU") || []
end

#disksArray<Hash>

Returns Disk information.

Returns:

  • (Array<Hash>)

    Disk information



206
207
208
# File 'lib/collins/asset.rb', line 206

def disks
  extract(extras, "HARDWARE", "DISK") || []
end

#gateway_address(pool = "default") ⇒ String

Note:

If there is no address in the specified pool, the gateway of the first usable address is

Return the gateway address for the specified pool, or the first gateway used, which may not be desired.

Parameters:

  • pool (String) (defaults to: "default")

    The address pool to find a gateway on

Returns:

  • (String)

    Gateway address, or nil



172
173
174
175
176
177
178
179
180
# File 'lib/collins/asset.rb', line 172

def gateway_address pool = "default"
  address = addresses.select{|a| a.pool == pool}.map{|a| a.gateway}.first
  return address if address
  if addresses.length > 0 then
    addresses.first.gateway
  else
    nil
  end
end

#get_attribute(name) ⇒ Object, NilClass

Returns See #method_missing.

Returns:

  • (Object, NilClass)

    See #method_missing



183
184
185
# File 'lib/collins/asset.rb', line 183

def get_attribute name
  extract(extras, "ATTRIBS", "0", name.to_s.upcase)
end

#gpu_countFixnum

Returns Number of GPU’s found.

Returns:

  • (Fixnum)

    Number of GPU’s found



197
198
199
# File 'lib/collins/asset.rb', line 197

def gpu_count
  (extract(extras, "HARDWARE", "GPU") || []).length
end

#gpusArray<Hash>

Returns GPU information.

Returns:

  • (Array<Hash>)

    GPU information



201
202
203
# File 'lib/collins/asset.rb', line 201

def gpus
  extract(extras, "HARDWARE", "GPU") || []
end

#mac_addressesArray<String>

Returns MAC addresses associated with assets.

Returns:

  • (Array<String>)

    MAC addresses associated with assets



223
224
225
# File 'lib/collins/asset.rb', line 223

def mac_addresses
  nics.map{|n| n["MAC_ADDRESS"]}.select{|a| !a.nil?}
end

#memoryArray<Hash>

Returns Memory information.

Returns:

  • (Array<Hash>)

    Memory information



210
211
212
# File 'lib/collins/asset.rb', line 210

def memory
  extract(extras, "HARDWARE", "MEMORY") || []
end

#nicsArray<Hash>

Returns NIC information.

Returns:

  • (Array<Hash>)

    NIC information



215
216
217
# File 'lib/collins/asset.rb', line 215

def nics
  extract(extras, "HARDWARE", "NIC") || []
end

#physical_nic_countFixnum

Returns Number of physical interfaces.

Returns:

  • (Fixnum)

    Number of physical interfaces



219
220
221
# File 'lib/collins/asset.rb', line 219

def physical_nic_count
  nics.length
end

#public_addressCollins::Address, NilClass

Returns First available public address.

Returns:



135
136
137
# File 'lib/collins/asset.rb', line 135

def public_address
  public_addresses.first if public_address?
end

#public_address?Boolean

Returns True if asset has a public address.

Returns:

  • (Boolean)

    True if asset has a public address



139
140
141
# File 'lib/collins/asset.rb', line 139

def public_address?
  public_addresses.length > 0
end

#public_addressesArray<Collins::Address>

Returns Array of public addresses.

Returns:



143
144
145
# File 'lib/collins/asset.rb', line 143

def public_addresses
  addresses.select{|a| a.is_public?}
end

#public_ip_addressString, NilClass

Deprecated.

Users are encouraged to use #public_address

Returns Address of first available public address.

Returns:

  • (String, NilClass)

    Address of first available public address



149
150
151
# File 'lib/collins/asset.rb', line 149

def public_ip_address
  public_address.address if public_address?
end

#public_ip_addressesArray<String>

Deprecated.

Users are encouraged to uses #public_addresses

Returns Public IP addresses.

Returns:

  • (Array<String>)

    Public IP addresses



154
155
156
# File 'lib/collins/asset.rb', line 154

def public_ip_addresses
  public_addresses.map{|a| a.address}
end

#respond_to?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


235
236
237
238
239
240
241
# File 'lib/collins/asset.rb', line 235

def respond_to? name, include_all=false
  if extract(extras, "ATTRIBS", "0", name.to_s.upcase).nil? then
    super
  else
    true
  end
end

#to_sString

Returns Human readable asset with no meta attributes.

Returns:

  • (String)

    Human readable asset with no meta attributes



228
229
230
231
232
233
# File 'lib/collins/asset.rb', line 228

def to_s
  updated_t = format_datetime(updated, "Never")
  created_t = format_datetime(created, "Never")
  ipmi_i = ipmi.nil? ? "No IPMI Data" : ipmi.to_s
  "Asset(id = #{id}, tag = #{tag}, status = #{status}, type = #{type}, created = #{created_t}, updated = #{updated_t}, ipmi = #{ipmi_i}, state = #{state.to_s})"
end