def put(self, nw_id, virtualnetwork): """ This function implements update record functionality of the RESTful request. It converts the requested body in JSON format to dictionary in string representation and verifies whether required ATTRIBUTES are present in the PUT request body and adds the record to UCM if all the UCM_ATTRIBUTES are present. :return: Dictionary of the added record. """ virtualnetwork.id = nw_id virtualnetworks = list(self.conn.get_virtualnetworks(nw_id=virtualnetwork.id)) if len(virtualnetworks) < 1: raise EntityNotFound(_('Virtual Network'), nw_id) old_vn = VirtualNetwork.from_db_model(virtualnetworks[0]).as_dict(api_models.VirtualNetwork) updated_vn = virtualnetwork.as_dict(api_models.VirtualNetwork) old_vn.update(updated_vn) try: vn_in = api_models.VirtualNetwork(**old_vn) except Exception: LOG.exception("Error while putting virtual network: %s" % old_vn) error = _("Virtual Network incorrect") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error)) vn_out = self.conn.update_virtualnetwork(vn_in) #UCM Configuration Start if cfg.CONF.api.ucm_support: body = vn_out.as_dict() ucm_record = utils.generate_ucm_data(self, body, []) if UCM_LOADED: try: req = {'name': {'type': constants.DATA_TYPES['string'], 'value': str(vn_out.name)}, 'dmpath': constants.PATH_PREFIX + '.' + self.dmpath} try: rec = _ucm.get_exact_record(req) if not rec: error = _("Unable to find Virtual Network record in UCM") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error)) except UCMException, msg: LOG.info(_("UCM Exception raised. %s\n"), msg) error = _("Unable to find Virtual Network record in UCM") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error)) ret_val = _ucm.update_record(ucm_record) if ret_val != 0: error = _("Unable to add Virtual Network record to UCM") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error)) except UCMException, msg: LOG.info(_("UCM Exception raised. %s\n"), msg) error = _("Unable to Update Virtual Network record to UCM") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error))
def put(self, switch_id, switch): """ This function implements update record functionality of the RESTful request. It converts the requested body in JSON format to dictionary in string representation and verifies whether required ATTRIBUTES are present in the PUT request body and adds the record to UCM if all the UCM_ATTRIBUTES are present. :return: Dictionary of the updated record. """ switch.id = switch_id switches = list(self.conn.get_switchs(switch_id=switch.id)) if len(switches) < 1: raise EntityNotFound(_('Switch'), switch_id) old_switch = Switch.from_db_model(switches[0]).as_dict(api_models.Switch) updated_switch = switch.as_dict(api_models.Switch) old_switch.update(updated_switch) try: sw_in = api_models.Switch(**old_switch) except Exception: LOG.exception("Error while putting switch: %s" % old_switch) error = _("Switch incorrect") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error)) switch_out = self.conn.update_virtualmachine(sw_in) #UCM Support Start if cfg.CONF.api.ucm_support: body = switch_out.as_dict() ucm_record = utils.generate_ucm_data(self, body, []) if UCM_LOADED: try: req = {'name': {'type': constants.DATA_TYPES['string'], 'value': str(switch.name)}, 'dmpath': constants.PATH_PREFIX + '.' + self.dmpath} try: rec = _ucm.get_exact_record(req) if not rec: error = _("Unable to find Switch record in UCM") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error)) except UCMException, msg: LOG.info(_("UCM Exception raised. %s\n"), msg) error = _("Unable to find Switch record in UCM") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error)) ret_val = _ucm.update_record(ucm_record) if ret_val != 0: error = _("Unable to add Switch record to UCM") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error)) except UCMException, msg: LOG.info(_("UCM Exception raised. %s\n"), msg) error = _("Unable to Update Switch record to UCM") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error))
def put(self, nw_id, sub_id, subnet): """ This function implements update record functionality of the RESTful request. It converts the requested body in JSON format to dictionary in string representation and verifies whether required ATTRIBUTES are present in the PUT request body and adds the record to UCM if all the UCM_ATTRIBUTES are present. :return: Dictionary of the added record. """ virtualnetworks = list(self.conn.get_virtualnetworks(nw_id=nw_id)) if len(virtualnetworks) < 1: raise EntityNotFound(_('Virtual Network'), nw_id) sn = subnet.as_dict(api_models.Subnet) if 'dns_servers' in sn: for ip in sn['dns_servers']: try: validate_ip(ip) except Exception: LOG.exception("Error while putting subnet: %s" % str(sn)) error = _("Subnet incorrect. Invalid DNS Server IP") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error)) subnets = list(self.conn.get_subnets(sub_id=sub_id, nw_id=nw_id)) if len(subnets) < 1: raise EntityNotFound(_('Subnet'), sub_id) subnets[0].pools = self.pools_from_db_model(subnets[0].pools) old_subnet = Subnet.from_db_model(subnets[0]).as_dict(api_models.Subnet) old_subnet.update(sn) try: sub_in = api_models.Subnet(**old_subnet) except Exception: LOG.exception("Error while putting subnet: %s" % old_subnet) error = _("Subnet incorrect") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error)) sub = self.conn.update_subnet(sub_in) sub.pools = self.pools_from_db_model(sub.pools, final=True) #UCM Configuration Start if cfg.CONF.api.ucm_support: body = sub.as_dict() pools = [] for pool in body['pools']: pools.append(pool.as_dict(api_models.Pool)) body['pools'] = pools ucm_record = utils.generate_ucm_data(self, body, []) del ucm_record['pool_start_addr'] del ucm_record['pool_end_addr'] if len(body['dns_servers']): for i in range(0, len(sub.dns_servers)): ucm_record['dns_server'+str(i + 1)] = {'type': constants.DATA_TYPES['ipaddr'], 'value': sub.dns_servers[i]} else: for i in range(0, 4): ucm_record['dns_server'+str(i + 1)] = {'type': constants.DATA_TYPES['ipaddr'], 'value': '0.0.0.0'} if UCM_LOADED: try: req = {'name': {'type': constants.DATA_TYPES['string'], 'value': str(sub.name)}, 'dmpath': constants.PATH_PREFIX + '.' + self.dmpath} try: rec = _ucm.get_exact_record(req) if not rec: error = _("Unable to find Subnet record in UCM") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error)) except UCMException, msg: LOG.info(_("UCM Exception raised. %s\n"), msg) error = _("Unable to find Subnet record in UCM") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error)) ret_val = _ucm.update_record(ucm_record) if ret_val != 0: error = _("Unable to add Subnet record to UCM") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error)) except UCMException, msg: LOG.info(_("UCM Exception raised. %s\n"), msg) error = _("Unable to Update Subnet record to UCM") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error))