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))
def post(self, nw_id, subnet): """ This function implements create 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 POST request body and adds the record to UCM if all the UCM_ATTRIBUTES are present. :return: Dictionary of the added record. """ sn = subnet.as_dict(api_models.Subnet) for ip in sn['dns_servers']: validate_ip(ip) subnets = list(self.conn.get_subnets(sub_id=subnet.id)) if len(subnets) > 0: error = _("Subnet with the given id exists") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error)) virtualnetworks = list(self.conn.get_virtualnetworks(nw_id=nw_id)) if len(virtualnetworks) < 1: raise EntityNotFound(_('Virtual Network'), nw_id) sn['nw_id'] = nw_id # Expand Pools and update the details with IDs pools = [] for pool in sn['pools']: pool.id = str(uuid.uuid4()) pool.subnet_id = sn['id'] pools.append(api_models.Pool(**pool.as_dict(api_models.Pool))) sn['pools'] = pools try: sub_in = api_models.Subnet(**sn) except Exception: LOG.exception("Error while posting Subnet: %s" % sn) error = _("Virtual Network incorrect") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error)) sub = self.conn.create_subnet(sub_in) sub.pools = self.pools_from_db_model(sub.pools, final=True) sub.vnname = VirtualNetwork.from_db_model(virtualnetworks[0]).name #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, []) ucm_record['cidrip']['value'], ucm_record['cidrmask']['value'] = decode_cidr(body['cidr']) if len(body['pools']): ucm_record['pool_start_addr']['value'] = body['pools'][0]['start'] ucm_record['pool_end_addr']['value'] = body['pools'][0]['end'] 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]} if UCM_LOADED: try: ret_val = _ucm.add_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 add Subnet record to UCM") response.translatable_error = error raise wsme.exc.ClientSideError(unicode(error))