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.



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

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:) ⇒ MicroVM

Build a MicroVM resource from an SDK response.

Parameters:

  • client (Client)

    lifecycle client

  • response (Object)

    SDK response

Returns:



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

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) ⇒ String?

Request an auth token for direct endpoint access.

Parameters:

  • ports (Array<Integer>, nil) (defaults to: nil)

    optional allowed ports

  • ttl_seconds (Integer, nil) (defaults to: nil)

    optional token lifetime

  • params (Hash)

    additional token request parameters

Returns:

  • (String, nil)

    auth token value



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

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) ⇒ Endpoint

Build an endpoint client for this MicroVM.

Parameters:

  • token (String, nil) (defaults to: nil)

    existing auth token

  • token_params (Hash)

    parameters used when requesting a token

Returns:

Raises:



86
87
88
89
90
91
# File 'lib/lambda/microvms/microvm.rb', line 86

def endpoint(token: nil, **token_params)
  url = endpoint_url
  raise EndpointError.new('MicroVM endpoint URL is unavailable', status: 0, body: nil) unless url

  Endpoint.new(url: url, token: token || auth_token(**token_params))
end

#endpoint_urlString?

Extract the direct endpoint URL from the current MicroVM data.

Returns:

  • (String, nil)


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

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) ⇒ Hash, ...

Send a GET request to the MicroVM endpoint.

Parameters:

  • path (String)

    endpoint path

  • token (String, nil) (defaults to: nil)

    existing auth token

Returns:

  • (Hash, String, nil)

    endpoint response



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

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

#pending?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/lambda/microvms/microvm.rb', line 53

def pending?
  state == :pending
end

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

Send a POST request to the MicroVM endpoint.

Parameters:

  • path (String)

    endpoint path

  • json (Hash, Array, nil) (defaults to: nil)

    JSON body to encode

  • body (String, nil) (defaults to: nil)

    raw request body

  • token (String, nil) (defaults to: nil)

    existing auth token

Returns:

  • (Hash, String, nil)

    endpoint response



109
110
111
# File 'lib/lambda/microvms/microvm.rb', line 109

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

#refreshself

Refresh MicroVM data from the service.

Returns:

  • (self)


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

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

#resume(**params) ⇒ self

Resume this MicroVM and update local data when the service returns a response.

Parameters:

  • params (Hash)

    additional resume parameters

Returns:

  • (self)


126
127
128
129
130
# File 'lib/lambda/microvms/microvm.rb', line 126

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

#running?Boolean

Returns:

  • (Boolean)


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

def running?
  state == :running
end

#stateSymbol

Current normalized MicroVM state.

Returns:

  • (Symbol)


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

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

#suspend(**params) ⇒ self

Suspend this MicroVM.

Parameters:

  • params (Hash)

    additional suspend parameters

Returns:

  • (self)


117
118
119
120
# File 'lib/lambda/microvms/microvm.rb', line 117

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

#suspended?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/lambda/microvms/microvm.rb', line 45

def suspended?
  state == :suspended
end

#terminate(**params) ⇒ self

Terminate this MicroVM.

Parameters:

  • params (Hash)

    additional terminate parameters

Returns:

  • (self)


136
137
138
139
# File 'lib/lambda/microvms/microvm.rb', line 136

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

#terminated?Boolean

Returns:

  • (Boolean)


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

def terminated?
  state == :terminated
end

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

Wait until this MicroVM is running.

Parameters:

  • delay (Numeric) (defaults to: Waiter::DEFAULT_DELAY)

    polling delay in seconds

  • timeout (Numeric) (defaults to: Waiter::DEFAULT_TIMEOUT)

    maximum wait in seconds

Returns:

  • (self)


146
147
148
149
150
151
# File 'lib/lambda/microvms/microvm.rb', line 146

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) ⇒ self

Wait until this MicroVM is suspended.

Parameters:

  • delay (Numeric) (defaults to: Waiter::DEFAULT_DELAY)

    polling delay in seconds

  • timeout (Numeric) (defaults to: Waiter::DEFAULT_TIMEOUT)

    maximum wait in seconds

Returns:

  • (self)


158
159
160
161
162
163
# File 'lib/lambda/microvms/microvm.rb', line 158

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) ⇒ self

Wait until this MicroVM is terminated.

Parameters:

  • delay (Numeric) (defaults to: Waiter::DEFAULT_DELAY)

    polling delay in seconds

  • timeout (Numeric) (defaults to: Waiter::DEFAULT_TIMEOUT)

    maximum wait in seconds

Returns:

  • (self)


170
171
172
173
174
175
# File 'lib/lambda/microvms/microvm.rb', line 170

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