Class: CollinsShell::Console::Asset

Inherits:
Object
  • Object
show all
Includes:
CommandHelpers
Defined in:
lib/collins_shell/console/asset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandHelpers

#asset_context?, #asset_exists?, #call_collins, #cli_options, #collins_client, #find_assets, #find_one_asset, #get_all_tags, #get_asset, #get_tag_values, #resolve_asset_tag, #shell_handle, #tag_from_stack, #virtual_tags

Methods included from Cache

#cache_get_or_else, #clear_cache

Constructor Details

#initialize(asset) ⇒ Asset

Returns a new instance of Asset.



6
7
8
9
# File 'lib/collins_shell/console/asset.rb', line 6

def initialize asset
  @tag = asset
  @asset_client = collins_client.with_asset(@tag)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



101
102
103
104
105
106
107
108
109
# File 'lib/collins_shell/console/asset.rb', line 101

def method_missing meth, *args, &block
  if meth.to_sym == :asset then
    underlying
  elsif @asset_client.respond_to?(meth) then
    @asset_client.send(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#tagObject (readonly)

Returns the value of attribute tag.



5
6
7
# File 'lib/collins_shell/console/asset.rb', line 5

def tag
  @tag
end

Instance Method Details

#key(key = nil) ⇒ Object



81
82
83
84
# File 'lib/collins_shell/console/asset.rb', line 81

def key key = nil
  (raise "key requires a key. key <key>") if key.nil?
  @asset_client.get.send(key.to_sym)
end

#key?(key = nil) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/collins_shell/console/asset.rb', line 77

def key? key = nil
  (raise "key? requires a key. key? <key>") if key.nil?
  @asset_client.get.send("#{key}?".to_sym)
end

#log!(msg, level = nil) ⇒ Object



50
51
52
53
# File 'lib/collins_shell/console/asset.rb', line 50

def log! msg, level = nil
  (raise "log requires a message. log <message>") if (msg.nil? || msg.to_s.empty?)
  @asset_client.log!(msg, level)
end

#logs(options = {}) ⇒ Object



54
55
56
# File 'lib/collins_shell/console/asset.rb', line 54

def logs options = {}
  @asset_client.logs(options)
end

#on?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/collins_shell/console/asset.rb', line 85

def on?
  power? == "on"
end

#power!(action = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/collins_shell/console/asset.rb', line 10

def power! action = nil
  Collins::Option(action).map do |action|
    action = Collins::Power.normalize_action(action)
    verifying_response("power #{action}") {
      @asset_client.power!(action)
    }
  end.get_or_else {
    cput("A power action argument is required. power <action>")
  }
end

#power?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/collins_shell/console/asset.rb', line 88

def power?
  @asset_client.power_status
end

#reboot!(how = "rebootSoft") ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/collins_shell/console/asset.rb', line 20

def reboot! how = "rebootSoft"
  Collins::Option(how).map do |how|
    action = Collins::Power.normalize_action(how)
    verifying_response("reboot") {
      @asset_client.power!(action)
    }
  end.get_or_else {
    cput("A reboot argument is required. reboot <action>")
  }
end

#respond_to?(meth, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
# File 'lib/collins_shell/console/asset.rb', line 91

def respond_to? meth, include_private = false
  if meth.to_sym == :asset then
    true
  elsif @asset_client.respond_to?(meth) then
    true
  else
    super
  end
end

#rm!(key = nil, group_id = nil) ⇒ Object



71
72
73
74
75
76
# File 'lib/collins_shell/console/asset.rb', line 71

def rm! key = nil, group_id = nil
  (raise "rm requires a key. rm <key>") if key.nil?
  verifying_response("delete the key '#{key}' on") {
    @asset_client.delete_attribute!(key, group_id)
  }
end

#set!(key = nil, value = nil, group_id = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/collins_shell/console/asset.rb', line 57

def set! key = nil, value = nil, group_id = nil
  msg = "set requires a %s. set <key>, <value>"
  (raise sprintf(msg, "key")) if key.nil?
  (raise sprintf(msg, "value")) if value.nil?
  case value
  when String, Symbol, Fixnum, TrueClass, FalseClass then
    value = value.to_s
  else
    raise "value can't be a #{value.class}"
  end
  verifying_response("set the key '#{key}' to '#{value}' on") {
    @asset_client.set_attribute!(key.to_s, value, group_id)
  }
end

#set_status!(status = nil, reason = nil) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/collins_shell/console/asset.rb', line 42

def set_status! status = nil, reason = nil
  msg = "set_status request a %s. set_status <status>, <reason>"
  (raise sprintf(msg, "status")) if status.nil?
  (raise sprintf(msg, "reason")) if reason.nil?
  verifying_response("set the status to '#{status}' on") {
    @asset_client.set_status!(status, reason)
  }
end

#statObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/collins_shell/console/asset.rb', line 30

def stat
  s = <<-STAT
     Asset: #{underlying.tag}
    Status: #{underlying.status}
      Type: #{underlying.type}
  Hostname: #{Collins::Option(underlying.hostname).get_or_else("(none)")}
   Created: #{Collins::Option(underlying.created).get_or_else("(none)")}
   Updated: #{Collins::Option(underlying.updated).get_or_else("(none)")}
   Deleted: #{Collins::Option(underlying.deleted).get_or_else("(none)")}
  STAT
  cput(s)
end