github.com/cyverse/go-irodsclient@v0.13.2/irods/message/ticket_admin_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  // IRODSMessageTicketAdminRequest stores ticket admin request
    11  type IRODSMessageTicketAdminRequest struct {
    12  	XMLName xml.Name             `xml:"ticketAdminInp_PI"`
    13  	Action  string               `xml:"arg1"` // session, create, or mod
    14  	Ticket  string               `xml:"arg2"` // ticket name
    15  	Arg3    string               `xml:"arg3"` // ticket type
    16  	Arg4    string               `xml:"arg4"` // path
    17  	Arg5    string               `xml:"arg5"` // ticket name again
    18  	Arg6    string               `xml:"arg6"`
    19  	KeyVals IRODSMessageSSKeyVal `xml:"KeyValPair_PI"`
    20  }
    21  
    22  // NewIRODSMessageTicketAdminRequest creates a new IRODSMessageTicketAdminRequest
    23  func NewIRODSMessageTicketAdminRequest(action string, ticket string, args ...string) *IRODSMessageTicketAdminRequest {
    24  	request := &IRODSMessageTicketAdminRequest{
    25  		Action: action,
    26  		Ticket: ticket,
    27  		KeyVals: IRODSMessageSSKeyVal{
    28  			Length: 0,
    29  		},
    30  	}
    31  
    32  	if len(args) > 0 {
    33  		request.Arg3 = args[0]
    34  	}
    35  
    36  	if len(args) > 1 {
    37  		request.Arg4 = args[1]
    38  	}
    39  
    40  	if len(args) > 2 {
    41  		request.Arg5 = args[2]
    42  	}
    43  
    44  	if len(args) > 3 {
    45  		request.Arg6 = args[3]
    46  	}
    47  
    48  	return request
    49  }
    50  
    51  // AddKeyVal adds a key-value pair
    52  func (msg *IRODSMessageTicketAdminRequest) AddKeyVal(key common.KeyWord, val string) {
    53  	msg.KeyVals.Add(string(key), val)
    54  }
    55  
    56  // GetBytes returns byte array
    57  func (msg *IRODSMessageTicketAdminRequest) GetBytes() ([]byte, error) {
    58  	xmlBytes, err := xml.Marshal(msg)
    59  	if err != nil {
    60  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    61  	}
    62  	return xmlBytes, nil
    63  }
    64  
    65  // FromBytes returns struct from bytes
    66  func (msg *IRODSMessageTicketAdminRequest) FromBytes(bytes []byte) error {
    67  	err := xml.Unmarshal(bytes, msg)
    68  	if err != nil {
    69  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    70  	}
    71  	return nil
    72  }
    73  
    74  // GetMessage builds a message
    75  func (msg *IRODSMessageTicketAdminRequest) GetMessage() (*IRODSMessage, error) {
    76  	bytes, err := msg.GetBytes()
    77  	if err != nil {
    78  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    79  	}
    80  
    81  	msgBody := IRODSMessageBody{
    82  		Type:    RODS_MESSAGE_API_REQ_TYPE,
    83  		Message: bytes,
    84  		Error:   nil,
    85  		Bs:      nil,
    86  		IntInfo: int32(common.TICKET_ADMIN_AN),
    87  	}
    88  
    89  	msgHeader, err := msgBody.BuildHeader()
    90  	if err != nil {
    91  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
    92  	}
    93  
    94  	return &IRODSMessage{
    95  		Header: msgHeader,
    96  		Body:   &msgBody,
    97  	}, nil
    98  }