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

     1  package message
     2  
     3  import (
     4  	"encoding/xml"
     5  	"fmt"
     6  
     7  	"github.com/cyverse/go-irodsclient/irods/common"
     8  	"golang.org/x/xerrors"
     9  )
    10  
    11  // IRODSMessageTrimDataObjectRequest stores data object replication request
    12  type IRODSMessageTrimDataObjectRequest IRODSMessageDataObjectRequest
    13  
    14  // NewIRODSMessageTrimDataObjectRequest creates a IRODSMessageTrimDataObjectRequest message
    15  func NewIRODSMessageTrimDataObjectRequest(path string, resource string, minCopies int, minAgeMinutes int) *IRODSMessageTrimDataObjectRequest {
    16  	request := &IRODSMessageTrimDataObjectRequest{
    17  		Path:          path,
    18  		CreateMode:    0,
    19  		OpenFlags:     0,
    20  		Offset:        0,
    21  		Size:          -1,
    22  		Threads:       0,
    23  		OperationType: 0,
    24  		KeyVals: IRODSMessageSSKeyVal{
    25  			Length: 0,
    26  		},
    27  	}
    28  
    29  	if len(resource) > 0 {
    30  		request.KeyVals.Add(string(common.RESC_NAME_KW), resource)
    31  	}
    32  
    33  	if minCopies > 0 {
    34  		request.KeyVals.Add(string(common.COPIES_KW), fmt.Sprintf("%d", minCopies))
    35  	}
    36  
    37  	if minAgeMinutes > 0 {
    38  		request.KeyVals.Add(string(common.AGE_KW), fmt.Sprintf("%d", minAgeMinutes))
    39  	}
    40  
    41  	return request
    42  }
    43  
    44  // AddKeyVal adds a key-value pair
    45  func (msg *IRODSMessageTrimDataObjectRequest) AddKeyVal(key common.KeyWord, val string) {
    46  	msg.KeyVals.Add(string(key), val)
    47  }
    48  
    49  // GetBytes returns byte array
    50  func (msg *IRODSMessageTrimDataObjectRequest) GetBytes() ([]byte, error) {
    51  	xmlBytes, err := xml.Marshal(msg)
    52  	if err != nil {
    53  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    54  	}
    55  	return xmlBytes, nil
    56  }
    57  
    58  // FromBytes returns struct from bytes
    59  func (msg *IRODSMessageTrimDataObjectRequest) FromBytes(bytes []byte) error {
    60  	err := xml.Unmarshal(bytes, msg)
    61  	if err != nil {
    62  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    63  	}
    64  	return nil
    65  }
    66  
    67  // GetMessage builds a message
    68  func (msg *IRODSMessageTrimDataObjectRequest) GetMessage() (*IRODSMessage, error) {
    69  	bytes, err := msg.GetBytes()
    70  	if err != nil {
    71  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    72  	}
    73  
    74  	msgBody := IRODSMessageBody{
    75  		Type:    RODS_MESSAGE_API_REQ_TYPE,
    76  		Message: bytes,
    77  		Error:   nil,
    78  		Bs:      nil,
    79  		IntInfo: int32(common.DATA_OBJ_TRIM_AN),
    80  	}
    81  
    82  	msgHeader, err := msgBody.BuildHeader()
    83  	if err != nil {
    84  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
    85  	}
    86  
    87  	return &IRODSMessage{
    88  		Header: msgHeader,
    89  		Body:   &msgBody,
    90  	}, nil
    91  }