packer-cn/builder/azure/chroot/shared_image_gallery_destin...

66 lines
2.5 KiB
Go
Raw Normal View History

2020-03-25 17:55:37 -04:00
//go:generate struct-markdown
2020-04-07 18:42:41 -04:00
//go:generate mapstructure-to-hcl2 -type SharedImageGalleryDestination,TargetRegion
2020-03-25 17:55:37 -04:00
package chroot
import (
"fmt"
"regexp"
)
2020-04-07 18:42:41 -04:00
// SharedImageGalleryDestination models an image version in a Shared
// Image Gallery that can be used as a destination.
2020-03-25 17:55:37 -04:00
type SharedImageGalleryDestination struct {
2020-04-07 18:42:41 -04:00
ResourceGroup string `mapstructure:"resource_group" required:"true"`
GalleryName string `mapstructure:"gallery_name" required:"true"`
ImageName string `mapstructure:"image_name" required:"true"`
ImageVersion string `mapstructure:"image_version" required:"true"`
2020-03-25 17:55:37 -04:00
TargetRegions []TargetRegion `mapstructure:"target_regions"`
ExcludeFromLatest bool `mapstructure:"exlude_from_latest"`
}
// TargetRegion describes a region where the shared image should be replicated
type TargetRegion struct {
2020-04-07 18:42:41 -04:00
// Name of the Azure region
Name string `mapstructure:"name" required:"true"`
2020-03-25 17:55:37 -04:00
// Number of replicas in this region. Default: 1
ReplicaCount int32 `mapstructure:"replicas"`
// Storage account type: Standard_LRS or Standard_ZRS. Default: Standard_ZRS
StorageAccountType string `mapstructure:"storage_account_type"`
}
// ResourceID returns the resource ID string
func (sigd SharedImageGalleryDestination) ResourceID(subscriptionID string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/galleries/%s/images/%s/versions/%s",
subscriptionID,
sigd.ResourceGroup,
sigd.GalleryName,
sigd.ImageName,
sigd.ImageVersion)
}
// Validate validates that the values in the shared image are valid (without checking them on the network)
func (sigd *SharedImageGalleryDestination) Validate(prefix string) (errs []error, warns []string) {
if sigd.ResourceGroup == "" {
errs = append(errs, fmt.Errorf("%s.resource_group is required", prefix))
}
if sigd.GalleryName == "" {
errs = append(errs, fmt.Errorf("%s.gallery_name is required", prefix))
}
if sigd.ImageName == "" {
errs = append(errs, fmt.Errorf("%s.image_name is required", prefix))
}
if match, err := regexp.MatchString("^[0-9]+\\.[0-9]+\\.[0-9]+$", sigd.ImageVersion); !match {
if err != nil {
warns = append(warns, fmt.Sprintf("Error matching pattern for %s.image_version: %s (this is probably a bug)", prefix, err))
}
errs = append(errs, fmt.Errorf("%s.image_version should match '^[0-9]+\\.[0-9]+\\.[0-9]+$'", prefix))
}
if len(sigd.TargetRegions) == 0 {
warns = append(warns,
fmt.Sprintf("%s.target_regions is empty; image will only be available in the region of the gallery", prefix))
}
return
}