Our company is using RightScale.
I’m using RightScale Client API to orchestrate deployment to RightScale. This gem is cool except it requires a Python dev to write Ruby script.
I was sufferred with writing Ruby script ( I don’t blame Ruby, just blame myself not good at it) until I have found Tortilla.
Basic Tortilla ( From the github page)
Tortilla uses a bit of magic to wrap APIs. Whenever you get or call an attribute of a wrapper, the URL is appended by that attribute’s name or method parameter. Let’s say we have the following code:
id, count = 71, 20
api = tortilla.wrap('https://api.example.org')
api.video(id).comments.get(count)
Every attribute and method call represents a part of the URL:
api -> https://api.example.org
.video -> /video
(id) -> /71
.comments -> /comments
.get(count) -> /20
Final URL -> https://api.example.org/video/71/comments/20
The last part of the chain (.get()
) executes the request. It also (optionally) appends one last part to the URL. Which allows you to do stuff like this:
api.video.get(id)
# instead of this
api.video(id).get()
So to summarize, getting attributes is used to define static parts of a URL and calling them is used to define dynamic parts of a URL.
Once you’ve chained everything together, Tortilla will execute the request and parse the response for you.
Nice?
Except at the moment, Tortilla only accepts JSON-formatted responses. Supporting more formats is on the roadmap for future Tortilla versions.
My Cheat
https://github.com/pilgrim2go/tortilla/commit/0c38b4192eae137584d62f0f061eb4cdbe25a1c1
Yet Another Rightscale Client
rightscale.py
class RightScale(object):
RS_ACCES_TOKEN = None
def __init__(self):
self.verbose = False
self.home = os.getcwd()
self.get_rs_authtoken()
def get_rs_authtoken(self):
if not RightScale.RS_ACCES_TOKEN:
# magic strings from the 1.5 api
DEFAULT_API_PREPATH = 'https://us-3.rightscale.com/api'
# authenticate here
OAUTH2_RES_PATH = '/'.join((DEFAULT_API_PREPATH, 'oauth2'))
login_data = {
'grant_type': 'refresh_token',
'refresh_token': os.getenv('RS_TOKEN'),
}
# client = self._client
response = requests.post(OAUTH2_RES_PATH, data=login_data, headers={'X_API_VERSION':'1.5'})
raw_token = response.json()
RightScale.RS_ACCES_TOKEN = "Bearer %s" % raw_token['access_token']
return RightScale.RS_ACCES_TOKEN
def get_rs_api(self,url='https://us-3.rightscale.com/api'):
if not url:
raise Exception('url can not be null')
api = tortilla.wrap(url, extension='json',debug=True)
api.headers.Authorization = self.get_rs_authtoken()
api.headers.X_API_VERSION='1.5'
return api
And how to use it
Terminate a server
import rightscale.py
rs = RightScale()
rs.get_rs_api().servers(server_id).terminate.post(extension=None)
I use extension=None to cheat Tortilla to call as a norma POST request.
It’s just a cheat. We better to use extension=http or any meaning name in addition to ‘json’.
Just one liner to terminate a server. Dangerous :p