Skip to content

Commit

Permalink
provisioning a clone worked once
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Gongaware committed Feb 9, 2017
1 parent 4510ee9 commit 002598d
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 24 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```


2 changes: 1 addition & 1 deletion proxmox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
134 changes: 112 additions & 22 deletions proxmox/resource_vm_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -26,38 +27,127 @@ func resourceVmQemu() *schema.Resource {
},
"desc": {
Type: schema.TypeString,
Required: false,
Optional: true,
ForceNew: true,
},
"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,
},
// memory
// diskGB
// storage
// os
// cores
// sockets
// iso
// nic
// bridge
// vlan
}}

// 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)
return nil
}
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)

func resourceVmQemuRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

func resourceVmQemuUpdate(d *schema.ResourceData, meta interface{}) error {
return nil
func resourceVmQemuRead(d *schema.ResourceData, meta interface{}) error {
return nil // all information in schema
}

func resourceVmQemuDelete(d *schema.ResourceData, meta interface{}) error {
Expand Down

0 comments on commit 002598d

Please sign in to comment.