2017-10-15 11:53:18 -04:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
2019-04-03 04:27:05 -04:00
|
|
|
|
|
|
|
packertpl "github.com/hashicorp/packer/common/template"
|
2017-10-15 11:53:18 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func isalphanumeric(b byte) bool {
|
|
|
|
if '0' <= b && b <= '9' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if 'a' <= b && b <= 'z' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up image name by replacing invalid characters with "-"
|
2017-10-18 21:45:48 -04:00
|
|
|
// and converting upper cases to lower cases
|
2017-10-15 22:45:18 -04:00
|
|
|
func templateCleanImageName(s string) string {
|
2019-09-16 13:10:37 -04:00
|
|
|
if validImageName.MatchString(s) {
|
2017-10-15 11:53:18 -04:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
b := []byte(strings.ToLower(s))
|
2017-10-18 21:45:48 -04:00
|
|
|
newb := make([]byte, len(b))
|
2017-10-15 11:53:18 -04:00
|
|
|
for i := range newb {
|
|
|
|
if isalphanumeric(b[i]) {
|
|
|
|
newb[i] = b[i]
|
|
|
|
} else {
|
|
|
|
newb[i] = '-'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(newb)
|
|
|
|
}
|
|
|
|
|
|
|
|
var TemplateFuncs = template.FuncMap{
|
2019-04-03 04:27:05 -04:00
|
|
|
"clean_resource_name": templateCleanImageName,
|
|
|
|
"clean_image_name": packertpl.DeprecatedTemplateFunc("clean_image_name", "clean_resource_name", templateCleanImageName),
|
2017-10-15 11:53:18 -04:00
|
|
|
}
|