Example #1
0
def _create_key(conf: Configuration, ostack: OpenStack,
                keyBuilder: VMKeyPairItemBuilder):
    key = keyBuilder.build()

    try:
        conf.add_key(key)
        ostack.create_key(key)

        Console.print(f"Key with name '{key.name}' successfully added")
    except ValueError as e:
        if ostack.has_errors:
            so = StatusOutput(None,
                              pool_size=0,
                              additional_errors=ostack.last_errors)
            so.check_issues()
        else:
            Console.print_error(
                f"Configuration already have the key with name {key.name}, please remove it first"
            )
Example #2
0
def __init__(conf: Configuration, name: str, private: str, public: str):
    if private == "@": private = ""
    if public == "@": public = ""

    ostack = OpenStack(
        conf)  # server keys are imported on object initialization

    if name not in conf.key_names:
        private, public = _check_keys(private, public, True, True)
        _create_key(conf, ostack,
          VMKeyPairItemBuilder()\
                    .set_name(name)\
                    .set_public_key(_get_file(public))\
                    .set_private_key(_get_file(private))
        )
    else:
        _key = conf.get_key(name)
        if private and not public:
            public = _key.public_key
            private, _ = _check_keys(private, None, True, False)
            conf.delete_key(name)
            conf.add_key(VMKeyPairItemBuilder()\
                         .set_name(name)\
                         .set_private_key(_get_file(private))\
                         .set_public_key(public)\
                         .build()
            )
            print("Private key imported")
            return

        _keys_del(conf, ostack, name, True)
        private, public = _check_keys(private, public, True, True)
        _create_key(conf, ostack,
                    VMKeyPairItemBuilder() \
                    .set_name(name) \
                    .set_public_key(_get_file(public)) \
                    .set_private_key(_get_file(private))
                    )