from osmclient.common.exceptions import NotFound
import yaml
import json
+import logging
class Vim(object):
def __init__(self, http=None, client=None):
self._http = http
self._client = client
+ self._logger = logging.getLogger('osmclient')
self._apiName = '/admin'
self._apiVersion = '/v1'
self._apiResource = '/vim_accounts'
self._apiBase = '{}{}{}'.format(self._apiName,
self._apiVersion, self._apiResource)
+
# VIM '--wait' option
def _wait(self, id, deleteFlag=False):
+ self._logger.debug("")
+ self._client.get_token()
# Endpoint to get operation status
apiUrlStatus = '{}{}{}'.format(self._apiName, self._apiVersion, '/vim_accounts')
# Wait for status for VIM instance creation/deletion
deleteFlag=deleteFlag)
def _get_id_for_wait(self, name):
- # Returns id of name, or the id itself if given as argument
+ """ Returns id of name, or the id itself if given as argument
+ """
+ self._logger.debug("")
+ self._client.get_token()
for vim in self.list():
if name == vim['uuid']:
return vim['uuid']
return ''
def create(self, name, vim_access, sdn_controller=None, sdn_port_mapping=None, wait=False):
+ self._logger.debug("")
+ self._client.get_token()
if 'vim-type' not in vim_access:
#'openstack' not in vim_access['vim-type']):
raise Exception("vim type not provided")
http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
postfields_dict=vim_account)
- #print 'HTTP CODE: {}'.format(http_code)
- #print 'RESP: {}'.format(resp)
- if http_code in (200, 201, 202, 204):
- if resp:
- resp = json.loads(resp)
- if not resp or 'id' not in resp:
- raise ClientException('unexpected response from server - {}'.format(
- resp))
- if wait:
- # Wait for status for VIM instance creation
- self._wait(resp.get('id'))
- print(resp['id'])
- else:
- msg = ""
- if resp:
- try:
- msg = json.loads(resp)
- except ValueError:
- msg = resp
- raise ClientException("failed to create vim {} - {}".format(name, msg))
+ #print('HTTP CODE: {}'.format(http_code))
+ #print('RESP: {}'.format(resp))
+ #if http_code in (200, 201, 202, 204):
+ if resp:
+ resp = json.loads(resp)
+ if not resp or 'id' not in resp:
+ raise ClientException('unexpected response from server - {}'.format(
+ resp))
+ if wait:
+ # Wait for status for VIM instance creation
+ self._wait(resp.get('id'))
+ print(resp['id'])
+ #else:
+ # msg = ""
+ # if resp:
+ # try:
+ # msg = json.loads(resp)
+ # except ValueError:
+ # msg = resp
+ # raise ClientException("failed to create vim {} - {}".format(name, msg))
def update(self, vim_name, vim_account, sdn_controller, sdn_port_mapping, wait=False):
+ self._logger.debug("")
+ self._client.get_token()
vim = self.get(vim_name)
vim_id_for_wait = self._get_id_for_wait(vim_name)
vim_config = {}
vim_config['sdn-port-mapping'] = yaml.safe_load(f.read())
vim_account['config'] = vim_config
#vim_account['config'] = json.dumps(vim_config)
- http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,vim['_id']),
+ http_code, resp = self._http.patch_cmd(endpoint='{}/{}'.format(self._apiBase,vim['_id']),
postfields_dict=vim_account)
- # print 'HTTP CODE: {}'.format(http_code)
- # print 'RESP: {}'.format(resp)
- if http_code in (200, 201, 202, 204):
- if wait:
- # In this case, 'resp' always returns None, so 'resp['id']' cannot be used.
- # Use the previously obtained id instead.
- wait_id = vim_id_for_wait
- # Wait for status for VI instance update
- self._wait(wait_id)
- else:
- pass
- else:
- msg = ""
- if resp:
- try:
- msg = json.loads(resp)
- except ValueError:
- msg = resp
- raise ClientException("failed to update vim {} - {}".format(vim_name, msg))
+ # print('HTTP CODE: {}'.format(http_code))
+ # print('RESP: {}'.format(resp))
+ #if http_code in (200, 201, 202, 204):
+ if wait:
+ # In this case, 'resp' always returns None, so 'resp['id']' cannot be used.
+ # Use the previously obtained id instead.
+ wait_id = vim_id_for_wait
+ # Wait for status for VI instance update
+ self._wait(wait_id)
+ # else:
+ # pass
+ #else:
+ # msg = ""
+ # if resp:
+ # try:
+ # msg = json.loads(resp)
+ # except ValueError:
+ # msg = resp
+ # raise ClientException("failed to update vim {} - {}".format(vim_name, msg))
def update_vim_account_dict(self, vim_account, vim_access):
+ self._logger.debug("")
vim_account['vim_type'] = vim_access['vim-type']
vim_account['description'] = vim_access['description']
vim_account['vim_url'] = vim_access['vim-url']
def get_id(self, name):
"""Returns a VIM id from a VIM name
"""
+ self._logger.debug("")
for vim in self.list():
if name == vim['name']:
return vim['uuid']
raise NotFound("vim {} not found".format(name))
def delete(self, vim_name, force=False, wait=False):
+ self._logger.debug("")
+ self._client.get_token()
vim_id = vim_name
if not utils.validate_uuid4(vim_name):
vim_id = self.get_id(vim_name)
querystring = '?FORCE=True'
http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
vim_id, querystring))
- #print 'HTTP CODE: {}'.format(http_code)
- #print 'RESP: {}'.format(resp)
+ #print('HTTP CODE: {}'.format(http_code))
+ #print('RESP: {}'.format(resp))
if http_code == 202:
if wait:
# When deleting an account, 'resp' may be None.
elif http_code == 204:
print('Deleted')
else:
- msg = ""
- if resp:
- try:
- msg = json.loads(resp)
- except ValueError:
- msg = resp
+ msg = resp or ""
+ # if resp:
+ # try:
+ # msg = json.loads(resp)
+ # except ValueError:
+ # msg = resp
raise ClientException("failed to delete vim {} - {}".format(vim_name, msg))
def list(self, filter=None):
"""Returns a list of VIM accounts
"""
+ self._logger.debug("")
+ self._client.get_token()
filter_string = ''
if filter:
filter_string = '?{}'.format(filter)
- resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string))
+ _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string))
if not resp:
return list()
vim_accounts = []
- for datacenter in resp:
+ for datacenter in json.loads(resp):
vim_accounts.append({"name": datacenter['name'], "uuid": datacenter['_id']
if '_id' in datacenter else None})
return vim_accounts
def get(self, name):
"""Returns a VIM account based on name or id
"""
+ self._logger.debug("")
+ self._client.get_token()
vim_id = name
if not utils.validate_uuid4(name):
vim_id = self.get_id(name)
- resp = self._http.get_cmd('{}/{}'.format(self._apiBase,vim_id))
- if not resp or '_id' not in resp:
- raise ClientException('failed to get vim info: '.format(
- resp))
- else:
+ try:
+ _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase,vim_id))
+ if resp:
+ resp = json.loads(resp)
+ if not resp or '_id' not in resp:
+ raise ClientException('failed to get vim info: {}'.format(resp))
return resp
- raise NotFound("vim {} not found".format(name))
+ except NotFound:
+ raise NotFound("vim '{}' not found".format(name))