2020-08-17 16:20:02 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker, Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-09-08 10:11:02 +02:00
|
|
|
package aci
|
2020-08-13 15:21:09 +02:00
|
|
|
|
|
|
|
import (
|
2020-08-14 16:24:43 +02:00
|
|
|
"context"
|
2020-08-13 15:21:09 +02:00
|
|
|
"fmt"
|
2020-09-08 14:20:01 +02:00
|
|
|
"github.com/Azure/go-autorest/autorest/to"
|
2020-09-08 10:11:02 +02:00
|
|
|
"github.com/docker/compose-cli/aci/login"
|
2020-09-07 18:23:28 +02:00
|
|
|
|
2020-09-07 14:13:24 +02:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"
|
2020-08-14 16:24:43 +02:00
|
|
|
|
2020-09-07 18:23:28 +02:00
|
|
|
"github.com/docker/compose-cli/api/volumes"
|
|
|
|
"github.com/docker/compose-cli/errdefs"
|
|
|
|
|
2020-08-14 16:24:43 +02:00
|
|
|
"github.com/pkg/errors"
|
2020-08-13 15:21:09 +02:00
|
|
|
|
2020-08-21 17:24:53 +02:00
|
|
|
"github.com/docker/compose-cli/context/store"
|
2020-08-13 15:21:09 +02:00
|
|
|
)
|
|
|
|
|
2020-09-08 10:11:02 +02:00
|
|
|
type aciVolumeService struct {
|
|
|
|
aciContext store.AciContext
|
2020-08-13 15:21:09 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 10:11:02 +02:00
|
|
|
func (cs *aciVolumeService) List(ctx context.Context) ([]volumes.Volume, error) {
|
|
|
|
accountClient, err := login.NewStorageAccountsClient(cs.aciContext.SubscriptionID)
|
2020-09-07 14:13:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-08 10:11:02 +02:00
|
|
|
result, err := accountClient.ListByResourceGroup(ctx, cs.aciContext.ResourceGroup)
|
2020-09-07 18:23:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-07 14:13:24 +02:00
|
|
|
accounts := result.Value
|
2020-09-08 10:11:02 +02:00
|
|
|
fileShareClient, err := login.NewFileShareClient(cs.aciContext.SubscriptionID)
|
2020-09-07 18:23:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fileShares := []volumes.Volume{}
|
2020-09-07 14:13:24 +02:00
|
|
|
for _, account := range *accounts {
|
2020-09-08 10:11:02 +02:00
|
|
|
fileSharePage, err := fileShareClient.List(ctx, cs.aciContext.ResourceGroup, *account.Name, "", "", "")
|
2020-09-07 14:13:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-07 18:23:28 +02:00
|
|
|
|
|
|
|
for fileSharePage.NotDone() {
|
2020-09-07 14:13:24 +02:00
|
|
|
values := fileSharePage.Values()
|
|
|
|
for _, fileShare := range values {
|
2020-09-07 18:23:28 +02:00
|
|
|
fileShares = append(fileShares, toVolume(account, *fileShare.Name))
|
|
|
|
}
|
|
|
|
if err := fileSharePage.NextWithContext(ctx); err != nil {
|
|
|
|
return nil, err
|
2020-09-07 14:13:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fileShares, nil
|
|
|
|
}
|
|
|
|
|
2020-09-08 10:11:02 +02:00
|
|
|
//VolumeCreateOptions options to create a new ACI volume
|
|
|
|
type VolumeCreateOptions struct {
|
|
|
|
Account string
|
|
|
|
Fileshare string
|
2020-09-07 18:23:28 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 10:11:02 +02:00
|
|
|
func (cs *aciVolumeService) Create(ctx context.Context, options interface{}) (volumes.Volume, error) {
|
|
|
|
opts, ok := options.(VolumeCreateOptions)
|
|
|
|
if !ok {
|
|
|
|
return volumes.Volume{}, errors.New("Could not read azure LoginParams struct from generic parameter")
|
|
|
|
}
|
|
|
|
accountClient, err := login.NewStorageAccountsClient(cs.aciContext.SubscriptionID)
|
2020-09-07 14:13:24 +02:00
|
|
|
if err != nil {
|
2020-09-07 18:23:28 +02:00
|
|
|
return volumes.Volume{}, err
|
2020-09-07 14:13:24 +02:00
|
|
|
}
|
2020-09-08 10:11:02 +02:00
|
|
|
account, err := accountClient.GetProperties(ctx, cs.aciContext.ResourceGroup, opts.Account, "")
|
2020-09-07 14:13:24 +02:00
|
|
|
if err != nil {
|
2020-09-07 18:23:28 +02:00
|
|
|
if account.StatusCode != 404 {
|
|
|
|
return volumes.Volume{}, err
|
2020-09-07 14:13:24 +02:00
|
|
|
}
|
2020-09-07 18:23:28 +02:00
|
|
|
//TODO confirm storage account creation
|
2020-09-08 10:11:02 +02:00
|
|
|
parameters := defaultStorageAccountParams(cs.aciContext)
|
2020-09-07 14:13:24 +02:00
|
|
|
// TODO progress account creation
|
2020-09-08 10:11:02 +02:00
|
|
|
future, err := accountClient.Create(ctx, cs.aciContext.ResourceGroup, opts.Account, parameters)
|
2020-09-07 14:13:24 +02:00
|
|
|
if err != nil {
|
2020-09-07 18:23:28 +02:00
|
|
|
return volumes.Volume{}, err
|
2020-09-07 14:13:24 +02:00
|
|
|
}
|
2020-09-08 14:20:01 +02:00
|
|
|
err = future.WaitForCompletionRef(ctx, accountClient.Client)
|
|
|
|
if err != nil {
|
|
|
|
return volumes.Volume{}, err
|
|
|
|
}
|
2020-09-07 14:13:24 +02:00
|
|
|
account, err = future.Result(accountClient)
|
2020-09-07 18:23:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return volumes.Volume{}, err
|
|
|
|
}
|
2020-09-07 14:13:24 +02:00
|
|
|
}
|
2020-09-08 10:11:02 +02:00
|
|
|
fileShareClient, err := login.NewFileShareClient(cs.aciContext.SubscriptionID)
|
2020-09-07 14:13:24 +02:00
|
|
|
if err != nil {
|
2020-09-07 18:23:28 +02:00
|
|
|
return volumes.Volume{}, err
|
2020-09-07 14:13:24 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 10:11:02 +02:00
|
|
|
fileShare, err := fileShareClient.Get(ctx, cs.aciContext.ResourceGroup, *account.Name, opts.Fileshare, "")
|
2020-09-07 18:23:28 +02:00
|
|
|
if err == nil {
|
2020-09-08 10:11:02 +02:00
|
|
|
return volumes.Volume{}, errors.Wrapf(errdefs.ErrAlreadyExists, "Azure fileshare %q already exists", opts.Fileshare)
|
2020-09-07 18:23:28 +02:00
|
|
|
}
|
|
|
|
if fileShare.StatusCode != 404 {
|
|
|
|
return volumes.Volume{}, err
|
|
|
|
}
|
2020-09-08 10:11:02 +02:00
|
|
|
fileShare, err = fileShareClient.Create(ctx, cs.aciContext.ResourceGroup, *account.Name, opts.Fileshare, storage.FileShare{})
|
2020-09-07 18:23:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return volumes.Volume{}, err
|
|
|
|
}
|
|
|
|
return toVolume(account, *fileShare.Name), nil
|
2020-09-07 14:13:24 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 10:11:02 +02:00
|
|
|
func toVolume(account storage.Account, fileShareName string) volumes.Volume {
|
|
|
|
return volumes.Volume{
|
|
|
|
ID: fmt.Sprintf("%s@%s", *account.Name, fileShareName),
|
|
|
|
Name: fileShareName,
|
|
|
|
Description: fmt.Sprintf("Fileshare %s in %s storage account", fileShareName, *account.Name),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:23:28 +02:00
|
|
|
func defaultStorageAccountParams(aciContext store.AciContext) storage.AccountCreateParameters {
|
|
|
|
return storage.AccountCreateParameters{
|
2020-09-08 14:20:01 +02:00
|
|
|
Location: to.StringPtr(aciContext.Location),
|
2020-09-07 18:23:28 +02:00
|
|
|
Sku: &storage.Sku{
|
|
|
|
Name: storage.StandardLRS,
|
|
|
|
},
|
2020-09-08 14:20:01 +02:00
|
|
|
Kind:storage.StorageV2,
|
|
|
|
AccountPropertiesCreateParameters: &storage.AccountPropertiesCreateParameters{},
|
2020-09-07 18:23:28 +02:00
|
|
|
}
|
|
|
|
}
|