Class: Lambda::MicroVMs::Image

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

Overview

Represents a Lambda MicroVM image/snapshot artifact.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, arn:, data: nil) ⇒ Image

Returns a new instance of Image.



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

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

Instance Attribute Details

#arnObject (readonly)

Returns the value of attribute arn.



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

def arn
  @arn
end

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

Class Method Details

.from_response(client:, response:) ⇒ Image

Build an image resource from an SDK response.

Parameters:

  • client (Client)

    lifecycle client

  • response (Object)

    SDK response

Returns:

  • (Image)

    image resource



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

def self.from_response(client:, response:)
  arn = Util.extract(response, :image_arn, :microvm_image_arn, :arn)
  new(client: client, arn: arn, data: response)
end

Instance Method Details

#ready?Boolean

Returns:

  • (Boolean)


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

def ready?
  %i[ready available active].include?(state)
end

#refreshself

Refresh image data from the service.

Returns:

  • (self)


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

def refresh
  fresh = client.get_image(image_arn: arn)
  @data = fresh.data
  self
end

#run(role_arn:, payload: nil, **params) ⇒ MicroVM

Run a MicroVM from this image.

Parameters:

  • role_arn (String)

    IAM role ARN used by the MicroVM runtime

  • payload (Object, nil) (defaults to: nil)

    optional runtime payload

  • params (Hash)

    additional run parameters

Returns:



63
64
65
66
67
# File 'lib/lambda/microvms/image.rb', line 63

def run(role_arn:, payload: nil, **params)
  request = params.merge(image_arn: arn, role_arn: role_arn)
  request[:payload] = payload if payload
  client.run(**request)
end

#stateSymbol

Current normalized image state.

Returns:

  • (Symbol)


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

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

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

Wait until the image reaches a ready-like state.

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)


50
51
52
53
54
55
# File 'lib/lambda/microvms/image.rb', line 50

def wait_until_ready(delay: Waiter::DEFAULT_DELAY, timeout: Waiter::DEFAULT_TIMEOUT)
  Waiter.new(delay: delay, timeout: timeout).wait(message: "image #{arn} to be ready") do
    refresh.ready?
  end
  self
end