2019-03-26 08:29:15 -04:00
|
|
|
package yandex
|
|
|
|
|
2019-10-04 05:31:33 -04:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
)
|
2019-03-26 08:29:15 -04:00
|
|
|
|
|
|
|
func isalphanumeric(b byte) bool {
|
|
|
|
if '0' <= b && b <= '9' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if 'a' <= b && b <= 'z' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-10-04 05:31:33 -04:00
|
|
|
// Clean up resource name by replacing invalid characters with "-"
|
2019-03-26 08:29:15 -04:00
|
|
|
// and converting upper cases to lower cases
|
2019-10-04 05:31:33 -04:00
|
|
|
func templateCleanResourceName(s string) string {
|
2019-03-26 08:29:15 -04:00
|
|
|
if reImageFamily.MatchString(s) {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
b := []byte(strings.ToLower(s))
|
|
|
|
newb := make([]byte, len(b))
|
|
|
|
for i := range newb {
|
|
|
|
if isalphanumeric(b[i]) {
|
|
|
|
newb[i] = b[i]
|
|
|
|
} else {
|
|
|
|
newb[i] = '-'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(newb)
|
|
|
|
}
|
|
|
|
|
|
|
|
var TemplateFuncs = template.FuncMap{
|
2019-10-04 05:31:33 -04:00
|
|
|
"clean_resource_name": templateCleanResourceName,
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|