Commit 8652fba3 authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

slap command line: update request and supply to use jIO API

parent e42bce91
......@@ -163,18 +163,45 @@ def do_request(logger, conf, local):
if conf.parameters_file:
parameters = getParametersFromFile(conf.parameters_file, software_schema_serialisation)
try:
partition = local['slap'].registerOpenOrder().request(
software_release=conf.software_url,
partition_reference=conf.reference,
partition_parameter_kw=parameters,
software_type=conf.type,
filter_kw=conf.node,
state=conf.state,
shared=conf.slave
)
logger.info('Instance requested.\nState is : %s.', partition.getState())
if local['slap'].jio_api_connector:
request_dict = {
"title": conf.reference,
"portal_type": "Software Instance",
"software_release_uri": conf.software_url,
}
if conf.state:
request_dict['state'] = conf.state
if conf.slave:
request_dict["shared"] = True
if conf.node:
request_dict["sla_parameters"] = conf.node
if conf.parameters:
request_dict["parameters"] = json.dumps(conf.parameters)
if conf.type:
request_dict["software_type"] = conf.type
partition_dict = local['slap'].jio_api_connector.post(request_dict)
if "$schema" in partition_dict and "error-response-schema.json" in partition_dict["$schema"]:
logger.warning('Server Response: %s' % json.dumps(partition_dict, indent=2))
logger.warning('Instance requested. Master is provisioning it. Please rerun in a '
'couple of minutes to get connection information.')
exit(2)
instance_state = partition_dict["state"]
connection_parameter_dict = partition_dict["connection_parameters"]
else:
partition = local['slap'].registerOpenOrder().request(
software_release=conf.software_url,
partition_reference=conf.reference,
partition_parameter_kw=parameters,
software_type=conf.type,
filter_kw=conf.node,
state=conf.state,
shared=conf.slave
)
instance_state = partition.getState()
connection_parameter_dict = partition.getConnectionParameterDict()
logger.info('Instance requested.\nState is : %s.', instance_state)
logger.info('Connection parameters of instance are:')
connection_parameter_dict = partition.getConnectionParameterDict()
if software_schema_serialisation == SoftwareReleaseSerialisation.JsonInXml:
if '_' in connection_parameter_dict:
connection_parameter_dict = json.loads(connection_parameter_dict['_'])
......
......@@ -27,6 +27,8 @@
#
##############################################################################
import json
from slapos.cli.config import ClientConfigCommand
from slapos.client import init, ClientConfig, _getSoftwareReleaseFromSoftwareString
......@@ -63,10 +65,21 @@ def do_supply(logger, software_release, computer_id, local):
software_release = _getSoftwareReleaseFromSoftwareString(
logger, software_release, local['product'])
local['supply'](
software_release=software_release,
computer_guid=computer_id,
state='available'
)
if local['slap'].jio_api_connector:
software_installation = local['slap'].jio_api_connector.post({
"portal_type": "Software Installation",
"software_release_uri": software_release,
"compute_node_id": computer_id,
"state": 'available'
})
if "$schema" in software_installation and "error-response-schema.json" in software_installation["$schema"]:
logger.warning('Issue during the request. Server Response: %s' % json.dumps(software_installation, indent=2))
logger.warning('Message: %s' % software_installation.get("message", ""))
exit(2)
else:
local['supply'](
software_release=software_release,
computer_guid=computer_id,
state='available'
)
logger.info('Done.')
......@@ -86,7 +86,8 @@ class ConnectionHelper:
self.session = CacheControl(self.uncached_session,
cache=FileCache(os.path.expanduser("~/.slapos_cached_get")))
def do_request(self, method, path, params=None, data=None, headers=None):
def do_request(self, method, path, params=None, data=None, headers=None,
expect_json_error=False):
url = parse.urljoin(self.slapgrid_uri, path)
if headers is None:
headers = {}
......@@ -106,7 +107,7 @@ class ConnectionHelper:
# Old behavior was to pass empty parameters as "None" value.
# Behavior kept for compatibility with old slapproxies (< v1.3.3).
# Can be removed when old slapproxies are no longer in use.
if data:
if data and isinstance(data, dict):
for k, v in six.iteritems(data):
if v is None:
data[k] = 'None'
......@@ -131,6 +132,8 @@ class ConnectionHelper:
"enabled on your machine and that the server is available. The "
"original error was:\n%s" % exc)
except requests.HTTPError as exc:
if expect_json_error and not int(exc.response.status_code) >= 500:
return req
if exc.response.status_code == requests.status_codes.codes.not_found:
msg = url
if params:
......
......@@ -741,7 +741,34 @@ class SlapConnectionHelper(ConnectionHelper):
return loads(xml)
class JioAPIConnectionHelper(ConnectionHelper):
def apiCall(self, path, data):
req = self.do_request(requests.post,
path=path,
data=json.dumps(data),
headers={'Content-type': 'application/json'},
expect_json_error=True)
return req.json()
def get(self, data):
return self.apiCall(path="get/",
data=data)
def post(self, data):
return self.apiCall(path="post/",
data=data)
def put(self, data):
return self.apiCall(path="put/",
data=data)
def allDocs(self, data):
return self.apiCall(path="allDocs/",
data=data)
getHateoasUrl_cache = {}
getjIOAPI_cache = {}
@implementer(interface.slap)
class slap:
......@@ -749,10 +776,10 @@ class slap:
key_file=None, cert_file=None,
master_ca_file=None,
timeout=60,
slapgrid_rest_uri=None):
slapgrid_rest_uri=None,
slapgrid_jio_uri=None):
if master_ca_file:
raise NotImplementedError('Master certificate not verified in this version: %s' % master_ca_file)
self._connection_helper = SlapConnectionHelper(
slapgrid_uri, key_file, cert_file, master_ca_file, timeout)
......@@ -777,6 +804,27 @@ class slap:
else:
self._hateoas_navigator = None
if not slapgrid_jio_uri:
getjIOAPI_cache_key = (slapgrid_uri, key_file, cert_file, master_ca_file, timeout)
try:
slapgrid_jio_uri = getjIOAPI_cache[getjIOAPI_cache_key]
except KeyError:
pass
if not slapgrid_jio_uri:
try:
slapgrid_jio_uri = getjIOAPI_cache[getjIOAPI_cache_key] = \
bytes2str(self._connection_helper.GET('getJIOAPIUrl'))
except:
pass
if slapgrid_jio_uri:
self.jio_api_connector = JioAPIConnectionHelper(
slapgrid_jio_uri,
key_file, cert_file,
master_ca_file, timeout
)
else:
self.jio_api_connector = None
# XXX-Cedric: this method is never used and thus should be removed.
def registerSoftwareRelease(self, software_release):
"""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment