Skip to content

Commit

Permalink
Fix: Resetting always_pxe flag as per user configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
codinja1188 committed Aug 23, 2023
1 parent 43a33f7 commit 38b4bee
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 41 deletions.
84 changes: 74 additions & 10 deletions internal/devices/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ package devices

import (
"context"
"encoding/json"
"fmt"
"os"
"time"

metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -75,18 +77,32 @@ func (c *Client) Create() *cobra.Command {
}
userdata = string(userdataRaw)
}
// var endDt time.Time

// if terminationTime != "" {
// parsedTime, err := time.Parse(time.RFC3339, terminationTime)
// if err != nil {
// return fmt.Errorf("could not parse time %q: %w", terminationTime, err)
// }
// endDt = parsedTime
// }
var endDt time.Time

if terminationTime != "" {
parsedTime, err := time.Parse(time.RFC3339, terminationTime)
if err != nil {
return fmt.Errorf("could not parse time %q: %w", terminationTime, err)
}
endDt = parsedTime
}
var publicSubnet int32
// In case of no configuration,
if publicIPv4SubnetSize == 0 {
publicSubnet = 31 // Assigin default to 31
} else {
publicSubnet = int32(publicIPv4SubnetSize)
}

formatedCdata := make(map[string]interface{})
if customdata != "" {
err := json.Unmarshal([]byte(customdata), &formatedCdata)
if err != nil {
return fmt.Errorf("could not convert customedata :%w", err)
}
}
var facilityArgs []string

// var deviceCreateInFacilityInput *metal.DeviceCreateInFacilityInput
var request metal.ApiCreateDeviceRequest
if facility != "" {
facilityArgs = append(facilityArgs, facility)
Expand All @@ -97,10 +113,36 @@ func (c *Client) Create() *cobra.Command {
Plan: plan,
OperatingSystem: operatingSystem,
Hostname: &hostname,
Userdata: &userdata,
},
}

if billingCycle != "" {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetBillingCycle(billingCycle)
}

if alwaysPXE {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetAlwaysPxe(alwaysPXE)
}

if ipxescripturl != "" {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetIpxeScriptUrl(ipxescripturl)
}

facilityDeviceRequest.DeviceCreateInFacilityInput.SetPublicIpv4SubnetSize(publicSubnet)
facilityDeviceRequest.DeviceCreateInFacilityInput.SetTerminationTime(endDt)
facilityDeviceRequest.DeviceCreateInFacilityInput.SetTags(tags)
facilityDeviceRequest.DeviceCreateInFacilityInput.SetSpotInstance(spotInstance)
facilityDeviceRequest.DeviceCreateInFacilityInput.SetSpotPriceMax(float32(spotPriceMax))
facilityDeviceRequest.DeviceCreateInFacilityInput.SetCustomdata(formatedCdata)

if hardwareReservationID != "" {
facilityDeviceRequest.DeviceCreateInFacilityInput.SetHardwareReservationId(hardwareReservationID)
}

request = c.Service.CreateDevice(context.Background(), projectID).CreateDeviceRequest(facilityDeviceRequest).Include(nil).Exclude(nil)
}

if metro != "" {

metroDeviceRequest := metal.CreateDeviceRequest{
Expand All @@ -109,9 +151,31 @@ func (c *Client) Create() *cobra.Command {
Plan: plan,
OperatingSystem: operatingSystem,
Hostname: &hostname,
Userdata: &userdata,
},
}
if billingCycle != "" {
metroDeviceRequest.DeviceCreateInMetroInput.SetBillingCycle(billingCycle)
}

if alwaysPXE {
metroDeviceRequest.DeviceCreateInMetroInput.SetAlwaysPxe(alwaysPXE)
}

if ipxescripturl != "" {
metroDeviceRequest.DeviceCreateInMetroInput.SetIpxeScriptUrl(ipxescripturl)
}

metroDeviceRequest.DeviceCreateInMetroInput.SetPublicIpv4SubnetSize(publicSubnet)
metroDeviceRequest.DeviceCreateInMetroInput.SetTerminationTime(endDt)
metroDeviceRequest.DeviceCreateInMetroInput.SetTags(tags)
metroDeviceRequest.DeviceCreateInMetroInput.SetSpotInstance(spotInstance)
metroDeviceRequest.DeviceCreateInMetroInput.SetSpotPriceMax(float32(spotPriceMax))
metroDeviceRequest.DeviceCreateInMetroInput.SetCustomdata(formatedCdata)

if hardwareReservationID != "" {
metroDeviceRequest.DeviceCreateInMetroInput.SetHardwareReservationId(hardwareReservationID)
}
request = c.Service.CreateDevice(context.Background(), projectID).CreateDeviceRequest(metroDeviceRequest).Include(nil).Exclude(nil)
}
device, _, err := request.Execute()
Expand Down
2 changes: 1 addition & 1 deletion internal/devices/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (c *Client) Update() *cobra.Command {
}

if alwaysPXE {
deviceUpdate.AlwaysPxe = &alwaysPXE
deviceUpdate.SetAlwaysPxe(true)
}

if ipxescripturl != "" {
Expand Down
24 changes: 19 additions & 5 deletions test/e2e/devices/devicecreatetest/device_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

func TestCli_Devices_create(t *testing.T) {
var projectId, deviceId string
var err error
var resp bool
subCommand := "device"
consumerToken := ""
apiURL := ""
Expand All @@ -40,10 +42,13 @@ func TestCli_Devices_create(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
projectId = helper.Create_test_project("create-device-pro")
projectId, err = helper.CreateTestProject("create-device-pro")
if err != nil {
t.Error(err)
}
if len(projectId) != 0 {

root.SetArgs([]string{subCommand, "create", "-p", projectId, "-P", "c3.small.x86", "-m", "da", "-O", "ubuntu_20_04", "-H", "create-device-dev"})
root.SetArgs([]string{subCommand, "create", "-p", projectId, "-P", "m3.small.x86", "-m", "da", "-O", "ubuntu_20_04", "-H", "create-device-dev"})
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
Expand All @@ -67,9 +72,18 @@ func TestCli_Devices_create(t *testing.T) {
// Extract the ID from the match
if len(match) > 1 {
deviceId = strings.TrimSpace(match[1])
if helper.Is_Device_state_active(deviceId) {
helper.Clean_test_device(deviceId)
helper.Clean_test_project(projectId)
resp, err = helper.IsDeviceStateActive(deviceId)
if err == nil && resp == true {
err = helper.CleanTestDevice(deviceId)
if err != nil {
t.Error(err)
}
err = helper.CleanTestProject(projectId)
if err != nil {
t.Error(err)
}
} else {
t.Error(err)
}
}
}
Expand Down
28 changes: 23 additions & 5 deletions test/e2e/devices/devicegettest/device_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (

func TestCli_Devices_get(t *testing.T) {
var projectId, deviceId string
var err error
var resp bool
subCommand := "device"
consumerToken := ""
apiURL := ""
Expand All @@ -39,8 +41,14 @@ func TestCli_Devices_get(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
projectId = helper.Create_test_project("get-by-device-id-pro")
deviceId = helper.Create_test_device(projectId, "get-by-device-id-dev")
projectId, err = helper.CreateTestProject("get-by-device-id-pro")
if err != nil {
t.Error(err)
}
deviceId, err = helper.CreateTestDevice(projectId, "get-by-device-id-dev")
if err != nil {
t.Error(err)
}
root.SetArgs([]string{subCommand, "get", "-i", deviceId})
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
Expand All @@ -55,10 +63,20 @@ func TestCli_Devices_get(t *testing.T) {
t.Error("expected output should include " + deviceId)
}
if len(projectId) != 0 && len(deviceId) != 0 {
if helper.Is_Device_state_active(deviceId) {
helper.Clean_test_device(deviceId)
helper.Clean_test_project(projectId)
resp, err = helper.IsDeviceStateActive(deviceId)
if err == nil && resp == true {
err = helper.CleanTestDevice(deviceId)
if err != nil {
t.Error(err)
}
err = helper.CleanTestProject(projectId)
if err != nil {
t.Error(err)
}
} else {
t.Error(err)
}

}
},
},
Expand Down
43 changes: 23 additions & 20 deletions test/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,80 +16,83 @@ func TestClient() *openapiclient.APIClient {
return apiClient
}

func Create_test_project(name string) string {
// func Create_test_project(name string) string {
func CreateTestProject(name string) (string, error) {
TestApiClient := TestClient()

projectCreateFromRootInput := *openapiclient.NewProjectCreateFromRootInput(name) // ProjectCreateFromRootInput | Project to create
include := []string{"Inner_example"}
exclude := []string{"Inner_example"}

projectResp, r, err := TestApiClient.ProjectsApi.CreateProject(context.Background()).ProjectCreateFromRootInput(projectCreateFromRootInput).Include(include).Exclude(exclude).Execute()
projectResp, r, err := TestApiClient.ProjectsApi.CreateProject(context.Background()).ProjectCreateFromRootInput(projectCreateFromRootInput).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `ProjectsApi.CreateProject``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
return ""
return "", err
}
return projectResp.GetId()
return projectResp.GetId(), nil
}

func Create_test_device(projectId, name string) string {
include := []string{"Inner_example"}
exclude := []string{"Inner_example"}

func CreateTestDevice(projectId, name string) (string, error) {
TestApiClient := TestClient()

hostname := name
metroDeviceRequest := openapiclient.CreateDeviceRequest{
DeviceCreateInMetroInput: &openapiclient.DeviceCreateInMetroInput{
Metro: "da",
Plan: "c3.small.x86",
Plan: "m3.small.x86",
OperatingSystem: "ubuntu_20_04",
Hostname: &hostname,
},
}
deviceResp, _, err := TestApiClient.DevicesApi.CreateDevice(context.Background(), projectId).CreateDeviceRequest(metroDeviceRequest).Include(include).Exclude(exclude).Execute()
deviceResp, _, err := TestApiClient.DevicesApi.CreateDevice(context.Background(), projectId).CreateDeviceRequest(metroDeviceRequest).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `DevicesApi.CreateDevice``: %v\n", err)
return ""
return "", err
}
return deviceResp.GetId()
return deviceResp.GetId(), nil
}

func Is_Device_state_active(deviceId string) bool {
func IsDeviceStateActive(deviceId string) (bool, error) {
var err error
var resp *openapiclient.Device
TestApiClient := TestClient()
predefinedTime := 200 * time.Second // Adjust this as needed
retryInterval := 10 * time.Second // Adjust this as needed
startTime := time.Now()
for time.Since(startTime) < predefinedTime {
resp, _, err := TestApiClient.DevicesApi.FindDeviceById(context.Background(), deviceId).Execute()
resp, _, err = TestApiClient.DevicesApi.FindDeviceById(context.Background(), deviceId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `DevicesApi.FindDeviceById``: %v\n", err)
return false, err
}
if resp.GetState() == "active" {
return true
return true, nil
}

// Sleep for the specified interval
time.Sleep(retryInterval)
}
return false
return false, err
}

func Clean_test_device(deviceId string) {
func CleanTestDevice(deviceId string) error {
forceDelete := true // bool | Force the deletion of the device, by detaching any storage volume still active. (optional)

TestApiClient := TestClient()
_, err := TestApiClient.DevicesApi.DeleteDevice(context.Background(), deviceId).ForceDelete(forceDelete).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `DevicesApi.DeleteDevice``: %v\n", err)
return err
}
return nil
}

func Clean_test_project(projectId string) {
func CleanTestProject(projectId string) error {
TestApiClient := TestClient()
r, err := TestApiClient.ProjectsApi.DeleteProject(context.Background(), projectId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `ProjectsApi.DeleteProject``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
return err
}
return nil
}

0 comments on commit 38b4bee

Please sign in to comment.