Class: IBM::Cloud::SDK::VPC::Instance

Inherits:
VPCInstance show all
Defined in:
lib/ibm/cloud/sdk/vpc/instances.rb

Overview

Work with a single instance.

Constant Summary collapse

TRANSITIONAL_STATES =
%w[pausing pending restarting resuming starting stopping].freeze
ERROR_STATE =
'failed'
RUNNING_STATE =
'running'
STOPPED_STATES =
%w[stopped paused].freeze

Instance Attribute Summary

Attributes included from IBM::Cloud::SDKHTTP::BaseHTTPMixin

#endpoint

Attributes inherited from IBM::Cloud::SDKHTTP::BaseInstance

#connection, #endpoint, #logger, #token

Instance Method Summary collapse

Methods inherited from VPCInstance

#crn, #tags

Methods included from VpcHTTP

#metadata

Methods included from IBM::Cloud::SDKHTTP::BaseHTTPMixin

#adhoc, #delete, #get, #metadata, #patch, #post, #put, #unchecked_response, #url

Methods inherited from IBM::Cloud::SDKHTTP::BaseInstance

#details, #initialize, #refresh, #remove, #update

Constructor Details

This class inherits a constructor from IBM::Cloud::SDKHTTP::BaseInstance

Instance Method Details

#actionsINSTANCE::Actions

Interact with instance actions.

Returns:



87
88
89
# File 'lib/ibm/cloud/sdk/vpc/instances.rb', line 87

def actions
  INSTANCE::Actions.new(self)
end

#failed?Boolean

Whether the state of the VM is in failed state.

Returns:

  • (Boolean)


63
64
65
# File 'lib/ibm/cloud/sdk/vpc/instances.rb', line 63

def failed?
  status == ERROR_STATE
end

#idObject

The id of this VM.



47
48
49
# File 'lib/ibm/cloud/sdk/vpc/instances.rb', line 47

def id
  @data[:id]
end

#initializationObject

Return the data used for initializing this VM.



104
105
106
# File 'lib/ibm/cloud/sdk/vpc/instances.rb', line 104

def initialization
  adhoc(method: 'get', path: 'initialization').json
end

#nameObject

The name of this VM.



52
53
54
# File 'lib/ibm/cloud/sdk/vpc/instances.rb', line 52

def name
  @data[:name]
end

#network_interfacesINSTANCE::NetworkInterfaces

Interact with instance network interfaces.



93
94
95
# File 'lib/ibm/cloud/sdk/vpc/instances.rb', line 93

def network_interfaces
  INSTANCE::NetworkInterfaces.new(self)
end

#started?Boolean

Whether the state of the VM is in the started state.

Returns:

  • (Boolean)


69
70
71
# File 'lib/ibm/cloud/sdk/vpc/instances.rb', line 69

def started?
  status == RUNNING_STATE
end

#statusObject

The status of the virtual server instance. Possible values: [failed,paused,pausing,pending,restarting,resuming,running,starting,stopped,stopping]



57
58
59
# File 'lib/ibm/cloud/sdk/vpc/instances.rb', line 57

def status
  @data[:status]
end

#stopped?Boolean

Whether the state of the VM is in a stopped or paused state.

Returns:

  • (Boolean)


75
76
77
# File 'lib/ibm/cloud/sdk/vpc/instances.rb', line 75

def stopped?
  STOPPED_STATES.include?(status)
end

#transitional?Boolean

Whether the state of the VM is in a transitional state.

Returns:

  • (Boolean)


81
82
83
# File 'lib/ibm/cloud/sdk/vpc/instances.rb', line 81

def transitional?
  TRANSITIONAL_STATES.include?(status)
end

#volume_attachmentsINSTANCE::VolumeAttachments

Interact with instance volume attachements.



99
100
101
# File 'lib/ibm/cloud/sdk/vpc/instances.rb', line 99

def volume_attachments
  INSTANCE::VolumeAttachments.new(self)
end

#wait_for!(sleep_time: 5, timeout: 600, &block) ⇒ Object

Wait for the VM instance to be in a stable state.

Parameters:

  • sleep_time (Integer) (defaults to: 5)

    The time to sleep between refreshes.

  • timeout (Integer) (defaults to: 600)

    The number of seconds before raising an error.

  • block (Proc)

    A block to test against. Must return a boolean.

Raises:

  • (RuntimeError)

    Instance goes into failed state.

  • (RuntimeError)

    Timeout has been reached.



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ibm/cloud/sdk/vpc/instances.rb', line 114

def wait_for!(sleep_time: 5, timeout: 600, &block)
  @logger.info("Starting wait for instance #{id}. Starts in state #{status}.")
  loop do
    refresh
    raise "VM #{id} is in a failed state." if failed?
    break if block.call(self)

    timeout = sleep_counter(sleep_time, timeout)
    raise "Time out while waiting #{id} to be stable." if timeout <= 0
  end
  @logger.info("Finished wait for instance #{id}. Ends in state #{status}.")
end

#wait_for_started!(sleep_time: 5, timeout: 600) ⇒ Object

Wait for the VM instance to be have a started status.

Parameters:

  • sleep_time (Integer) (defaults to: 5)

    The time to sleep between refreshes.

  • timeout (Integer) (defaults to: 600)

    The number of seconds before raising an error.

Raises:

  • (RuntimeError)

    Instance goes into failed state.

  • (RuntimeError)

    Timeout has been reached.



132
133
134
# File 'lib/ibm/cloud/sdk/vpc/instances.rb', line 132

def wait_for_started!(sleep_time: 5, timeout: 600)
  wait_for!(sleep_time: sleep_time, timeout: timeout, &:started?)
end

#wait_for_stopped!(sleep_time: 5, timeout: 600) ⇒ Object

Wait for the VM instance to be have a stopped status.

Parameters:

  • sleep_time (Integer) (defaults to: 5)

    The time to sleep between refreshes.

  • timeout (Integer) (defaults to: 600)

    The number of seconds before raising an error.

Raises:

  • (RuntimeError)

    Instance goes into failed state.

  • (RuntimeError)

    Timeout has been reached.



141
142
143
# File 'lib/ibm/cloud/sdk/vpc/instances.rb', line 141

def wait_for_stopped!(sleep_time: 5, timeout: 600)
  wait_for!(sleep_time: sleep_time, timeout: timeout, &:stopped?)
end