Image resource

Import the image resource module and instantiate the client

[ ]:
from collections.abc import Iterator
from typing import Any

from peak.resources import images

image_client: images.Image = images.get_client()

List all existing image(s) and iterate over them

[ ]:
images_list_iterator: Iterator[dict[str, Any]] = image_client.list_images(
    name="test",
    status=["in-use"],
)
images_list_iterated: dict[str, Any] = next(images_list_iterator)

List all existing image version(s) and iterate over them

[ ]:
image_versions_list_iterator: Iterator[dict[str, Any]] = image_client.list_image_versions(
    image_id=9999,
    version="test",
    status=["in-use"],
    tags=["dnd"],
)
image_versions_iterated: dict[str, Any] = next(image_versions_list_iterator)

Create a new image resource

[ ]:
# Creating an image with upload source
upload_body = {
    "version": "0.0.1",
    "name": "image-sdk-101",
    "description": "Hello from SDK",
    "type": "workflow",
    "buildDetails": {
        "source": "upload",
        "useCache": False,
        "buildArguments": [
            {
                "name": "HELLO",
                "value": "world",
            },
        ],
        "context": ".",
    },
}

artifact = {
    "path": "./image",
}

upload_image = image_client.create_image(
    body=upload_body,
    artifact=artifact,
)

# Creating an image with dockerfile source
dockerfile_body = {
    "version": "0.0.1",
    "name": "image-sdk-101",
    "description": "Hello from SDK",
    "type": "workflow",
    "buildDetails": {
        "source": "dockerfile",
        "useCache": False,
        "buildArguments": [
            {
                "name": "HELLO",
                "value": "world",
            },
        ],
        "dockerfile": "FROM nginx",
    },
}

dockerfile_image = image_client.create_image(
    body=dockerfile_body,
)

# Creating image with github source
github_body = {
    "version": "0.0.1",
    "name": "image-sdk-101",
    "description": "Hello from SDK",
    "type": "workflow",
    "buildDetails": {
        "source": "github",
        "useCache": True,
        "buildArguments": [
            {
                "name": "HELLO",
                "value": "world",
            },
        ],
        "context": ".",
        "branch": "main",
        "repository": "https://github.com/username/repository",
        "token": "token-name",
    },
}

github_image = image_client.create_image(
    body=github_body,
)

Create a new image version resource

[ ]:
# Creating a version with upload source
upload_body = {
    "version": "0.0.1",
    "description": "Hello from SDK",
    "buildDetails": {
        "source": "upload",
        "useCache": False,
        "buildArguments": [
            {
                "name": "HELLO",
                "value": "world",
            },
        ],
        "context": ".",
    },
}

artifact = {
    "path": "./image",
}

upload_version = image_client.create_version(
    image_id=upload_image["imageId"],
    body=upload_body,
    artifact=artifact,
)

# Creating a version with dockerfile source
dockerfile_body = {
    "version": "0.0.1",
    "description": "Hello from SDK",
    "buildDetails": {
        "source": "dockerfile",
        "useCache": False,
        "buildArguments": [
            {
                "name": "HELLO",
                "value": "world",
            },
        ],
        "dockerfile": "FROM nginx",
    },
}

dockerfile_version = image_client.create_version(
    image_id=dockerfile_image["imageId"],
    body=dockerfile_body,
)

# Creating version with github source
github_body = {
    "version": "0.0.1",
    "description": "Hello from SDK",
    "buildDetails": {
        "source": "github",
        "useCache": True,
        "buildArguments": [
            {
                "name": "HELLO",
                "value": "world",
            },
        ],
        "context": ".",
        "branch": "main",
        "repository": "https://github.com/username/repository",
        "token": "token-name",
    },
}

github_version = image_client.create_version(
    image_id=github_image["imageId"],
    body=github_body,
)

Create or update a image version

[ ]:
# Create a image/version
upload_body = {
    "version": "0.0.1",
    "name": "image-sdk-101",
    "description": "Hello from SDK",
    "type": "workflow",
    "buildDetails": {
        "source": "upload",
        "useCache": False,
        "buildArguments": [
            {
                "name": "HELLO",
                "value": "world",
            },
        ],
        "context": ".",
    },
}

artifact = {
    "path": "./image",
}

upload_image = image_client.create_or_update_image_version(
    body=upload_body,
    artifact=artifact,
)

# Update an existing image version
dockerfile_body = {
    "version": "0.0.1",
    "name": "image-sdk-101",
    "description": "Hello from SDK",
    "type": "workflow",
    "buildDetails": {
        "source": "dockerfile",
        "useCache": False,
        "buildArguments": [
            {
                "name": "HELLO",
                "value": "world",
            },
        ],
        "dockerfile": "FROM nginx",
    },
}

dockerfile_image = image_client.create_or_update_image_version(
    body=dockerfile_body,
)

Describe an existing image resource

[ ]:
image_client.describe_image(image_id=int(upload_image["imageId"]))

Describe an existing version resource

[ ]:
image_client.describe_version(image_id=int(upload_image["imageId"]), version_id=int(upload_image["versionId"]))

Update an existing image version resource

[ ]:
# Update a version with upload source
updated_upload_body = {
    "description": "Hello from SDK",
    "buildDetails": {
        "source": "upload",
        "useCache": False,
        "buildArguments": [
            {
                "name": "HELLO",
                "value": "world",
            },
        ],
        "context": ".",
    },
}

artifact = {
    "path": "./image",
}

updated_upload_version = image_client.update_version(
    image_id=upload_image["imageId"],
    version_id=upload_version["versionId"],
    body=updated_upload_body,
    artifact=artifact,
)

# Update a version with dockerfile source
updated_dockerfile_body = {
    "description": "Hello from SDK",
    "buildDetails": {
        "source": "dockerfile",
        "useCache": False,
        "buildArguments": [
            {
                "name": "HELLO",
                "value": "world",
            },
        ],
        "dockerfile": "FROM nginx",
    },
}

updated_dockerfile_version = image_client.update_version(
    image_id=dockerfile_image["imageId"],
    version_id=dockerfile_version["versionId"],
    body=updated_dockerfile_body,
)

# Update a version with github source
updated_github_body = {
    "description": "Hello from SDK",
    "buildDetails": {
        "source": "github",
        "useCache": True,
        "buildArguments": [
            {
                "name": "HELLO",
                "value": "world",
            },
        ],
        "context": ".",
        "branch": "main",
        "repository": "https://github.com/username/repository",
        "token": "token-name",
    },
}

updated_github_version = image_client.update_version(
    image_id=github_image["imageId"],
    version_id=github_version["versionId"],
    body=updated_github_body,
)

List all builds in an existing image resource and iterate over them

[ ]:
image_builds_iterator: Iterator[dict[str, Any]] = image_client.list_image_builds(image_id=int(upload_image["imageId"]))
image_builds_iterated: dict[str, Any] = next(image_builds_iterator)

Delete an existing image version resource

[ ]:
image_client.delete_version(image_id=int(upload_image["imageId"]), version_id=int(upload_image["versionId"]))

Delete an existing image resource

[ ]:
image_client.delete_image(image_id=int(upload_image["imageId"]))