def r_update(_id): ssh_key = SSHKey() args_rules = [Rules.ID.value] if 'label' in request.json: args_rules.append(Rules.LABEL.value, ) if 'public_key' in request.json: args_rules.append(Rules.PUBLIC_KEY.value, ) if args_rules.__len__() < 2: ret = dict() ret['state'] = ji.Common.exchange_state(20000) return ret request.json['id'] = _id try: ji.Check.previewing(args_rules, request.json) ssh_key.id = request.json.get('id') ssh_key.get() ssh_key.label = request.json.get('label', ssh_key.label) ssh_key.public_key = request.json.get('public_key', ssh_key.public_key) ssh_key.update() ssh_key.get() ret = dict() ret['state'] = ji.Common.exchange_state(20000) ret['data'] = ssh_key.__dict__ return ret except ji.PreviewingError, e: return json.loads(e.message)
def r_bind(ssh_key_id, uuids): args_rules = [Rules.SSH_KEY_ID_EXT.value, Rules.UUIDS.value] try: ji.Check.previewing(args_rules, { 'ssh_key_id': ssh_key_id, 'uuids': uuids }) ret = dict() ret['state'] = ji.Common.exchange_state(20000) ssh_key = SSHKey() ssh_key.id = ssh_key_id # 判断 ssh_key id 为 ssh_key_id 的对象是否存在 if not ssh_key.exist(): ret['state'] = ji.Common.exchange_state(40401) ret['state']['sub']['zh-cn'] = ''.join( [ret['state']['sub']['zh-cn'], ': ssh_key_id: ', ssh_key_id]) return ret # 获取已经和该 ssh_key 绑定过的 guest uuid 集合,用于判断是否已经绑定过该 ssh_key,避免重复绑定 rows, _ = SSHKeyGuestMapping.get_by_filter( filter_str=':'.join(['ssh_key_id', 'eq', ssh_key_id])) guests_uuid = list() for row in rows: guests_uuid.append(row['guest_uuid']) ssh_key_guest_mapping = SSHKeyGuestMapping() for uuid in uuids.split(','): # 如果已经绑定过,则忽略 if uuid in guests_uuid: continue ssh_key_guest_mapping.ssh_key_id = ssh_key_id ssh_key_guest_mapping.guest_uuid = uuid ssh_key_guest_mapping.create() update_ssh_key(uuid=uuid) # 返回执行结果 rows, _ = SSHKeyGuestMapping.get_by_filter( filter_str=':'.join(['ssh_key_id', 'eq', ssh_key_id])) guests_uuid = list() for row in rows: guests_uuid.append(row['guest_uuid']) if guests_uuid.__len__() == 0: guests_uuid.append('_') request.__setattr__( 'args', ImmutableMultiDict([ ('filter', ':'.join(['uuid', 'in', ','.join(guests_uuid)])), ('page_size', 10000) ])) return guest_base.get_by_filter() except ji.PreviewingError, e: return json.loads(e.message)