From 002598d0a3d9b037a7638464977e0a84ceeb8243 Mon Sep 17 00:00:00 2001 From: Grant Gongaware Date: Thu, 9 Feb 2017 13:36:31 -0800 Subject: [PATCH] provisioning a clone worked once --- README.md | 38 +++++++++- proxmox/provider.go | 2 +- proxmox/resource_vm_qemu.go | 134 ++++++++++++++++++++++++++++++------ 3 files changed, 150 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index b9013f3..8fd462e 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,46 @@ Terraform provider plugin for proxmox + +## Work in progress + +### TODO + +* document terraform-ubuntu1404-template creation process +* implement pre-provision phase + ## Build ``` go build -o terraform-provider-proxmox +cp terraform-provider-proxmox $GOPATH/bin ``` -## Work in progress +## Run + +``` +terraform apply +``` + +### Sample file + +main.tf: +``` +provider "proxmox" { +} + +resource "proxmox_vm_qemu" "test" { + name = "tftest1.xyz.com" + desc = "tf description" + target_node = "proxmox1-xx" + ssh_forward_ip = "10.0.0.1" + clone = "terraform-ubuntu1404-template" + storage = "local" + cores = 3 + sockets = 1 + memory = 2560 +} + +``` + + diff --git a/proxmox/provider.go b/proxmox/provider.go index 4c8e239..82304ba 100644 --- a/proxmox/provider.go +++ b/proxmox/provider.go @@ -34,7 +34,7 @@ func Provider() *schema.Provider { }, ResourcesMap: map[string]*schema.Resource{ - "vm_qemu": resourceVmQemu(), + "proxmox_vm_qemu": resourceVmQemu(), // TODO - storage_iso // TODO - bridge // TODO - vm_qemu_template diff --git a/proxmox/resource_vm_qemu.go b/proxmox/resource_vm_qemu.go index b135688..041cef5 100644 --- a/proxmox/resource_vm_qemu.go +++ b/proxmox/resource_vm_qemu.go @@ -3,20 +3,21 @@ package proxmox import ( pxapi "github.com/Telmate/proxmox-api-go/proxmox" "github.com/hashicorp/terraform/helper/schema" + "log" ) func resourceVmQemu() *schema.Resource { + *pxapi.Debug = true return &schema.Resource{ Create: resourceVmQemuCreate, Read: resourceVmQemuRead, - Update: resourceVmQemuUpdate, + Update: nil, // TODO - updates? Delete: resourceVmQemuDelete, Schema: map[string]*schema.Schema{ "vmid": { Type: schema.TypeInt, - Required: true, - Computed: true, + Optional: true, ForceNew: true, }, "name": { @@ -26,38 +27,127 @@ func resourceVmQemu() *schema.Resource { }, "desc": { Type: schema.TypeString, - Required: false, + Optional: true, + ForceNew: true, }, - // memory - // diskGB - // storage - // os - // cores - // sockets - // iso - // nic - // bridge - // vlan - }} + "target_node": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "ssh_forward_ip": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "iso": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "clone": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "storage": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "memory": { + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, + "cores": { + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, + "sockets": { + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, + + // TODO - diskGB + // TODO - os + // TODO - cores + // TODO - nic + // TODO - bridge + // TODO - vlan + // TODO - eth0 OS config + }, + } } func resourceVmQemuCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*providerConfiguration).Client config := pxapi.ConfigQemu{ - Name: d.Get("Name").(string), + Name: d.Get("name").(string), Description: d.Get("desc").(string), + Storage: d.Get("storage").(string), + Memory: d.Get("memory").(int), + QemuCores: d.Get("cores").(int), + QemuSockets: d.Get("sockets").(int), + // TODO - diskGB + // TODO - os + // TODO - nic + // TODO - bridge + // TODO - vlan + } + if d.Get("vmid").(int) == 0 { + maxid, err := pxapi.MaxVmId(client) + if err != nil { + return err + } + log.Println("MaxVmId: %d", maxid) + d.Set("vmid", maxid+1) } vmr := pxapi.NewVmRef(d.Get("vmid").(int)) - config.CreateVm(vmr, client) + vmr.SetNode(d.Get("target_node").(string)) + + // check if ISO or clone + if d.Get("clone").(string) != "" { + sourceVmr, err := client.GetVmRefByName(d.Get("clone").(string)) + if err != nil { + return err + } + err = config.CloneVm(sourceVmr, vmr, client) + if err != nil { + return err + } + // TODO - resize disk + } else if d.Get("iso").(string) != "" { + config.QemuIso = d.Get("iso").(string) + err := config.CreateVm(vmr, client) + if err != nil { + return err + } + } + _, err := client.StartVm(vmr) + if err != nil { + return err + } + sshPort, err := pxapi.SshForwardUsernet(vmr, client) + if err != nil { + return err + } + + d.SetConnInfo(map[string]string{ + "type": "ssh", + "host": d.Get("ssh_forward_ip").(string), + "port": sshPort, + }) + + // TODO - preprovision VM (setup eth0 and hostname) + return nil } func resourceVmQemuRead(d *schema.ResourceData, meta interface{}) error { - return nil -} - -func resourceVmQemuUpdate(d *schema.ResourceData, meta interface{}) error { - return nil + return nil // all information in schema } func resourceVmQemuDelete(d *schema.ResourceData, meta interface{}) error {