github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/event/arn.go (about) 1 // Copyright (c) 2015-2021 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package event 19 20 import ( 21 "encoding/xml" 22 "strings" 23 ) 24 25 // ARN - SQS resource name representation. 26 type ARN struct { 27 TargetID 28 region string 29 } 30 31 // String - returns string representation. 32 func (arn ARN) String() string { 33 if arn.TargetID.ID == "" && arn.TargetID.Name == "" && arn.region == "" { 34 return "" 35 } 36 37 return "arn:minio:sqs:" + arn.region + ":" + arn.TargetID.String() 38 } 39 40 // MarshalXML - encodes to XML data. 41 func (arn ARN) MarshalXML(e *xml.Encoder, start xml.StartElement) error { 42 return e.EncodeElement(arn.String(), start) 43 } 44 45 // UnmarshalXML - decodes XML data. 46 func (arn *ARN) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { 47 var s string 48 if err := d.DecodeElement(&s, &start); err != nil { 49 return err 50 } 51 52 parsedARN, err := parseARN(s) 53 if err != nil { 54 return err 55 } 56 57 *arn = *parsedARN 58 return nil 59 } 60 61 // parseARN - parses string to ARN. 62 func parseARN(s string) (*ARN, error) { 63 // ARN must be in the format of arn:minio:sqs:<REGION>:<ID>:<TYPE> 64 if !strings.HasPrefix(s, "arn:minio:sqs:") { 65 return nil, &ErrInvalidARN{s} 66 } 67 68 tokens := strings.Split(s, ":") 69 if len(tokens) != 6 { 70 return nil, &ErrInvalidARN{s} 71 } 72 73 if tokens[4] == "" || tokens[5] == "" { 74 return nil, &ErrInvalidARN{s} 75 } 76 77 return &ARN{ 78 region: tokens[3], 79 TargetID: TargetID{ 80 ID: tokens[4], 81 Name: tokens[5], 82 }, 83 }, nil 84 }