github.com/cyverse/go-irodsclient@v0.13.2/irods/message/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  // IRODSMessageAdminRequest stores alter metadata request
    11  type IRODSMessageAdminRequest struct {
    12  	XMLName xml.Name `xml:"generalAdminInp_PI"`
    13  	Action  string   `xml:"arg0"` // add, modify, rm, ...
    14  	Target  string   `xml:"arg1"` // user, group, zone, resource, ...
    15  	Arg2    string   `xml:"arg2"`
    16  	Arg3    string   `xml:"arg3"`
    17  	Arg4    string   `xml:"arg4"`
    18  	Arg5    string   `xml:"arg5"`
    19  	Arg6    string   `xml:"arg6"`
    20  	Arg7    string   `xml:"arg7"`
    21  	Arg8    string   `xml:"arg8"` // unused
    22  	Arg9    string   `xml:"arg9"` // unused
    23  }
    24  
    25  // NewIRODSMessageAdminRequest creates a new IRODSMessageAdminRequest
    26  func NewIRODSMessageAdminRequest(action, target string, args ...string) *IRODSMessageAdminRequest {
    27  	request := &IRODSMessageAdminRequest{
    28  		Action: action,
    29  		Target: target,
    30  	}
    31  
    32  	if len(args) > 0 {
    33  		request.Arg2 = args[0]
    34  	}
    35  
    36  	if len(args) > 1 {
    37  		request.Arg3 = args[1]
    38  	}
    39  
    40  	if len(args) > 2 {
    41  		request.Arg4 = args[2]
    42  	}
    43  
    44  	if len(args) > 3 {
    45  		request.Arg5 = args[3]
    46  	}
    47  
    48  	if len(args) > 4 {
    49  		request.Arg6 = args[4]
    50  	}
    51  
    52  	if len(args) > 5 {
    53  		request.Arg7 = args[5]
    54  	}
    55  
    56  	if len(args) > 6 {
    57  		request.Arg8 = args[6]
    58  	}
    59  
    60  	if len(args) > 7 {
    61  		request.Arg9 = args[7]
    62  	}
    63  
    64  	return request
    65  }
    66  
    67  // GetBytes returns byte array
    68  func (msg *IRODSMessageAdminRequest) GetBytes() ([]byte, error) {
    69  	xmlBytes, err := xml.Marshal(msg)
    70  	if err != nil {
    71  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    72  	}
    73  	return xmlBytes, nil
    74  }
    75  
    76  // FromBytes returns struct from bytes
    77  func (msg *IRODSMessageAdminRequest) FromBytes(bytes []byte) error {
    78  	err := xml.Unmarshal(bytes, msg)
    79  	if err != nil {
    80  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    81  	}
    82  	return nil
    83  }
    84  
    85  // GetMessage builds a message
    86  func (msg *IRODSMessageAdminRequest) GetMessage() (*IRODSMessage, error) {
    87  	bytes, err := msg.GetBytes()
    88  	if err != nil {
    89  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    90  	}
    91  
    92  	msgBody := IRODSMessageBody{
    93  		Type:    RODS_MESSAGE_API_REQ_TYPE,
    94  		Message: bytes,
    95  		Error:   nil,
    96  		Bs:      nil,
    97  		IntInfo: int32(common.GENERAL_ADMIN_AN),
    98  	}
    99  
   100  	msgHeader, err := msgBody.BuildHeader()
   101  	if err != nil {
   102  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
   103  	}
   104  
   105  	return &IRODSMessage{
   106  		Header: msgHeader,
   107  		Body:   &msgBody,
   108  	}, nil
   109  }