github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/event/rulesmap.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  // RulesMap - map of rules for every event name.
    21  type RulesMap map[Name]Rules
    22  
    23  // add - adds event names, prefixes, suffixes and target ID to rules map.
    24  func (rulesMap RulesMap) add(eventNames []Name, pattern string, targetID TargetID) {
    25  	rules := make(Rules)
    26  	rules.Add(pattern, targetID)
    27  
    28  	for _, eventName := range eventNames {
    29  		for _, name := range eventName.Expand() {
    30  			rulesMap[name] = rulesMap[name].Union(rules)
    31  		}
    32  	}
    33  }
    34  
    35  // Clone - returns copy of this rules map.
    36  func (rulesMap RulesMap) Clone() RulesMap {
    37  	rulesMapCopy := make(RulesMap)
    38  
    39  	for eventName, rules := range rulesMap {
    40  		rulesMapCopy[eventName] = rules.Clone()
    41  	}
    42  
    43  	return rulesMapCopy
    44  }
    45  
    46  // Add - adds given rules map.
    47  func (rulesMap RulesMap) Add(rulesMap2 RulesMap) {
    48  	for eventName, rules := range rulesMap2 {
    49  		rulesMap[eventName] = rules.Union(rulesMap[eventName])
    50  	}
    51  }
    52  
    53  // Remove - removes given rules map.
    54  func (rulesMap RulesMap) Remove(rulesMap2 RulesMap) {
    55  	for eventName, rules := range rulesMap {
    56  		if nr := rules.Difference(rulesMap2[eventName]); len(nr) != 0 {
    57  			rulesMap[eventName] = nr
    58  		} else {
    59  			delete(rulesMap, eventName)
    60  		}
    61  	}
    62  }
    63  
    64  // MatchSimple - returns true if matching object name and event name in rules map.
    65  func (rulesMap RulesMap) MatchSimple(eventName Name, objectName string) bool {
    66  	return rulesMap[eventName].MatchSimple(objectName)
    67  }
    68  
    69  // Match - returns TargetIDSet matching object name and event name in rules map.
    70  func (rulesMap RulesMap) Match(eventName Name, objectName string) TargetIDSet {
    71  	return rulesMap[eventName].Match(objectName)
    72  }
    73  
    74  // NewRulesMap - creates new rules map with given values.
    75  func NewRulesMap(eventNames []Name, pattern string, targetID TargetID) RulesMap {
    76  	// If pattern is empty, add '*' wildcard to match all.
    77  	if pattern == "" {
    78  		pattern = "*"
    79  	}
    80  
    81  	rulesMap := make(RulesMap)
    82  	rulesMap.add(eventNames, pattern, targetID)
    83  	return rulesMap
    84  }