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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/fog/core/service.rb', line 161

def coerce_options(options)
  options.each do |key, value|
    value_string = value.to_s.downcase
    if value.nil?
      options.delete(key)
    elsif value_string.to_i.to_s == value
      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, path = nil) ⇒ Object



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

def collection(new_collection, path = nil)
  collection_files << [path, new_collection]
  collections << new_collection
end

.collection_filesObject



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

def collection_files
  @collection_files ||= []
end

.collectionsObject



157
158
159
# File 'lib/fog/core/service.rb', line 157

def collections
  @collections ||= []
end

.fetch_credentials(_options) ⇒ Object

Deprecated.


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

def fetch_credentials(_options)
  # attempt to load credentials from config file
  Fog.credentials.reject { |key, _value| !(recognized | requirements).include?(key) }
rescue ::Fog::Errors::LoadError
  # if there are no configured credentials, do nothing
  {}
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 <<-EOS, __FILE__, __LINE__
    class Error < Fog::Service::Error; end
    class NotFound < Fog::Service::NotFound; end

    module Collections
      include Fog::Service::Collections

      def service
        #{child}
      end
    end

    def self.service
      #{child}
    end
  EOS
end

.mocked_requestsObject



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

def mocked_requests
  @mocked_requests ||= []
end

.model(new_model, path = nil) ⇒ Object



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

def model(new_model, path = nil)
  model_files << [path, new_model]
  models << [new_model]
end

.model_filesObject



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

def model_files
  @model_files ||= []
end

.model_path(new_path) ⇒ Object

Note:

This path is used to require model and collection files



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

def model_path(new_path)
  @model_path = new_path
end

.modelsObject



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

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



232
233
234
# File 'lib/fog/core/service.rb', line 232

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

.recognizes(*args) ⇒ Object



228
229
230
# File 'lib/fog/core/service.rb', line 228

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

.request(new_request, path = nil) ⇒ Object



202
203
204
# File 'lib/fog/core/service.rb', line 202

def request(new_request, path = nil)
  requests << [path, new_request]
end

.request_path(new_path) ⇒ Object



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

def request_path(new_path)
  @request_path = new_path
end

.requestsObject



206
207
208
# File 'lib/fog/core/service.rb', line 206

def requests
  @requests ||= []
end

.requirementsObject



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

def requirements
  @requirements ||= []
end

.requires(*args) ⇒ Object



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

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

.secrets(*args) ⇒ Object



210
211
212
213
214
215
216
217
218
# File 'lib/fog/core/service.rb', line 210

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

.setup_requirementsObject



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

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

  @required ||= false

  return false if @required

  require_models
  require_collections_and_define
  require_requests_and_mock
  @required = true
end

.validate_options(options) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/fog/core/service.rb', line 236

def validate_options(options)
  keys = []
  options.each_pair do |key, value|
    keys << key unless value.nil?
  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