storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/event/arn.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2018 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package event
    18  
    19  import (
    20  	"encoding/xml"
    21  	"strings"
    22  )
    23  
    24  // ARN - SQS resource name representation.
    25  type ARN struct {
    26  	TargetID
    27  	region string
    28  }
    29  
    30  // String - returns string representation.
    31  func (arn ARN) String() string {
    32  	if arn.TargetID.ID == "" && arn.TargetID.Name == "" && arn.region == "" {
    33  		return ""
    34  	}
    35  
    36  	return "arn:minio:sqs:" + arn.region + ":" + arn.TargetID.String()
    37  }
    38  
    39  // MarshalXML - encodes to XML data.
    40  func (arn ARN) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    41  	return e.EncodeElement(arn.String(), start)
    42  }
    43  
    44  // UnmarshalXML - decodes XML data.
    45  func (arn *ARN) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    46  	var s string
    47  	if err := d.DecodeElement(&s, &start); err != nil {
    48  		return err
    49  	}
    50  
    51  	parsedARN, err := parseARN(s)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	*arn = *parsedARN
    57  	return nil
    58  }
    59  
    60  // parseARN - parses string to ARN.
    61  func parseARN(s string) (*ARN, error) {
    62  	// ARN must be in the format of arn:minio:sqs:<REGION>:<ID>:<TYPE>
    63  	if !strings.HasPrefix(s, "arn:minio:sqs:") {
    64  		return nil, &ErrInvalidARN{s}
    65  	}
    66  
    67  	tokens := strings.Split(s, ":")
    68  	if len(tokens) != 6 {
    69  		return nil, &ErrInvalidARN{s}
    70  	}
    71  
    72  	if tokens[4] == "" || tokens[5] == "" {
    73  		return nil, &ErrInvalidARN{s}
    74  	}
    75  
    76  	return &ARN{
    77  		region: tokens[3],
    78  		TargetID: TargetID{
    79  			ID:   tokens[4],
    80  			Name: tokens[5],
    81  		},
    82  	}, nil
    83  }