Skip to content

Commit

Permalink
return the last error with any error we get (#50)
Browse files Browse the repository at this point in the history
* Fix linting warnings

* in workflow.run() append the last_error to any error

* Improve the error handling

* things caught by handling errors
  • Loading branch information
asalkeld authored Apr 8, 2021
1 parent 215014f commit 5b0c62f
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 144 deletions.
48 changes: 38 additions & 10 deletions ironic/data_source_ironic_introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package ironic

import (
"fmt"
"time"

"github.com/gophercloud/gophercloud/openstack/baremetalintrospection/v1/introspection"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"time"
)

// Schema resource for an introspection data source, that has some selected details about the node exposed.
Expand Down Expand Up @@ -79,11 +80,26 @@ func dataSourceIronicIntrospectionRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("could not get introspection status: %s", err.Error())
}

d.Set("finished", status.Finished)
d.Set("finished_at", status.FinishedAt)
d.Set("started_at", status.StartedAt)
d.Set("error", status.Error)
d.Set("state", status.State)
err = d.Set("finished", status.Finished)
if err != nil {
return err
}
err = d.Set("finished_at", status.FinishedAt.Format("2006-01-02T15:04:05"))
if err != nil {
return err
}
err = d.Set("started_at", status.StartedAt.Format("2006-01-02T15:04:05"))
if err != nil {
return err
}
err = d.Set("error", status.Error)
if err != nil {
return err
}
err = d.Set("state", status.State)
if err != nil {
return err
}

if status.Finished {
data, err := introspection.GetIntrospectionData(client, uuid).Extract()
Expand All @@ -100,14 +116,26 @@ func dataSourceIronicIntrospectionRead(d *schema.ResourceData, meta interface{})
"ip": v.IP,
})
}
d.Set("interfaces", interfaces)
err = d.Set("interfaces", interfaces)
if err != nil {
return err
}

// CPU data
d.Set("cpu_arch", data.CPUArch)
d.Set("cpu_count", data.CPUs)
err = d.Set("cpu_arch", data.CPUArch)
if err != nil {
return err
}
err = d.Set("cpu_count", data.CPUs)
if err != nil {
return err
}

// Memory info
d.Set("memory_mb", data.MemoryMB)
err = d.Set("memory_mb", data.MemoryMB)
if err != nil {
return err
}
}

d.SetId(time.Now().UTC().String())
Expand Down
15 changes: 8 additions & 7 deletions ironic/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package ironic
import (
"context"
"fmt"
"log"
"net/http"
"strings"
"sync"
"time"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/baremetal/httpbasic"
"github.com/gophercloud/gophercloud/openstack/baremetal/noauth"
Expand All @@ -13,11 +19,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"log"
"net/http"
"strings"
"sync"
"time"
)

// Clients stores the client connection information for Ironic and Inspector
Expand Down Expand Up @@ -354,7 +355,7 @@ func waitForConductor(ctx context.Context, client *gophercloud.ServiceClient) {
log.Printf("[DEBUG] Waiting for conductor API to become available...")
driverCount := 0

drivers.ListDrivers(client, drivers.ListDriversOpts{
err := drivers.ListDrivers(client, drivers.ListDriversOpts{
Detail: false,
}).EachPage(func(page pagination.Page) (bool, error) {
actual, err := drivers.ExtractDrivers(page)
Expand All @@ -365,7 +366,7 @@ func waitForConductor(ctx context.Context, client *gophercloud.ServiceClient) {
return true, nil
})
// If we have any drivers, conductor is up.
if driverCount > 0 {
if err == nil && driverCount > 0 {
return
}

Expand Down
82 changes: 50 additions & 32 deletions ironic/resource_ironic_allocation_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,34 +81,33 @@ func resourceAllocationV1Create(d *schema.ResourceData, meta interface{}) error
d.SetId(result.UUID)

// Wait for state to change from allocating
var state string
timeout := 1 * time.Minute
checkInterval := 2 * time.Second

getState := func() string {
resourceAllocationV1Read(d, meta)
return d.Get("state").(string)
}

for state = getState(); state == "allocating"; state = getState() {
for {
err = resourceAllocationV1Read(d, meta)
if err != nil {
return err
}
state := d.Get("state").(string)
log.Printf("[DEBUG] Requested allocation %s; current state is '%s'\n", d.Id(), state)

time.Sleep(checkInterval)
checkInterval += 2
timeout -= checkInterval
if timeout < 0 {
return fmt.Errorf("timed out waiting for allocation")
switch state {
case "allocating":
time.Sleep(checkInterval)
checkInterval += 2
timeout -= checkInterval
if timeout < 0 {
return fmt.Errorf("timed out waiting for allocation")
}
case "error":
err := d.Get("last_error").(string)
_ = resourceAllocationV1Delete(d, meta)
d.SetId("")
return fmt.Errorf("error creating resource: %s", err)
default:
return nil
}
}

if state == "error" {
err := d.Get("last_error").(string)
resourceAllocationV1Delete(d, meta)
d.SetId("")
return fmt.Errorf("error creating resource: %s", err)
}

return nil
}

// Read the allocation's data from Ironic
Expand All @@ -123,16 +122,35 @@ func resourceAllocationV1Read(d *schema.ResourceData, meta interface{}) error {
return err
}

d.Set("name", result.Name)
d.Set("resource_class", result.ResourceClass)
d.Set("candidate_nodes", result.CandidateNodes)
d.Set("traits", result.Traits)
d.Set("extra", result.Extra)
d.Set("node_uuid", result.NodeUUID)
d.Set("state", result.State)
d.Set("last_error", result.LastError)

return nil
err = d.Set("name", result.Name)
if err != nil {
return err
}
err = d.Set("resource_class", result.ResourceClass)
if err != nil {
return err
}
err = d.Set("candidate_nodes", result.CandidateNodes)
if err != nil {
return err
}
err = d.Set("traits", result.Traits)
if err != nil {
return err
}
err = d.Set("extra", result.Extra)
if err != nil {
return err
}
err = d.Set("node_uuid", result.NodeUUID)
if err != nil {
return err
}
err = d.Set("state", result.State)
if err != nil {
return err
}
return d.Set("last_error", result.LastError)
}

// Delete an allocation from Ironic if it exists
Expand Down
49 changes: 26 additions & 23 deletions ironic/resource_ironic_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"crypto/x509"
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"net/http"

"github.com/gophercloud/gophercloud/openstack/baremetal/v1/nodes"
utils "github.com/gophercloud/utils/openstack/baremetal/v1/nodes"
retryablehttp "github.com/hashicorp/go-retryablehttp"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"io/ioutil"
"log"
"net/http"
)

// Schema resource definition for an Ironic deployment.
Expand Down Expand Up @@ -88,7 +89,7 @@ func resourceDeploymentCreate(d *schema.ResourceData, meta interface{}) error {
}

// Reload the resource before returning
defer resourceDeploymentRead(d, meta)
defer func() { _ = resourceDeploymentRead(d, meta) }()

// Set instance info
instanceInfo := d.Get("instance_info").(map[string]interface{})
Expand Down Expand Up @@ -163,10 +164,8 @@ func fetchFullIgnition(userDataURL string, userDataCaCert string, userDataHeader
log.Printf("could not get user_data_url: %s", err)
return "", err
}
if userDataHeaders != nil {
for k, v := range userDataHeaders {
req.Header.Add(k, v.(string))
}
for k, v := range userDataHeaders {
req.Header.Add(k, v.(string))
}
resp, err := client.Do(req)
if err != nil {
Expand All @@ -187,9 +186,15 @@ func fetchFullIgnition(userDataURL string, userDataCaCert string, userDataHeader

// buildConfigDrive handles building a config drive appropriate for the Ironic version we are using. Newer versions
// support sending the user data directly, otherwise we need to build an ISO image
func buildConfigDrive(apiVersion, userData string, networkData, metaData map[string]interface{}) (configDrive interface{}, err error) {
func buildConfigDrive(apiVersion, userData string, networkData, metaData map[string]interface{}) (interface{}, error) {
actual, err := version.NewVersion(apiVersion)
if err != nil {
return nil, err
}
minimum, err := version.NewVersion("1.56")
if err != nil {
return nil, err
}

if minimum.GreaterThan(actual) {
// Create config drive ISO directly with gophercloud/utils
Expand All @@ -202,17 +207,14 @@ func buildConfigDrive(apiVersion, userData string, networkData, metaData map[str
if err != nil {
return nil, err
}
configDrive = &configDriveISO
} else {
// Let Ironic handle creating the config drive
configDrive = &nodes.ConfigDrive{
UserData: userData,
NetworkData: networkData,
MetaData: metaData,
}
return &configDriveISO, nil
}

return
// Let Ironic handle creating the config drive
return &nodes.ConfigDrive{
UserData: userData,
NetworkData: networkData,
MetaData: metaData,
}, nil
}

// Read the deployment's data from Ironic
Expand All @@ -229,10 +231,11 @@ func resourceDeploymentRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("could not find node %s: %s", id, err)
}

d.Set("provision_state", result.ProvisionState)
d.Set("last_error", result.LastError)

return nil
err = d.Set("provision_state", result.ProvisionState)
if err != nil {
return err
}
return d.Set("last_error", result.LastError)
}

// Delete an deployment from Ironic - this cleans the node and returns it's state to 'available'
Expand Down
Loading

0 comments on commit 5b0c62f

Please sign in to comment.