Module: Scaleway

Extended by:
Scaleway
Included in:
Scaleway
Defined in:
lib/scaleway.rb,
lib/scaleway/version.rb

Defined Under Namespace

Classes: APIError, NotFound

Constant Summary collapse

DEFINITIONS =
{
  "Image" => {
    :all => {
      :method => :get,
      :endpoint => "#{Scaleway.compute_endpoint}/images",
      :default_params => {
        :organization => Proc.new { Scaleway.organization }
      }
    },
    :marketplace => {
      :method => :get,
      :endpoint => "#{Scaleway.compute_endpoint}/images",
      :default_params => {
        :public => true
      }
    },
    :find => {
      :method => :get,
      :endpoint => "#{Scaleway.compute_endpoint}/images/%s",
    },
    :find_by_name => {
      :method => :get,
      :endpoint => "#{Scaleway.compute_endpoint}/images",
      :filters => [
        Proc.new { |item, params| item.name.include? params.first }
      ],
      :transform => Proc.new { |item, params| item.first },
    },
    :create => {
      :method => :post,
      :endpoint => "#{Scaleway.compute_endpoint}/images",
      :default_params => {
        :name => 'default',
        :root_volume => 'required',
        :organization => Proc.new { Scaleway.organization },
      }
    },
    :edit => {
      :method => :put,
      :endpoint => "#{Scaleway.compute_endpoint}/images/%s",
    },
    :destroy => {
      :method => :delete,
      :endpoint => "#{Scaleway.compute_endpoint}/images/%s",
    },
  },
  "Volume" => {
    :all => {
      :method => :get,
      :endpoint => "#{Scaleway.compute_endpoint}/volumes",
    },
    :find => {
      :method => :get,
      :endpoint => "#{Scaleway.compute_endpoint}/volumes/%s",
    },
    :edit => {
      :method => :put,
      :endpoint => "#{Scaleway.compute_endpoint}/volumes/%s",
    },
    :destroy => {
      :method => :delete,
      :endpoint => "#{Scaleway.compute_endpoint}/volumes/%s",
    },
    :create => {
      :method => :post,
      :endpoint => "#{Scaleway.compute_endpoint}/volumes",
      :default_params => {
        :name => 'default',
        :size => 20 * 10**9,
        :volume_type => 'l_hdd',
        :organization => Proc.new { Scaleway.organization },
      }
    },
  },
  "Ip" => {
    :all => {
      :method => :get,
      :endpoint => "#{Scaleway.compute_endpoint}/ips",
    },
    :find => {
      :method => :get,
      :endpoint => "#{Scaleway.compute_endpoint}/ips/%s",
    },
    :edit => {
      :method => :put,
      :endpoint => "#{Scaleway.compute_endpoint}/ips/%s",
    },
    :destroy => {
      :method => :delete,
      :endpoint => "#{Scaleway.compute_endpoint}/ips/%s",
    },
    :reserve => {
      :method => :post,
      :endpoint => "#{Scaleway.compute_endpoint}/ips",
      :default_params => {
        :organization => Proc.new { Scaleway.organization },
      }
    },
    :create => {
      :method => :post,
      :endpoint => "#{Scaleway.compute_endpoint}/ips",
      :default_params => {
        :organization => Proc.new { Scaleway.organization },
      }
    },
  },
  "Snapshot" => {
    :all => {
      :method => :get,
      :endpoint => "#{Scaleway.compute_endpoint}/snapshots",
    },
    :find => {
      :method => :get,
      :endpoint => "#{Scaleway.compute_endpoint}/snapshots/%s",
    },
    :edit => {
      :method => :put,
      :endpoint => "#{Scaleway.compute_endpoint}/snapshots/%s",
    },
    :destroy => {
      :method => :delete,
      :endpoint => "#{Scaleway.compute_endpoint}/snapshots/%s",
    },
    :create => {
      :method => :post,
      :endpoint => "#{Scaleway.compute_endpoint}/snapshots",
      :default_params => {
        :name => 'default',
        :volume_id => 'required',
        :organization => Proc.new { Scaleway.organization },
      }
    },
  },
  "Server" => {
    :all => {
      :method => :get,
      :endpoint => "#{Scaleway.compute_endpoint}/servers",
    },
    :power_on => {
      :method => :post,
      :endpoint => "#{Scaleway.compute_endpoint}/servers/%s/action",
      :default_params => {
        :action => :poweron
      }
    },
    :reboot => {
      :method => :post,
      :endpoint => "#{Scaleway.compute_endpoint}/servers/%s/action",
      :default_params => {
        :action => :reboot
      }
    },
    :power_off => {
      :method => :post,
      :endpoint => "#{Scaleway.compute_endpoint}/servers/%s/action",
      :default_params => {
        :action => :poweroff
      }
    },
    :terminate => {
      :method => :post,
      :endpoint => "#{Scaleway.compute_endpoint}/servers/%s/action",
      :default_params => {
        :action => :terminate
      }
    },
    :find => {
      :method => :get,
      :endpoint => "#{Scaleway.compute_endpoint}/servers/%s",
    },
    :edit => {
      :method => :put,
      :endpoint => "#{Scaleway.compute_endpoint}/servers/%s",
    },
    :destroy => {
      :method => :delete,
      :endpoint => "#{Scaleway.compute_endpoint}/servers/%s",
    },
    :create => {
      :method => :post,
      :endpoint => "#{Scaleway.compute_endpoint}/servers",
      :default_params => {
        :name => 'default',
        :image => Proc.new { Scaleway::Image.find_by_name('Ubuntu').id },
        :volumes => {},
        :organization => Proc.new { Scaleway.organization },
      }
    }
  },
}
VERSION =
'0.2.1'

Instance Method Summary collapse

Instance Method Details

#account_endpointObject



14
15
16
# File 'lib/scaleway.rb', line 14

def 
  "https://account.scaleway.com"
end

#compute_endpointObject



10
11
12
# File 'lib/scaleway.rb', line 10

def compute_endpoint
  "https://api.scaleway.com"
end

#organizationObject



35
36
37
38
# File 'lib/scaleway.rb', line 35

def organization
  return @organization if @organization
  "organization_required"
end

#organization=(organization) ⇒ Object



29
30
31
32
33
# File 'lib/scaleway.rb', line 29

def organization=(organization)
  @organization = organization
  setup!
  @organization
end

#requestObject



251
252
253
# File 'lib/scaleway.rb', line 251

def request
  @request
end

#request=(request) ⇒ Object



247
248
249
# File 'lib/scaleway.rb', line 247

def request=(request)
  @request = request
end

#request_and_respond(query, *params) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/scaleway.rb', line 255

def request_and_respond(query, *params)
  body = {}
  body.merge! query[:default_params] || {}
  endpoint = query[:endpoint]

  params.each do |param|
    if param.is_a? Hash or param.is_a? RecursiveOpenStruct
      body.merge! param
    elsif not param.nil?
      endpoint = endpoint % param
    end
  end

  body = Hash[body.map { |k, v|
    if v.respond_to? :call then [k, v.call()] else [k, v] end
  }]

  resp = Scaleway.request.send(query[:method], endpoint, body)
  body = resp.body
  if resp.status == 204
    return
  end

  if resp.status == 404
    raise Scaleway::NotFound, resp
  elsif resp.status >= 300
    raise Scaleway::APIError, resp
  end

  hash = RecursiveOpenStruct.new(body, :recurse_over_arrays => true)

  if body.length == 1
    hash = hash.send(body.keys.first)
  end

  if query[:filters]
    filters = query[:filters]
    hash = hash.find_all do |item|
      filters.all? do |filter|
        filter.call(item, params)
      end
    end
  end

  if query[:transform]
    hash = query[:transform].call(hash, params)
  end

  hash
end

#tokenObject



24
25
26
27
# File 'lib/scaleway.rb', line 24

def token
  return @token if @token
  "token_required"
end

#token=(token) ⇒ Object



18
19
20
21
22
# File 'lib/scaleway.rb', line 18

def token=(token)
  @token = token
  setup!
  @token
end