Skip to content

Commit e3479c1

Browse files
committed
storage account
1 parent f0a97f4 commit e3479c1

File tree

12 files changed

+70
-107
lines changed

12 files changed

+70
-107
lines changed

src/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerinstance/armcontainerinstance/v2 v2.4.0
2222
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources/v2 v2.1.0
2323
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.8.1
24+
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.2
2425
github.com/DefangLabs/secret-detector v0.0.0-20250108223530-c2b44d4c1f8f
2526
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
2627
github.com/aws/aws-sdk-go-v2 v1.32.6

src/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources/v2
5252
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources/v2 v2.1.0/go.mod h1:M3QD7IyKZBaC4uAKjitTOSOXdcPC6JS1A9oOW3hYjbQ=
5353
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.8.1 h1:/Zt+cDPnpC3OVDm/JKLOs7M2DKmLRIIp3XIx9pHHiig=
5454
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.8.1/go.mod h1:Ng3urmn6dYe8gnbCMoHHVl5APYz2txho3koEkV2o2HA=
55+
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.2 h1:FwladfywkNirM+FZYLBR2kBz5C8Tg0fw5w5Y7meRXWI=
56+
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.2/go.mod h1:vv5Ad0RrIoT1lJFdWBZwt4mB1+j+V8DUroixmKDTCdk=
5557
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
5658
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
5759
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1 h1:WJTmL004Abzc5wDB5VtZG2PJk5ndYDgVacGqfirKxjM=

src/pkg/cli/client/byoc/azure/byoc.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package azure
22

33
import (
44
"context"
5+
"net/url"
56

7+
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/sas"
68
"github.com/DefangLabs/defang/src/pkg/cli/client"
79
"github.com/DefangLabs/defang/src/pkg/cli/client/byoc"
810
"github.com/DefangLabs/defang/src/pkg/clouds/azure/aci"
@@ -28,7 +30,11 @@ func NewByocAzure(ctx context.Context, tenantName types.TenantName) *ByocAzure {
2830

2931
// AccountInfo implements client.Provider.
3032
func (b *ByocAzure) AccountInfo(context.Context) (*client.AccountInfo, error) {
31-
panic("unimplemented")
33+
return &client.AccountInfo{
34+
AccountID: b.driver.SubscriptionID,
35+
Provider: client.ProviderAzure,
36+
Region: b.driver.Location.String(),
37+
}, nil
3238
}
3339

3440
// BootstrapCommand implements client.Provider.
@@ -43,7 +49,10 @@ func (b *ByocAzure) BootstrapList(context.Context) ([]string, error) {
4349

4450
// CreateUploadURL implements client.Provider.
4551
func (b *ByocAzure) CreateUploadURL(context.Context, *defangv1.UploadURLRequest) (*defangv1.UploadURLResponse, error) {
46-
panic("unimplemented")
52+
sasQueryParameters := sas.NewQueryParameters(url.Values{}, true)
53+
return &defangv1.UploadURLResponse{
54+
Url: sasQueryParameters.Encode(),
55+
}, nil
4756
}
4857

4958
// Delete implements client.Provider.

src/pkg/clouds/azure/aci/common.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,48 @@ func NewContainerInstance(resourceGroupName string, location azure.Location) *Co
2121
location = azure.Location(os.Getenv("AZURE_LOCATION"))
2222
}
2323
return &ContainerInstance{
24-
Azure: azure.Azure{Location: location},
24+
Azure: azure.Azure{
25+
Location: location,
26+
SubscriptionID: os.Getenv("AZURE_SUBSCRIPTION_ID"),
27+
},
2528
resourceGroupName: resourceGroupName, // TODO: append location?
2629
storageAccount: os.Getenv("DEFANG_CD_BUCKET"),
2730
}
2831
}
29-
func newContainerGroupClient() (*armcontainerinstance.ContainerGroupsClient, error) {
30-
subscriptionID, cred, err := azure.NewCreds()
32+
33+
func (c ContainerInstance) newContainerGroupClient() (*armcontainerinstance.ContainerGroupsClient, error) {
34+
cred, err := c.NewCreds()
3135
if err != nil {
3236
return nil, err
3337
}
3438

35-
clientFactory, err := armcontainerinstance.NewClientFactory(subscriptionID, cred, nil)
39+
clientFactory, err := armcontainerinstance.NewClientFactory(c.SubscriptionID, cred, nil)
3640
if err != nil {
3741
return nil, fmt.Errorf("failed to create container group client: %w", err)
3842
}
3943
return clientFactory.NewContainerGroupsClient(), nil
4044
}
4145

42-
func newContainerClient() (*armcontainerinstance.ContainersClient, error) {
43-
subscriptionID, cred, err := azure.NewCreds()
46+
func (c ContainerInstance) newContainerClient() (*armcontainerinstance.ContainersClient, error) {
47+
cred, err := c.NewCreds()
4448
if err != nil {
4549
return nil, err
4650
}
4751

48-
clientFactory, err := armcontainerinstance.NewClientFactory(subscriptionID, cred, nil)
52+
clientFactory, err := armcontainerinstance.NewClientFactory(c.SubscriptionID, cred, nil)
4953
if err != nil {
5054
return nil, fmt.Errorf("failed to create container client: %w", err)
5155
}
5256
return clientFactory.NewContainersClient(), nil
5357
}
5458

55-
func newResourceGroupClient() (*armresources.ResourceGroupsClient, error) {
56-
subscriptionID, cred, err := azure.NewCreds()
59+
func (c ContainerInstance) newResourceGroupClient() (*armresources.ResourceGroupsClient, error) {
60+
cred, err := c.NewCreds()
5761
if err != nil {
5862
return nil, err
5963
}
6064

61-
resourcesClientFactory, err := armresources.NewClientFactory(subscriptionID, cred, nil)
65+
resourcesClientFactory, err := armresources.NewClientFactory(c.SubscriptionID, cred, nil)
6266
if err != nil {
6367
return nil, fmt.Errorf("failed to create resource group client: %w", err)
6468
}

src/pkg/clouds/azure/aci/common_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
func TestNewClient(t *testing.T) {
1010
t.Setenv("AZURE_SUBSCRIPTION_ID", uuid.NewString())
1111

12-
client, err := newContainerGroupClient()
12+
c := NewContainerInstance(testResourceGroupName, "")
13+
14+
client, err := c.newContainerGroupClient()
1315
if err != nil {
1416
t.Fatalf("Failed to create client: %v", err)
1517
}

src/pkg/clouds/azure/aci/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func safeAppend[T any](slice []T, elems ...T) []T {
1818
}
1919

2020
func (c *ContainerInstance) Run(ctx context.Context, env map[string]string, args ...string) (ContainerGroupName, error) {
21-
containerGroupClient, err := newContainerGroupClient()
21+
containerGroupClient, err := c.newContainerGroupClient()
2222
if err != nil {
2323
return nil, err
2424
}

src/pkg/clouds/azure/aci/setup.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ import (
1212
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources/v2"
1313
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage"
1414
"github.com/DefangLabs/defang/src/pkg"
15-
"github.com/DefangLabs/defang/src/pkg/clouds/azure"
1615
"github.com/DefangLabs/defang/src/pkg/types"
1716
)
1817

1918
const containerGroupPrefix = "defang-cd-"
2019
const storageAccountPrefix = "defangcd"
2120

2221
func (c *ContainerInstance) SetUp(ctx context.Context, containers []types.Container) error {
23-
resourceGroupClient, err := newResourceGroupClient()
22+
resourceGroupClient, err := c.newResourceGroupClient()
2423
if err != nil {
2524
return err
2625
}
@@ -74,20 +73,20 @@ func (c *ContainerInstance) SetUp(ctx context.Context, containers []types.Contai
7473
}
7574
}
7675

77-
bucketName, err := c.getStorageAccount(ctx)
76+
storageAccount, err := c.setUpStorageAccount(ctx)
7877
if err != nil {
7978
return fmt.Errorf("failed to get bucket name: %w", err)
8079
}
8180

82-
if bucketName == "" {
81+
if storageAccount == "" {
8382

8483
}
8584

8685
return nil
8786
}
8887

8988
func (c *ContainerInstance) TearDown(ctx context.Context) error {
90-
resourceGroupClient, err := newResourceGroupClient()
89+
resourceGroupClient, err := c.newResourceGroupClient()
9190
if err != nil {
9291
return err
9392
}
@@ -104,16 +103,11 @@ func (c *ContainerInstance) TearDown(ctx context.Context) error {
104103
return nil
105104
}
106105

107-
func (c *ContainerInstance) getStorageAccount(ctx context.Context) (string, error) {
106+
func (c *ContainerInstance) getStorageAccount(ctx context.Context, accountsClient *armstorage.AccountsClient) (string, error) {
108107
if c.storageAccount != "" {
109108
return c.storageAccount, nil
110109
}
111110

112-
accountsClient, err := azure.NewStorageAccountsClient()
113-
if err != nil {
114-
return "", err
115-
}
116-
117111
for pager := accountsClient.NewListByResourceGroupPager(c.resourceGroupName, nil); pager.More(); {
118112
page, err := pager.NextPage(ctx)
119113
if err != nil {
@@ -129,11 +123,15 @@ func (c *ContainerInstance) getStorageAccount(ctx context.Context) (string, erro
129123
}
130124

131125
func (c *ContainerInstance) setUpStorageAccount(ctx context.Context) (string, error) {
132-
accountsClient, err := azure.NewStorageAccountsClient()
126+
accountsClient, err := c.NewStorageAccountsClient()
133127
if err != nil {
134128
return "", err
135129
}
136130

131+
if x, err := c.getStorageAccount(ctx, accountsClient); err == nil {
132+
return x, nil
133+
}
134+
137135
storageAccount := storageAccountPrefix + pkg.RandomID() // unique storage account name
138136
createResponse, err := accountsClient.BeginCreate(ctx, c.resourceGroupName, storageAccount, armstorage.AccountCreateParameters{
139137
Kind: to.Ptr(armstorage.KindStorageV2),

src/pkg/clouds/azure/aci/setup_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,3 @@ func TestSetup(t *testing.T) {
4040
}
4141
})
4242
}
43-
44-
func TestStorage(t *testing.T) {
45-
c := NewContainerInstance(testResourceGroupName, "westeurope")
46-
47-
storageAccountName, err := c.setUpStorageAccount(context.Background())
48-
if err != nil {
49-
t.Fatalf("Failed to set up storage account: %v", err)
50-
}
51-
52-
foundAccountName, err := c.getStorageAccount(context.Background())
53-
if err != nil {
54-
t.Fatalf("Failed to get storage account name: %v", err)
55-
}
56-
if foundAccountName != storageAccountName {
57-
t.Fatalf("Expected storage account name %s, got %s", storageAccountName, foundAccountName)
58-
}
59-
}

src/pkg/clouds/azure/aci/stop.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
func (c *ContainerInstance) Stop(ctx context.Context, groupName ContainerGroupName) error {
8-
containerGroupClient, err := newContainerGroupClient()
8+
containerGroupClient, err := c.newContainerGroupClient()
99
if err != nil {
1010
return err
1111
}

src/pkg/clouds/azure/aci/tail.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (c *ContainerInstance) Tail(ctx context.Context, groupName ContainerGroupNa
4242
}
4343

4444
func (c *ContainerInstance) QueryLogs(ctx context.Context, groupName ContainerGroupName, containerName string) (string, error) {
45-
client, err := newContainerClient()
45+
client, err := c.newContainerClient()
4646
if err != nil {
4747
return "", err
4848
}
@@ -65,7 +65,7 @@ func (c *ContainerInstance) QueryLogs(ctx context.Context, groupName ContainerGr
6565
}
6666

6767
func (c *ContainerInstance) StreamLogs(ctx context.Context, groupName ContainerGroupName, containerName string) (<-chan logEntry, error) {
68-
client, err := newContainerClient()
68+
client, err := c.newContainerClient()
6969
if err != nil {
7070
return nil, err
7171
}

0 commit comments

Comments
 (0)