Skip to content

Commit d0a23ef

Browse files
committed
- Create helpers.go
- Update provider.go - Update resouce_node.go
1 parent 37b1656 commit d0a23ef

File tree

3 files changed

+82
-52
lines changed

3 files changed

+82
-52
lines changed

provider/helpers.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package provider
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"net/http"
7+
"strings"
8+
"time"
9+
)
10+
11+
func checkPowerStatus(node int) string {
12+
// Simulate checking power status
13+
fmt.Printf("Checking power status for node %d\n", node)
14+
// Replace this with an actual API call
15+
return "off"
16+
}
17+
18+
func turnOffNode(node int) {
19+
fmt.Printf("Turning off node %d\n", node)
20+
// Replace this with an API call to turn off the node
21+
}
22+
23+
func turnOnNode(node int) {
24+
fmt.Printf("Turning on node %d\n", node)
25+
// Replace this with an API call to turn on the node
26+
}
27+
28+
func flashNode(node int, firmware string) {
29+
fmt.Printf("Flashing node %d with firmware %s\n", node, firmware)
30+
// Replace this with an API call to flash the firmware
31+
}
32+
33+
func checkBootStatus(node int, timeout int, token string) (bool, error) {
34+
url := fmt.Sprintf("https://turingpi.local/api/bmc?opt=get&type=uart&node=%d", node)
35+
client := &http.Client{}
36+
37+
deadline := time.Now().Add(time.Duration(timeout) * time.Second)
38+
39+
for time.Now().Before(deadline) {
40+
req, err := http.NewRequest("GET", url, nil)
41+
if err != nil {
42+
return false, fmt.Errorf("failed to create UART request: %v", err)
43+
}
44+
45+
req.Header.Set("Authorization", "Bearer "+token)
46+
resp, err := client.Do(req)
47+
if err != nil {
48+
return false, fmt.Errorf("UART request failed: %v", err)
49+
}
50+
51+
defer resp.Body.Close()
52+
body, err := ioutil.ReadAll(resp.Body)
53+
if err != nil {
54+
return false, fmt.Errorf("failed to read UART response: %v", err)
55+
}
56+
57+
// Simulate login prompt detection
58+
if strings.Contains(string(body), "login:") {
59+
fmt.Printf("Node %d booted successfully: login prompt detected.\n", node)
60+
return true, nil
61+
}
62+
63+
time.Sleep(5 * time.Second)
64+
}
65+
66+
return false, fmt.Errorf("timeout reached: node %d did not boot successfully", node)
67+
}

provider/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func Provider() *schema.Provider {
2121
ResourcesMap: map[string]*schema.Resource{
2222
"turingpi_power": resourcePower(),
2323
"turingpi_flash": resourceFlash(),
24-
"turingpi_node": resourceNode(),
24+
"turingpi_node": resourceNode(), // Add this resource
2525
},
2626
}
2727
}

provider/resource_node.go

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package provider
22

33
import (
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

9692
func 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

Comments
 (0)