class LeasewebAPI
Public Class Methods
new()
click to toggle source
# File lib/leaseweb-rest-api.rb, line 15 def initialize @tmpdir = '/tmp/lsw-rest-api' Dir.mkdir(@tmpdir) unless Dir.exist?(@tmpdir) end
Public Instance Methods
addDedicatedServerToPrivateNetwork(id, server_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 222 def addDedicatedServerToPrivateNetwork(id, server_id) opt = @options.merge!(body: { serverId: server_id }.to_json) self.class.post("/bareMetals/v2/privateNetworks/#{id}/servers", opt) end
apiKeyAuth(apikey)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 20 def apiKeyAuth(apikey) @options = { headers: { 'X-Lsw-Auth' => apikey, 'Content-Type' => 'application/json' } } end
createDedicatedServerDhcpReservation(server_id, boot_file_name)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 131 def createDedicatedServerDhcpReservation(server_id, boot_file_name) opt = @options.merge!(body: { bootFileName: boot_file_name }.to_json) self.class.post("/bareMetals/v2/servers/#{server_id}/leases", opt) end
createPrivateNetworks(name = '')
click to toggle source
TODO: check post with name
# File lib/leaseweb-rest-api.rb, line 201 def createPrivateNetworks(name = '') opt = @options.merge!(body: { name: name }.to_json) self.class.post('/bareMetals/v2/privateNetworks', opt) end
delete(url)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 60 def delete(url) self.class.delete(url, @options) end
deletePrivateNetwork(id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 218 def deletePrivateNetwork(id) self.class.delete("/bareMetals/v2/privateNetworks/#{id}", @options) end
get(url)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 46 def get(url) self.class.get(url, @options) end
getControlPanel(operating_system_id, control_panel_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 245 def getControlPanel(operating_system_id, control_panel_id) self.class.get("/bareMetals/v2/operatingSystems/#{operating_system_id}/controlPanels/#{control_panel_id}", @options) end
getControlPanels(operating_system_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 241 def getControlPanels(operating_system_id) self.class.get("/bareMetals/v2/operatingSystems/#{operating_system_id}/controlPanels", @options) end
getDedicatedServer(server_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 118 def getDedicatedServer(server_id) self.class.get("/bareMetals/v2/servers/#{server_id}", @options) end
getDedicatedServerBandwidthMetrics(server_id, date_from, date_to, format = 'json')
click to toggle source
# File lib/leaseweb-rest-api.rb, line 264 def getDedicatedServerBandwidthMetrics(server_id, date_from, date_to, format = 'json') self.class.get("/bareMetals/v2/servers/#{server_id}/metrics/bandwidth", formatRequest(date_from, date_to, 'AVG', format)) end
getDedicatedServerByIp(ip)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 114 def getDedicatedServerByIp(ip) self.class.get("/internal/dedicatedserverapi/v2/servers?ip=#{ip}", @options) end
getDedicatedServerDatatrafficMetrics(server_id, date_from, date_to, format = 'json')
click to toggle source
# File lib/leaseweb-rest-api.rb, line 268 def getDedicatedServerDatatrafficMetrics(server_id, date_from, date_to, format = 'json') self.class.get("/bareMetals/v2/servers/#{server_id}/metrics/datatraffic", formatRequest(date_from, date_to, 'SUM', format)) end
getDedicatedServerDhcpReservation(server_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 126 def getDedicatedServerDhcpReservation(server_id) result = self.class.get("/bareMetals/v2/servers/#{server_id}/leases", @options) result['leases'].pop end
getDedicatedServerHardware(server_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 122 def getDedicatedServerHardware(server_id) self.class.get("/bareMetals/v2/servers/#{server_id}/hardwareInfo", @options) end
getDedicatedServers(result = nil)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 95 def getDedicatedServers(result = nil) partialSize = (result && result['servers'] && result['servers'].size) || 0 partialResult = self.class.get("/bareMetals/v2/servers?offset=#{partialSize}&limit=50", @options) return partialResult if partialResult['errorMessage'] if result.nil? result = partialResult else result['servers'] += partialResult['servers'] result['_metadata']['offset'] = 0 result['_metadata']['limit'] = partialResult['_metadata']['totalCount'] end return getDedicatedServers(result) if result['servers'].size < partialResult['_metadata']['totalCount'] result end
getIp(ip)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 254 def getIp(ip) self.class.get("/ipMgmt/v2/ips/#{ip}", @options) end
getIps()
click to toggle source
IPs
# File lib/leaseweb-rest-api.rb, line 250 def getIps self.class.get('/ipMgmt/v2/ips', @options) end
getOauthToken(client_id, client_secret)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 29 def getOauthToken(client_id, client_secret) access_token = validate_token(client_id) if access_token == false response = self.class.post('https://auth.leaseweb.com/token', basic_auth: { username: client_id, password: client_secret }, body: { grant_type: 'client_credentials' }) access_token = response.parsed_response['access_token'] cache_token(client_id, access_token, response.parsed_response['expires_in']) end @options = { headers: { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' } } end
getOperatingSystem(operating_system_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 237 def getOperatingSystem(operating_system_id) self.class.get("/bareMetals/v2/operatingSystems/#{operating_system_id}", @options) end
getOperatingSystems()
click to toggle source
Operating Systems
# File lib/leaseweb-rest-api.rb, line 233 def getOperatingSystems self.class.get('/bareMetals/v2/operatingSystems', @options) end
getPrivateNetwork(id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 207 def getPrivateNetwork(id) self.class.get("/bareMetals/v2/privateNetworks/#{id}", @options) end
getPrivateNetworks()
click to toggle source
Private Networks
# File lib/leaseweb-rest-api.rb, line 196 def getPrivateNetworks self.class.get('/bareMetals/v2/privateNetworks', @options) end
getVirtualServer(server_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 175 def getVirtualServer(server_id) self.class.get("/cloud/v2/virtualServers/#{server_id}", @options) end
getVirtualServerControlPanelCredentials(server_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 179 def getVirtualServerControlPanelCredentials(server_id) self.class.get("/cloud/v2/virtualServers/#{server_id}/credentials/CONTROL_PANEL", @options) end
getVirtualServerControlPanelCredentialsForUser(server_id, username)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 183 def getVirtualServerControlPanelCredentialsForUser(server_id, username) self.class.get("/cloud/v2/virtualServers/#{server_id}/credentials/CONTROL_PANEL/#{username}", @options) end
getVirtualServerOsCredentials(server_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 187 def getVirtualServerOsCredentials(server_id) self.class.get("/cloud/v2/virtualServers/#{server_id}/credentials/OPERATING_SYSTEM", @options) end
getVirtualServerOsCredentialsForUser(server_id, username)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 191 def getVirtualServerOsCredentialsForUser(server_id, username) self.class.get("/cloud/v2/virtualServers/#{server_id}/credentials/OPERATING_SYSTEM/#{username}", @options) end
getVirtualServers(result = nil)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 156 def getVirtualServers(result = nil) partialSize = (result && result['virtualServers'] && result['virtualServers'].size) || 0 partialResult = self.class.get("/cloud/v2/virtualServers?offset=#{partialSize}&limit=50", @options) return partialResult if partialResult['errorMessage'] if result.nil? result = partialResult else result['virtualServers'] += partialResult['virtualServers'] result['_metadata']['offset'] = 0 result['_metadata']['limit'] = partialResult['_metadata']['totalCount'] end return getVirtualServers(result) if result['virtualServers'].size < partialResult['_metadata']['totalCount'] result end
get_paginated(url, key, offset = 0, limit = 50)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 64 def get_paginated(url, key, offset = 0, limit = 50) data = [] loop do response = self.class.get("#{url}&offset=#{offset}&limit=#{limit}", @options) total = response.parsed_response['_metadata']['totalCount'] data += response.parsed_response[key] offset += limit break unless offset < total end data end
launchRescueModeDedicatedServer(server_id, rescue_image_id, ssh_key)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 87 def launchRescueModeDedicatedServer(server_id, rescue_image_id, ssh_key) self.class.post("/bareMetals/v2/servers/#{server_id}/cancelActiveJob", @options) opt = @options.merge!(body: { rescueImageId: rescue_image_id, sshKeys: ssh_key, powerCycle: true }.to_json) self.class.post("/bareMetals/v2/servers/#{server_id}/rescueMode", opt) end
post(url, body)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 50 def post(url, body) opt = @options.merge!(body: body) self.class.post(url, opt) end
powerOffVirtualServer(server_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 144 def powerOffVirtualServer(server_id) self.class.post("/cloud/v2/virtualServers/#{server_id}/powerOff", @options) end
powerOnDedicatedServer(server_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 83 def powerOnDedicatedServer(server_id) self.class.post("/bareMetals/v2/servers/#{server_id}/powerOn", @options) end
powerOnVirtualServer(server_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 140 def powerOnVirtualServer(server_id) self.class.post("/cloud/v2/virtualServers/#{server_id}/powerOn", @options) end
put(url, body)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 55 def put(url, body) opt = @options.merge!(body: body) self.class.put(url, opt) end
rebootDedicatedServer(server_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 79 def rebootDedicatedServer(server_id) self.class.post("/bareMetals/v2/servers/#{server_id}/powerCycle", @options) end
rebootVirtualServer(server_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 148 def rebootVirtualServer(server_id) self.class.post("/cloud/v2/virtualServers/#{server_id}/reboot", @options) end
reinstallVirtualServer(server_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 152 def reinstallVirtualServer(server_id) self.class.post("/cloud/v2/virtualServers/#{server_id}/reinstall", @options) end
removeDedicatedServerDhcpReservation(server_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 136 def removeDedicatedServerDhcpReservation(server_id) self.class.delete("/bareMetals/v2/servers/#{server_id}/leases", @options) end
removeDedicatedServerFromPrivateNetwork(id, server_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 228 def removeDedicatedServerFromPrivateNetwork(id, server_id) self.class.delete("/bareMetals/v2/privateNetworks/#{id}/servers/#{server_id}", @options) end
updateIp(ip, reverse_lookup = '', null_routed = 0)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 258 def updateIp(ip, reverse_lookup = '', null_routed = 0) opt = @options.merge!(body: { reverseLookup: reverse_lookup, nullRouted: null_routed }.to_json) self.class.put("/ipMgmt/v2/ips/#{ip}", opt) end
updatePrivateNetwork(id, name = '')
click to toggle source
TODO: Check with Jeroen if it works
# File lib/leaseweb-rest-api.rb, line 212 def updatePrivateNetwork(id, name = '') opt = @options.merge!(body: { name: name }.to_json) self.class.put("/bareMetals/v2/privateNetworks/#{id}", opt) end
Protected Instance Methods
cache_token(client_id, access_token, expires_in)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 290 def cache_token(client_id, access_token, expires_in) file = "#{@tmpdir}/#{client_id}.json" content = { access_token: access_token, expires_at: Time.now.getutc + expires_in }.to_json File.write(file, content) end
dateFormat(date)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 296 def dateFormat(date) Date.parse(date).strftime('%Y-%m-%dT00:00:00Z') end
formatHeader(format)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 300 def formatHeader(format) header = if format == 'json' { 'Accept' => 'application/json' }.merge!(@options[:headers]) else { 'Accept' => 'image/png' }.merge!(@options[:headers]) end header end
formatRequest(date_from, date_to, aggregation, format)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 309 def formatRequest(date_from, date_to, aggregation, format) @options.merge!(query: { from: dateFormat(date_from), to: dateFormat(date_to), aggregation: aggregation, granularity: 'DAY' }, headers: formatHeader(format)) end
validate_token(client_id)
click to toggle source
# File lib/leaseweb-rest-api.rb, line 274 def validate_token(client_id) begin file = "#{@tmpdir}/#{client_id}.json" content = JSON.parse(File.read(file)) expires_at = DateTime.parse(content['expires_at']) return content['access_token'] if expires_at > DateTime.now File.delete(file) rescue return false end false end