Skip to content

Commit b30a50b

Browse files
committed
query resource groups and storage accounts
1 parent 99f8d41 commit b30a50b

File tree

5 files changed

+92
-360
lines changed

5 files changed

+92
-360
lines changed

pkg/provider/azure.go

Lines changed: 26 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,12 @@ import (
3535
pluralerr "github.com/pluralsh/plural-cli/pkg/utils/errors"
3636
)
3737

38-
// ResourceGroupClient is the subset of functions we need from armresources.VirtualResourceGroupsClient;
39-
// this interface is purely here for allowing unit tests.
40-
type ResourceGroupClient interface {
41-
CreateOrUpdate(ctx context.Context, resourceGroupName string, parameters armresources.ResourceGroup, options *armresources.ResourceGroupsClientCreateOrUpdateOptions) (armresources.ResourceGroupsClientCreateOrUpdateResponse, error)
42-
Get(ctx context.Context, resourceGroupName string, options *armresources.ResourceGroupsClientGetOptions) (armresources.ResourceGroupsClientGetResponse, error)
43-
}
44-
45-
type AccountsClient interface {
46-
GetProperties(ctx context.Context, resourceGroupName string, accountName string, options *armstorage.AccountsClientGetPropertiesOptions) (armstorage.AccountsClientGetPropertiesResponse, error)
47-
BeginCreate(ctx context.Context, resourceGroupName string, accountName string, parameters armstorage.AccountCreateParameters, options *armstorage.AccountsClientBeginCreateOptions) (*runtime.Poller[armstorage.AccountsClientCreateResponse], error)
48-
NewListPager(options *armstorage.AccountsClientListOptions) *runtime.Pager[armstorage.AccountsClientListResponse]
49-
ListKeys(ctx context.Context, resourceGroupName string, accountName string, options *armstorage.AccountsClientListKeysOptions) (armstorage.AccountsClientListKeysResponse, error)
50-
}
51-
52-
type ContainerClient interface {
53-
GetProperties(ctx context.Context, ac azblob.LeaseAccessConditions) (*azblob.ContainerGetPropertiesResponse, error)
54-
Create(ctx context.Context, metadata azblob.Metadata, publicAccessType azblob.PublicAccessType) (*azblob.ContainerCreateResponse, error)
55-
}
56-
57-
type SubscriptionClient interface {
58-
NewListLocationsPager(subscriptionID string, options *armsubscription.SubscriptionsClientListLocationsOptions) *runtime.Pager[armsubscription.SubscriptionsClientListLocationsResponse]
59-
}
60-
61-
type ZonesClient interface {
62-
NewListByResourceGroupPager(resourceGroupName string, options *armdns.ZonesClientListByResourceGroupOptions) *runtime.Pager[armdns.ZonesClientListByResourceGroupResponse]
63-
}
64-
6538
type ClientSet struct {
66-
Subscriptions SubscriptionClient
67-
Groups ResourceGroupClient
68-
Accounts AccountsClient
69-
Containers ContainerClient
70-
Zones ZonesClient
39+
Subscriptions *armsubscription.SubscriptionsClient
40+
Groups *armresources.ResourceGroupsClient
41+
Accounts *armstorage.AccountsClient
42+
Zones *armdns.ZonesClient
43+
Containers *azblob.ContainerURL
7144
}
7245

7346
func GetClientSet(subscriptionId string) (*ClientSet, error) {
@@ -125,48 +98,42 @@ func mkAzure(conf config.Config) (prov *AzureProvider, err error) {
12598
return
12699
}
127100

128-
ctx := context.Background()
129-
locations := []string{}
130-
locationsPager := clients.Subscriptions.NewListLocationsPager(subId, nil)
131-
for locationsPager.More() {
132-
page, err := locationsPager.NextPage(ctx)
133-
if err != nil {
134-
return nil, err
135-
}
101+
locations, err := AzureLocations(context.Background(), clients.Subscriptions, subId)
102+
if err != nil {
103+
return
104+
}
136105

137-
for _, v := range page.Value {
138-
if v != nil {
139-
locations = append(locations, *v.Name)
140-
}
141-
}
106+
groups, err := AzureResourceGroups(context.Background(), clients.Groups)
107+
if err != nil {
108+
return
142109
}
143110

144-
var resp struct {
145-
Cluster string
146-
Storage string
147-
Region string
148-
Resource string
111+
accounts, err := AzureStorageAccounts(context.Background(), clients.Accounts)
112+
if err != nil {
113+
return
149114
}
115+
116+
var resp struct{ Cluster, Region, Resource, Storage string }
150117
var azureSurvey = []*survey.Question{
151118
{
152119
Name: "cluster",
153120
Prompt: &survey.Input{Message: "Enter the name of your cluster:", Default: clusterFlag},
154121
Validate: validCluster,
155122
},
156-
{
157-
Name: "storage",
158-
Prompt: &survey.Input{Message: "Enter the name of the storage account to use for your stage, must be globally unique or already owned by your subscription:"},
159-
Validate: utils.ValidateStorageAccountName,
160-
},
161123
{
162124
Name: "region",
163-
Prompt: &survey.Select{Message: "Enter the region you want to deploy to:", Default: "eastus", Options: locations},
125+
Prompt: &survey.Select{Message: "Select the region you want to deploy to:", Default: "eastus", Options: locations},
164126
Validate: survey.Required,
165127
},
166128
{
167129
Name: "resource",
168-
Prompt: &survey.Input{Message: "Enter the name of the resource group to use:"},
169-
Validate: utils.ValidateResourceGroupName,
130+
Prompt: &survey.Select{Message: "Select the resource group to use:", Options: groups},
131+
Validate: survey.Required,
132+
},
133+
{
134+
Name: "storage",
135+
Prompt: &survey.Select{Message: "Select the storage account to use:", Options: accounts},
136+
Validate: survey.Required,
170137
},
171138
}
172139

@@ -443,7 +410,7 @@ func (az *AzureProvider) upsertStorageContainer(acc armstorage.Account, name str
443410
u, _ := url.Parse(fmt.Sprintf(`https://%s.blob.core.windows.net`, accountName))
444411
service := azblob.NewServiceURL(*u, p)
445412
containerClient := service.NewContainerURL(name)
446-
az.clients.Containers = containerClient
413+
az.clients.Containers = &containerClient
447414
}
448415

449416
_, err = az.clients.Containers.GetProperties(ctx, azblob.LeaseAccessConditions{})

pkg/provider/azure_api.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
6+
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
7+
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage"
8+
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription"
9+
)
10+
11+
func AzureLocations(ctx context.Context, client *armsubscription.SubscriptionsClient, subscriptionID string) ([]string, error) {
12+
locations := make([]string, 0)
13+
pager := client.NewListLocationsPager(subscriptionID, nil)
14+
for pager.More() {
15+
page, err := pager.NextPage(ctx)
16+
if err != nil {
17+
return nil, err
18+
}
19+
20+
for _, v := range page.Value {
21+
if v != nil {
22+
locations = append(locations, *v.Name)
23+
}
24+
}
25+
}
26+
27+
return locations, nil
28+
}
29+
30+
func AzureResourceGroups(ctx context.Context, client *armresources.ResourceGroupsClient) ([]string, error) {
31+
groups := make([]string, 0)
32+
pager := client.NewListPager(nil)
33+
for pager.More() {
34+
page, err := pager.NextPage(ctx)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
for _, v := range page.Value {
40+
if v != nil {
41+
groups = append(groups, *v.Name)
42+
}
43+
}
44+
}
45+
46+
return groups, nil
47+
}
48+
49+
func AzureStorageAccounts(ctx context.Context, client *armstorage.AccountsClient) ([]string, error) {
50+
accounts := make([]string, 0)
51+
pager := client.NewListPager(nil)
52+
for pager.More() {
53+
page, err := pager.NextPage(ctx)
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
for _, v := range page.Value {
59+
if v != nil {
60+
accounts = append(accounts, *v.Name)
61+
}
62+
}
63+
}
64+
65+
return accounts, nil
66+
}

pkg/provider/azure_test.go

Lines changed: 0 additions & 131 deletions
This file was deleted.

0 commit comments

Comments
 (0)