Class: Larrow::Qingcloud::Base

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/larrow/qingcloud/base.rb

Overview

base class for Qingcloud model

Direct Known Subclasses

Eip, Image, Instance, KeyPair, Snapshot

Constant Summary collapse

DESTROY_STATUSES =
[:deleted, :ceased,:released]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

#debug, #err, included, #info

Constructor Details

#initialize(id, options = {}) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
# File 'lib/larrow/qingcloud/base.rb', line 11

def initialize(id,options={})
  self.id = id
  options.each_pair do |k,v|
    self.send "#{k}=",v
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

just for state access, such as:

model.running?

do not affect ‘respond_to?`



58
59
60
61
62
63
64
# File 'lib/larrow/qingcloud/base.rb', line 58

def method_missing(method, *args, &block)
  if method.to_s.last == '?'
    status == method.to_s[0..-2].to_sym
  else
    super
  end
end

Instance Attribute Details

#delegatorObject

Returns the value of attribute delegator.



7
8
9
# File 'lib/larrow/qingcloud/base.rb', line 7

def delegator
  @delegator
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/larrow/qingcloud/base.rb', line 7

def id
  @id
end

#statusObject

Returns the value of attribute status.



7
8
9
# File 'lib/larrow/qingcloud/base.rb', line 7

def status
  @status
end

Class Method Details

.connObject



51
52
53
# File 'lib/larrow/qingcloud/base.rb', line 51

def self.conn
  Qingcloud.connection
end

.describe(ids, params) ⇒ Object

convert hash data to object when block given



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/larrow/qingcloud/base.rb', line 89

def self.describe(ids, params)
  params = param_by(ids, params)
  datas = conn.service(
    'get', "Describe#{model_name}s", params
  )["#{singular_name}_set"]
  if block_given?
    datas.map do |data|
      yield data
    end
  else
    datas
  end
end

.destroy_action(action) ⇒ Object

destroy method generator

destroy can be called by end user, so it will return a future



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/larrow/qingcloud/base.rb', line 106

def self.destroy_action(action)
  define_method :destroy do
    params = self.class.param_by [id]
    future(timeout:90) do
      loop do
        begin
          result = conn.get action, params
          info "destroy #{self.class.name}: #{result}"
          break result['ret_code'] == 0
        rescue ServiceError => e
          if e.message =~ /has already been deleted/
            break :already_deleted
          else
            sleep 2
          end
        end
      end
    end
  end
  define_method :ensure_destroy do
    params = self.class.param_by [id]
    future(timeout:90) do
      loop do
        break true if DESTROY_STATUSES.include?(@status) 
        conn.get action, params rescue nil
        sleep 2
        @status = show['status'].to_sym
      end
    end
  end
end

.model_nameObject

multi names( used as param_name ) KeyPair -> keypair -> keypairs



68
69
70
# File 'lib/larrow/qingcloud/base.rb', line 68

def self.model_name
  name.split(/::/).last
end

.param_by(ids, init_params = {}) ⇒ Object



82
83
84
85
86
# File 'lib/larrow/qingcloud/base.rb', line 82

def self.param_by(ids, init_params={})
  ids.each_with_index.reduce(init_params) do |result, (id, index)|
    result.update :"#{plural_name}.#{index + 1}" => id
  end
end

.plural_nameObject



74
75
76
# File 'lib/larrow/qingcloud/base.rb', line 74

def self.plural_name
  "#{singular_name}s"
end

.singular_nameObject



71
72
73
# File 'lib/larrow/qingcloud/base.rb', line 71

def self.singular_name
  name.split(/::/).last.downcase
end

Instance Method Details

#connObject



23
24
25
# File 'lib/larrow/qingcloud/base.rb', line 23

def conn
  self.class.conn
end

#model_nameObject



27
28
29
# File 'lib/larrow/qingcloud/base.rb', line 27

def model_name
  self.class.model_name
end

#param_by(*args) ⇒ Object



78
79
80
# File 'lib/larrow/qingcloud/base.rb', line 78

def param_by(*args)
  self.class.param_by(*args)
end

#show(params = {}) ⇒ Object



31
32
33
# File 'lib/larrow/qingcloud/base.rb', line 31

def show(params = {})
  self.class.describe([self.id],params).first
end

#wait_for(status) ⇒ Object

block method, should be delayed at caller function



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/larrow/qingcloud/base.rb', line 36

def wait_for(status)
  loop do
    data = show
    if data['status'].to_sym == status
      info "#{model_name} status changed: #{id} - #{status}"
      self.status = status
      yield data if block_given?
      break self
    else
      debug "#{model_name} wait for status: #{id} - #{data['status']}"
    end
    sleep 2
  end
end