github.com/cyverse/go-irodsclient@v0.13.2/irods/message/get_process_stat_request.go (about)

     1  package message
     2  
     3  import (
     4  	"encoding/xml"
     5  
     6  	"github.com/cyverse/go-irodsclient/irods/common"
     7  	"github.com/cyverse/go-irodsclient/irods/util"
     8  	"golang.org/x/xerrors"
     9  )
    10  
    11  // IRODSMessageGetProcessstatRequest stores process stat request
    12  type IRODSMessageGetProcessstatRequest struct {
    13  	XMLName xml.Name             `xml:"ProcStatInp_PI"`
    14  	Address string               `xml:"addr"`
    15  	Zone    string               `xml:"rodsZone"`
    16  	KeyVals IRODSMessageSSKeyVal `xml:"KeyValPair_PI"`
    17  }
    18  
    19  // NewIRODSMessageGetProcessstatRequest creates a IRODSMessageGetProcessstatRequest message
    20  func NewIRODSMessageGetProcessstatRequest(address string, zone string) *IRODSMessageGetProcessstatRequest {
    21  	return &IRODSMessageGetProcessstatRequest{
    22  		Address: address,
    23  		Zone:    zone,
    24  		KeyVals: IRODSMessageSSKeyVal{
    25  			Length: 0,
    26  		},
    27  	}
    28  }
    29  
    30  // AddKeyVal adds a key-value pair
    31  func (msg *IRODSMessageGetProcessstatRequest) AddKeyVal(key common.KeyWord, val string) {
    32  	escapedVal := util.EscapeXMLSpecialChars(val)
    33  	msg.KeyVals.Add(string(key), escapedVal)
    34  }
    35  
    36  // GetBytes returns byte array
    37  func (msg *IRODSMessageGetProcessstatRequest) GetBytes() ([]byte, error) {
    38  	xmlBytes, err := xml.Marshal(msg)
    39  	if err != nil {
    40  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    41  	}
    42  	return xmlBytes, nil
    43  }
    44  
    45  // FromBytes returns struct from bytes
    46  func (msg *IRODSMessageGetProcessstatRequest) FromBytes(bytes []byte) error {
    47  	err := xml.Unmarshal(bytes, msg)
    48  	if err != nil {
    49  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    50  	}
    51  	return nil
    52  }
    53  
    54  // GetMessage builds a message
    55  func (msg *IRODSMessageGetProcessstatRequest) GetMessage() (*IRODSMessage, error) {
    56  	bytes, err := msg.GetBytes()
    57  	if err != nil {
    58  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    59  	}
    60  
    61  	msgBody := IRODSMessageBody{
    62  		Type:    RODS_MESSAGE_API_REQ_TYPE,
    63  		Message: bytes,
    64  		Error:   nil,
    65  		Bs:      nil,
    66  		IntInfo: int32(common.PROC_STAT_AN),
    67  	}
    68  
    69  	msgHeader, err := msgBody.BuildHeader()
    70  	if err != nil {
    71  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
    72  	}
    73  
    74  	return &IRODSMessage{
    75  		Header: msgHeader,
    76  		Body:   &msgBody,
    77  	}, nil
    78  }