add lxc resource

This commit is contained in:
Andreas Gruhler 2019-06-03 09:01:19 +02:00
parent 06d6363421
commit c3d462e390
3 changed files with 166 additions and 0 deletions

23
examples/lxc_example.tf Normal file
View File

@ -0,0 +1,23 @@
provider "proxmox" {
pm_tls_insecure = true
pm_api_url = "https://proxmox.org/api2/json"
pm_password = "supersecret"
pm_user = "terraform-user@pve"
}
resource "proxmox_lxc" "lxc-test" {
hostname = "terraform-test-container"
ostemplate = "shared:vztmpl/centos-7-default_20171212_amd64.tar.xz"
target_node = "node-01"
networks = [
{
name = "eth0"
bridge = "vmbr0"
ip = "dhcp"
ip6 = "dhcp"
}
]
storage = "local-lvm"
pool = "terraform"
passsword = "rootroot"
}

View File

@ -58,6 +58,7 @@ func Provider() *schema.Provider {
ResourcesMap: map[string]*schema.Resource{
"proxmox_vm_qemu": resourceVmQemu(),
"proxmox_lxc": resourceLxc(),
// TODO - storage_iso
// TODO - bridge
// TODO - vm_qemu_template

142
proxmox/resource_lxc.go Normal file
View File

@ -0,0 +1,142 @@
package proxmox
import (
pxapi "github.com/Telmate/proxmox-api-go/proxmox"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceLxc() *schema.Resource {
*pxapi.Debug = true
return &schema.Resource{
Create: resourceLxcCreate,
Read: resourceLxcRead,
Update: resourceLxcUpdate,
Delete: resourceLxcDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"hostname": {
Type: schema.TypeString,
Required: true,
},
"target_node": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ostemplate": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"networks": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"bridge": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "vmbr0",
},
"ip": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "dhcp",
},
"ip6": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "dhcp",
},
},
},
},
"storage": {
Type: schema.TypeString,
Optional: true,
Default: "local-lvm",
},
"pool": {
Type: schema.TypeString,
Optional: true,
},
"password": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceLxcCreate(d *schema.ResourceData, meta interface{}) error {
pconf := meta.(*providerConfiguration)
pmParallelBegin(pconf)
client := pconf.Client
vmName := d.Get("hostname").(string)
networks := d.Get("networks").(*schema.Set)
lxcNetworks := lxcDevicesSetToMap(networks)
config := pxapi.ConfigLxc{
Ostemplate: d.Get("ostemplate").(string),
Storage: d.Get("storage").(string),
Pool: d.Get("pool").(string),
Password: d.Get("password").(string),
Hostname: vmName,
Networks: lxcNetworks,
}
targetNode := d.Get("target_node").(string)
//vmr, _ := client.GetVmRefByName(vmName)
// get unique id
nextid, err := nextVmId(pconf)
if err != nil {
pmParallelEnd(pconf)
return err
}
vmr := pxapi.NewVmRef(nextid)
vmr.SetNode(targetNode)
err = config.CreateLxc(vmr, client)
if err != nil {
pmParallelEnd(pconf)
return err
}
// The existence of a non-blank ID is what tells Terraform that a resource was created
d.SetId(resourceId(targetNode, "lxc", vmr.VmId()))
return resourceLxcRead(d, meta)
}
func resourceLxcUpdate(d *schema.ResourceData, meta interface{}) error {
return resourceLxcRead(d, meta)
}
func resourceLxcRead(d *schema.ResourceData, meta interface{}) error {
return nil
}
func resourceLxcDelete(d *schema.ResourceData, meta interface{}) error {
return nil
}
func lxcDevicesSetToMap(devicesSet *schema.Set) pxapi.LxcDevices {
devicesMap := pxapi.LxcDevices{}
for _, set := range devicesSet.List() {
setMap, isMap := set.(map[string]interface{})
if isMap {
setID := setMap["id"].(int)
devicesMap[setID] = setMap
}
}
return devicesMap
}