class Scaleway::Client

Attributes

access_key[RW]
token[RW]

Public Class Methods

instance() click to toggle source
# File lib/chef/knife/scaleway.rb, line 16
def self.instance
  #raise StandardError, 'Create client before accessing methods' if @instance.nil?
  #@instance
  Scaleway::Client.new(Chef::Config[:knife][:scaleway_access_key], Chef::Config[:knife][:scaleway_token])
end
new(access_key, token) click to toggle source
# File lib/chef/knife/scaleway.rb, line 8
def initialize(access_key, token)
  @host1 = 'https://api.scaleway.com'
  @host2 = 'https://account.scaleway.com'
  @access_key = access_key
  @token = token
  @instance = self
end

Public Instance Methods

get(path) click to toggle source
# File lib/chef/knife/scaleway.rb, line 45
def get(path)
  request(path, :get)
end
post(path, data) click to toggle source
# File lib/chef/knife/scaleway.rb, line 49
def post(path, data)
  request(path, :post, data)
end
put(path, data) click to toggle source
# File lib/chef/knife/scaleway.rb, line 53
def put(path, data)
  request(path, :put, data)
end
request(path, method, payload = nil) click to toggle source
# File lib/chef/knife/scaleway.rb, line 22
def request(path, method, payload = nil)
  host = @host1 if path.index('/servers') || path.index('/images') || path.index('/volumes') || path.index('/ips')
  host = @host2 if path.index('/organizations')
  raise StandardError, "Add /#{path.split('/')[1]} to host map" if host.nil?

  headers = {:'X-Auth-Token' => @token, :'Content-Type' => 'application/json'}
  url = host + path

  options = { url: url, method: method, verify_ssl: false, headers: headers }
  if method == :post
    options.merge!(payload: payload)
    puts payload if ENV['DEBUG']
  end

  begin
    puts "### Req #{method.upcase} #{host + path}" if ENV['DEBUG']
    JSON.parse(RestClient::Request.execute(options).body, object_class: OpenStruct, array_class: Array)
  rescue => e
    data = JSON.parse(e.response, object_class: OpenStruct)
    puts "#{data.type}: #{data.message}"
  end
end