diff --git a/examples/lxc_example.tf b/examples/lxc_example.tf new file mode 100644 index 0000000..8587cef --- /dev/null +++ b/examples/lxc_example.tf @@ -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" +} diff --git a/proxmox/provider.go b/proxmox/provider.go index 24bcb30..c829ddc 100644 --- a/proxmox/provider.go +++ b/proxmox/provider.go @@ -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 diff --git a/proxmox/resource_lxc.go b/proxmox/resource_lxc.go new file mode 100644 index 0000000..2ebcaa8 --- /dev/null +++ b/proxmox/resource_lxc.go @@ -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 +}