diff --git a/azure/backend.go b/azure/backend.go index b043e5975..4def23cb6 100644 --- a/azure/backend.go +++ b/azure/backend.go @@ -50,6 +50,19 @@ const ( // ErrNoSuchContainer is returned when the mentioned container does not exist var ErrNoSuchContainer = errors.New("no such container") +// ContextParams options for creating ACI context +type ContextParams struct { + Description string + Location string + SubscriptionID string + ResourceGroup string +} + +// LoginParams azure login options +type LoginParams struct { + TenantID string +} + func init() { backend.Register("aci", "aci", service, getCloudService) } @@ -351,15 +364,17 @@ type aciCloudService struct { loginService login.AzureLoginService } -func (cs *aciCloudService) Login(ctx context.Context, params map[string]string) error { - return cs.loginService.Login(ctx, params[login.TenantIDLoginParam]) +func (cs *aciCloudService) Login(ctx context.Context, params interface{}) error { + createOpts := params.(LoginParams) + return cs.loginService.Login(ctx, createOpts.TenantID) } 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.(ContextParams) + return contextHelper.createContextData(ctx, createOpts) } diff --git a/azure/context.go b/azure/context.go index 4c0b0e31f..41af99706 100644 --- a/azure/context.go +++ b/azure/context.go @@ -43,10 +43,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 ContextParams) (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 +61,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 +80,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 +108,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 ContextParams, 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 +124,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 diff --git a/azure/context_test.go b/azure/context_test.go index 87fd3166a..f61a90745 100644 --- a/azure/context_test.go +++ b/azure/context_test.go @@ -175,11 +175,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) ContextParams { + return ContextParams{ + SubscriptionID: subscriptionID, + ResourceGroup: resourceGroupName, + Location: "eastus", } } diff --git a/azure/login/login.go b/azure/login/login.go index 7117ccb76..59dd3cd87 100644 --- a/azure/login/login.go +++ b/azure/login/login.go @@ -45,9 +45,6 @@ const ( // v1 scope like "https://management.azure.com/.default" for ARM access scopes = "offline_access https://management.azure.com/.default" clientID = "04b07795-8ddb-461a-bbee-02f9e1bf7b46" // Azure CLI client id - - // TenantIDLoginParam - TenantIDLoginParam = "tenantId" ) type ( diff --git a/cli/cmd/context/createaci.go b/cli/cmd/context/createaci.go index 68c690dc1..61e68cd80 100644 --- a/cli/cmd/context/createaci.go +++ b/cli/cmd/context/createaci.go @@ -22,20 +22,14 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/docker/api/azure" "github.com/docker/api/client" "github.com/docker/api/context/store" "github.com/docker/api/errdefs" ) -type aciCreateOpts struct { - description string - location string - subscriptionID string - resourceGroup string -} - func createAciCommand() *cobra.Command { - var opts aciCreateOpts + var opts azure.ContextParams cmd := &cobra.Command{ Use: "aci CONTEXT [flags]", Short: "Create a context for Azure Container Instances", @@ -45,15 +39,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 azure.ContextParams) error { if contextExists(ctx, contextName) { return errors.Wrapf(errdefs.ErrAlreadyExists, "context %s", contextName) } @@ -65,19 +59,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 azure.ContextParams) (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) } diff --git a/cli/cmd/login/azurelogin.go b/cli/cmd/login/azurelogin.go index 8c7e7824f..363b6cbf4 100644 --- a/cli/cmd/login/azurelogin.go +++ b/cli/cmd/login/azurelogin.go @@ -3,26 +3,22 @@ package login import ( "github.com/spf13/cobra" - "github.com/docker/api/azure/login" + "github.com/docker/api/azure" ) -type azureLoginOpts struct { - tenantID string -} - // AzureLoginCommand returns the azure login command func AzureLoginCommand() *cobra.Command { - opts := azureLoginOpts{} + opts := azure.LoginParams{} cmd := &cobra.Command{ Use: "azure", Short: "Log in to azure", Args: cobra.MaximumNArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - return cloudLogin(cmd, "aci", map[string]string{login.TenantIDLoginParam: opts.tenantID}) + return cloudLogin(cmd, "aci", opts) }, } flags := cmd.Flags() - flags.StringVar(&opts.tenantID, "tenant-id", "", "Specify tenant ID to use from your azure account") + flags.StringVar(&opts.TenantID, "tenant-id", "", "Specify tenant ID to use from your azure account") return cmd } diff --git a/cli/cmd/login/login.go b/cli/cmd/login/login.go index 79dc912f4..0fee72222 100644 --- a/cli/cmd/login/login.go +++ b/cli/cmd/login/login.go @@ -59,7 +59,7 @@ func runLogin(cmd *cobra.Command, args []string) error { return mobycli.ExecCmd(cmd) } -func cloudLogin(cmd *cobra.Command, backendType string, params map[string]string) error { +func cloudLogin(cmd *cobra.Command, backendType string, params interface{}) error { ctx := cmd.Context() cs, err := client.GetCloudService(ctx, backendType) if err != nil { diff --git a/context/cloud/api.go b/context/cloud/api.go index d6bb755e7..84c2c242f 100644 --- a/context/cloud/api.go +++ b/context/cloud/api.go @@ -25,11 +25,11 @@ import ( // Service cloud specific services type Service interface { // Login login to cloud provider - Login(ctx context.Context, params map[string]string) error + Login(ctx context.Context, params interface{}) error // 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 @@ -45,10 +45,10 @@ func (cs notImplementedCloudService) Logout(ctx context.Context) error { return errdefs.ErrNotImplemented } -func (cs notImplementedCloudService) Login(ctx context.Context, params map[string]string) error { +func (cs notImplementedCloudService) Login(ctx context.Context, params interface{}) error { 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 }