...

Source file src/edge-infra.dev/pkg/sds/interlock/topic/host/kpower_state.go

Documentation: edge-infra.dev/pkg/sds/interlock/topic/host

     1  package host
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net/http"
     7  
     8  	"github.com/gin-contrib/requestid"
     9  	"github.com/gin-gonic/gin"
    10  	"github.com/gin-gonic/gin/binding"
    11  
    12  	"edge-infra.dev/pkg/sds/interlock/topic"
    13  )
    14  
    15  var (
    16  	ErrNoRequestID = fmt.Errorf("no request id")
    17  )
    18  
    19  type KpowerState struct {
    20  	Status    PowerStatus `json:"status"`
    21  	Op        Operation   `json:"operation"`
    22  	Timestamp string      `json:"timestamp"`
    23  }
    24  
    25  type Operation struct {
    26  	OpType    OperationType `json:"type"`
    27  	Opts      Options       `json:"options"`
    28  	RequestID string        `json:"requestID"`
    29  }
    30  
    31  type Options struct {
    32  	NoCordonOrDrain bool `json:"noCordonOrDrain"`
    33  }
    34  
    35  type PowerStatus string
    36  
    37  const (
    38  	UNKNOWN    PowerStatus = "UNKNOWN"
    39  	RUNNING    PowerStatus = "RUNNING"
    40  	CORDONED   PowerStatus = "CORDONED"
    41  	DRAINED    PowerStatus = "DRAINED"
    42  	TERMINATED PowerStatus = "TERMINATED"
    43  )
    44  
    45  type OperationType string
    46  
    47  const (
    48  	REBOOT   OperationType = "REBOOT"
    49  	SHUTDOWN OperationType = "SHUTDOWN"
    50  )
    51  
    52  func (h *Host) KpowerPost(c *gin.Context) {
    53  	err := h.topic.UpdateState(
    54  		func(state interface{}) error {
    55  			hostState := state.(*State)
    56  			if hostState.Kpower.Op.RequestID != "" {
    57  				c.String(http.StatusConflict, "%s\n", "conflict with existing operation")
    58  				return nil
    59  			}
    60  
    61  			var payload postKpowerPayload
    62  			err := c.ShouldBindWith(&payload, binding.JSON)
    63  			if err == io.EOF {
    64  				c.String(http.StatusBadRequest, "%s: %s\n", "missing request body", err.Error())
    65  				return nil
    66  			}
    67  			if err != nil {
    68  				c.String(http.StatusBadRequest, "%s: %s\n", "error binding request body", err.Error())
    69  				return nil
    70  			}
    71  			payload.RequestID = requestid.Get(c)
    72  			payload.updateState(hostState)
    73  			c.JSON(http.StatusAccepted, state)
    74  			return nil
    75  		},
    76  	)
    77  	if err != nil {
    78  		topic.HandleBindingError(c, err)
    79  		return
    80  	}
    81  }
    82  
    83  func (h *Host) KpowerPut(c *gin.Context) {
    84  	err := h.topic.UpdateState(
    85  		func(state interface{}) error {
    86  			var payload putKpowerPayload
    87  			if err := c.ShouldBindWith(&payload, binding.JSON); err != nil {
    88  				return err
    89  			}
    90  			hostState := state.(*State)
    91  			payload.updateState(hostState)
    92  			c.JSON(http.StatusAccepted, state)
    93  			return nil
    94  		},
    95  	)
    96  	if err != nil {
    97  		topic.HandleBindingError(c, err)
    98  		return
    99  	}
   100  }
   101  

View as plain text