diff --git a/cmd/upload.go b/cmd/upload.go index d7b5b65..1c82eaf 100644 --- a/cmd/upload.go +++ b/cmd/upload.go @@ -21,6 +21,7 @@ const ( uploadFlagFormat = "format" uploadFlagArchitecture = "architecture" uploadFlagServerType = "server-type" + uploadFlagLocation = "location" uploadFlagDescription = "description" uploadFlagLabels = "labels" ) @@ -52,6 +53,7 @@ var uploadCmd = &cobra.Command{ imageFormat, _ := cmd.Flags().GetString(uploadFlagFormat) architecture, _ := cmd.Flags().GetString(uploadFlagArchitecture) serverType, _ := cmd.Flags().GetString(uploadFlagServerType) + location, _ := cmd.Flags().GetString(uploadFlagLocation) description, _ := cmd.Flags().GetString(uploadFlagDescription) labels, _ := cmd.Flags().GetStringToString(uploadFlagLabels) @@ -102,6 +104,10 @@ var uploadCmd = &cobra.Command{ options.ServerType = &hcloud.ServerType{Name: serverType} } + if location != "" { + options.Location = &hcloud.Location{Name: location} + } + image, err := client.Upload(ctx, options) if err != nil { return fmt.Errorf("failed to upload the image: %w", err) @@ -140,6 +146,7 @@ func init() { ) uploadCmd.Flags().String(uploadFlagServerType, "", "Explicitly use this server type to generate the image. Mutually exclusive with --architecture.") + uploadCmd.Flags().String(uploadFlagLocation, "", "Explicitly use this location to generate the image.") // Only one of them needs to be set uploadCmd.MarkFlagsOneRequired(uploadFlagArchitecture, uploadFlagServerType) diff --git a/hcloudimages/client.go b/hcloudimages/client.go index 5940f98..140f8d1 100644 --- a/hcloudimages/client.go +++ b/hcloudimages/client.go @@ -84,6 +84,10 @@ type UploadOptions struct { // - The default server type is no longer available, or not temporarily out of stock. ServerType *hcloud.ServerType + // Location can be optionally set to override the default location. + // This is useful is the default location is unavailable. + Location *hcloud.Location + // Description is an optional description that the resulting image (snapshot) will have. There is no way to // select images by its description, you should use Labels if you need to identify your image later. Description *string @@ -214,9 +218,16 @@ func (s *Client) Upload(ctx context.Context, options UploadOptions) (*hcloud.Ima } } + var location *hcloud.Location + if options.Location != nil { + location = options.Location + } else { + location = defaultLocation + } + logger.DebugContext(ctx, "creating server with config", "image", defaultImage.Name, - "location", defaultLocation.Name, + "location", location.Name, "serverType", serverType.Name, ) serverCreateResult, _, err := s.c.Server.Create(ctx, hcloud.ServerCreateOpts{ @@ -230,7 +241,7 @@ func (s *Client) Upload(ctx context.Context, options UploadOptions) (*hcloud.Ima StartAfterCreate: hcloud.Ptr(false), // Image will never be booted, we only boot into rescue system Image: defaultImage, - Location: defaultLocation, + Location: location, Labels: labels, }) if err != nil {