...

Source file src/edge-infra.dev/pkg/sds/emergencyaccess/msgdata/response.go

Documentation: edge-infra.dev/pkg/sds/emergencyaccess/msgdata

     1  package msgdata
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"strings"
     8  )
     9  
    10  type CommandResponse interface {
    11  	json.Marshaler
    12  	Data() ResponseData
    13  	Attributes() ResponseAttributes
    14  }
    15  
    16  type ResponseData struct {
    17  	Type      string  `json:"type"`
    18  	ExitCode  int     `json:"exitCode"`
    19  	Output    string  `json:"output"`
    20  	TimeStamp string  `json:"timestamp"`
    21  	Duration  float64 `json:"duration"`
    22  }
    23  
    24  type ResponseAttributes struct {
    25  	GeneralAttributes
    26  	ReqMsgID string `json:"request-message-uuid"`
    27  }
    28  
    29  type RequestAttributes struct {
    30  	GeneralAttributes
    31  	CommandID string `json:"commandId"`
    32  }
    33  
    34  type commandResponse struct {
    35  	data       ResponseData
    36  	attributes ResponseAttributes
    37  }
    38  
    39  func (resp *commandResponse) Data() ResponseData {
    40  	return resp.data
    41  }
    42  
    43  func (resp *commandResponse) Attributes() ResponseAttributes {
    44  	return resp.attributes
    45  }
    46  
    47  func (resp *commandResponse) MarshalJSON() ([]byte, error) {
    48  	return json.Marshal(struct {
    49  		Data       ResponseData       `json:"data"`
    50  		Attributes ResponseAttributes `json:"attributes"`
    51  	}{
    52  		Data:       resp.data,
    53  		Attributes: resp.attributes,
    54  	})
    55  }
    56  
    57  func NewCommandResponse(databytes []byte, attrmap map[string]string) (CommandResponse, error) {
    58  	var data ResponseData
    59  	err := json.Unmarshal(databytes, &data)
    60  	if err != nil {
    61  		return nil, fmt.Errorf("unmarshalling ResponseData: %w", err)
    62  	}
    63  	genAttr, err := newGeneralAttributes(attrmap)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	attr := ResponseAttributes{
    68  		GeneralAttributes: *genAttr,
    69  		ReqMsgID:          attrmap["request-message-uuid"],
    70  	}
    71  	resp := &commandResponse{data: data, attributes: attr}
    72  	if err = resp.validate(); err != nil {
    73  		return nil, err
    74  	}
    75  	return resp, nil
    76  }
    77  
    78  type CommandResponseErr []error
    79  
    80  func (myerr CommandResponseErr) Error() string {
    81  	var msg []string
    82  	for _, err := range myerr {
    83  		msg = append(msg, err.Error())
    84  	}
    85  	return strings.Join(msg, ", ")
    86  }
    87  
    88  func (resp *commandResponse) validate() error {
    89  	errs := CommandResponseErr{}
    90  
    91  	if resp.data.Type == "" {
    92  		errs = append(errs, errors.New("response missing type field"))
    93  	}
    94  	if resp.attributes.ReqMsgID == "" {
    95  		errs = append(errs, errors.New("response missing reqmsgid field"))
    96  	}
    97  	errs = resp.attributes.validate(errs)
    98  
    99  	if len(errs) != 0 {
   100  		return errs
   101  	}
   102  	return nil
   103  }
   104  

View as plain text