Skip to content

Commit

Permalink
condition/broker: define the server broker condition types
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrebel committed Jun 25, 2024
1 parent 6e53dbb commit 5f1c4bb
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions condition/broker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package condition

import (
"encoding/json"

"github.com/google/uuid"
)

type (
// BrokerAction identifies the kind of boot image action to be taken.
BrokerAction string
// BrokerActionPurpose hints the controller as to the reason for the action.
BrokerActionPurpose string
)

const (
Broker Kind = "broker"

// These two Kinds are added just to enable the Condition API, Orchestrator to differentiate
// between the two Kinds when applying an Update.
//
// https://github.com/metal-toolbox/conditionorc/blob/92f9b1619074b7c4ef4b35f39c3beab3c254966f/internal/store/nats.go#L263
//
BrokerAcquireServer Kind = "broker.acquireServer"
BrokerReleaseServer Kind = "broker.releaseServer"

// PurposeFirmwareInstall tells the target controller to take necessary steps
// to ensure the server is reserved for a firmware install.
PurposeFirmwareInstall BrokerActionPurpose = "firmwareInstall"

// Acquire indicates the server is to be flagged into a state where its unavailable to other systems.
AcquireServer BrokerAction = "acquireServer"
// ReleaseServer indicates the server is to be reverted to its previous state.
ReleaseServer BrokerAction = "releaseServer"
)

type BrokerTaskParameters struct {
AssetID uuid.UUID `json:"asset_id"`
Action BrokerAction `json:"action"`
Purpose BrokerActionPurpose `json:"action_purpose"`
ServerAcquire *ServerAcquire `json:"acquire,omitempty"`
ServerRelease *ServerRelease `json:"release,omitempty"`
}

func (p *BrokerTaskParameters) MapStringInterfaceToStruct(m map[string]interface{}) error {
jsonData, err := json.Marshal(m)
if err != nil {
return err
}

return json.Unmarshal(jsonData, p)
}

func (p *BrokerTaskParameters) Unmarshal(r json.RawMessage) error {
return json.Unmarshal(r, p)
}

func (p *BrokerTaskParameters) Marshal() (json.RawMessage, error) {
return json.Marshal(p)
}

func (p *BrokerTaskParameters) MustMarshal() json.RawMessage {
byt, err := json.Marshal(p)
if err != nil {
panic(err)
}

return byt
}

type ServerAcquire struct {
Info string `json:"info"`
CustomIpxe bool `json:"custom_ipxe"`
PersistentCustomIpxe bool `json:"persistent_custom_ipxe"`
}

type ServerRelease struct {
Info string `json:"info"`
}

func NewBrokerTaskParameters(assetID uuid.UUID, action BrokerAction, purpose BrokerActionPurpose, info string) *BrokerTaskParameters {
switch action {
case AcquireServer:
return &BrokerTaskParameters{
AssetID: assetID,
Action: AcquireServer,
Purpose: purpose,
ServerAcquire: &ServerAcquire{
Info: info,
CustomIpxe: true,
PersistentCustomIpxe: true,
},
}
case ReleaseServer:
return &BrokerTaskParameters{
AssetID: assetID,
Action: ReleaseServer,
Purpose: purpose,
ServerRelease: &ServerRelease{
Info: info,
},
}
}

return nil
}

0 comments on commit 5f1c4bb

Please sign in to comment.