generated from TrueCloudLab/basic
[#26] schema: Add resource name validation method
Close #26 Signed-off-by: Airat Arifullin <aarifullin@yadro.com>
This commit is contained in:
parent
62ea96b82c
commit
e57d213595
3 changed files with 162 additions and 0 deletions
45
schema/native/util/validation.go
Normal file
45
schema/native/util/validation.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/policy-engine/schema/native"
|
||||
)
|
||||
|
||||
var nativePatterns = []string{
|
||||
native.ResourceFormatNamespaceObjects, native.ResourceFormatNamespaceContainerObjects,
|
||||
native.ResourceFormatNamespaceContainerObject, native.ResourceFormatRootObjects,
|
||||
native.ResourceFormatRootContainerObjects, native.ResourceFormatRootContainerObject,
|
||||
native.ResourceFormatAllObjects, native.ResourceFormatNamespaceContainer,
|
||||
native.ResourceFormatNamespaceContainers, native.ResourceFormatRootContainer,
|
||||
native.ResourceFormatRootContainers, native.ResourceFormatAllContainers,
|
||||
}
|
||||
|
||||
func match(resource, pattern string) bool {
|
||||
rTokens := strings.Split(resource, "/")
|
||||
pToken := strings.Split(pattern, "/")
|
||||
|
||||
if len(rTokens) != len(pToken) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := range rTokens {
|
||||
if pToken[i] == "%s" {
|
||||
continue
|
||||
}
|
||||
if pToken[i] != rTokens[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func IsNativeResourceNameValid(resource string) bool {
|
||||
for _, pattern := range nativePatterns {
|
||||
if match(resource, pattern) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue