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

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