Sha256: b3e91bae1a53a05915d758bff748e57e9627cf767d5b9ebaf89a2ea5cd518f92

Contents?: true

Size: 835 Bytes

Versions: 1

Compression:

Stored size: 835 Bytes

Contents

module OpenStax::Aws
  class Image

    attr_reader :aws_image

    def initialize(id: nil, region: nil, aws_image: nil)
      if aws_image
        @aws_image = aws_image
      else
        if id.nil? || region.nil?
          raise ArgumentError, "`id` and `region` must be provided"
        end

        @aws_image = Aws::EC2::Image.new(id, region: region)
      end
    end

    def get_tag(key:)
      tag = aws_image.tags.find{|tag| tag.key == key}
      raise "No tag with key #{key} on AMI #{aws_image.image_id}" if tag.nil?
      tag.value
    end

    def sha
      get_tag(key: "sha")
    end

    def self.find_by_sha(sha:, region:)  
      Aws::EC2::Client.new(region: region).describe_images({
        filters: [{name: "tag:sha", values: [sha]}]
      }).images.map{|aws_image| new(aws_image: aws_image)}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
openstax_aws-1.0.0 lib/openstax/aws/image.rb