github.com/openshift/installer@v1.4.17/pkg/infrastructure/azure/compute.go (about) 1 package azure 2 3 import ( 4 "context" 5 "fmt" 6 "time" 7 8 "github.com/Azure/azure-sdk-for-go/sdk/azcore" 9 "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" 10 "github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud" 11 "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" 12 "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" 13 "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" 14 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v4" 15 "github.com/sirupsen/logrus" 16 ) 17 18 // CreateImageGalleryInput contains the input parameters for creating a image 19 // gallery. 20 type CreateImageGalleryInput struct { 21 SubscriptionID string 22 ResourceGroupName string 23 GalleryName string 24 Region string 25 Tags map[string]*string 26 TokenCredential azcore.TokenCredential 27 CloudConfiguration cloud.Configuration 28 } 29 30 // CreateImageGalleryOutput contains the return values after creating a image 31 // gallery. 32 type CreateImageGalleryOutput struct { 33 ComputeClientFactory *armcompute.ClientFactory 34 Gallery *armcompute.Gallery 35 } 36 37 // CreateImageGallery creates a image gallery. 38 func CreateImageGallery(ctx context.Context, in *CreateImageGalleryInput) (*CreateImageGalleryOutput, error) { 39 logrus.Debugf("Creating image gallery: %s", in.GalleryName) 40 computeClientFactory, err := armcompute.NewClientFactory( 41 in.SubscriptionID, 42 in.TokenCredential, 43 &arm.ClientOptions{ 44 ClientOptions: policy.ClientOptions{ 45 Cloud: in.CloudConfiguration, 46 }, 47 }, 48 ) 49 if err != nil { 50 return nil, fmt.Errorf("failed to get compute client factory: %w", err) 51 } 52 53 galleriesClient := computeClientFactory.NewGalleriesClient() 54 55 galleriesPoller, err := galleriesClient.BeginCreateOrUpdate( 56 ctx, 57 in.ResourceGroupName, 58 in.GalleryName, 59 armcompute.Gallery{ 60 Location: to.Ptr(in.Region), 61 Properties: &armcompute.GalleryProperties{ 62 // Fill this in 63 }, 64 Tags: in.Tags, 65 }, 66 nil, 67 ) 68 if err != nil { 69 return nil, fmt.Errorf("failed to create gallery %s: %w", in.GalleryName, err) 70 } 71 72 galleriesPollDone, err := galleriesPoller.PollUntilDone(ctx, nil) 73 if err != nil { 74 return nil, fmt.Errorf("failed to finish creating gallery %s: %w", in.GalleryName, err) 75 } 76 77 logrus.Debugf("Image gallery %s successfully created", in.GalleryName) 78 return &CreateImageGalleryOutput{ 79 ComputeClientFactory: computeClientFactory, 80 Gallery: to.Ptr(galleriesPollDone.Gallery), 81 }, nil 82 } 83 84 // CreateGalleryImageInput contains the input parameters for creating a gallery 85 // image. 86 type CreateGalleryImageInput struct { 87 ResourceGroupName string 88 GalleryName string 89 GalleryImageName string 90 Region string 91 Publisher string 92 Offer string 93 SKU string 94 Tags map[string]*string 95 TokenCredential azcore.TokenCredential 96 CloudConfiguration cloud.Configuration 97 Architecture armcompute.Architecture 98 OSType armcompute.OperatingSystemTypes 99 OSState armcompute.OperatingSystemStateTypes 100 HyperVGeneration armcompute.HyperVGeneration 101 ComputeClientFactory *armcompute.ClientFactory 102 } 103 104 // CreateGalleryImageOutput contains the return values after creating a gallery 105 // image. 106 type CreateGalleryImageOutput struct { 107 GalleryImage *armcompute.GalleryImage 108 } 109 110 // CreateGalleryImage creates a gallery image. 111 func CreateGalleryImage(ctx context.Context, in *CreateGalleryImageInput) (*CreateGalleryImageOutput, error) { 112 logrus.Debugf("Creating gallery image: %s", in.GalleryImageName) 113 114 galleryImagesClient := in.ComputeClientFactory.NewGalleryImagesClient() 115 galleryImagesPoller, err := galleryImagesClient.BeginCreateOrUpdate( 116 ctx, 117 in.ResourceGroupName, 118 in.GalleryName, 119 in.GalleryImageName, 120 armcompute.GalleryImage{ 121 Location: to.Ptr(in.Region), 122 Properties: &armcompute.GalleryImageProperties{ 123 Architecture: to.Ptr(in.Architecture), 124 OSType: to.Ptr(in.OSType), 125 OSState: to.Ptr(in.OSState), 126 HyperVGeneration: to.Ptr(in.HyperVGeneration), 127 Identifier: &armcompute.GalleryImageIdentifier{ 128 Publisher: to.Ptr(in.Publisher), 129 Offer: to.Ptr(in.Offer), 130 SKU: to.Ptr(in.SKU), 131 }, 132 }, 133 Tags: in.Tags, 134 }, 135 nil, 136 ) 137 if err != nil { 138 return nil, fmt.Errorf("failed to create gallery image %s: %w", in.GalleryImageName, err) 139 } 140 141 galleryImagesPollDone, err := galleryImagesPoller.PollUntilDone(ctx, nil) 142 if err != nil { 143 return nil, fmt.Errorf("failed to finish creating gallery image %s: %w", in.GalleryImageName, err) 144 } 145 galleryImage := galleryImagesPollDone.GalleryImage 146 logrus.Infof("GalleryImage.ID=%s", *galleryImage.ID) 147 148 logrus.Debugf("Gallery image %s successfully created", in.GalleryImageName) 149 return &CreateGalleryImageOutput{ 150 GalleryImage: to.Ptr(galleryImage), 151 }, nil 152 } 153 154 // CreateGalleryImageVersionInput contains the input parameters for creating a 155 // gallery image version. 156 type CreateGalleryImageVersionInput struct { 157 ResourceGroupName string 158 GalleryName string 159 GalleryImageName string 160 GalleryImageVersionName string 161 Region string 162 StorageAccountID string 163 BlobURL string 164 Tags map[string]*string 165 RegionalReplicaCount int32 166 ComputeClientFactory *armcompute.ClientFactory 167 } 168 169 // CreateGalleryImageVersionOutput contains the return values after create a 170 // gallery image version. 171 type CreateGalleryImageVersionOutput struct { 172 GalleryImageVersion *armcompute.GalleryImageVersion 173 } 174 175 // CreateGalleryImageVersion creates a gallery image version. 176 func CreateGalleryImageVersion(ctx context.Context, in *CreateGalleryImageVersionInput) (*CreateGalleryImageVersionOutput, error) { 177 logrus.Debugf("Creating gallery image version: %s", in.GalleryImageVersionName) 178 galleryImageVersionsClient := in.ComputeClientFactory.NewGalleryImageVersionsClient() 179 180 galleryImageVersionProperties := armcompute.GalleryImageVersionProperties{ 181 StorageProfile: &armcompute.GalleryImageVersionStorageProfile{ 182 OSDiskImage: &armcompute.GalleryOSDiskImage{ 183 Source: &armcompute.GalleryDiskImageSource{ 184 StorageAccountID: to.Ptr(in.StorageAccountID), 185 URI: to.Ptr(in.BlobURL), 186 }, 187 }, 188 }, 189 PublishingProfile: &armcompute.GalleryImageVersionPublishingProfile{ 190 TargetRegions: []*armcompute.TargetRegion{ 191 { 192 Name: to.Ptr(in.Region), 193 RegionalReplicaCount: to.Ptr(in.RegionalReplicaCount), 194 }, 195 }, 196 }, 197 } 198 199 galleryImageVersionPoller, err := galleryImageVersionsClient.BeginCreateOrUpdate( 200 ctx, 201 in.ResourceGroupName, 202 in.GalleryName, 203 in.GalleryImageName, 204 in.GalleryImageVersionName, 205 armcompute.GalleryImageVersion{ 206 Location: to.Ptr(in.Region), 207 Properties: &galleryImageVersionProperties, 208 }, 209 &armcompute.GalleryImageVersionsClientBeginCreateOrUpdateOptions{}, 210 ) 211 if err != nil { 212 return nil, fmt.Errorf("failed to create gallery image version %s: %w", in.GalleryImageVersionName, err) 213 } 214 215 galleryImageVersionPollDone, err := galleryImageVersionPoller.PollUntilDone(ctx, 216 &runtime.PollUntilDoneOptions{ 217 Frequency: time.Minute * 5, 218 }, 219 ) 220 if err != nil { 221 return nil, fmt.Errorf("failed to finish creating gallery image version %s: %w", in.GalleryImageVersionName, err) 222 } 223 224 logrus.Debugf("Gallery image version %s successfully created", in.GalleryImageVersionName) 225 return &CreateGalleryImageVersionOutput{ 226 GalleryImageVersion: to.Ptr(galleryImageVersionPollDone.GalleryImageVersion), 227 }, nil 228 }