Class: Amigrind::Core::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/amigrind/core/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region, credentials) ⇒ Client



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/amigrind/core/client.rb', line 12

def initialize(region, credentials)
  raise "'region' must be a String or nil." unless region.is_a?(String) || region.nil?
  raise "'credentials' (#{credentials}) must be nil or an Aws::CredentialProvider." \
    unless credentials.class.ancestors.include?(Aws::CredentialProvider) || credentials.nil?

  @region = region
  @credentials = credentials

  ec2_opts = { region: region, credentials: credentials }.delete_if { |k, v| v.nil? }

  @ec2 = Aws::EC2::Client.new(ec2_opts)
  @ec2_rsrc = Aws::EC2::Resource.new(client: @ec2)
end

Instance Attribute Details

#credentialsObject (readonly)

Returns the value of attribute credentials.



10
11
12
# File 'lib/amigrind/core/client.rb', line 10

def credentials
  @credentials
end

#regionObject (readonly)

IDEA: future enhancement - maybe have an optional caching server that

Amigrind clients can hit for quicker responses? because this is
pretty slow when it has to hit the API for read requests.


9
10
11
# File 'lib/amigrind/core/client.rb', line 9

def region
  @region
end

Instance Method Details

#get_image_by_channel(name:, channel:, steps_back: 0) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/amigrind/core/client.rb', line 36

def get_image_by_channel(name:, channel:, steps_back: 0)
  raise "'name' must be a String." unless name.is_a?(String)
  raise "'channel' must be a String or Symbol." \
    unless channel.is_a?(Symbol) || channel.is_a?(String)
  raise "'steps_back' must be a Fixnum." unless steps_back.is_a?(Fixnum)

  channel = channel.to_sym

  tags = {
    Amigrind::Core::AMIGRIND_NAME_TAG => name
  }

  if channel != :latest
    channel_tag =
      Amigrind::Core::AMIGRIND_CHANNEL_TAG % { channel_name: channel }
    tags[channel_tag] = 1
  end

  images = find_images_for_tags(tags.delete_if { |_, v| v.nil? })
  images.sort { |a, b| a.creation_date <=> b.creation_date }.reverse[steps_back]
end

#get_image_by_id(name:, id:) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/amigrind/core/client.rb', line 26

def get_image_by_id(name:, id:)
  raise "'name' must be a String." unless name.is_a?(String)
  raise "'id' must be a Fixnum." unless id.is_a?(Fixnum)

  find_images_for_tags(
    Amigrind::Core::AMIGRIND_NAME_TAG => name,
    Amigrind::Core::AMIGRIND_ID_TAG => id.to_s
  ).first
end

#get_images(name:) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/amigrind/core/client.rb', line 58

def get_images(name:)
  raise "'name' must be a String." unless name.is_a?(String)

  find_images_for_tags(
    Amigrind::Core::AMIGRIND_NAME_TAG => name
  )
end