Class: BuildpackSupport::Component::Services

Inherits:
Array
  • Object
show all
Defined in:
lib/buildpack_support/component/services.rb

Overview

An abstraction encapsulating the VCAP_SERVICES of an application.

A new instance of this type should be created once for the application.

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Services

Returns a new instance of Services.



27
28
29
# File 'lib/buildpack_support/component/services.rb', line 27

def initialize(raw)
  concat raw.values.flatten
end

Instance Method Details

#find_service(filter) ⇒ Hash?

Compares the name, label, and tags of each service to the given filter. The method returns the first service that the filter matches. If no service matches, returns nil

Parameters:

  • filter (Regexp, String)

    a RegExp or String to match against the name, label, and tags of the services

Returns:

  • (Hash, nil)

    the first service that filter matches. If no service matches, returns nil.



63
64
65
# File 'lib/buildpack_support/component/services.rb', line 63

def find_service(filter)
  find(&matcher(filter))
end

#one_service?(filter, *required_credentials) ⇒ Boolean

Compares the name, label, and tags of each service to the given filter. The method returns true if the filter matches exactly one service, false otherwise.

Parameters:

  • filter (Regexp, String)

    a RegExp or String to match against the name, label, and tags of the services

  • required_credentials (String)

    an optional list of keys that must exist in the credentials payload of the candidate service

Returns:

  • (Boolean)

    true if the filter matches exactly one service with the required credentials, false otherwise.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/buildpack_support/component/services.rb', line 39

def one_service?(filter, *required_credentials)
  candidates = select(&matcher(filter))

  match = false
  if candidates.one?
    if credentials?(candidates.first['credentials'], required_credentials)
      match = true
    else
      logger = BuildpackSupport::Logging::LoggerFactory.instance.get_logger Services
      logger.warn do
        "A service with a name label or tag matching #{filter} was found, but was missing one of the required" \
      " credentials #{required_credentials}"
      end
    end
  end

  match
end