mirror of https://github.com/docker/compose.git
Do not allow context creation for unknown types
This commit is contained in:
parent
c37f666c07
commit
4a4b62ded2
|
@ -29,13 +29,12 @@ package context
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/docker/api/client"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/docker/api/client"
|
||||||
"github.com/docker/api/context/store"
|
"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 {
|
func runCreate(ctx context.Context, opts AciCreateOpts, name string, contextType string) error {
|
||||||
var description string
|
contextData, description, err := getContextData(ctx, contextType, opts)
|
||||||
var contextData interface{}
|
|
||||||
|
|
||||||
switch contextType {
|
|
||||||
case "aci":
|
|
||||||
cs, err := client.GetCloudService(ctx, "aci")
|
|
||||||
if err != nil {
|
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)
|
s := store.ContextStore(ctx)
|
||||||
return s.Create(
|
return s.Create(
|
||||||
name,
|
name,
|
||||||
|
@ -99,3 +78,26 @@ func runCreate(ctx context.Context, opts AciCreateOpts, name string, contextType
|
||||||
contextData,
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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))
|
||||||
|
}
|
Loading…
Reference in New Issue