Class: Fog::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/core/service.rb

Defined Under Namespace

Modules: Collections, NoLeakInspector Classes: Error, NotFound

Class Method Summary collapse

Class Method Details

.coerce_options(options) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/fog/core/service.rb', line 157

def coerce_options(options)
  options.each do |key, value|
    value_string = value.to_s.downcase
    if value.nil?
      options.delete(key)
    elsif value == value_string.to_i.to_s
      options[key] = value.to_i
    else
      options[key] = case value_string
      when 'false'
        false
      when 'true'
        true
      else
        value
      end
    end
  end
end

.collection(new_collection) ⇒ Object



149
150
151
# File 'lib/fog/core/service.rb', line 149

def collection(new_collection)
  collections << new_collection
end

.collectionsObject



153
154
155
# File 'lib/fog/core/service.rb', line 153

def collections
  @collections ||= []
end

.fetch_credentials(options) ⇒ Object

Deprecated.


120
121
122
123
124
125
126
127
128
# File 'lib/fog/core/service.rb', line 120

def fetch_credentials(options)
  # attempt to load credentials from config file
  begin
    Fog.credentials.reject { |key, value| !(recognized | requirements).include?(key) }
  rescue LoadError
    # if there are no configured credentials, do nothing
    {}
  end
end

.inherited(child) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fog/core/service.rb', line 33

def inherited(child)
  child.class_eval "class Error < Fog::Service::Error; end\nclass NotFound < Fog::Service::NotFound; end\n\nmodule Collections\ninclude Fog::Service::Collections\n\ndef service\n\#{child}\nend\nend\n\ndef self.service\n\#{child}\nend\n", __FILE__, __LINE__
end

.mocked_requestsObject



177
178
179
# File 'lib/fog/core/service.rb', line 177

def mocked_requests
  @mocked_requests ||= []
end

.model(new_model) ⇒ Object



181
182
183
# File 'lib/fog/core/service.rb', line 181

def model(new_model)
  models << new_model
end

.model_path(new_path) ⇒ Object

Note:

This path is used to require model and collection files



145
146
147
# File 'lib/fog/core/service.rb', line 145

def model_path(new_path)
  @model_path = new_path
end

.modelsObject



185
186
187
# File 'lib/fog/core/service.rb', line 185

def models
  @models ||= []
end

.new(config = {}) ⇒ Fog::Service::Provider::Real, Fog::Service::Provider::Mock

This method is abstract.

Subclass and implement real or mock code

Fog::Service is (unfortunately) both a builder class and the subclass for any fog service.

Creating a new instance using the builder will return either an instance of Fog::<Service>::<Provider>::Real or Fog::<Service>::<Provider>::Mock based on the value of Fog.mock? when the builder is used.

Each provider can require or recognize different settings (often prefixed with the providers name). These settings map to keys in the ~/.fog file.

Settings can be passed as either a Hash or an object that responds to config_service? with true. This object will be passed through unchanged to the Real or Mock service that is created. It is up to providers to adapt services to use these config objects.

Examples:

Minimal options (dependent on ~/.fog)

@service = Fog::Compute::Example.new # => <#Fog::Compute::Example::Real>

Mocked service

Fog.mock!
@service = Fog::Compute::Example.new # => <#Fog::Compute::Example::Mock>

Configured using many options (options merged into ~/.fog)

@options = {
  :example_username => "fog",
  :example_password => "fog"
}
@service = Fog::Compute::Example.new(@options)

Configured using external config object (~/.fog ignored completely)

@config = Fog::Example::Config.new(...)
@service = Fog::Compute::Example.new(@config)

Parameters:

  • config (Hash, #config_service?) (defaults to: {})

    Settings or an object used to build a service instance

Options Hash (config):

Returns:

  • (Fog::Service::Provider::Real)

    if created while mocking is disabled

  • (Fog::Service::Provider::Mock)

    if created while mocking is enabled

Raises:

  • (ArgumentError)

    if a setting required by the provider was not passed in



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fog/core/service.rb', line 94

def new(config = {})
  if config.respond_to?(:config_service?) && config.config_service?
    cleaned_settings = config
  else
    cleaned_settings = handle_settings(config)
  end
  setup_requirements

  svc = service
  if Fog.mocking?
    while svc != Fog::Service
      service::Mock.send(:include, svc::Collections)
      svc = svc.superclass
    end
    service::Mock.new(cleaned_settings)
  else
    while svc != Fog::Service
      service::Real.send(:include, svc::Collections)
      svc = svc.superclass
    end
    service::Real.send(:include, service::NoLeakInspector)
    service::Real.new(cleaned_settings)
  end
end

.recognizedObject



223
224
225
# File 'lib/fog/core/service.rb', line 223

def recognized
  @recognized ||= [:connection_options]
end

.recognizes(*args) ⇒ Object



219
220
221
# File 'lib/fog/core/service.rb', line 219

def recognizes(*args)
  recognized.concat(args)
end

.request(new_request) ⇒ Object



193
194
195
# File 'lib/fog/core/service.rb', line 193

def request(new_request)
  requests << new_request
end

.request_path(new_path) ⇒ Object



189
190
191
# File 'lib/fog/core/service.rb', line 189

def request_path(new_path)
  @request_path = new_path
end

.requestsObject



197
198
199
# File 'lib/fog/core/service.rb', line 197

def requests
  @requests ||= []
end

.requirementsObject



215
216
217
# File 'lib/fog/core/service.rb', line 215

def requirements
  @requirements ||= []
end

.requires(*args) ⇒ Object



211
212
213
# File 'lib/fog/core/service.rb', line 211

def requires(*args)
  requirements.concat(args)
end

.secrets(*args) ⇒ Object



201
202
203
204
205
206
207
208
209
# File 'lib/fog/core/service.rb', line 201

def secrets(*args)
  if args.empty?
    @secrets ||= []
  else
    args.inject(secrets) do |secrets, secret|
      secrets << "@#{secret}".to_sym
    end
  end
end

.setup_requirementsObject



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/fog/core/service.rb', line 130

def setup_requirements
  if superclass.respond_to?(:setup_requirements)
    superclass.setup_requirements
  end

  @required ||= false
  unless @required
    require_models
    require_collections_and_define
    require_requests_and_mock
    @required = true
  end
end

.validate_options(options) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/fog/core/service.rb', line 227

def validate_options(options)
  keys = []
  for key, value in options
    unless value.nil?
      keys << key
    end
  end
  missing = requirements - keys

  unless missing.empty?
    raise ArgumentError, "Missing required arguments: #{missing.join(', ')}"
  end

  unless recognizes.empty?
    unrecognized = options.keys - requirements - recognized
    unless unrecognized.empty?
      Fog::Logger.warning("Unrecognized arguments: #{unrecognized.join(', ')}")
    end
  end
end