mirror of
https://github.com/docker/compose.git
synced 2025-07-23 13:45:00 +02:00
Add progress on volume creation
Signed-off-by: Guillaume Tardif <guillaume.tardif@docker.com>
This commit is contained in:
parent
96d785a5bd
commit
b96a6d1086
@ -20,6 +20,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/docker/compose-cli/progress"
|
||||||
|
|
||||||
"github.com/Azure/go-autorest/autorest/to"
|
"github.com/Azure/go-autorest/autorest/to"
|
||||||
|
|
||||||
"github.com/docker/compose-cli/aci/login"
|
"github.com/docker/compose-cli/aci/login"
|
||||||
@ -90,12 +92,16 @@ func (cs *aciVolumeService) Create(ctx context.Context, options interface{}) (vo
|
|||||||
if !ok {
|
if !ok {
|
||||||
return volumes.Volume{}, errors.New("Could not read azure LoginParams struct from generic parameter")
|
return volumes.Volume{}, errors.New("Could not read azure LoginParams struct from generic parameter")
|
||||||
}
|
}
|
||||||
|
w := progress.ContextWriter(ctx)
|
||||||
|
w.Event(event(opts.Account, progress.Working, "Validating"))
|
||||||
accountClient, err := login.NewStorageAccountsClient(cs.aciContext.SubscriptionID)
|
accountClient, err := login.NewStorageAccountsClient(cs.aciContext.SubscriptionID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return volumes.Volume{}, err
|
return volumes.Volume{}, err
|
||||||
}
|
}
|
||||||
account, err := accountClient.GetProperties(ctx, cs.aciContext.ResourceGroup, opts.Account, "")
|
account, err := accountClient.GetProperties(ctx, cs.aciContext.ResourceGroup, opts.Account, "")
|
||||||
if err != nil {
|
if err == nil {
|
||||||
|
w.Event(event(opts.Account, progress.Done, "Use existing"))
|
||||||
|
} else {
|
||||||
if account.StatusCode != 404 {
|
if account.StatusCode != 404 {
|
||||||
return volumes.Volume{}, err
|
return volumes.Volume{}, err
|
||||||
}
|
}
|
||||||
@ -111,20 +117,27 @@ func (cs *aciVolumeService) Create(ctx context.Context, options interface{}) (vo
|
|||||||
return volumes.Volume{}, errors.New("error: " + *result.Message)
|
return volumes.Volume{}, errors.New("error: " + *result.Message)
|
||||||
}
|
}
|
||||||
parameters := defaultStorageAccountParams(cs.aciContext)
|
parameters := defaultStorageAccountParams(cs.aciContext)
|
||||||
// TODO progress account creation
|
|
||||||
|
w.Event(event(opts.Account, progress.Working, "Creating"))
|
||||||
|
|
||||||
future, err := accountClient.Create(ctx, cs.aciContext.ResourceGroup, opts.Account, parameters)
|
future, err := accountClient.Create(ctx, cs.aciContext.ResourceGroup, opts.Account, parameters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
w.Event(errorEvent(opts.Account))
|
||||||
return volumes.Volume{}, err
|
return volumes.Volume{}, err
|
||||||
}
|
}
|
||||||
err = future.WaitForCompletionRef(ctx, accountClient.Client)
|
err = future.WaitForCompletionRef(ctx, accountClient.Client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
w.Event(errorEvent(opts.Account))
|
||||||
return volumes.Volume{}, err
|
return volumes.Volume{}, err
|
||||||
}
|
}
|
||||||
account, err = future.Result(accountClient)
|
account, err = future.Result(accountClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
w.Event(errorEvent(opts.Account))
|
||||||
return volumes.Volume{}, err
|
return volumes.Volume{}, err
|
||||||
}
|
}
|
||||||
|
w.Event(event(opts.Account, progress.Done, "Created"))
|
||||||
}
|
}
|
||||||
|
w.Event(event(opts.Fileshare, progress.Working, "Creating"))
|
||||||
fileShareClient, err := login.NewFileShareClient(cs.aciContext.SubscriptionID)
|
fileShareClient, err := login.NewFileShareClient(cs.aciContext.SubscriptionID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return volumes.Volume{}, err
|
return volumes.Volume{}, err
|
||||||
@ -132,26 +145,48 @@ func (cs *aciVolumeService) Create(ctx context.Context, options interface{}) (vo
|
|||||||
|
|
||||||
fileShare, err := fileShareClient.Get(ctx, cs.aciContext.ResourceGroup, *account.Name, opts.Fileshare, "")
|
fileShare, err := fileShareClient.Get(ctx, cs.aciContext.ResourceGroup, *account.Name, opts.Fileshare, "")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
w.Event(errorEvent(opts.Fileshare))
|
||||||
return volumes.Volume{}, errors.Wrapf(errdefs.ErrAlreadyExists, "Azure fileshare %q already exists", opts.Fileshare)
|
return volumes.Volume{}, errors.Wrapf(errdefs.ErrAlreadyExists, "Azure fileshare %q already exists", opts.Fileshare)
|
||||||
}
|
}
|
||||||
if fileShare.StatusCode != 404 {
|
if fileShare.StatusCode != 404 {
|
||||||
|
w.Event(errorEvent(opts.Fileshare))
|
||||||
return volumes.Volume{}, err
|
return volumes.Volume{}, err
|
||||||
}
|
}
|
||||||
|
//TODO tag fileshare
|
||||||
fileShare, err = fileShareClient.Create(ctx, cs.aciContext.ResourceGroup, *account.Name, opts.Fileshare, storage.FileShare{})
|
fileShare, err = fileShareClient.Create(ctx, cs.aciContext.ResourceGroup, *account.Name, opts.Fileshare, storage.FileShare{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
w.Event(errorEvent(opts.Fileshare))
|
||||||
return volumes.Volume{}, err
|
return volumes.Volume{}, err
|
||||||
}
|
}
|
||||||
|
w.Event(event(opts.Fileshare, progress.Done, "Created"))
|
||||||
return toVolume(account, *fileShare.Name), nil
|
return toVolume(account, *fileShare.Name), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (cs *aciVolumeService) Delete(ctx context.Context, options interface{}) error {
|
func (cs *aciVolumeService) Delete(ctx context.Context, options interface{}) error {
|
||||||
opts, ok := options.(VolumeDeleteOptions)
|
opts, ok := options.(VolumeDeleteOptions)
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("Could not read azure VolumeDeleteOptions struct from generic parameter")
|
return errors.New("Could not read azure VolumeDeleteOptions struct from generic parameter")
|
||||||
}
|
}
|
||||||
if opts.DeleteAccount {
|
if opts.DeleteAccount {
|
||||||
//TODO check if there are other shares on this account
|
//TODO check if there are other fileshares on this account
|
||||||
//TODO flag account and only delete ours
|
//TODO flag account and only delete ours?
|
||||||
|
//TODO error when not found
|
||||||
storageAccountsClient, err := login.NewStorageAccountsClient(cs.aciContext.SubscriptionID)
|
storageAccountsClient, err := login.NewStorageAccountsClient(cs.aciContext.SubscriptionID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -17,12 +17,13 @@
|
|||||||
package volume
|
package volume
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
|
|
||||||
"github.com/docker/compose-cli/aci"
|
"github.com/docker/compose-cli/aci"
|
||||||
"github.com/docker/compose-cli/api/client"
|
"github.com/docker/compose-cli/api/client"
|
||||||
|
"github.com/docker/compose-cli/progress"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Command manage volumes
|
// Command manage volumes
|
||||||
@ -47,16 +48,23 @@ func createVolume() *cobra.Command {
|
|||||||
Short: "Creates an Azure file share to use as ACI volume.",
|
Short: "Creates an Azure file share to use as ACI volume.",
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
c, err := client.New(cmd.Context())
|
ctx := cmd.Context()
|
||||||
|
c, err := client.New(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
id, err := c.VolumeService().Create(cmd.Context(), opts)
|
err = progress.Run(ctx, func(ctx context.Context) error {
|
||||||
|
_, err := c.VolumeService().Create(ctx, opts)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println(id)
|
fmt.Printf("volume successfully created\n")
|
||||||
return nil
|
return err
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user