示例#1
0
文件: port.py 项目: amadev/open4k
async def port_change_handler(body, name, namespace, **kwargs):
    LOG.info(f"Got Port change event {name}")
    if body["spec"].get("managed") == False:
        LOG.info(f"{name} is not managed")
        return

    c = client.get_client(settings.OPEN4K_NAMESPACE, body["spec"]["cloud"],
                          "network")
    obj = kube.find(Port, name, namespace=namespace)

    klass = Port

    if body.get("status", {}).get("applied") == True:
        LOG.info(f"{name} exists, updating ...")
        obj_id = body["status"]["object"].get("id")
        id_name = None
        if not obj_id:
            id_name = "uuid"
            obj_id = body["status"]["object"].get("uuid")
        os_obj = klass.get_os_obj(c, obj_id, id_name)
        obj.patch(
            {"status": {
                "object": os_obj
            }},
            subresource="status",
        )
        return

    try:
        os_obj = klass.create_os_obj(c, body["spec"]["body"])
    except Exception as e:
        obj.patch(
            {"status": {
                "applied": False,
                "error": str(e)
            }},
            subresource="status",
        )
        raise
    obj.patch(
        {"status": {
            "applied": True,
            "error": "",
            "object": os_obj
        }},
        subresource="status",
    )
    await hooks.call("port", "post_create", c, klass, obj, os_obj)
示例#2
0
文件: port.py 项目: amadev/open4k
async def port_delete_handler(body, name, namespace, **kwargs):
    LOG.info(f"Got Port delete event {name}")
    if body["spec"].get("managed") == False:
        LOG.info(f"{name} is not managed")
        return

    if not body.get("status", {}).get("applied"):
        LOG.info(f"{name} was not applied successfully")
        return

    klass = Port

    os_obj_id = body["status"].get("object", {}).get("id")
    if not os_obj_id:
        LOG.info(f"Cannot get id for {name}")
        return

    c = client.get_client(settings.OPEN4K_NAMESPACE, body["spec"]["cloud"],
                          "network")
    klass.delete_os_obj(c, os_obj_id)
示例#3
0
文件: resource.py 项目: amadev/open4k
def import_resources(cloud, resource, list_filter=None, dry_run=False):
    klass = RESOURCES[resource]
    cl = client.get_client(settings.OPEN4K_NAMESPACE, cloud,
                           klass.api["service"])
    api_object = getattr(cl, klass.api["objects"])
    func = getattr(api_object, klass.api["list"])
    os_objs = func(**(list_filter or {}))[klass.api["objects"]]
    for os_obj in os_objs:
        if dry_run:
            print(json.dumps(os_obj))
            continue
        part = os_obj.get("name")
        if not part:
            part = os_obj["id"]
        name = kube.escape(f"{cloud}-{part}")
        data = {
            "apiVersion": klass.version,
            "kind": klass.kind,
            "metadata": {
                "name": name,
                "namespace": settings.OPEN4K_NAMESPACE,
            },
            "spec": {
                "managed": False,
                "cloud": cloud
            },
        }
        obj = klass(kube.api, data)
        start = time.time()
        if not obj.exists():
            obj.create()
            status = {"status": {"applied": True, "object": os_obj}}
            obj.patch(status, subresource="status")
            op = "created"
        else:
            status = {"status": {"object": os_obj}}
            obj.patch(status, subresource="status")
            op = "updated"
        print(f"{klass.kind} {name}: {op}", time.time() - start)