def create(self, request, **kwargs): """Create a new Credential.""" context = request.context self._check_admin(context) body = validators.validate_request(request) key_list = ['name', 'snmpv1', 'snmpv2c', 'snmpv3', 'netconf_ssh', 'netconf_soap'] keys = body.keys() validators.validate_attributes(keys, key_list) protocol = validators.validate_access_parameters(body) if protocol in ['snmpv1', 'snmpv2c', 'snmpv3']: db_snmp_cred = self._create_snmp_creds(context, body, protocol) db_snmp_cred = self._creds_to_show(db_snmp_cred) return {const.BNP_CREDENTIAL_RESOURCE_NAME: dict(db_snmp_cred)} else: if body[protocol].get('password'): body[protocol]['password'] = credential_manager.create_secret( body[protocol]['password']) db_netconf_cred = self._create_netconf_creds( context, body, protocol) db_netconf_cred = self._creds_to_show(db_netconf_cred) return {const.BNP_CREDENTIAL_RESOURCE_NAME: dict(db_netconf_cred)}
def update(self, request, id, **kwargs): context = request.context self._check_admin(context) body = validators.validate_request(request) protocol = validators.validate_access_parameters_for_update(body) key_list = ['name', 'snmpv1', 'snmpv2c', 'snmpv3', 'netconf_ssh', 'netconf_soap'] keys = body.keys() validators.validate_attributes(keys, key_list) if not uuidutils.is_uuid_like(id): raise webob.exc.HTTPBadRequest( _("Invalid Id")) if not protocol: switch_creds = db.get_snmp_cred_by_id(context, id) if switch_creds: switch_creds_dict = self._update_dict(body, dict(switch_creds)) db.update_bnp_snmp_cred_by_id(context, id, switch_creds_dict) return switch_creds_dict switch_creds = db.get_netconf_cred_by_id(context, id) if switch_creds: if switch_creds.get('password'): password = credential_manager.retrieve_secret( switch_creds['password']) credential_manager.delete_secret(switch_creds['password']) switch_creds['password'] = password switch_creds_dict = self._update_dict(body, dict(switch_creds)) if switch_creds_dict.get('password'): password = credential_manager.create_secret( switch_creds_dict['password']) switch_creds_dict['password'] = password db.update_bnp_netconf_cred_by_id( context, id, switch_creds_dict) return switch_creds_dict raise webob.exc.HTTPNotFound( _("Credential with id=%s does not exist") % id) elif protocol in [const.SNMP_V1, const.SNMP_V2C]: switch_creds = db.get_snmp_cred_by_id(context, id) if not switch_creds: raise webob.exc.HTTPNotFound( _("Credential with id=%s does not exist") % id) self.check_creds_proto_type(switch_creds, id, protocol) params = body.pop(protocol) for key, value in params.iteritems(): body[key] = value creds_dict = self._update_dict(body, dict(switch_creds)) db.update_bnp_snmp_cred_by_id(context, id, creds_dict) return creds_dict elif protocol == const.SNMP_V3: switch_creds = db.get_snmp_cred_by_id(context, id) if not switch_creds: raise webob.exc.HTTPNotFound( _("Credential with id=%s does not exist") % id) self.check_creds_proto_type(switch_creds, id, protocol) params = body.pop(protocol) if ('auth_protocol' in params.keys()) ^ ( 'auth_key' in params.keys()): if (not switch_creds['auth_protocol']) and ( not switch_creds['auth_key']): raise webob.exc.HTTPBadRequest( _("auth_protocol and auth_key values does not exist," " so both has to be provided")) if ('priv_protocol' in params.keys()) ^ ('priv_key' in params.keys()): if (not switch_creds['priv_protocol']) and ( not switch_creds['priv_key']): raise webob.exc.HTTPBadRequest( _("priv_protocol and priv_key values does not exist," " so both has to be provided")) for key, value in params.iteritems(): body[key] = value creds_dict = self._update_dict(body, dict(switch_creds)) db.update_bnp_snmp_cred_by_id(context, id, creds_dict) return creds_dict elif protocol == const.NETCONF_SOAP: switch_creds = db.get_netconf_cred_by_id(context, id) if not switch_creds: raise webob.exc.HTTPNotFound( _("Credential with id=%s does not exist") % id) self.check_creds_proto_type(switch_creds, id, protocol) params = body.pop(protocol) for key, value in params.iteritems(): body[key] = value if switch_creds.get('password'): password = credential_manager.retrieve_secret( switch_creds['password']) credential_manager.delete_secret(switch_creds['password']) switch_creds['password'] = password creds_dict = self._update_dict(body, dict(switch_creds)) if creds_dict.get('password'): creds_dict['password'] = credential_manager.create_secret( creds_dict['password']) db.update_bnp_netconf_cred_by_id(context, id, creds_dict) return creds_dict elif protocol == const.NETCONF_SSH: switch_creds = db.get_netconf_cred_by_id(context, id) if not switch_creds: raise webob.exc.HTTPNotFound( _("Credential with id=%s does not exist") % id) self.check_creds_proto_type(switch_creds, id, protocol) params = body.pop(protocol) if ('user_name' in params.keys()) ^ ('password' in params.keys()): if (not switch_creds['user_name']) and ( not switch_creds['password']): raise webob.exc.HTTPBadRequest( _("user_name and password values does not exist, so" " both has to be provided")) for key, value in params.iteritems(): body[key] = value if switch_creds.get('password'): password = credential_manager.retrieve_secret( switch_creds['password']) credential_manager.delete_secret(switch_creds['password']) switch_creds['password'] = password creds_dict = self._update_dict(body, dict(switch_creds)) if creds_dict.get('password'): creds_dict['password'] = credential_manager.create_secret( creds_dict['password']) db.update_bnp_netconf_cred_by_id(context, id, creds_dict) return creds_dict