Implement Import capability for disk resource
This commit is contained in:
@@ -277,6 +277,10 @@ func resourceDisk() *schema.Resource {
|
|||||||
Delete: resourceDiskDelete,
|
Delete: resourceDiskDelete,
|
||||||
Exists: resourceDiskExists,
|
Exists: resourceDiskExists,
|
||||||
|
|
||||||
|
Importer: &schema.ResourceImporter{
|
||||||
|
State: schema.ImportStatePassthrough,
|
||||||
|
},
|
||||||
|
|
||||||
Timeouts: &schema.ResourceTimeout{
|
Timeouts: &schema.ResourceTimeout{
|
||||||
Create: &Timeout180s,
|
Create: &Timeout180s,
|
||||||
Read: &Timeout30s,
|
Read: &Timeout30s,
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
@@ -56,11 +57,24 @@ func utilityDiskCheckPresence(d *schema.ResourceData, m interface{}) (string, er
|
|||||||
controller := m.(*ControllerCfg)
|
controller := m.(*ControllerCfg)
|
||||||
urlValues := &url.Values{}
|
urlValues := &url.Values{}
|
||||||
|
|
||||||
diskId, argSet := d.GetOk("disk_id")
|
// make it possible to use "read" & "check presence" functions with disk ID set so
|
||||||
if argSet {
|
// that Import of preexisting Disk resource is possible
|
||||||
// go straight for the disk by its ID
|
idSet := false
|
||||||
log.Debugf("utilityDiskCheckPresence: locating disk by its ID %d", diskId.(int))
|
theId, err := strconv.Atoi(d.Id())
|
||||||
urlValues.Add("diskId", fmt.Sprintf("%d", diskId.(int)))
|
if err != nil || theId <= 0 {
|
||||||
|
diskId, argSet := d.GetOk("disk_id")
|
||||||
|
if argSet {
|
||||||
|
theId =diskId.(int)
|
||||||
|
idSet = true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
idSet = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if idSet {
|
||||||
|
// disk ID is specified, try to get disk instance straight by this ID
|
||||||
|
log.Debugf("utilityDiskCheckPresence: locating disk by its ID %d", theId)
|
||||||
|
urlValues.Add("diskId", fmt.Sprintf("%d", theId))
|
||||||
diskFacts, err := controller.decortAPICall("POST", DisksGetAPI, urlValues)
|
diskFacts, err := controller.decortAPICall("POST", DisksGetAPI, urlValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -68,6 +82,8 @@ func utilityDiskCheckPresence(d *schema.ResourceData, m interface{}) (string, er
|
|||||||
return diskFacts, nil
|
return diskFacts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ID or disk_di was not set in the schema upon entering this function - rely on Disk name
|
||||||
|
// and Account ID to find the disk
|
||||||
diskName, argSet := d.GetOk("name")
|
diskName, argSet := d.GetOk("name")
|
||||||
if !argSet {
|
if !argSet {
|
||||||
// no disk ID and no disk name - we cannot locate disk in this case
|
// no disk ID and no disk name - we cannot locate disk in this case
|
||||||
@@ -87,7 +103,7 @@ func utilityDiskCheckPresence(d *schema.ResourceData, m interface{}) (string, er
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("utilityDiskCheckPresence: ready to unmarshal string %q", diskFacts)
|
log.Debugf("utilityDiskCheckPresence: ready to unmarshal string %s", diskFacts)
|
||||||
|
|
||||||
disksList := DisksListResp{}
|
disksList := DisksListResp{}
|
||||||
err = json.Unmarshal([]byte(diskFacts), &disksList)
|
err = json.Unmarshal([]byte(diskFacts), &disksList)
|
||||||
|
|||||||
Reference in New Issue
Block a user