Class: McInstance

Inherits:
Object
  • Object
show all
Extended by:
RightScale::Api::GatewayExtend, RightScale::Api::McTaggableExtend
Includes:
RightScale::Api::Gateway, RightScale::Api::McInput, RightScale::Api::McTaggable
Defined in:
lib/rest_connection/rightscale/mc_instance.rb

Overview

API 1.5

Instance Attribute Summary collapse

Attributes included from RightScale::Api::Base

#params

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RightScale::Api::GatewayExtend

create, filters, find, find_all, find_by, find_with_filter, load, load_all, parse_args, resource_post_name

Methods included from RightScale::Api::GatewayConnection

#connection

Methods included from RightScale::Api::BaseExtend

#[], #create, #deny_methods, #filters, #find, #find_all, #find_by, #find_by_cloud_id, #find_by_id, #find_by_nickname, #find_by_nickname_speed, #find_with_filter

Methods included from RightScale::Api::BaseConnection

#connection

Methods included from RightScale::Api::McTaggableExtend

find_by_tags

Methods included from RightScale::Api::McInput

#get_inputs

Methods included from RightScale::Api::McTaggable

#add_tags, #remove_tags, #tags

Methods included from RightScale::Api::Taggable

#add_tags, #clear_tags, #get_info_tags, #get_tags_by_namespace, #remove_info_tags, #remove_tags, #remove_tags_by_namespace, #set_info_tags, #set_tags_by_namespace, #set_tags_to, #tags

Methods included from RightScale::Api::Gateway

#[], #[]=, #actions, #hash_of_links, #href, #initialize, #load, #method_missing, #nickname, #parse_params, #rediscover

Methods included from RightScale::Api::Base

#[], #[]=, #destroy, #initialize, #method_missing, #reload, #rs_id

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RightScale::Api::Gateway

Instance Attribute Details

#monitoring_metricsObject

Returns the value of attribute monitoring_metrics.



33
34
35
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 33

def monitoring_metrics
  @monitoring_metrics
end

Class Method Details

.filtersObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 57

def self.filters
  [
    :datacenter_href,
    :deployment_href,
    :name,
    :os_platform,
    :parent_href,
    :private_dns_name,
    :private_ip_address,
    :public_dns_name,
    :public_ip_address,
    :resource_uid,
    :server_template_href,
    :state
  ]
end

.parse_args(cloud_id) ⇒ Object



53
54
55
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 53

def self.parse_args(cloud_id)
  "clouds/#{cloud_id}/"
end

.resource_plural_nameObject



45
46
47
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 45

def self.resource_plural_name
  "instances"
end

.resource_singular_nameObject



49
50
51
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 49

def self.resource_singular_name
  "instance"
end

Instance Method Details

#fetch_monitoring_metricsObject



181
182
183
184
185
186
187
188
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 181

def fetch_monitoring_metrics
  @monitoring_metrics = []
  return @monitoring_metrics if self.state != "operational"
  connection.get(URI.parse(self.href).path + '/monitoring_metrics').each { |mm|
    @monitoring_metrics << MonitoringMetric.new(mm)
  }
  @monitoring_metrics
end

#get_data(params) ⇒ Object



196
197
198
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 196

def get_data(params)
  get_sketchy_data(params)
end

#get_sketchy_data(params) ⇒ Object



190
191
192
193
194
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 190

def get_sketchy_data(params)
  metric = fetch_monitoring_metrics.detect { |mm| mm.plugin == params['plugin_name'] and mm.view == params['plugin_type'] }
  raise "Metric not found!" unless metric
  metric.data(params['start'], params['end'])
end

#launchObject



120
121
122
123
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 120

def launch
  inst_href = URI.parse(self.href)
  connection.post(inst_href.path + '/launch')
end

#map_security_groups(to, sg_ary) ⇒ Object



83
84
85
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 83

def map_security_groups(to, sg_ary)
  sg_ary.map { |hsh| hsh["href"] }
end

#map_user_data(to, user_data) ⇒ Object



87
88
89
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 87

def map_user_data(to, user_data)
  user_data
end

#multi_update(input_ary) ⇒ Object



130
131
132
133
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 130

def multi_update(input_ary)
  inst_href = URI.parse(self.href)
  connection.put(inst_href.path + '/inputs/multi_update', {'inputs' => input_ary})
end

#rebootObject



200
201
202
203
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 200

def reboot
  self.show
  connection.post(URI.parse(self.href).path + '/reboot')
end

#resource_plural_nameObject



37
38
39
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 37

def resource_plural_name
  "instances"
end

#resource_singular_nameObject



41
42
43
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 41

def resource_singular_name
  "instance"
end

#run_executable(executable, opts = nil, ignore_lock = false) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 154

def run_executable(executable, opts=nil, ignore_lock=false)
  run_options = Hash.new
  if executable.is_a?(Executable)
    if executable.recipe?
      run_options[:recipe_name] = executable.recipe
    else
      run_options[:right_script_href] = translate_href(executable.right_script.href)
    end
  elsif executable.is_a?(RightScript)
    run_options[:right_script_href] = translate_href(executable.href)
  else
    raise "Invalid class passed to run_executable, needs Executable or RightScript, was:#{executable.class}"
  end

  if not opts.nil? and opts.has_key?(:ignore_lock)
    run_options[:ignore_lock] = "true"
    opts.delete(:ignore_lock)
    opts = nil if opts.empty?
  end

  inst_href = URI.parse(self.href)
  run_options[:inputs] = transform_inputs(:to_a, opts) unless opts.nil?
  run_options[:ignore_lock] = "true" if ignore_lock
  location = connection.post(inst_href.path + '/run_executable', run_options)
  Task.new('href' => location)
end

#saveObject



79
80
81
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 79

def save
  update
end

#showObject



74
75
76
77
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 74

def show
  inst_href = URI.parse(self.href)
  @params.merge! connection.get(inst_href.path, 'view' => "full")
end

#terminateObject



125
126
127
128
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 125

def terminate
  inst_href = URI.parse(self.href)
  connection.post(inst_href.path + '/terminate')
end

#transform_inputs(sym, parameters) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 135

def transform_inputs(sym, parameters)
  ret = nil
  if parameters.is_a?(Array) and sym == :to_h
    ret = {}
    parameters.each { |hash| ret[hash['name']] = hash['value'] }
  elsif parameters.is_a?(Hash) and sym == :to_a
    ret = []
    parameters.each { |key,val| ret << {'name' => key, 'value' => val} }
  end
  ret
end

#translate_href(old_href) ⇒ Object



147
148
149
150
151
152
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 147

def translate_href(old_href)
  href = old_href.dup
  href.gsub!(/ec2_/,'')
  href.gsub!(/\/acct\/[0-9]*/,'')
  return href
end

#updateObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rest_connection/rightscale/mc_instance.rb', line 91

def update
  fields = [{"attr" => :datacenter,                                       "api" => :datacenter_href},
            {"attr" => :image,                                            "api" => :image_href},
            {"attr" => :instance_type,                                    "api" => :instance_type_href},
            {                                                             "api" => :kernel_image_href},
            {"attr" => :multi_cloud_image,                                "api" => :multi_cloud_image_href},
            {                                                             "api" => :ramdisk_image_href},
            {"attr" => :security_groups,    "fn" => :map_security_groups, "api" => :security_group_hrefs},
            {"attr" => :server_template,                                  "api" => :server_template_href},
            {"attr" => :ssh_key,                                          "api" => :ssh_key_href},
            {"attr" => :user_data,          "fn" => :map_user_data,       "api" => :user_data}]

  opts = {"instance" => {}}
  instance = opts["instance"]
  to = "api"
  from = "attr"
  fields.each { |hsh|
    next unless hsh[from]
    val = self[hsh[from]]
    if hsh["fn"]
      instance[hsh[to].to_s] = __send__(hsh["fn"], to, val) unless val.nil? || val.empty?
    else
      instance[hsh[to].to_s] = val unless val.nil? || val.empty?
    end
  }
  inst_href = URI.parse(self.href)
  connection.put(inst_href.path, opts)
end