|
| 1 | +// Copyright 2025 Red Hat |
| 2 | +// Copyright 2018 CoreOS, Inc. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package azure |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + |
| 23 | + "github.com/spf13/cobra" |
| 24 | +) |
| 25 | + |
| 26 | +var ( |
| 27 | + cmdCreateGalleryImage = &cobra.Command{ |
| 28 | + Use: "create-gallery-image", |
| 29 | + Short: "Create Azure Gallery image", |
| 30 | + Long: "Create Azure Gallery image from a blob url", |
| 31 | + RunE: runCreateGalleryImage, |
| 32 | + Aliases: []string{"create-gallery-image-arm"}, |
| 33 | + |
| 34 | + SilenceUsage: true, |
| 35 | + } |
| 36 | + |
| 37 | + galleryImageName string |
| 38 | + galleryName string |
| 39 | + sourceImageId string |
| 40 | +) |
| 41 | + |
| 42 | +func init() { |
| 43 | + sv := cmdCreateGalleryImage.Flags().StringVar |
| 44 | + |
| 45 | + sv(&galleryImageName, "gallery-image-name", "", "gallery image name") |
| 46 | + sv(&galleryName, "gallery-name", "kola", "gallery name") |
| 47 | + sv(&blobUrl, "image-blob", "", "source blob url") |
| 48 | + sv(&sourceImageId, "source-image", "", "source Azure disk uri") |
| 49 | + sv(&resourceGroup, "resource-group", "kola", "resource group name") |
| 50 | + |
| 51 | + Azure.AddCommand(cmdCreateGalleryImage) |
| 52 | +} |
| 53 | + |
| 54 | +func runCreateGalleryImage(cmd *cobra.Command, args []string) error { |
| 55 | + if sourceImageId != "" && blobUrl != "" { |
| 56 | + fmt.Fprintf(os.Stderr, "cannot use --image-blob with --source-image\n") |
| 57 | + os.Exit(1) |
| 58 | + } |
| 59 | + |
| 60 | + if err := api.SetupClients(); err != nil { |
| 61 | + fmt.Fprintf(os.Stderr, "setting up clients: %v\n", err) |
| 62 | + os.Exit(1) |
| 63 | + } |
| 64 | + |
| 65 | + if sourceImageId == "" { |
| 66 | + img, err := api.CreateImage(galleryImageName, resourceGroup, blobUrl) |
| 67 | + if err != nil { |
| 68 | + fmt.Fprintf(os.Stderr, "Couldn't create Azure image: %v\n", err) |
| 69 | + os.Exit(1) |
| 70 | + } |
| 71 | + if img.ID == nil { |
| 72 | + fmt.Fprintf(os.Stderr, "received nil image\n") |
| 73 | + os.Exit(1) |
| 74 | + } |
| 75 | + sourceImageId = *img.ID |
| 76 | + } |
| 77 | + |
| 78 | + galleryImage, err := api.CreateGalleryImage(galleryImageName, galleryName, resourceGroup, sourceImageId) |
| 79 | + if err != nil { |
| 80 | + fmt.Fprintf(os.Stderr, "Couldn't create Azure Shared Image Gallery image: %v\n", err) |
| 81 | + os.Exit(1) |
| 82 | + } |
| 83 | + if galleryImage.ID == nil { |
| 84 | + fmt.Fprintf(os.Stderr, "received nil gallery image\n") |
| 85 | + os.Exit(1) |
| 86 | + } |
| 87 | + err = json.NewEncoder(os.Stdout).Encode(&struct { |
| 88 | + ID *string |
| 89 | + Location *string |
| 90 | + }{ |
| 91 | + ID: galleryImage.ID, |
| 92 | + Location: galleryImage.Location, |
| 93 | + }) |
| 94 | + if err != nil { |
| 95 | + fmt.Fprintf(os.Stderr, "Couldn't encode result: %v\n", err) |
| 96 | + os.Exit(1) |
| 97 | + } |
| 98 | + return nil |
| 99 | +} |
0 commit comments