Class: Lambda::MicroVMs::MicroVM

Inherits:
Object
  • Object
show all
Defined in:
lib/lambda/microvms/microvm.rb

Overview

Represents one running, suspended, or terminated Lambda MicroVM session.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, id:, data: nil) ⇒ MicroVM

Returns a new instance of MicroVM.



14
15
16
17
18
# File 'lib/lambda/microvms/microvm.rb', line 14

def initialize(client:, id:, data: nil)
  @client = client
  @id = id
  @data = data
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/lambda/microvms/microvm.rb', line 7

def client
  @client
end

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/lambda/microvms/microvm.rb', line 7

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/lambda/microvms/microvm.rb', line 7

def id
  @id
end

Class Method Details

.from_response(client:, response:) ⇒ Object



9
10
11
12
# File 'lib/lambda/microvms/microvm.rb', line 9

def self.from_response(client:, response:)
  id = Util.extract(response, :microvm_id, :microvm_arn, :id, :arn)
  new(client: client, id: id, data: response)
end

Instance Method Details

#auth_token(ports: nil, ttl_seconds: nil, **params) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/lambda/microvms/microvm.rb', line 53

def auth_token(ports: nil, ttl_seconds: nil, **params)
  request = params.merge(microvm_id: id)
  request[:ports] = ports if ports
  request[:ttl_seconds] = ttl_seconds if ttl_seconds
  response = client.create_auth_token(**request)
  Util.extract(response, :token, :auth_token, :microvm_auth_token)
end

#endpoint(token: nil, **token_params) ⇒ Object



61
62
63
# File 'lib/lambda/microvms/microvm.rb', line 61

def endpoint(token: nil, **token_params)
  Endpoint.new(url: endpoint_url, token: token || auth_token(**token_params))
end

#endpoint_urlObject



46
47
48
49
50
51
# File 'lib/lambda/microvms/microvm.rb', line 46

def endpoint_url
  endpoint = Util.extract(@data, :endpoint, :endpoint_url, :url)
  return endpoint if endpoint.is_a?(String)

  Util.extract(endpoint, :url, :endpoint_url) if endpoint
end

#get(path, token: nil) ⇒ Object



65
66
67
# File 'lib/lambda/microvms/microvm.rb', line 65

def get(path, token: nil, **)
  endpoint(token: token).get(path, **)
end

#pending?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/lambda/microvms/microvm.rb', line 42

def pending?
  state == :pending
end

#post(path, json: nil, body: nil, token: nil) ⇒ Object



69
70
71
# File 'lib/lambda/microvms/microvm.rb', line 69

def post(path, json: nil, body: nil, token: nil, **)
  endpoint(token: token).post(path, json: json, body: body, **)
end

#refreshObject



20
21
22
23
24
# File 'lib/lambda/microvms/microvm.rb', line 20

def refresh
  fresh = client.get_microvm(microvm_id: id)
  @data = fresh.data
  self
end

#resume(**params) ⇒ Object



78
79
80
81
82
# File 'lib/lambda/microvms/microvm.rb', line 78

def resume(**params)
  response = client.resume_microvm(**params, microvm_id: id)
  @data = response if response
  self
end

#running?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/lambda/microvms/microvm.rb', line 30

def running?
  state == :running
end

#stateObject



26
27
28
# File 'lib/lambda/microvms/microvm.rb', line 26

def state
  Util.normalize_state(Util.extract(@data, :state, :status, :microvm_state))
end

#suspend(**params) ⇒ Object



73
74
75
76
# File 'lib/lambda/microvms/microvm.rb', line 73

def suspend(**params)
  client.suspend_microvm(**params, microvm_id: id)
  self
end

#suspended?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/lambda/microvms/microvm.rb', line 34

def suspended?
  state == :suspended
end

#terminate(**params) ⇒ Object



84
85
86
87
# File 'lib/lambda/microvms/microvm.rb', line 84

def terminate(**params)
  client.terminate_microvm(**params, microvm_id: id)
  self
end

#terminated?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/lambda/microvms/microvm.rb', line 38

def terminated?
  state == :terminated
end

#wait_until_running(delay: Waiter::DEFAULT_DELAY, timeout: Waiter::DEFAULT_TIMEOUT) ⇒ Object



89
90
91
92
93
94
# File 'lib/lambda/microvms/microvm.rb', line 89

def wait_until_running(delay: Waiter::DEFAULT_DELAY, timeout: Waiter::DEFAULT_TIMEOUT)
  Waiter.new(delay: delay, timeout: timeout).wait(message: "MicroVM #{id} to be running") do
    refresh.running?
  end
  self
end

#wait_until_suspended(delay: Waiter::DEFAULT_DELAY, timeout: Waiter::DEFAULT_TIMEOUT) ⇒ Object



96
97
98
99
100
101
# File 'lib/lambda/microvms/microvm.rb', line 96

def wait_until_suspended(delay: Waiter::DEFAULT_DELAY, timeout: Waiter::DEFAULT_TIMEOUT)
  Waiter.new(delay: delay, timeout: timeout).wait(message: "MicroVM #{id} to be suspended") do
    refresh.suspended?
  end
  self
end

#wait_until_terminated(delay: Waiter::DEFAULT_DELAY, timeout: Waiter::DEFAULT_TIMEOUT) ⇒ Object



103
104
105
106
107
108
# File 'lib/lambda/microvms/microvm.rb', line 103

def wait_until_terminated(delay: Waiter::DEFAULT_DELAY, timeout: Waiter::DEFAULT_TIMEOUT)
  Waiter.new(delay: delay, timeout: timeout).wait(message: "MicroVM #{id} to be terminated") do
    refresh.terminated?
  end
  self
end