Merge pull request #170 from docker/context_create_check

Do not allow context creation for unknown types
This commit is contained in:
Guillaume Tardif 2020-06-04 16:51:11 +02:00 committed by GitHub
commit b91eab0544
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 26 deletions

View File

@ -29,13 +29,12 @@ package context
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/docker/api/client"
"github.com/spf13/cobra"
"github.com/docker/api/client"
"github.com/docker/api/context/store"
)
@ -67,30 +66,10 @@ func createCommand() *cobra.Command {
}
func runCreate(ctx context.Context, opts AciCreateOpts, name string, contextType string) error {
var description string
var contextData interface{}
switch contextType {
case "aci":
cs, err := client.GetCloudService(ctx, "aci")
contextData, description, err := getContextData(ctx, contextType, opts)
if err != nil {
return errors.Wrap(err, "cannot connect to backend")
return nil
}
params := map[string]string{
"aciSubscriptionId": opts.aciSubscriptionID,
"aciResourceGroup": opts.aciResourceGroup,
"aciLocation": opts.aciLocation,
"description": opts.description,
}
contextData, description, err = cs.CreateContextData(ctx, params)
if err != nil {
return errors.Wrap(err, "cannot create context")
}
default: // TODO: we need to implement different contexts for known backends
description = opts.description
contextData = store.ExampleContext{}
}
s := store.ContextStore(ctx)
return s.Create(
name,
@ -99,3 +78,26 @@ func runCreate(ctx context.Context, opts AciCreateOpts, name string, contextType
contextData,
)
}
func getContextData(ctx context.Context, contextType string, opts AciCreateOpts) (interface{}, string, error) {
switch contextType {
case "aci":
cs, err := client.GetCloudService(ctx, "aci")
if err != nil {
return nil, "", errors.Wrap(err, "cannot connect to ACI backend")
}
params := map[string]string{
"aciSubscriptionId": opts.aciSubscriptionID,
"aciResourceGroup": opts.aciResourceGroup,
"aciLocation": opts.aciLocation,
"description": opts.description,
}
return cs.CreateContextData(ctx, params)
case "moby":
return store.MobyContext{}, opts.description, nil
case "example":
return store.ExampleContext{}, opts.description, nil
default:
return nil, "", errors.New(fmt.Sprintf("incorrect context type %s, must be one of (aci | moby | docker)", contextType))
}
}

View File

@ -0,0 +1,34 @@
package context
import (
"context"
"testing"
"github.com/docker/api/context/store"
_ "github.com/docker/api/example"
"github.com/docker/api/tests/framework"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/suite"
)
type PsSuite struct {
framework.CliSuite
}
func (sut *PsSuite) TestCreateContextDataMoby() {
data, description, err := getContextData(context.TODO(), "moby", AciCreateOpts{})
Expect(err).To(BeNil())
Expect(data).To(Equal(store.MobyContext{}))
Expect(description).To(Equal(""))
}
func (sut *PsSuite) TestErrorOnUnknownContextType() {
_, _, err := getContextData(context.TODO(), "foo", AciCreateOpts{})
Expect(err).To(MatchError("incorrect context type foo, must be one of (aci | moby | docker)"))
}
func TestPs(t *testing.T) {
RegisterTestingT(t)
suite.Run(t, new(PsSuite))
}