def create(self, entity='{}', _id=""): headers = { "content-type": "application/json", "accept": "application/octet-stream" } entity = entity.strip() if entity.endswith("}") or entity.endswith("]"): try: entity_json = json.loads(entity) except: raise SdkException('The passed JSON seems to be invalid.') return self._client.post(self.url + "/create/%s" % _id, json.dumps(entity_json), headers=headers) else: if not os.path.isfile(entity): raise WrongParameters("%s is not a file") with open(entity) as f: file_content = f.read().replace('\n', '') try: entity_json = json.loads(file_content) except: raise SdkException('The passed JSON seems to be invalid.') return self._client.post(self.url + "/create/%s" % _id, body=json.dumps(entity_json), headers=headers)
def create(self, entity, nsr_id, vnfr_id, vdu_id="", standby=False): entity = entity.strip() if entity.endswith("}") or entity.endswith("]"): try: entity_dict = json.loads(entity) except: raise SdkException('The passed JSON seems to be invalid.') else: if not os.path.isfile(entity): raise WrongParameters( "{} is neither a file nor does it seem to be valid JSON". format(entity)) with open(entity) as f: file_content = f.read().replace('\n', '') try: entity_dict = json.loads(file_content) except: raise SdkException('The passed JSON seems to be invalid.') if standby: if (vdu_id is None or vdu_id == ''): raise WrongParameters( 'When creating standby VNFCInstances you have to specify the VDU.' ) return self._client.post( "{}/{}/vnfrecords/{}/vdunits/{}/vnfcinstances/standby".format( self.url, nsr_id, vnfr_id, vdu_id), json.dumps(entity_dict)) else: return self._client.post( "{}/{}/vnfrecords/{}/vdunits/{}/vnfcinstances".format( self.url, nsr_id, vnfr_id, vdu_id), json.dumps(entity_dict))
def _update_entity(old_value, entity): try: new_entity = _format_update_value(entity) except: raise SdkException('The passed JSON seems to be invalid.') for k, v in new_entity.items(): if k in old_value: if type(old_value[k]) in WRONG_UPDATE_TYPES: raise SdkException( 'Cannot update a sub object, execute update directly to that entity' ) if isinstance(old_value[k], type(new_entity[k])): old_value[k] = new_entity[k] else: correct_type = type(old_value[k]) new_entity[k] = _cast(correct_type, new_entity[k]) old_value[k] = new_entity[k] else: raise SdkException('Key does not exist for the entity.') return old_value
def _cast(correct_type, new_entity): if correct_type == bool: true_ = new_entity.lower() == 'true' false_ = new_entity.lower() == 'false' if true_: return true_ if false_: return not false_ else: raise SdkException("Boolean value can only be 'true' or 'false'") else: return correct_type(new_entity)
def add(self, entity='', nsr_id='', vnfd_id='', _id="{}"): nsr_id = entity[0] vnfd_id = entity[1] param = entity[2:] if (param == ()): body = "{}" else: try: body = param[0] except: raise SdkException('The passed JSON seems to be invalid.') return self._client.put(self.url + "/%s/vnfd/%s" % (nsr_id, vnfd_id), body)
def create(self, entity='{}', _id=""): entity = entity.strip() if entity.endswith("}") or entity.endswith("]"): try: entity_json = json.loads(entity) except: raise SdkException('The passed JSON seems to be invalid.') result = json.loads( self._client.post(self.url + "/%s" % _id, json.dumps(entity_json))) return result else: if not os.path.isfile(entity): raise WrongParameters("{} is not a file".format(entity)) with open(entity) as f: file_content = f.read().replace('\n', '') try: entity_json = json.loads(file_content) except: raise SdkException('The passed JSON seems to be invalid.') return json.loads( self._client.post(self.url + "/%s" % _id, json.dumps(entity_json)))
def upgrade(self, entity='', nsr_id='', vnfr_id='', _id="{}"): nsr_id = entity[0] vnfr_id = entity[1] param = entity[2:] if (param == ()): body = "{}" else: try: body = param[0] except: raise SdkException('The passed JSON seems to be invalid.') return json.dumps( self._client.post( self.url + "/%s/vnfrecords/%s/upgrade" % (nsr_id, vnfr_id), body))
def create(self, entity='', _id=None): if os.path.exists(entity) and os.path.isfile(entity): with open(entity, 'r') as f: entity = f.read().replace('\n', '') entity = entity.strip() if entity.endswith("}") or entity.endswith("]"): try: entity_json = json.loads(entity) except: raise SdkException('The passed JSON seems to be invalid.') result = json.loads( self._client.post(self.url, json.dumps(entity_json))) return result else: # generate key = self._client.post("%s/%s" % (self.url, 'generate'), entity) return key
def update(self, _id, entity): file_path = entity[0] vnfpackage_id, script = _get_parents_obj_id_from_id( _id, self._main_agent, self.sub_obj) if os.path.exists(file_path) and os.path.isfile(file_path): with open(file_path) as f: file_content = json.dumps(f.read().replace('\n', '')) try: entity_json = json.loads(file_content) except: raise SdkException('The passed JSON seems to be invalid.') response = self._client.put_file( "{}/{}/{}/{}".format(self.url, vnfpackage_id, self.sub_url, _id), entity_json) script['payload'] = json.dumps(response) return script else: raise WrongParameters("{} is not a file".format(file_path))