mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-12-11 00:44:20 -05:00
0d12e37c96
* Include EXC0014 and fix issues. * Include EXC0012 and fix issues. Signed-off-by: Fabian Kammel <fk@edgeless.systems> Co-authored-by: Otto Bittner <cobittner@posteo.net>
75 lines
1.8 KiB
Go
75 lines
1.8 KiB
Go
/*
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
package azure
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
|
|
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v2"
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
|
)
|
|
|
|
// Client for Azure Gallery API.
|
|
type Client struct {
|
|
log *logger.Logger
|
|
opts Options
|
|
versionClient *armcompute.GalleryImageVersionsClient
|
|
}
|
|
|
|
// NewClient creates a new Client.
|
|
func NewClient(log *logger.Logger, opts Options) *Client {
|
|
log = log.Named("azure-client")
|
|
|
|
cred, err := azidentity.NewDefaultAzureCredential(nil)
|
|
if err != nil {
|
|
log.Fatalf("unable to create default credentials: %v", err)
|
|
}
|
|
|
|
versionClient, err := armcompute.NewGalleryImageVersionsClient(opts.SubscriptionID, cred, &arm.ClientOptions{})
|
|
if err != nil {
|
|
log.Fatalf("unable to create version client: %v", err)
|
|
}
|
|
|
|
return &Client{
|
|
log: log,
|
|
opts: opts,
|
|
versionClient: versionClient,
|
|
}
|
|
}
|
|
|
|
// FetchImages for the given client options.
|
|
func (c *Client) FetchImages(ctx context.Context) (map[string]string, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second*5)
|
|
defer cancel()
|
|
|
|
imageVersionPager := c.versionClient.NewListByGalleryImagePager(
|
|
c.opts.ResourceGroupName,
|
|
c.opts.GalleryName,
|
|
c.opts.ImageDefinition,
|
|
&armcompute.GalleryImageVersionsClientListByGalleryImageOptions{},
|
|
)
|
|
|
|
images := map[string]string{}
|
|
|
|
for imageVersionPager.More() {
|
|
imageVersionPage, err := imageVersionPager.NextPage(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to advance page: %v", err)
|
|
}
|
|
for _, imageVersion := range imageVersionPage.Value {
|
|
imageName := "v" + *imageVersion.Name
|
|
images[imageName] = *imageVersion.ID
|
|
}
|
|
}
|
|
|
|
return images, nil
|
|
}
|