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

     1  package message
     2  
     3  import (
     4  	"encoding/base64"
     5  	"encoding/json"
     6  	"encoding/xml"
     7  
     8  	"github.com/cyverse/go-irodsclient/irods/common"
     9  	"golang.org/x/xerrors"
    10  )
    11  
    12  // IRODSMessageCloseDataObjectReplicaRequest stores data object replica close request
    13  // Uses JSON, not XML
    14  // Supported v4.2.9 or above
    15  type IRODSMessageCloseDataObjectReplicaRequest struct {
    16  	FileDescriptor            int  `json:"fd"`
    17  	SendNotification          bool `json:"send_notification"`
    18  	UpdateSize                bool `json:"update_size"`
    19  	UpdateStatus              bool `json:"update_status"`
    20  	ComputeChecksum           bool `json:"compute_checksum"`
    21  	PreserveReplicaStateTable bool `json:"preserve_replica_state_table"`
    22  }
    23  
    24  // NewIRODSMessageCloseDataObjectReplicaRequest creates a IRODSMessageCloseDataObjectReplicaRequest message
    25  func NewIRODSMessageCloseDataObjectReplicaRequest(desc int, sendNotification bool, updateSize bool, updateStatus bool, computeChecksum bool, preserveReplicaStateTable bool) *IRODSMessageCloseDataObjectReplicaRequest {
    26  	request := &IRODSMessageCloseDataObjectReplicaRequest{
    27  		FileDescriptor:            desc,
    28  		SendNotification:          sendNotification,
    29  		UpdateSize:                updateSize,
    30  		UpdateStatus:              updateStatus,
    31  		ComputeChecksum:           computeChecksum,
    32  		PreserveReplicaStateTable: preserveReplicaStateTable,
    33  	}
    34  
    35  	return request
    36  }
    37  
    38  // GetBytes returns byte array
    39  func (msg *IRODSMessageCloseDataObjectReplicaRequest) GetBytes() ([]byte, error) {
    40  	jsonBody, err := json.Marshal(msg)
    41  	if err != nil {
    42  		return nil, xerrors.Errorf("failed to marshal irods message to json: %w", err)
    43  	}
    44  
    45  	jsonBodyBin := base64.StdEncoding.EncodeToString(jsonBody)
    46  
    47  	binBytesBuf := IRODSMessageBinBytesBuf{
    48  		Length: len(jsonBody), // use original data's length
    49  		Data:   jsonBodyBin,
    50  	}
    51  
    52  	xmlBytes, err := xml.Marshal(binBytesBuf)
    53  	if err != nil {
    54  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    55  	}
    56  	return xmlBytes, nil
    57  }
    58  
    59  // FromBytes returns struct from bytes
    60  func (msg *IRODSMessageCloseDataObjectReplicaRequest) FromBytes(bytes []byte) error {
    61  	binBytesBuf := IRODSMessageBinBytesBuf{}
    62  	err := xml.Unmarshal(bytes, &binBytesBuf)
    63  	if err != nil {
    64  		return xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    65  	}
    66  
    67  	jsonBody, err := base64.StdEncoding.DecodeString(binBytesBuf.Data)
    68  	if err != nil {
    69  		return xerrors.Errorf("failed to decode base64 data: %w", err)
    70  	}
    71  
    72  	err = json.Unmarshal(jsonBody, msg)
    73  	if err != nil {
    74  		return xerrors.Errorf("failed to unmarshal json to irods message: %w", err)
    75  	}
    76  	return nil
    77  }
    78  
    79  // GetMessage builds a message
    80  func (msg *IRODSMessageCloseDataObjectReplicaRequest) GetMessage() (*IRODSMessage, error) {
    81  	bytes, err := msg.GetBytes()
    82  	if err != nil {
    83  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    84  	}
    85  
    86  	msgBody := IRODSMessageBody{
    87  		Type:    RODS_MESSAGE_API_REQ_TYPE,
    88  		Message: bytes,
    89  		Error:   nil,
    90  		Bs:      nil,
    91  		IntInfo: int32(common.REPLICA_CLOSE_APN),
    92  	}
    93  
    94  	msgHeader, err := msgBody.BuildHeader()
    95  	if err != nil {
    96  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
    97  	}
    98  
    99  	return &IRODSMessage{
   100  		Header: msgHeader,
   101  		Body:   &msgBody,
   102  	}, nil
   103  }