Class: CFRuntime::CloudApp

Inherits:
Object
  • Object
show all
Defined in:
lib/cf-runtime/properties.rb

Class Method Summary collapse

Class Method Details

.hostObject

Returns the application host name



14
15
16
# File 'lib/cf-runtime/properties.rb', line 14

def host
  ENV['VCAP_APP_HOST']
end

.portObject

Returns the port bound to the application



19
20
21
# File 'lib/cf-runtime/properties.rb', line 19

def port
  ENV['VCAP_APP_PORT']
end

.running_in_cloud?Boolean

Returns true if this code is running on Cloud Foundry

Returns:

  • (Boolean)


9
10
11
# File 'lib/cf-runtime/properties.rb', line 9

def running_in_cloud?()
  !ENV['VCAP_APPLICATION'].nil?
end

.service_namesObject

Parses the VCAP_SERVICES environment variable and returns an array of Service names bound to the current application.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cf-runtime/properties.rb', line 57

def service_names
  service_names = []
  if ENV['VCAP_SERVICES']
    CFRuntime::OkJson.decode(ENV['VCAP_SERVICES']).each do |key,list|
      list.each do |svc|
        service_names << svc["name"]
      end
    end
  end
  service_names
end

.service_names_of_type(type) ⇒ Object

Parses the VCAP_SERVICES environment variable and returns an array of Service names of the specified type bound to the current application. Example: service_names_of_type(‘mysql’)



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cf-runtime/properties.rb', line 72

def service_names_of_type(type)
  service_names = []
  if ENV['VCAP_SERVICES']
    CFRuntime::OkJson.decode(ENV['VCAP_SERVICES']).each do |key,list|
      label, version = key.split('-')
      list.each do |svc|
        if label == type
          service_names << svc["name"]
        end
      end
    end
  end
  service_names
end

.service_props(service_name) ⇒ Object

Parses the VCAP_SERVICES environment variable and returns a Hash of properties for the specified service name. If only one service of a particular type is bound to the application, service_props(type) will also work. Example: service_props(‘mysql’). Returns nil if service with specified name is not found or if zero or multiple services of a specified type are found.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cf-runtime/properties.rb', line 29

def service_props(service_name)
  registered_svcs = {}
  svcs = ENV['VCAP_SERVICES'] ? CFRuntime::OkJson.decode(ENV['VCAP_SERVICES']) : {}
  svcs.each do |key,list|
    label, version = key.split('-')
    begin
      parser = Object.const_get("CFRuntime").const_get("#{label.capitalize}Parser")
    rescue NameError
      parser = Object.const_get("CFRuntime").const_get("DefaultParser")
    end
    list.each do |svc|
      name = svc["name"]
      serviceopts = {}
      serviceopts[:label] = label
      serviceopts[:version] = version
      serviceopts[:name] = name
      serviceopts.merge!(parser.parse(svc))
      registered_svcs[name] = serviceopts
      if list.count == 1
        registered_svcs[label] = serviceopts
      end
    end
  end
  registered_svcs[service_name]
end