Created
March 18, 2013 20:12
-
-
Save holmboe/5190406 to your computer and use it in GitHub Desktop.
Some proposed changes to saltapi/__init__.py
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
''' | |
Make api awesomeness | |
''' | |
# Import Python libs | |
import inspect | |
# Import Salt libs | |
import salt.client | |
import salt.runner | |
import salt.wheel | |
import salt.utils | |
from salt.exceptions import SaltException | |
import urllib | |
import urlparse | |
import requests | |
from requests import RequestException | |
class APIClient(object): | |
''' | |
Provide a uniform method of accessing the various client interfaces in Salt | |
in the form of low-data data structures. For example: | |
>>> client = APIClient(__opts__) | |
>>> lowdata = {'client': 'local', 'tgt': '*', 'fun': 'test.ping', 'arg': ''} | |
>>> client.run(lowdata) | |
''' | |
def __init__(self, opts): | |
self.opts = opts | |
def run(self, low): | |
''' | |
Execute the specified function in the specified client by passing the | |
LowData | |
''' | |
if not 'client' in low: | |
raise SaltException('No client specified') | |
l_fun = getattr(self, low['client']) | |
f_call = salt.utils.format_call(l_fun, low) | |
ret = l_fun(*f_call.get('args', ()), **f_call.get('kwargs', {})) | |
return ret | |
def local_async(self, *args, **kwargs): | |
''' | |
Wrap LocalClient for running :ref:`execution modules <all-salt.modules>` | |
and immediately return the job ID. The results of the job can then be | |
retrieved at a later time. | |
''' | |
local = salt.client.LocalClient(self.opts['conf_file']) | |
return local.run_job(*args, **kwargs) | |
def local(self, *args, **kwargs): | |
''' | |
Wrap LocalClient for running :ref:`execution modules <all-salt.modules>` | |
''' | |
local = salt.client.LocalClient(self.opts['conf_file']) | |
return local.cmd(*args, **kwargs) | |
def runner(self, fun, **kwargs): | |
''' | |
Wrap RunnerClient for executing :ref:`runner modules <all-salt.runners>` | |
''' | |
runner = salt.runner.RunnerClient(self.opts) | |
return runner.low(fun, kwargs) | |
def wheel(self, fun, **kwargs): | |
''' | |
Wrap Wheel to enable executing :ref:`wheel modules <all-salt.wheel>` | |
''' | |
kwargs['fun'] = fun | |
wheel = salt.wheel.Wheel(self.opts) | |
return wheel.master_call(**kwargs) | |
def formaturl(base, *paths, **queryargs): | |
parts = [] | |
for path in paths: | |
path = path.lstrip('/').rstrip('/') | |
parts.extend(path.split('/')) | |
url = urlparse.urlsplit(base) | |
scheme, netloc, path, query, fragment = url | |
path = path + '/'.join(parts) | |
path += '/' if not path.endswith('/') else '' | |
if queryargs: | |
query = urllib.urlencode(queryargs) | |
url = urlparse.urlunsplit(data=(scheme,netloc,path,query,fragment)) | |
return url | |
class RESTAPIClient(APIClient): | |
def geturl(self, *paths, **queryargs): | |
return formaturl(self.opts['api_url'], *paths, **queryargs) | |
def remote(self, *args, **kwargs): | |
kwargs['client'] = 'local' | |
try: | |
r = requests.post(self.geturl(), data=kwargs) | |
except RequestException as exc: | |
raise | |
return r.content | |
def remote_async(self, *args, **kwargs): | |
kwargs['client'] = 'local_async' | |
try: | |
r = requests.post(self.geturl(), data=kwargs) | |
except RequestException as exc: | |
raise | |
return r.content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment