@@ -2,7 +2,6 @@ package provider
22
33import (
44 "fmt"
5- "time"
65
76 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
87)
@@ -52,35 +51,32 @@ func resourceNodeProvision(d *schema.ResourceData, meta interface{}) error {
5251 powerState := d .Get ("power_state" ).(string )
5352 bootCheck := d .Get ("boot_check" ).(bool )
5453 timeout := d .Get ("login_prompt_timeout" ).(int )
54+ token := meta .(string ) // Assuming token is passed in metadata
5555
56- // Step 1: Check and adjust power state
57- currentPower := checkPowerStatus (node )
58- if currentPower == "on" && powerState == "off" {
59- turnOffNode (node )
60- } else if currentPower == "off" && powerState == "on" {
56+ // Step 1: Turn on the node
57+ if powerState == "on" {
6158 turnOnNode (node )
59+ } else {
60+ turnOffNode (node )
6261 }
6362
64- // Step 2: Flash firmware if needed
63+ // Step 2: Flash firmware if provided
6564 if firmware != "" {
66- fmt .Printf ("Flashing node %d with firmware %s\n " , node , firmware )
6765 flashNode (node , firmware )
6866 }
6967
70- // Step 3: Boot the node
71- if powerState == "on" {
72- turnOnNode (node )
73- }
74-
75- // Step 4: Optional boot check via UART
68+ // Step 3: Boot check
7669 if bootCheck {
77- fmt .Printf ("Checking boot status for node %d\n " , node )
78- if ! waitForLoginPrompt (node , timeout ) {
79- return fmt .Errorf ("node %d failed to reach login prompt within %d seconds" , node , timeout )
70+ fmt .Printf ("Checking boot status for node %d...\n " , node )
71+ success , err := checkBootStatus (node , timeout , token )
72+ if err != nil {
73+ return fmt .Errorf ("boot status check failed for node %d: %v" , node , err )
74+ }
75+ if ! success {
76+ return fmt .Errorf ("node %d did not boot successfully" , node )
8077 }
8178 }
8279
83- // Set resource ID to uniquely identify the node
8480 d .SetId (fmt .Sprintf ("node-%d" , node ))
8581 return nil
8682}
@@ -95,39 +91,6 @@ func resourceNodeStatus(d *schema.ResourceData, meta interface{}) error {
9591
9692func resourceNodeDelete (d * schema.ResourceData , meta interface {}) error {
9793 node := d .Get ("node" ).(int )
98-
99- // Turn off the node during resource deletion
10094 turnOffNode (node )
10195 return nil
10296}
103-
104- // Helper functions
105- func checkPowerStatus (node int ) string {
106- // Simulate checking power status
107- fmt .Printf ("Checking power status for node %d\n " , node )
108- // Replace this with API call
109- return "off"
110- }
111-
112- func turnOffNode (node int ) {
113- fmt .Printf ("Turning off node %d\n " , node )
114- // Replace this with API call to turn off the node
115- }
116-
117- func turnOnNode (node int ) {
118- fmt .Printf ("Turning on node %d\n " , node )
119- // Replace this with API call to turn on the node
120- }
121-
122- func flashNode (node int , firmware string ) {
123- fmt .Printf ("Flashing node %d with firmware %s\n " , node , firmware )
124- // Replace this with API call to flash the firmware
125- }
126-
127- func waitForLoginPrompt (node int , timeout int ) bool {
128- fmt .Printf ("Waiting for login prompt on node %d for up to %d seconds\n " , node , timeout )
129- // Simulate waiting with a sleep (replace with UART API call)
130- time .Sleep (5 * time .Second )
131- // Simulate successful boot
132- return true
133- }
0 commit comments