def create_record(self, context, record, zone_id): # insert operation history type with Creating in DB input_str = json.dumps(record) input_operation_history = {} input_operation_history['input'] = input_str input_operation_history['method'] = 'CREATE' input_operation_history['status'] = 'FAILED' history = self.db_common.insert_operation_history( context, **input_operation_history) # check zone_record is or not exsit target_record = self.is_exist_zone_record(context, record) if target_record is not None: LOG.warning(_LW("the record object with name = %(record)s" " already exists in DB"), {"record": record['name']}) raise exception.HaveSameObject(object_name=record['name']) # check zone is or not exsit target_zone = self.is_exist_zone(context, zone_id) # dev_zone_id is zone_id of device if target_zone is None: raise exception.IsNotExistError(param_name=zone_id) dev_zone_id = target_zone["zone_id"] LOG.info(_LI("the zone object with id=%(zone_id)s is existed"), {"zone_id": zone_id}) json_name = record['name'] zones = self.get_zone_name_bytenant_id(context, record['tenant_id']) if len(zones) == 0: raise exception.IsNotExistError(param_name="tenant_id") flag = True for zone in zones: dev_zone_name = zone["zone_name"] dev_zone_name = '%s%s' % ('.', dev_zone_name) if json_name.endswith(dev_zone_name): flag = False break if flag: raise exception.ZoneOfRecordIsError(json_name=json_name) record_values = self._make_dns_record_object(record, zone_id) record_obj = objects.DnsZoneRrs(context, **record_values) # return response from DB response = self._create_in_storage(context, record_obj) new_name = tools.clean_end_str(dev_zone_name, record['name']) record['name'] = new_name try: response_dev = self.rpc_api.create_record(context, record, dev_zone_id) except Exception as e: LOG.error(_LE("Create response on device failed")) # since create failed in device, so delete object in DB self.delete_rrs_info(context, response["id"]) raise e # update dns_rrs_info table, since record id would be changed response = self.update_rrs_info(context, response["id"], None, response_dev) # update operation history type with Failed in DB input_operation_history['status'] = 'SUCCESS' self.db_common.update_operation_history( context, history.id, **input_operation_history) return response
def get_all_db_zone(self, context): """Todo call DB to get all zones""" # init the DB operations object zone_obj = objects.DnsZone(context) # Filter the data that has been disabled zone_name_dic = {} zone_name_dic['deleted'] = False # Todo call DB to get all zones zone_objs = zone_obj.get_objects(context, **zone_name_dic) if zone_objs is None: LOG.warning(_LW("There is no data in the DNS_ZONE_INFO")) raise exception.IsNotExistError(param_name="Zone with id=" + id) return zone_objs
def get_zone_db_details(self, context, id): """Todo call DB to get one zone""" # init the DB operations object zone_obj = objects.DnsZone(context) zone_name_dic = {} zone_name_dic['id'] = id zone_name_dic['deleted'] = False # try/catch the no one get try: # Todo call DB to get one zone by id zone_obj = zone_obj.get_object(context, **zone_name_dic) except Exception: LOG.warning(_LW("No zone with id=%(id)s in DB"), {"id": id}) raise exception.IsNotExistError(param_name="Zone with id=" + id) return zone_obj
def safe_rstrip(value, chars=None): """Removes trailing characters from a string if that does not make it empty :param value: A string value that will be stripped. :param chars: Characters to remove. :return: Stripped value. """ if not isinstance(value, six.string_types): LOG.warning( _LW( "Failed to remove trailing character. Returning " "original object. Supplied object is not a string: " "%s," ), value, ) return value return value.rstrip(chars) or value
def __init__(self, message=None, **kwargs): self.kwargs = kwargs if 'code' not in self.kwargs: try: self.kwargs['code'] = self.code except AttributeError: pass if not message: # Check if class is using deprecated 'message' attribute. if (hasattr(self, 'message') and self.message): LOG.warning( _LW("Exception class: %s Using the 'message' " "attribute in an exception has been " "deprecated. The exception class should be " "modified to use the '_msg_fmt' " "attribute."), self.__class__.__name__) self._msg_fmt = self.message try: message = self._msg_fmt % kwargs except Exception as e: # kwargs doesn't match a variable in self._msg_fmt # log the issue and the kwargs LOG.exception(_LE('Exception in string format operation')) for name, value in kwargs.items(): LOG.error("%s: %s" % (name, value)) if CONF.fatal_exception_format_errors: raise e else: # at least get the core self._msg_fmt out if something # happened message = self._msg_fmt super(Nca47Exception, self).__init__(message)
def __init__(self, message=None, **kwargs): self.kwargs = kwargs if 'code' not in self.kwargs: try: self.kwargs['code'] = self.code except AttributeError: pass if not message: # Check if class is using deprecated 'message' attribute. if (hasattr(self, 'message') and self.message): LOG.warning(_LW("Exception class: %s Using the 'message' " "attribute in an exception has been " "deprecated. The exception class should be " "modified to use the '_msg_fmt' " "attribute."), self.__class__.__name__) self._msg_fmt = self.message try: message = self._msg_fmt % kwargs except Exception as e: # kwargs doesn't match a variable in self._msg_fmt # log the issue and the kwargs LOG.exception(_LE('Exception in string format operation')) for name, value in kwargs.items(): LOG.error("%s: %s" % (name, value)) if CONF.fatal_exception_format_errors: raise e else: # at least get the core self._msg_fmt out if something # happened message = self._msg_fmt super(Nca47Exception, self).__init__(message)
def create_zone(self, context, zone): """ create zone handling DB operations and calling rpc client's corresponding method to send messaging to agent endpoints """ # change the zone values with dic format target_values = self._make_dns_zone_object(zone) # init the DB operations object zone_obj = objects.DnsZone(context, **target_values) # Check the zone which have same name if is exist in DB target_zone = self._valid_if_zone_exist(context, zone_obj) if target_zone is not None: LOG.warning(_LW("Have same zone id/name in DB")) raise exception.HaveSameObject(object_name=target_zone.zone_name) # insert operation history type with Creating in DB input_str = json.dumps(zone) input_operation_history = {} input_operation_history['input'] = input_str input_operation_history['method'] = 'CREATE' input_operation_history['status'] = 'FAILED' history = self.db_common.insert_operation_history( context, **input_operation_history) # create the zone info in db db_zone_obj = self._create_in_storage(context, zone_obj) try: # handling create zone method in RPC response = self.rpc_api.create_zone(context, zone) # get the default zone records zone_id = target_values['zone_id'] rrs_results = self.rpc_api.get_records(context, zone_id) for resourc in rrs_results['resources']: records = {} records['rrs_id'] = resourc['id'] records['zone_id'] = db_zone_obj['id'] records['rrs_name'] = resourc['name'] records['type'] = resourc['type'] records['ttl'] = resourc['ttl'] records['klass'] = resourc['klass'] records['rdata'] = resourc['rdata'] # init the DB operations objec with zone_record zone_rrs_obj = objects.DnsZoneRrs(context, **records) # create the zone info in db self._create_in_storage(context, zone_rrs_obj) except Exception as e: LOG.error(_LE("Create corresponding response on device failed")) # get the default zone records from db rrs_dic = {} rrs_dic['zone_id'] = db_zone_obj['id'] rrs_dic['deleted'] = False rrs_obj = objects.DnsZoneRrs(context, **rrs_dic) zone_records = rrs_obj.get_objects(context, **rrs_dic) if zone_records is not None: # get the all id of the zone_records del_rrs_obj = objects.DnsZoneRrs(context) for record in zone_records: # delete the DB operations objec with zone_record del_rrs_obj.delete(context, record['id']) # DB rollback since create zone failed in Device zone_obj.delete(context, db_zone_obj['id']) # handling delete zone method in RPC response = self.rpc_api.delete_zone(context, zone, zone_id) raise e # update operation history type with Failed in DB input_operation_history['status'] = 'SUCCESS' self.db_common.update_operation_history( context, history.id, **input_operation_history) return db_zone_obj