Пример #1
0
def get_keypair_info(keypair_id):
    if keypair_id == "":
        return None
    keypair_obj = KeypairsModel.get_keypair_by_id(keypair_id)
    if keypair_obj:
        keypair = {}
        keypair["keypair_id"] = keypair_id
        keypair["keypair_name"] = keypair_obj.name
        keypair["encryption"] = keypair_obj.encryption
        keypair["create_datetime"] =\
            datetime_to_timestamp(keypair_obj.create_datetime)
        return keypair
Пример #2
0
def describe_keypairs(payload):
    """
    List keypairs by user
    :param payload:
    :return:
    """

    if payload.get("keypair_id") is not None:
        payload.update({"name": payload.get("keypair_id")})

    total_count = 0
    ret_set = []
    ret_code, ret_msg = 0, "succ"

    resp = api.get(payload=payload)  # call api
    if resp.get("code") != 0:
        ret_code = CommonErrorCode.REQUEST_API_ERROR
        ret_msg = resp["msg"]
    else:
        keypair_set = resp["data"].get("ret_set", [])
        keypair_list = []

        for keypair in keypair_set:
            if keypair.get("keypair") is not None:
                keypair = keypair["keypair"]
            keypair_id = keypair.pop("name", None)
            keypair.pop("fingerprint", None)
            keypair["keypair_id"] = keypair_id
            keypair["encryption"] = "ssh-rsa"

            keypair_inst = KeypairsModel.get_keypair_by_id(keypair_id)
            if keypair_inst:
                keypair.update({"keypair_name": keypair_inst.name})
                create_datetime = \
                    time.mktime(keypair_inst.create_datetime.timetuple())
                keypair.update({"create_datetime": create_datetime})

                instances = get_keypair_instances(owner=payload["owner"],
                                                  zone=payload["zone"],
                                                  keypair_id=keypair_id)
                keypair.update({"instances": instances})

                keypair_list.append(keypair)
                total_count += 1

            else:
                pass
        keypair_list.sort(key=itemgetter('create_datetime'), reverse=True)
        ret_set = keypair_list

    return console_response(ret_code, ret_msg, total_count, ret_set)
Пример #3
0
def update_keypair(payload):
    """
    Attach keypair to host
    """
    try:
        keypair = KeypairsModel.get_keypair_by_id(payload["keypair_id"])

        # update name
        if payload.get("name") is not None:
            keypair.name = payload.get("name")
            keypair.save()
            return console_response()
    except Exception as exp:
        return console_response(code=CommonErrorCode.REQUEST_API_ERROR,
                                msg="update failed")

    return console_response(code=CommonErrorCode.PARAMETER_ERROR,
                            msg="nothing to update")