Skip to content

Instantly share code, notes, and snippets.

@eduard93
Created April 5, 2016 10:06
Show Gist options
  • Save eduard93/5323249677c8ee42866cc8c9b76510de to your computer and use it in GitHub Desktop.
Save eduard93/5323249677c8ee42866cc8c9b76510de to your computer and use it in GitHub Desktop.
Work with Digital Ocean from InterSystems Caché
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25" zv="Cache for UNIX (Red Hat Enterprise Linux for x86-64) 2015.2 (Build 664U)" ts="2016-04-05 13:01:04">
<Class name="DO.Utils">
<Super>%CSP.REST</Super>
<TimeChanged>64013,44584.975713</TimeChanged>
<TimeCreated>64013,41190.502637</TimeCreated>
<Parameter name="KEY">
<Description>
Your API token</Description>
<Default>1</Default>
</Parameter>
<Parameter name="IDLIST">
<Description>
Comma-separated list of droplet id's</Description>
<Default>111111,222222</Default>
</Parameter>
<Parameter name="SSLConfig">
<Description>
SSL configuration name</Description>
<Default>DO</Default>
</Parameter>
<XData name="UrlMap">
<XMLNamespace>http://www.intersystems.com/urlmap</XMLNamespace>
<Data><![CDATA[
<Routes>
<Route Url="/action/:dropletid/:action" Method="GET" Call="Action"/>
</Routes>
]]></Data>
</XData>
<Method name="Info">
<Description>
Do ##class(DO.Utils).Info()</Description>
<ClassMethod>1</ClassMethod>
<Implementation><![CDATA[
Set Request = ##class(%Net.HttpRequest).%New()
Set Request.Server = "api.digitalocean.com"
Set Request.Https = 1
Set Request.SSLConfiguration = ..#SSLConfig
Do Request.SetHeader("Authorization","Bearer " _ ..#KEY)
Set Request.Location = "/v2/droplets"
Do Request.SetParam("page",2)
Do Request.Get("",2)
]]></Implementation>
</Method>
<Method name="Action">
<Description>
Do ##class(DO.Utils).Action()</Description>
<ClassMethod>1</ClassMethod>
<FormalSpec>Id=-1,Action="reboot"</FormalSpec>
<Implementation><![CDATA[
Return:((Action'="reboot") && (Action'="power_cycle")) $$$ERROR($$$GeneralError, "Availible actions are ""reboot"" or ""power_cycle""")
Return:(..#IDLIST '[ Id) $$$ERROR($$$GeneralError, "This is not an approved ID")
Set Request = ##class(%Net.HttpRequest).%New()
Set Request.Server = "api.digitalocean.com"
Set Request.Https = 1
Set Request.SSLConfiguration = ..#SSLConfig
Set Request.ContentType = "application/json"
Do Request.SetHeader("Authorization","Bearer " _ ..#KEY)
Set Request.Location = "/v2/droplets/" _ Id _ "/actions"
Set Body = ##class(%ZEN.proxyObject).%New()
Set Body.type = Action
Do ##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(Request.EntityBody, Body)
Set st = Request.Post("")
Return:$$$ISERR(st) st
Set Data = Request.HttpResponse.Data
Write Data.Read($$$MaxCacheInt)
Return $$$OK
]]></Implementation>
</Method>
</Class>
</Export>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment