- self._apiResource = '/ns_instances'
- self._apiBase = '{}{}{}'.format(self._apiName,
- self._apiVersion, self._apiResource)
- endpoint = '{}/{}/{}'.format(self._apiBase, ns['_id'], op_name)
- #print 'OP_NAME: {}'.format(op_name)
- #print 'OP_DATA: {}'.format(json.dumps(op_data))
- http_code, resp = self._http.post_cmd(endpoint=endpoint, postfields_dict=op_data)
- #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))
- print(resp['id'])
+ ns = self.get(name)
+ self._apiResource = "/ns_instances"
+ self._apiBase = "{}{}{}".format(
+ self._apiName, self._apiVersion, self._apiResource
+ )
+ endpoint = "{}/{}/{}".format(self._apiBase, ns["_id"], op_name)
+ # print('OP_NAME: {}'.format(op_name))
+ # print('OP_DATA: {}'.format(json.dumps(op_data)))
+ http_code, resp = self._http.post_cmd(
+ endpoint=endpoint, postfields_dict=op_data
+ )
+ # 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 NS instance action
+ # For the 'action' operation, 'id' is used
+ self._wait(resp.get("id"), wait)
+ return resp["id"]
+ # else:
+ # msg = ""
+ # if resp:
+ # try:
+ # msg = json.loads(resp)
+ # except ValueError:
+ # msg = resp
+ # raise ClientException(msg)
+ except ClientException as exc:
+ message = "failed to exec operation {}:\nerror:\n{}".format(name, str(exc))
+ raise ClientException(message)
+
+ def cancel_op(self, operation_id, cancel_mode, wait=False):
+ """Cancels an LCM operation"""
+ self._client.get_token()
+ self._apiResource = "/ns_lcm_op_occs"
+ self._apiBase = "{}{}{}".format(
+ self._apiName, self._apiVersion, self._apiResource
+ )
+ endpoint = "{}/{}/cancel".format(self._apiBase, operation_id)
+ op_data = {"cancelMode": cancel_mode}
+ try:
+ http_code, resp = self._http.post_cmd(
+ endpoint=endpoint, postfields_dict=op_data
+ )
+ if http_code == 202:
+ if wait:
+ self._wait(operation_id, wait, deleteFlag=True, entity="OPCANCEL")
+ else:
+ print("Cancellation in progress")