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