2020-08-17 16:20:02 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-08-17 16:20:02 +02:00
|
|
|
|
|
|
|
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-10 14:17:49 +02:00
|
|
|
"net/http"
|
2020-09-09 15:49:43 +02:00
|
|
|
"strings"
|
2020-09-08 15:27:56 +02:00
|
|
|
|
2020-09-10 14:17:49 +02:00
|
|
|
"github.com/pkg/errors"
|
2020-09-08 17:19:42 +02:00
|
|
|
|
2020-09-10 14:17:49 +02:00
|
|
|
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"
|
2020-09-08 14:20:01 +02:00
|
|
|
"github.com/Azure/go-autorest/autorest/to"
|
2020-09-08 15:27:56 +02:00
|
|
|
|
2020-09-08 10:11:02 +02:00
|
|
|
"github.com/docker/compose-cli/aci/login"
|
2020-09-07 18:23:28 +02:00
|
|
|
"github.com/docker/compose-cli/api/volumes"
|
2020-08-21 17:24:53 +02:00
|
|
|
"github.com/docker/compose-cli/context/store"
|
2020-09-10 14:17:49 +02:00
|
|
|
"github.com/docker/compose-cli/errdefs"
|
|
|
|
"github.com/docker/compose-cli/progress"
|
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-10 14:17:49 +02:00
|
|
|
// VolumeCreateOptions options to create a new ACI volume
|
2020-09-08 10:11:02 +02:00
|
|
|
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 {
|
2020-09-10 14:17:49 +02:00
|
|
|
return volumes.Volume{}, errors.New("could not read Azure VolumeCreateOptions struct from generic parameter")
|
2020-09-08 10:11:02 +02:00
|
|
|
}
|
2020-09-08 17:19:42 +02:00
|
|
|
w := progress.ContextWriter(ctx)
|
|
|
|
w.Event(event(opts.Account, progress.Working, "Validating"))
|
2020-09-08 10:11:02 +02:00
|
|
|
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-08 17:19:42 +02:00
|
|
|
if err == nil {
|
|
|
|
w.Event(event(opts.Account, progress.Done, "Use existing"))
|
2020-09-24 09:52:17 +02:00
|
|
|
} else if !account.HasHTTPStatus(http.StatusNotFound) {
|
|
|
|
return volumes.Volume{}, err
|
2020-09-08 17:19:42 +02:00
|
|
|
} else {
|
2020-09-08 15:27:56 +02:00
|
|
|
result, err := accountClient.CheckNameAvailability(ctx, storage.AccountCheckNameAvailabilityParameters{
|
|
|
|
Name: to.StringPtr(opts.Account),
|
|
|
|
Type: to.StringPtr("Microsoft.Storage/storageAccounts"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return volumes.Volume{}, err
|
|
|
|
}
|
|
|
|
if !*result.NameAvailable {
|
|
|
|
return volumes.Volume{}, errors.New("error: " + *result.Message)
|
|
|
|
}
|
2020-09-08 10:11:02 +02:00
|
|
|
parameters := defaultStorageAccountParams(cs.aciContext)
|
2020-09-08 17:19:42 +02:00
|
|
|
|
|
|
|
w.Event(event(opts.Account, progress.Working, "Creating"))
|
|
|
|
|
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-08 17:19:42 +02:00
|
|
|
w.Event(errorEvent(opts.Account))
|
2020-09-07 18:23:28 +02:00
|
|
|
return volumes.Volume{}, err
|
2020-09-07 14:13:24 +02:00
|
|
|
}
|
2020-09-10 14:17:49 +02:00
|
|
|
if err := future.WaitForCompletionRef(ctx, accountClient.Client); err != nil {
|
2020-09-08 17:19:42 +02:00
|
|
|
w.Event(errorEvent(opts.Account))
|
2020-09-08 14:20:01 +02:00
|
|
|
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 {
|
2020-09-08 17:19:42 +02:00
|
|
|
w.Event(errorEvent(opts.Account))
|
2020-09-07 18:23:28 +02:00
|
|
|
return volumes.Volume{}, err
|
|
|
|
}
|
2020-09-08 17:19:42 +02:00
|
|
|
w.Event(event(opts.Account, progress.Done, "Created"))
|
2020-09-07 14:13:24 +02:00
|
|
|
}
|
2020-09-08 17:19:42 +02:00
|
|
|
w.Event(event(opts.Fileshare, progress.Working, "Creating"))
|
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 17:19:42 +02:00
|
|
|
w.Event(errorEvent(opts.Fileshare))
|
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
|
|
|
}
|
2020-09-24 09:52:17 +02:00
|
|
|
if !fileShare.HasHTTPStatus(http.StatusNotFound) {
|
2020-09-08 17:19:42 +02:00
|
|
|
w.Event(errorEvent(opts.Fileshare))
|
2020-09-07 18:23:28 +02:00
|
|
|
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 {
|
2020-09-08 17:19:42 +02:00
|
|
|
w.Event(errorEvent(opts.Fileshare))
|
2020-09-07 18:23:28 +02:00
|
|
|
return volumes.Volume{}, err
|
|
|
|
}
|
2020-09-08 17:19:42 +02:00
|
|
|
w.Event(event(opts.Fileshare, progress.Done, "Created"))
|
2020-09-07 18:23:28 +02:00
|
|
|
return toVolume(account, *fileShare.Name), nil
|
2020-09-07 14:13:24 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 17:19:42 +02:00
|
|
|
func event(resource string, status progress.EventStatus, text string) progress.Event {
|
|
|
|
return progress.Event{
|
|
|
|
ID: resource,
|
|
|
|
Status: status,
|
|
|
|
StatusText: text,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func errorEvent(resource string) progress.Event {
|
|
|
|
return progress.Event{
|
|
|
|
ID: resource,
|
|
|
|
Status: progress.Error,
|
|
|
|
StatusText: "Error",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 15:49:43 +02:00
|
|
|
func (cs *aciVolumeService) Delete(ctx context.Context, id string, options interface{}) error {
|
2020-09-10 16:07:22 +02:00
|
|
|
tokens := strings.Split(id, "/")
|
2020-09-09 15:49:43 +02:00
|
|
|
if len(tokens) != 2 {
|
2020-09-11 16:14:14 +02:00
|
|
|
return errors.New("invalid format for volume ID, expected storageaccount/fileshare")
|
2020-09-08 15:27:56 +02:00
|
|
|
}
|
2020-09-09 15:49:43 +02:00
|
|
|
storageAccount := tokens[0]
|
|
|
|
fileshare := tokens[1]
|
2020-09-09 10:26:30 +02:00
|
|
|
|
2020-09-09 15:49:43 +02:00
|
|
|
fileShareClient, err := login.NewFileShareClient(cs.aciContext.SubscriptionID)
|
|
|
|
if err != nil {
|
2020-09-08 15:27:56 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-09-09 15:49:43 +02:00
|
|
|
fileShareItemsPage, err := fileShareClient.List(ctx, cs.aciContext.ResourceGroup, storageAccount, "", "", "")
|
2020-09-08 15:27:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-09 15:49:43 +02:00
|
|
|
fileshares := fileShareItemsPage.Values()
|
|
|
|
if len(fileshares) == 1 && *fileshares[0].Name == fileshare {
|
|
|
|
storageAccountsClient, err := login.NewStorageAccountsClient(cs.aciContext.SubscriptionID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
account, err := storageAccountsClient.GetProperties(ctx, cs.aciContext.ResourceGroup, storageAccount, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
if _, ok := account.Tags[dockerVolumeTag]; ok {
|
|
|
|
result, err := storageAccountsClient.Delete(ctx, cs.aciContext.ResourceGroup, storageAccount)
|
2020-09-10 14:17:49 +02:00
|
|
|
if result.StatusCode == http.StatusNoContent {
|
2020-09-09 15:49:43 +02:00
|
|
|
return errors.Wrapf(errdefs.ErrNotFound, "storage account %s does not exist", storageAccount)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-08 15:27:56 +02:00
|
|
|
|
2020-09-09 15:49:43 +02:00
|
|
|
result, err := fileShareClient.Delete(ctx, cs.aciContext.ResourceGroup, storageAccount, fileshare)
|
2020-09-09 10:26:30 +02:00
|
|
|
if result.StatusCode == 204 {
|
2020-09-10 14:17:49 +02:00
|
|
|
return errors.Wrapf(errdefs.ErrNotFound, "fileshare %q", fileshare)
|
2020-09-09 10:26:30 +02:00
|
|
|
}
|
2020-09-08 15:27:56 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-08 10:11:02 +02:00
|
|
|
func toVolume(account storage.Account, fileShareName string) volumes.Volume {
|
|
|
|
return volumes.Volume{
|
2020-09-14 10:01:59 +02:00
|
|
|
ID: volumeID(*account.Name, fileShareName),
|
2020-09-08 10:11:02 +02:00
|
|
|
Description: fmt.Sprintf("Fileshare %s in %s storage account", fileShareName, *account.Name),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 10:01:59 +02:00
|
|
|
func volumeID(storageAccount string, fileShareName string) string {
|
2020-09-10 16:07:22 +02:00
|
|
|
return fmt.Sprintf("%s/%s", storageAccount, fileShareName)
|
2020-09-09 15:49:43 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 18:23:28 +02:00
|
|
|
func defaultStorageAccountParams(aciContext store.AciContext) storage.AccountCreateParameters {
|
2020-09-08 15:27:56 +02:00
|
|
|
tags := map[string]*string{dockerVolumeTag: to.StringPtr(dockerVolumeTag)}
|
2020-09-07 18:23:28 +02:00
|
|
|
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 15:27:56 +02:00
|
|
|
Tags: tags,
|
2020-09-07 18:23:28 +02:00
|
|
|
}
|
|
|
|
}
|