mirror of
https://github.com/docker/compose.git
synced 2025-07-01 10:54:29 +02:00
Replaced map[string] string by existing aciCreateOpts struct for context create
This commit is contained in:
parent
c36b64c10b
commit
97d408d25d
@ -23,6 +23,8 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
acicontext "github.com/docker/api/cli/cmd/context"
|
||||||
|
|
||||||
"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
|
"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
|
||||||
"github.com/Azure/go-autorest/autorest/to"
|
"github.com/Azure/go-autorest/autorest/to"
|
||||||
"github.com/compose-spec/compose-go/cli"
|
"github.com/compose-spec/compose-go/cli"
|
||||||
@ -359,7 +361,8 @@ func (cs *aciCloudService) Logout(ctx context.Context) error {
|
|||||||
return cs.loginService.Logout(ctx)
|
return cs.loginService.Logout(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *aciCloudService) CreateContextData(ctx context.Context, params map[string]string) (interface{}, string, error) {
|
func (cs *aciCloudService) CreateContextData(ctx context.Context, params interface{}) (interface{}, string, error) {
|
||||||
contextHelper := newContextCreateHelper()
|
contextHelper := newContextCreateHelper()
|
||||||
return contextHelper.createContextData(ctx, params)
|
createOpts := params.(acicontext.AciCreateOpts)
|
||||||
|
return contextHelper.createContextData(ctx, createOpts)
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
acicontext "github.com/docker/api/cli/cmd/context"
|
||||||
|
|
||||||
"github.com/AlecAivazis/survey/v2"
|
"github.com/AlecAivazis/survey/v2"
|
||||||
"github.com/Azure/azure-sdk-for-go/profiles/preview/preview/subscription/mgmt/subscription"
|
"github.com/Azure/azure-sdk-for-go/profiles/preview/preview/subscription/mgmt/subscription"
|
||||||
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
|
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
|
||||||
@ -43,10 +45,10 @@ func newContextCreateHelper() contextCreateACIHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (helper contextCreateACIHelper) createContextData(ctx context.Context, opts map[string]string) (interface{}, string, error) {
|
func (helper contextCreateACIHelper) createContextData(ctx context.Context, opts acicontext.AciCreateOpts) (interface{}, string, error) {
|
||||||
var subscriptionID string
|
var subscriptionID string
|
||||||
if opts["aciSubscriptionID"] != "" {
|
if opts.SubscriptionID != "" {
|
||||||
subscriptionID = opts["aciSubscriptionID"]
|
subscriptionID = opts.SubscriptionID
|
||||||
} else {
|
} else {
|
||||||
subs, err := helper.resourceGroupHelper.GetSubscriptionIDs(ctx)
|
subs, err := helper.resourceGroupHelper.GetSubscriptionIDs(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -61,10 +63,10 @@ func (helper contextCreateACIHelper) createContextData(ctx context.Context, opts
|
|||||||
var group resources.Group
|
var group resources.Group
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if opts["aciResourceGroup"] != "" {
|
if opts.ResourceGroup != "" {
|
||||||
group, err = helper.resourceGroupHelper.GetGroup(ctx, subscriptionID, opts["aciResourceGroup"])
|
group, err = helper.resourceGroupHelper.GetGroup(ctx, subscriptionID, opts.ResourceGroup)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", errors.Wrapf(err, "Could not find resource group %q", opts["aciResourceGroup"])
|
return nil, "", errors.Wrapf(err, "Could not find resource group %q", opts.ResourceGroup)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
groups, err := helper.resourceGroupHelper.ListGroups(ctx, subscriptionID)
|
groups, err := helper.resourceGroupHelper.ListGroups(ctx, subscriptionID)
|
||||||
@ -80,8 +82,8 @@ func (helper contextCreateACIHelper) createContextData(ctx context.Context, opts
|
|||||||
location := *group.Location
|
location := *group.Location
|
||||||
|
|
||||||
description := fmt.Sprintf("%s@%s", *group.Name, location)
|
description := fmt.Sprintf("%s@%s", *group.Name, location)
|
||||||
if opts["description"] != "" {
|
if opts.Description != "" {
|
||||||
description = fmt.Sprintf("%s (%s)", opts["description"], description)
|
description = fmt.Sprintf("%s (%s)", opts.Description, description)
|
||||||
}
|
}
|
||||||
|
|
||||||
return store.AciContext{
|
return store.AciContext{
|
||||||
@ -108,7 +110,7 @@ func (helper contextCreateACIHelper) createGroup(ctx context.Context, subscripti
|
|||||||
return g, nil
|
return g, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (helper contextCreateACIHelper) chooseGroup(ctx context.Context, subscriptionID string, opts map[string]string, groups []resources.Group) (resources.Group, error) {
|
func (helper contextCreateACIHelper) chooseGroup(ctx context.Context, subscriptionID string, opts acicontext.AciCreateOpts, groups []resources.Group) (resources.Group, error) {
|
||||||
groupNames := []string{"create a new resource group"}
|
groupNames := []string{"create a new resource group"}
|
||||||
for _, g := range groups {
|
for _, g := range groups {
|
||||||
groupNames = append(groupNames, fmt.Sprintf("%s (%s)", *g.Name, *g.Location))
|
groupNames = append(groupNames, fmt.Sprintf("%s (%s)", *g.Name, *g.Location))
|
||||||
@ -124,7 +126,7 @@ func (helper contextCreateACIHelper) chooseGroup(ctx context.Context, subscripti
|
|||||||
}
|
}
|
||||||
|
|
||||||
if group == 0 {
|
if group == 0 {
|
||||||
return helper.createGroup(ctx, subscriptionID, opts["aciLocation"])
|
return helper.createGroup(ctx, subscriptionID, opts.Location)
|
||||||
}
|
}
|
||||||
|
|
||||||
return groups[group-1], nil
|
return groups[group-1], nil
|
||||||
|
@ -20,6 +20,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
acicontext "github.com/docker/api/cli/cmd/context"
|
||||||
|
|
||||||
"github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/resources/mgmt/resources"
|
"github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/resources/mgmt/resources"
|
||||||
"github.com/Azure/azure-sdk-for-go/profiles/preview/preview/subscription/mgmt/subscription"
|
"github.com/Azure/azure-sdk-for-go/profiles/preview/preview/subscription/mgmt/subscription"
|
||||||
"github.com/Azure/go-autorest/autorest/to"
|
"github.com/Azure/go-autorest/autorest/to"
|
||||||
@ -175,11 +177,11 @@ func aciContext(subscriptionID string, resourceGroupName string, location string
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func options(subscriptionID string, resourceGroupName string) map[string]string {
|
func options(subscriptionID string, resourceGroupName string) acicontext.AciCreateOpts {
|
||||||
return map[string]string{
|
return acicontext.AciCreateOpts{
|
||||||
"aciSubscriptionID": subscriptionID,
|
SubscriptionID: subscriptionID,
|
||||||
"aciResourceGroup": resourceGroupName,
|
ResourceGroup: resourceGroupName,
|
||||||
"aciLocation": "eastus",
|
Location: "eastus",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,15 +27,16 @@ import (
|
|||||||
"github.com/docker/api/errdefs"
|
"github.com/docker/api/errdefs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type aciCreateOpts struct {
|
// AciCreateOpts options for creating ACI context
|
||||||
description string
|
type AciCreateOpts struct {
|
||||||
location string
|
Description string
|
||||||
subscriptionID string
|
Location string
|
||||||
resourceGroup string
|
SubscriptionID string
|
||||||
|
ResourceGroup string
|
||||||
}
|
}
|
||||||
|
|
||||||
func createAciCommand() *cobra.Command {
|
func createAciCommand() *cobra.Command {
|
||||||
var opts aciCreateOpts
|
var opts AciCreateOpts
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "aci CONTEXT [flags]",
|
Use: "aci CONTEXT [flags]",
|
||||||
Short: "Create a context for Azure Container Instances",
|
Short: "Create a context for Azure Container Instances",
|
||||||
@ -45,15 +46,15 @@ func createAciCommand() *cobra.Command {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
addDescriptionFlag(cmd, &opts.description)
|
addDescriptionFlag(cmd, &opts.Description)
|
||||||
cmd.Flags().StringVar(&opts.location, "location", "eastus", "Location")
|
cmd.Flags().StringVar(&opts.Location, "location", "eastus", "Location")
|
||||||
cmd.Flags().StringVar(&opts.subscriptionID, "subscription-id", "", "Location")
|
cmd.Flags().StringVar(&opts.SubscriptionID, "subscription-id", "", "Location")
|
||||||
cmd.Flags().StringVar(&opts.resourceGroup, "resource-group", "", "Resource group")
|
cmd.Flags().StringVar(&opts.ResourceGroup, "resource-group", "", "Resource group")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func runCreateAci(ctx context.Context, contextName string, opts aciCreateOpts) error {
|
func runCreateAci(ctx context.Context, contextName string, opts AciCreateOpts) error {
|
||||||
if contextExists(ctx, contextName) {
|
if contextExists(ctx, contextName) {
|
||||||
return errors.Wrapf(errdefs.ErrAlreadyExists, "context %s", contextName)
|
return errors.Wrapf(errdefs.ErrAlreadyExists, "context %s", contextName)
|
||||||
}
|
}
|
||||||
@ -65,19 +66,10 @@ func runCreateAci(ctx context.Context, contextName string, opts aciCreateOpts) e
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAciContextData(ctx context.Context, opts aciCreateOpts) (interface{}, string, error) {
|
func getAciContextData(ctx context.Context, opts AciCreateOpts) (interface{}, string, error) {
|
||||||
cs, err := client.GetCloudService(ctx, store.AciContextType)
|
cs, err := client.GetCloudService(ctx, store.AciContextType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", errors.Wrap(err, "cannot connect to ACI backend")
|
return nil, "", errors.Wrap(err, "cannot connect to ACI backend")
|
||||||
}
|
}
|
||||||
return cs.CreateContextData(ctx, convertAciOpts(opts))
|
return cs.CreateContextData(ctx, opts)
|
||||||
}
|
|
||||||
|
|
||||||
func convertAciOpts(opts aciCreateOpts) map[string]string {
|
|
||||||
return map[string]string{
|
|
||||||
"aciSubscriptionID": opts.subscriptionID,
|
|
||||||
"aciResourceGroup": opts.resourceGroup,
|
|
||||||
"aciLocation": opts.location,
|
|
||||||
"description": opts.description,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@ type Service interface {
|
|||||||
// Logout logout from cloud provider
|
// Logout logout from cloud provider
|
||||||
Logout(ctx context.Context) error
|
Logout(ctx context.Context) error
|
||||||
// CreateContextData create data for cloud context
|
// CreateContextData create data for cloud context
|
||||||
CreateContextData(ctx context.Context, params map[string]string) (contextData interface{}, description string, err error)
|
CreateContextData(ctx context.Context, params interface{}) (contextData interface{}, description string, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotImplementedCloudService to use for backend that don't provide cloud services
|
// NotImplementedCloudService to use for backend that don't provide cloud services
|
||||||
@ -49,6 +49,6 @@ func (cs notImplementedCloudService) Login(ctx context.Context, params map[strin
|
|||||||
return errdefs.ErrNotImplemented
|
return errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs notImplementedCloudService) CreateContextData(ctx context.Context, params map[string]string) (interface{}, string, error) {
|
func (cs notImplementedCloudService) CreateContextData(ctx context.Context, params interface{}) (interface{}, string, error) {
|
||||||
return nil, "", errdefs.ErrNotImplemented
|
return nil, "", errdefs.ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user