Module: Rancher::Api::Helpers::Model

Included in:
Environment, Rancher::Api::Host, Instance, Ipaddress, Machine, Project, Registry, Registrycredential, Service
Defined in:
lib/rancher/api/helpers/model.rb

Defined Under Namespace

Classes: RancherActionNotAvailableError, RancherModelError, RancherWaitTimeOutError

Constant Summary collapse

TIMEOUT_LIMIT =
900

Instance Method Summary collapse

Instance Method Details

#handle_response(response) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rancher/api/helpers/model.rb', line 27

def handle_response(response)
  case response
  when Her::Collection
    response
  when Her::Model
    raise RancherModelError, response.inspect if response.type.eql?('error')
    response
  else
    raise RancherModelError, response.inspect
  end
end

#reloadObject



17
18
19
# File 'lib/rancher/api/helpers/model.rb', line 17

def reload
  assign_attributes(self.class.find(id).attributes)
end

#run(action, data: {}) ⇒ Object



21
22
23
24
25
# File 'lib/rancher/api/helpers/model.rb', line 21

def run(action, data: {})
  url = actions[action.to_s]
  raise RancherActionNotAvailableError, "Available actions: '#{actions.inspect}'" if url.blank?
  handle_response(self.class.post(url, data))
end

#self_urlObject



13
14
15
# File 'lib/rancher/api/helpers/model.rb', line 13

def self_url
  links['self']
end

#wait_for_state(desired_state) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rancher/api/helpers/model.rb', line 39

def wait_for_state(desired_state)
  EM.run do
    EM.add_timer(TIMEOUT_LIMIT) do
      raise RancherWaitTimeOutError, "Timeout while waiting for transition to: #{desired_state}"
    end
    EM.tick_loop do
      reload
      current_state = state
      if current_state.eql?(desired_state.to_s)
        Logger.log.info "state changed from: #{current_state} => #{desired_state}"
        EM.stop
      else
        Logger.log.info "waiting for state change: #{current_state} => #{desired_state}"
        sleep(1)
      end
    end
  end
end