This repository was archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 381
Add marketplace command to svcat #2409
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package extra_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"testing" | ||
) | ||
|
||
func TestExtra(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Extra Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package extra | ||
|
||
import ( | ||
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/command" | ||
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/output" | ||
"github.com/kubernetes-incubator/service-catalog/pkg/svcat/service-catalog" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// MarketplaceCmd contains the information needed to query the marketplace of | ||
// services available to the user | ||
type MarketplaceCmd struct { | ||
*command.Namespaced | ||
*command.Formatted | ||
} | ||
|
||
// NewMarketplaceCmd builds a "svcat marketplace" command | ||
func NewMarketplaceCmd(cxt *command.Context) *cobra.Command { | ||
mpCmd := &MarketplaceCmd{ | ||
Namespaced: command.NewNamespaced(cxt), | ||
Formatted: command.NewFormatted(), | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "marketplace", | ||
Aliases: []string{"marketplace", "mp"}, | ||
Short: "List available service offerings", | ||
Example: command.NormalizeExamples(` | ||
svcat marketplace | ||
svcat marketplace --namespace dev | ||
`), | ||
PreRunE: command.PreRunE(mpCmd), | ||
RunE: command.RunE(mpCmd), | ||
} | ||
|
||
mpCmd.AddOutputFlags(cmd.Flags()) | ||
mpCmd.AddNamespaceFlags(cmd.Flags(), true) | ||
return cmd | ||
} | ||
|
||
// Validate always returns true, there are no args to validate | ||
func (c *MarketplaceCmd) Validate(args []string) error { | ||
return nil | ||
} | ||
|
||
// Run retrieves all service classes visible in the current namespace, | ||
// retrieves the plans belonging to those classses, and then displays | ||
// that to the user | ||
func (c *MarketplaceCmd) Run() error { | ||
opts := servicecatalog.ScopeOptions{ | ||
Namespace: c.Namespace, | ||
Scope: servicecatalog.AllScope, | ||
} | ||
classes, err := c.App.RetrieveClasses(opts) | ||
if err != nil { | ||
return err | ||
} | ||
plans := make([][]servicecatalog.Plan, len(classes)) | ||
classPlans, err := c.App.RetrievePlans("", opts) | ||
if err != nil { | ||
return err | ||
} | ||
for i, class := range classes { | ||
for _, plan := range classPlans { | ||
if plan.GetClassID() == class.GetName() { | ||
plans[i] = append(plans[i], plan) | ||
} | ||
} | ||
} | ||
output.WriteClassAndPlanDetails(c.Output, classes, plans) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package extra_test | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/command" | ||
. "github.com/kubernetes-incubator/service-catalog/cmd/svcat/extra" | ||
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/test" | ||
_ "github.com/kubernetes-incubator/service-catalog/internal/test" | ||
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1" | ||
"github.com/kubernetes-incubator/service-catalog/pkg/svcat" | ||
servicecatalog "github.com/kubernetes-incubator/service-catalog/pkg/svcat/service-catalog" | ||
servicecatalogfakes "github.com/kubernetes-incubator/service-catalog/pkg/svcat/service-catalog/service-catalogfakes" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var _ = Describe("Register Command", func() { | ||
Describe("NewMarketplaceCmd", func() { | ||
It("Builds and returns a cobra command with the correct flags", func() { | ||
cxt := &command.Context{} | ||
cmd := NewMarketplaceCmd(cxt) | ||
Expect(*cmd).NotTo(BeNil()) | ||
|
||
Expect(cmd.Use).To(Equal("marketplace")) | ||
Expect(cmd.Short).To(ContainSubstring("List available service offerings")) | ||
Expect(cmd.Example).To(ContainSubstring("svcat marketplace --namespace dev")) | ||
Expect(cmd.Aliases).To(ConsistOf("marketplace", "mp")) | ||
|
||
urlFlag := cmd.Flags().Lookup("namespace") | ||
Expect(urlFlag).NotTo(BeNil()) | ||
Expect(urlFlag.Usage).To(ContainSubstring("If present, the namespace scope for this request")) | ||
}) | ||
}) | ||
Describe("Validate", func() { | ||
}) | ||
Describe("Run", func() { | ||
It("Calls the pkg/svcat libs methods to retrieve all classes and plans and prints output to the user", func() { | ||
namespace := "banana" | ||
|
||
className := "foobarclass" | ||
classDescription := "This class foobars" | ||
className2 := "barbazclass" | ||
classDescription2 := "This class barbazs" | ||
planName := "foobarplan1" | ||
planName2 := "foobarplan2" | ||
planName3 := "barbazplan" | ||
classToReturn := &v1beta1.ClusterServiceClass{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: namespace, | ||
Name: className, | ||
}, | ||
Spec: v1beta1.ClusterServiceClassSpec{ | ||
CommonServiceClassSpec: v1beta1.CommonServiceClassSpec{ | ||
Description: classDescription, | ||
ExternalName: className, | ||
}, | ||
}, | ||
} | ||
classToReturn2 := &v1beta1.ClusterServiceClass{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: namespace, | ||
Name: className2, | ||
}, | ||
Spec: v1beta1.ClusterServiceClassSpec{ | ||
CommonServiceClassSpec: v1beta1.CommonServiceClassSpec{ | ||
Description: classDescription2, | ||
ExternalName: className2, | ||
}, | ||
}, | ||
} | ||
planToReturn := &v1beta1.ClusterServicePlan{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: namespace, | ||
Name: planName, | ||
}, | ||
Spec: v1beta1.ClusterServicePlanSpec{ | ||
CommonServicePlanSpec: v1beta1.CommonServicePlanSpec{ | ||
ExternalName: planName, | ||
}, | ||
ClusterServiceClassRef: v1beta1.ClusterObjectReference{ | ||
Name: className, | ||
}, | ||
}, | ||
} | ||
planToReturn2 := &v1beta1.ClusterServicePlan{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: namespace, | ||
Name: planName2, | ||
}, | ||
Spec: v1beta1.ClusterServicePlanSpec{ | ||
CommonServicePlanSpec: v1beta1.CommonServicePlanSpec{ | ||
ExternalName: planName2, | ||
}, | ||
ClusterServiceClassRef: v1beta1.ClusterObjectReference{ | ||
Name: className, | ||
}, | ||
}, | ||
} | ||
planToReturn3 := &v1beta1.ClusterServicePlan{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: namespace, | ||
Name: planName3, | ||
}, | ||
Spec: v1beta1.ClusterServicePlanSpec{ | ||
CommonServicePlanSpec: v1beta1.CommonServicePlanSpec{ | ||
ExternalName: planName3, | ||
}, | ||
ClusterServiceClassRef: v1beta1.ClusterObjectReference{ | ||
Name: className2, | ||
}, | ||
}, | ||
} | ||
|
||
outputBuffer := &bytes.Buffer{} | ||
fakeApp, _ := svcat.NewApp(nil, nil, "default") | ||
fakeSDK := new(servicecatalogfakes.FakeSvcatClient) | ||
fakeSDK.RetrieveClassesReturns([]servicecatalog.Class{classToReturn, classToReturn2}, nil) | ||
fakeSDK.RetrievePlansReturns([]servicecatalog.Plan{planToReturn, planToReturn2, planToReturn3}, nil) | ||
fakeApp.SvcatClient = fakeSDK | ||
cmd := MarketplaceCmd{ | ||
Namespaced: &command.Namespaced{Context: svcattest.NewContext(outputBuffer, fakeApp)}, | ||
Formatted: command.NewFormatted(), | ||
} | ||
cmd.Namespace = namespace | ||
|
||
err := cmd.Run() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(fakeSDK.RetrieveClassesCallCount()).To(Equal(1)) | ||
scopeOpts := fakeSDK.RetrieveClassesArgsForCall(0) | ||
Expect(scopeOpts).To(Equal(servicecatalog.ScopeOptions{ | ||
Scope: servicecatalog.AllScope, | ||
Namespace: namespace, | ||
})) | ||
|
||
Expect(fakeSDK.RetrievePlansCallCount()).To(Equal(1)) | ||
class, scopeOpts := fakeSDK.RetrievePlansArgsForCall(0) | ||
Expect(class).To(Equal("")) | ||
Expect(scopeOpts).To(Equal(servicecatalog.ScopeOptions{ | ||
Scope: servicecatalog.AllScope, | ||
Namespace: namespace, | ||
})) | ||
|
||
output := outputBuffer.String() | ||
Expect(output).To(ContainSubstring(className)) | ||
Expect(output).To(ContainSubstring(planName)) | ||
Expect(output).To(ContainSubstring(planName2)) | ||
Expect(output).To(ContainSubstring(classDescription)) | ||
Expect(output).To(ContainSubstring(className2)) | ||
Expect(output).To(ContainSubstring(planName3)) | ||
Expect(output).To(ContainSubstring(classDescription2)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about putting this under
cmd/svcat/plan
which has other verbs related to displaying and interacting with plans?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so, I considered about where to put this - it's only sort of related to only classes or only plans. I figured we might as well have a place for commands that aren't strictly related to one of the types, and I didn't want to name it
commands
because we already have acommand
dir in there. I was planning on moving the other commands like completion and install here at some point. I'm open to suggestions for a better name.