Skip to content

Instantly share code, notes, and snippets.

@huseyin
Last active May 6, 2018 13:05
Show Gist options
  • Save huseyin/33b1a6412e41b5e3ba49825136cbb84f to your computer and use it in GitHub Desktop.
Save huseyin/33b1a6412e41b5e3ba49825136cbb84f to your computer and use it in GitHub Desktop.
Scaleway API (create, start and stop server)
#!/usr/bin/env ruby
require 'json'
require 'net/https'
require 'openssl'
require 'uri'
# Scaleway => APIClass
class Scaleway
API_ENDPOINT = 'https://cp-ams1.scaleway.com'.freeze
def initialize(access_token)
@access_token = access_token
uri = URI(API_ENDPOINT)
@http = Net::HTTP.new(uri.host, uri.port)
@http.use_ssl = true
@http.verify_mode = OpenSSL::SSL::VERIFY_PEER
end
def create_server(options = {})
resp = post '/servers', options
resp['server']['id']
end
def start_server(server_id)
action server_id, 'poweron'
end
def stop_server(server_id)
action server_id, 'poweroff'
end
private
def post(url, options = {})
req = Net::HTTP::Post.new(url)
req.add_field 'Content-Type', 'application/json'
req.add_field 'X-Auth-Token', access_token
req.body = options.to_json
JSON.parse @http.request(req).body
end
def action(server_id, action)
post "/servers/#{server_id}/action", {'action': action}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment