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 f3082b1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 11 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

0 comments on commit f3082b1

Please sign in to comment.