From a6163cec933efa41747476c563898cec053166b0 Mon Sep 17 00:00:00 2001 From: Bruno Meneguello <1322552+bkmeneguello@users.noreply.github.com> Date: Thu, 23 May 2019 08:39:39 -0300 Subject: [PATCH] Better import method and error message --- proxmox/provider.go | 6 +++--- proxmox/resource_vm_qemu.go | 9 ++------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/proxmox/provider.go b/proxmox/provider.go index ac372ca..24bcb30 100644 --- a/proxmox/provider.go +++ b/proxmox/provider.go @@ -130,10 +130,10 @@ func resourceId(targetNode string, resType string, vmId int) string { var rxRsId = regexp.MustCompile("([^/]+)/([^/]+)/(\\d+)") func parseResourceId(resId string) (targetNode string, resType string, vmId int, err error) { - idMatch := rxRsId.FindStringSubmatch(resId) - if idMatch == nil { - err = fmt.Errorf("Invalid resource id: %s", resId) + if !rxRsId.MatchString(resId) { + return "", "", -1, fmt.Errorf("Invalid resource format: %s. Must be node/type/vmId", resId) } + idMatch := rxRsId.FindStringSubmatch(resId) targetNode = idMatch[1] resType = idMatch[2] vmId, err = strconv.Atoi(idMatch[3]) diff --git a/proxmox/resource_vm_qemu.go b/proxmox/resource_vm_qemu.go index 18565a5..c19fd19 100644 --- a/proxmox/resource_vm_qemu.go +++ b/proxmox/resource_vm_qemu.go @@ -22,7 +22,7 @@ func resourceVmQemu() *schema.Resource { Update: resourceVmQemuUpdate, Delete: resourceVmQemuDelete, Importer: &schema.ResourceImporter{ - State: resourceVmQemuImport, + State: schema.ImportStatePassthrough, }, Schema: map[string]*schema.Schema{ @@ -577,6 +577,7 @@ func resourceVmQemuRead(d *schema.ResourceData, meta interface{}) error { _, _, vmID, err := parseResourceId(d.Id()) if err != nil { pmParallelEnd(pconf) + d.SetId("") return err } vmr := pxapi.NewVmRef(vmID) @@ -630,12 +631,6 @@ func resourceVmQemuRead(d *schema.ResourceData, meta interface{}) error { return nil } -func resourceVmQemuImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - // TODO: research proper import - err := resourceVmQemuRead(d, meta) - return []*schema.ResourceData{d}, err -} - func resourceVmQemuDelete(d *schema.ResourceData, meta interface{}) error { pconf := meta.(*providerConfiguration) pmParallelBegin(pconf)