Class: Lambda::MicroVMs::MicroVM
- Inherits:
-
Object
- Object
- Lambda::MicroVMs::MicroVM
- Defined in:
- lib/lambda/microvms/microvm.rb
Overview
Represents one running, suspended, or terminated Lambda MicroVM session.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.from_response(client:, response:) ⇒ MicroVM
Build a MicroVM resource from an SDK response.
Instance Method Summary collapse
-
#auth_token(ports: nil, ttl_seconds: nil, **params) ⇒ String?
Request an auth token for direct endpoint access.
-
#endpoint(token: nil, **token_params) ⇒ Endpoint
Build an endpoint client for this MicroVM.
-
#endpoint_url ⇒ String?
Extract the direct endpoint URL from the current MicroVM data.
-
#get(path, token: nil) ⇒ Hash, ...
Send a GET request to the MicroVM endpoint.
-
#initialize(client:, id:, data: nil) ⇒ MicroVM
constructor
A new instance of MicroVM.
- #pending? ⇒ Boolean
-
#post(path, json: nil, body: nil, token: nil) ⇒ Hash, ...
Send a POST request to the MicroVM endpoint.
-
#refresh ⇒ self
Refresh MicroVM data from the service.
-
#resume(**params) ⇒ self
Resume this MicroVM and update local data when the service returns a response.
- #running? ⇒ Boolean
-
#state ⇒ Symbol
Current normalized MicroVM state.
-
#suspend(**params) ⇒ self
Suspend this MicroVM.
- #suspended? ⇒ Boolean
-
#terminate(**params) ⇒ self
Terminate this MicroVM.
- #terminated? ⇒ Boolean
-
#wait_until_running(delay: Waiter::DEFAULT_DELAY, timeout: Waiter::DEFAULT_TIMEOUT) ⇒ self
Wait until this MicroVM is running.
-
#wait_until_suspended(delay: Waiter::DEFAULT_DELAY, timeout: Waiter::DEFAULT_TIMEOUT) ⇒ self
Wait until this MicroVM is suspended.
-
#wait_until_terminated(delay: Waiter::DEFAULT_DELAY, timeout: Waiter::DEFAULT_TIMEOUT) ⇒ self
Wait until this MicroVM is terminated.
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
#client ⇒ Object (readonly)
Returns the value of attribute client.
7 8 9 |
# File 'lib/lambda/microvms/microvm.rb', line 7 def client @client end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/lambda/microvms/microvm.rb', line 7 def data @data end |
#id ⇒ Object (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.
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.
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.
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_url ⇒ String?
Extract the direct endpoint URL from the current MicroVM data.
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.
98 99 100 |
# File 'lib/lambda/microvms/microvm.rb', line 98 def get(path, token: nil, **) endpoint(token: token).get(path, **) end |
#pending? ⇒ 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.
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 |
#refresh ⇒ self
Refresh MicroVM data from the service.
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.
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
41 42 43 |
# File 'lib/lambda/microvms/microvm.rb', line 41 def running? state == :running end |
#state ⇒ Symbol
Current normalized MicroVM state.
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.
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
45 46 47 |
# File 'lib/lambda/microvms/microvm.rb', line 45 def suspended? state == :suspended end |
#terminate(**params) ⇒ self
Terminate this MicroVM.
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
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.
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.
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.
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 |