~tieong/pulumi-kimsufi

1482c28edab071d0a93beb02dcfeef32c50ccfa1 — Thomas Ieong 1 year, 1 month ago 706dde8
Converted to component resource

I do get type errors from mypy, from resource.name and resource.ip.

This is because the name and ip attributes were not declared in the
constructor of the VM class, just need to change the constructor to
take a helper class as an arg.

See this for more info: https://github.com/pulumi/examples/blob/master/aws-py-wordpress-fargate-rds/frontend.py
2 files changed, 22 insertions(+), 12 deletions(-)

M __main__.py
M vm.py
M __main__.py => __main__.py +2 -2
@@ 94,5 94,5 @@ vm_outputs = vm.new_vm(
    cloud_init.id,
    network.id
)
pulumi.export("IP Address", vm_outputs["IP Address"])
pulumi.export("VM name", vm_outputs["VM name"])
pulumi.export("IP Address", vm_outputs.ip)
pulumi.export("VM name", vm_outputs.name)

M vm.py => vm.py +20 -10
@@ 1,19 1,22 @@
from typing import Optional

from pulumi import Output
import pulumi
from pulumi import Output, ResourceOptions, ComponentResource
import pulumi_libvirt as libvirt

class VM(ComponentResource):
    def __init__(self, name: str, opts: ResourceOptions = None):
        # Change pkg something something to my state
        super().__init__('kimsufi-infra:pkg/vm:vm', name, None, opts)

def new_vm(
        name: str,
        pool_name: Output[str],
        base_volume_id: Output[str],
        cloud_init_disk_id: Output[str],
        network_id: Output[str],
        opts: Optional[pulumi.ResourceOptions] = None,
):
    # return info about the newly created VM
    outputs = {}
        opts: Optional[ResourceOptions] = None,
) -> VM:
    resource = VM(name)

    # create a filesystem volume for our VM
    # This filesystem will be based on the `ubuntu` volume above


@@ 24,7 27,9 @@ def new_vm(
        base_volume_id=base_volume_id,
        pool=pool_name,
        size=10000000000,
        opts=ResourceOptions(parent=resource)
    )

    # create a VM that has a name starting with ubuntu
    domain = libvirt.Domain(
        f"{name}-domain",


@@ 44,13 49,18 @@ def new_vm(
                wait_for_lease=True
            )
        ],
        opts=pulumi.ResourceOptions(
        opts=ResourceOptions(
            parent=resource,
            delete_before_replace=True,
            replace_on_changes=["*"],
        ),
    )

    outputs["IP Address"] = domain.network_interfaces[0].addresses[0]
    outputs["VM name"] = domain.name
    resource.name = domain.name
    resource.ip = domain.network_interfaces[0].addresses[0]
    resource.register_outputs({
        "ip": domain.network_interfaces[0].addresses[0],
        "name": domain.name
    })

    return outputs
    return resource