Replaced map[string] string by existing aciCreateOpts struct for context create

This commit is contained in:
Guillaume Tardif 2020-07-10 16:39:09 +02:00
parent c36b64c10b
commit 97d408d25d
5 changed files with 40 additions and 41 deletions

View File

@ -23,6 +23,8 @@ import (
"strconv"
"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/go-autorest/autorest/to"
"github.com/compose-spec/compose-go/cli"
@ -359,7 +361,8 @@ func (cs *aciCloudService) Logout(ctx context.Context) error {
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()
return contextHelper.createContextData(ctx, params)
createOpts := params.(acicontext.AciCreateOpts)
return contextHelper.createContextData(ctx, createOpts)
}

View File

@ -21,6 +21,8 @@ import (
"fmt"
"os"
acicontext "github.com/docker/api/cli/cmd/context"
"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/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
if opts["aciSubscriptionID"] != "" {
subscriptionID = opts["aciSubscriptionID"]
if opts.SubscriptionID != "" {
subscriptionID = opts.SubscriptionID
} else {
subs, err := helper.resourceGroupHelper.GetSubscriptionIDs(ctx)
if err != nil {
@ -61,10 +63,10 @@ func (helper contextCreateACIHelper) createContextData(ctx context.Context, opts
var group resources.Group
var err error
if opts["aciResourceGroup"] != "" {
group, err = helper.resourceGroupHelper.GetGroup(ctx, subscriptionID, opts["aciResourceGroup"])
if opts.ResourceGroup != "" {
group, err = helper.resourceGroupHelper.GetGroup(ctx, subscriptionID, opts.ResourceGroup)
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 {
groups, err := helper.resourceGroupHelper.ListGroups(ctx, subscriptionID)
@ -80,8 +82,8 @@ func (helper contextCreateACIHelper) createContextData(ctx context.Context, opts
location := *group.Location
description := fmt.Sprintf("%s@%s", *group.Name, location)
if opts["description"] != "" {
description = fmt.Sprintf("%s (%s)", opts["description"], description)
if opts.Description != "" {
description = fmt.Sprintf("%s (%s)", opts.Description, description)
}
return store.AciContext{
@ -108,7 +110,7 @@ func (helper contextCreateACIHelper) createGroup(ctx context.Context, subscripti
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"}
for _, g := range groups {
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 {
return helper.createGroup(ctx, subscriptionID, opts["aciLocation"])
return helper.createGroup(ctx, subscriptionID, opts.Location)
}
return groups[group-1], nil

View File

@ -20,6 +20,8 @@ import (
"context"
"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/preview/preview/subscription/mgmt/subscription"
"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 {
return map[string]string{
"aciSubscriptionID": subscriptionID,
"aciResourceGroup": resourceGroupName,
"aciLocation": "eastus",
func options(subscriptionID string, resourceGroupName string) acicontext.AciCreateOpts {
return acicontext.AciCreateOpts{
SubscriptionID: subscriptionID,
ResourceGroup: resourceGroupName,
Location: "eastus",
}
}

View File

@ -27,15 +27,16 @@ import (
"github.com/docker/api/errdefs"
)
type aciCreateOpts struct {
description string
location string
subscriptionID string
resourceGroup string
// AciCreateOpts options for creating ACI context
type AciCreateOpts struct {
Description string
Location string
SubscriptionID string
ResourceGroup string
}
func createAciCommand() *cobra.Command {
var opts aciCreateOpts
var opts AciCreateOpts
cmd := &cobra.Command{
Use: "aci CONTEXT [flags]",
Short: "Create a context for Azure Container Instances",
@ -45,15 +46,15 @@ func createAciCommand() *cobra.Command {
},
}
addDescriptionFlag(cmd, &opts.description)
cmd.Flags().StringVar(&opts.location, "location", "eastus", "Location")
cmd.Flags().StringVar(&opts.subscriptionID, "subscription-id", "", "Location")
cmd.Flags().StringVar(&opts.resourceGroup, "resource-group", "", "Resource group")
addDescriptionFlag(cmd, &opts.Description)
cmd.Flags().StringVar(&opts.Location, "location", "eastus", "Location")
cmd.Flags().StringVar(&opts.SubscriptionID, "subscription-id", "", "Location")
cmd.Flags().StringVar(&opts.ResourceGroup, "resource-group", "", "Resource group")
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) {
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)
if err != nil {
return nil, "", errors.Wrap(err, "cannot connect to ACI backend")
}
return cs.CreateContextData(ctx, convertAciOpts(opts))
}
func convertAciOpts(opts aciCreateOpts) map[string]string {
return map[string]string{
"aciSubscriptionID": opts.subscriptionID,
"aciResourceGroup": opts.resourceGroup,
"aciLocation": opts.location,
"description": opts.description,
}
return cs.CreateContextData(ctx, opts)
}

View File

@ -29,7 +29,7 @@ type Service interface {
// Logout logout from cloud provider
Logout(ctx context.Context) error
// 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
@ -49,6 +49,6 @@ func (cs notImplementedCloudService) Login(ctx context.Context, params map[strin
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
}