-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
652 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// +build acceptance | ||
|
||
package ironic | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package ironic | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/gophercloud/gophercloud" | ||
"github.com/gophercloud/gophercloud/openstack/baremetal/v1/allocations" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
// Schema resource definition for an Ironic allocation. | ||
func resourceAllocationV1() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAllocationV1Create, | ||
Read: resourceAllocationV1Read, | ||
Delete: resourceAllocationV1Delete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"resource_class": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"candidate_nodes": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"traits": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"extra": { | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"node_uuid": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"state": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"last_error": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// Create an allocation, including driving Ironic's state machine | ||
func resourceAllocationV1Create(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*gophercloud.ServiceClient) | ||
|
||
result, err := allocations.Create(client, allocationSchemaToCreateOpts(d)).Extract() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(result.UUID) | ||
|
||
// Wait for state to change from allocating | ||
var state string | ||
timeout := 1 * time.Minute | ||
checkInterval := 5 * time.Second | ||
|
||
|
||
for resourceAllocationV1Read(d, meta) ; state == "allocating" || state == ""; resourceAllocationV1Read(d, meta) { | ||
state = d.Get("state").(string) | ||
log.Printf("[DEBUG] Requested allocation %s; current state is '%s'\n", d.Id(), state) | ||
|
||
time.Sleep(checkInterval) | ||
timeout -= checkInterval | ||
if timeout < 0 { | ||
return fmt.Errorf("timed out waiting for allocation") | ||
} | ||
} | ||
|
||
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 | ||
func resourceAllocationV1Read(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*gophercloud.ServiceClient) | ||
|
||
result, err := allocations.Get(client, d.Id()).Extract() | ||
if err != nil { | ||
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 | ||
} | ||
|
||
// Delete an allocation from Ironic | ||
func resourceAllocationV1Delete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*gophercloud.ServiceClient) | ||
return allocations.Delete(client, d.Id()).ExtractErr() | ||
} | ||
|
||
func allocationSchemaToCreateOpts(d *schema.ResourceData) *allocations.CreateOpts { | ||
candidateNodesRaw := d.Get("candidate_nodes").([]interface{}) | ||
traitsRaw := d.Get("traits").([]interface{}) | ||
extraRaw := d.Get("extra").(map[string]interface{}) | ||
|
||
candidateNodes := make([]string, len(candidateNodesRaw)) | ||
for i := range candidateNodesRaw { | ||
candidateNodes[i] = candidateNodesRaw[i].(string) | ||
} | ||
|
||
traits := make([]string, len(traitsRaw)) | ||
for i := range traitsRaw { | ||
traits[i] = traitsRaw[i].(string) | ||
} | ||
|
||
extra := make(map[string]string) | ||
for k, v := range extraRaw { | ||
extra[k] = v.(string) | ||
} | ||
|
||
return &allocations.CreateOpts{ | ||
Name: d.Get("name").(string), | ||
ResourceClass: d.Get("resource_class").(string), | ||
CandidateNodes: candidateNodes, | ||
Traits: traits, | ||
Extra: extra, | ||
} | ||
} |
Oops, something went wrong.