Last active
May 6, 2018 13:05
-
-
Save huseyin/33b1a6412e41b5e3ba49825136cbb84f to your computer and use it in GitHub Desktop.
Scaleway API (create, start and stop server)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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