瀏覽代碼

Merge pull request #387 from docker/fix_subscriptionID_param

Fixing subscription-id parameter not passed to backend...
Djordje Lukic 5 年之前
父節點
當前提交
f7d1b8d4bf
共有 8 個文件被更改,包括 52 次插入59 次删除
  1. 19 4
      azure/backend.go
  2. 10 10
      azure/context.go
  3. 5 5
      azure/context_test.go
  4. 0 3
      azure/login/login.go
  5. 9 24
      cli/cmd/context/createaci.go
  6. 4 8
      cli/cmd/login/azurelogin.go
  7. 1 1
      cli/cmd/login/login.go
  8. 4 4
      context/cloud/api.go

+ 19 - 4
azure/backend.go

@@ -50,6 +50,19 @@ const (
 // ErrNoSuchContainer is returned when the mentioned container does not exist
 // ErrNoSuchContainer is returned when the mentioned container does not exist
 var ErrNoSuchContainer = errors.New("no such container")
 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() {
 func init() {
 	backend.Register("aci", "aci", service, getCloudService)
 	backend.Register("aci", "aci", service, getCloudService)
 }
 }
@@ -351,15 +364,17 @@ type aciCloudService struct {
 	loginService login.AzureLoginService
 	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 {
 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.(ContextParams)
+	return contextHelper.createContextData(ctx, createOpts)
 }
 }

+ 10 - 10
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
 	var subscriptionID string
-	if opts["aciSubscriptionID"] != "" {
-		subscriptionID = opts["aciSubscriptionID"]
+	if opts.SubscriptionID != "" {
+		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 +61,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"] != "" {
-		group, err = helper.resourceGroupHelper.GetGroup(ctx, subscriptionID, opts["aciResourceGroup"])
+	if opts.ResourceGroup != "" {
+		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 +80,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"] != "" {
-		description = fmt.Sprintf("%s (%s)", opts["description"], description)
+	if opts.Description != "" {
+		description = fmt.Sprintf("%s (%s)", opts.Description, description)
 	}
 	}
 
 
 	return store.AciContext{
 	return store.AciContext{
@@ -108,7 +108,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 ContextParams, 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 +124,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

+ 5 - 5
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",
 	}
 	}
 }
 }
 
 

+ 0 - 3
azure/login/login.go

@@ -45,9 +45,6 @@ const (
 	// v1 scope like "https://management.azure.com/.default" for ARM access
 	// v1 scope like "https://management.azure.com/.default" for ARM access
 	scopes   = "offline_access https://management.azure.com/.default"
 	scopes   = "offline_access https://management.azure.com/.default"
 	clientID = "04b07795-8ddb-461a-bbee-02f9e1bf7b46" // Azure CLI client id
 	clientID = "04b07795-8ddb-461a-bbee-02f9e1bf7b46" // Azure CLI client id
-
-	// TenantIDLoginParam
-	TenantIDLoginParam = "tenantId"
 )
 )
 
 
 type (
 type (

+ 9 - 24
cli/cmd/context/createaci.go

@@ -22,20 +22,14 @@ import (
 	"github.com/pkg/errors"
 	"github.com/pkg/errors"
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 
 
+	"github.com/docker/api/azure"
 	"github.com/docker/api/client"
 	"github.com/docker/api/client"
 	"github.com/docker/api/context/store"
 	"github.com/docker/api/context/store"
 	"github.com/docker/api/errdefs"
 	"github.com/docker/api/errdefs"
 )
 )
 
 
-type aciCreateOpts struct {
-	description    string
-	location       string
-	subscriptionID string
-	resourceGroup  string
-}
-
 func createAciCommand() *cobra.Command {
 func createAciCommand() *cobra.Command {
-	var opts aciCreateOpts
+	var opts azure.ContextParams
 	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 +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
 	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) {
 	if contextExists(ctx, contextName) {
 		return errors.Wrapf(errdefs.ErrAlreadyExists, "context %s", 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)
 	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))
-}
-
-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)
 }
 }

+ 4 - 8
cli/cmd/login/azurelogin.go

@@ -3,26 +3,22 @@ package login
 import (
 import (
 	"github.com/spf13/cobra"
 	"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
 // AzureLoginCommand returns the azure login command
 func AzureLoginCommand() *cobra.Command {
 func AzureLoginCommand() *cobra.Command {
-	opts := azureLoginOpts{}
+	opts := azure.LoginParams{}
 	cmd := &cobra.Command{
 	cmd := &cobra.Command{
 		Use:   "azure",
 		Use:   "azure",
 		Short: "Log in to azure",
 		Short: "Log in to azure",
 		Args:  cobra.MaximumNArgs(0),
 		Args:  cobra.MaximumNArgs(0),
 		RunE: func(cmd *cobra.Command, args []string) error {
 		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 := 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
 	return cmd
 }
 }

+ 1 - 1
cli/cmd/login/login.go

@@ -59,7 +59,7 @@ func runLogin(cmd *cobra.Command, args []string) error {
 	return mobycli.ExecCmd(cmd)
 	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()
 	ctx := cmd.Context()
 	cs, err := client.GetCloudService(ctx, backendType)
 	cs, err := client.GetCloudService(ctx, backendType)
 	if err != nil {
 	if err != nil {

+ 4 - 4
context/cloud/api.go

@@ -25,11 +25,11 @@ import (
 // Service cloud specific services
 // Service cloud specific services
 type Service interface {
 type Service interface {
 	// Login login to cloud provider
 	// 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 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
@@ -45,10 +45,10 @@ func (cs notImplementedCloudService) Logout(ctx context.Context) error {
 	return errdefs.ErrNotImplemented
 	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
 	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
 }
 }