This repository has been archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 382
Add svcat command to create user-defined cluster-scoped classes #2190
Merged
k8s-ci-robot
merged 16 commits into
kubernetes-retired:master
from
artmello:user_defined_classes
Jul 30, 2018
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1424086
Add svcat command to create user-defined cluster-scoped classes
artmello 3f901e2
Code improvements following review
artmello ec0300a
Merge remote-tracking branch 'origin/master' into user_defined_classes
artmello c6c49b6
Fix help messaging language
artmello 0cef4a5
Use class data got from RetrieveClassByName to pass to CreateClass in…
artmello 25141be
Fix create class unit test on pkg
artmello 39e34bf
Merge remote-tracking branch 'origin/master' into user_defined_classes
artmello b736b21
Fix pkg tests for CreateClass
artmello a30b93b
Fix cmd tests for CreateClass
artmello a5c5e64
Update golden files
artmello 4049623
Use WriteClassDetails instead of only printing the name of new class
artmello 473bb08
Add CreateClass to fake_svcat_client.go
artmello a477501
Add create_cmd_test.go
artmello 4a84457
Add entry to docs/cli
artmello ff4a0b7
Add comments to fix lint messages
artmello 1cb3560
Fix wrongly removed function from FakeSvcatClient
artmello 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,81 @@ | ||
/* | ||
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 class | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/command" | ||
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/output" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// CreateCmd contains the information needed to create a new class | ||
type CreateCmd struct { | ||
*command.Context | ||
Name string | ||
From string | ||
} | ||
|
||
// NewCreateCmd builds a "svcat create class" command | ||
func NewCreateCmd(cxt *command.Context) *cobra.Command { | ||
createCmd := &CreateCmd{Context: cxt} | ||
cmd := &cobra.Command{ | ||
Use: "class [NAME] --from [EXISTING_NAME]", | ||
Short: "Copies an existing class into a new user-defined cluster-scoped class", | ||
Example: command.NormalizeExamples(` | ||
svcat create class newclass --from mysqldb | ||
`), | ||
PreRunE: command.PreRunE(createCmd), | ||
RunE: command.RunE(createCmd), | ||
} | ||
cmd.Flags().StringVarP(&createCmd.From, "from", "f", "", | ||
"Name from an existing class that will be copied (Required)", | ||
) | ||
cmd.MarkFlagRequired("from") | ||
|
||
return cmd | ||
} | ||
|
||
// Validate checks that the required arguments have been provided | ||
func (c *CreateCmd) Validate(args []string) error { | ||
if len(args) <= 0 { | ||
return fmt.Errorf("new class name should be provided") | ||
} | ||
|
||
c.Name = args[0] | ||
|
||
return nil | ||
} | ||
|
||
// Run calls out to the pkg lib to create the class and displays the output | ||
func (c *CreateCmd) Run() error { | ||
class, err := c.App.RetrieveClassByName(c.From) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
class.Name = c.Name | ||
|
||
createdClass, err := c.App.CreateClass(class) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
output.WriteClassDetails(c.Output, createdClass) | ||
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,99 @@ | ||
/* | ||
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 class_test | ||
|
||
import ( | ||
"bytes" | ||
|
||
. "github.com/kubernetes-incubator/service-catalog/cmd/svcat/class" | ||
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/command" | ||
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/test" | ||
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1" | ||
"github.com/kubernetes-incubator/service-catalog/pkg/svcat" | ||
servicecatalogfakes "github.com/kubernetes-incubator/service-catalog/pkg/svcat/service-catalog/service-catalogfakes" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var _ = Describe("Create Command", func() { | ||
Describe("NewCreateCmd", func() { | ||
It("Builds and returns a cobra command", func() { | ||
cxt := &command.Context{} | ||
cmd := NewCreateCmd(cxt) | ||
Expect(*cmd).NotTo(BeNil()) | ||
Expect(cmd.Use).To(Equal("class [NAME] --from [EXISTING_NAME]")) | ||
Expect(cmd.Short).To(ContainSubstring("Copies an existing class into a new user-defined cluster-scoped class")) | ||
Expect(cmd.Example).To(ContainSubstring("svcat create class newclass --from mysqldb")) | ||
Expect(len(cmd.Aliases)).To(Equal(0)) | ||
}) | ||
}) | ||
Describe("Validate name is provided", func() { | ||
It("errors if a new class name is not provided", func() { | ||
cmd := CreateCmd{ | ||
Context: nil, | ||
Name: "", | ||
From: "existingclass", | ||
} | ||
err := cmd.Validate([]string{}) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
Describe("Validate from is provided", func() { | ||
It("errors if a existing class name is not provided using from", func() { | ||
cmd := CreateCmd{ | ||
Context: nil, | ||
Name: "newclass", | ||
From: "", | ||
} | ||
err := cmd.Validate([]string{}) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
Describe("Create", func() { | ||
It("Calls the pkg/svcat libs Create method with the passed in variables and prints output to the user", func() { | ||
className := "newclass" | ||
existingClassName := "existingclass" | ||
|
||
classToReturn := &v1beta1.ClusterServiceClass{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: className, | ||
}, | ||
} | ||
|
||
outputBuffer := &bytes.Buffer{} | ||
|
||
fakeApp, _ := svcat.NewApp(nil, nil, "default") | ||
fakeSDK := new(servicecatalogfakes.FakeSvcatClient) | ||
fakeSDK.CreateClassReturns(classToReturn, nil) | ||
fakeApp.SvcatClient = fakeSDK | ||
cmd := CreateCmd{ | ||
Context: svcattest.NewContext(outputBuffer, fakeApp), | ||
Name: className, | ||
From: existingClassName, | ||
} | ||
err := cmd.Run() | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
class := fakeSDK.CreateClassArgsForCall(0) | ||
Expect(class.Name).To(Equal(className)) | ||
|
||
output := outputBuffer.String() | ||
Expect(output).To(ContainSubstring(className)) | ||
}) | ||
}) | ||
}) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Name: user-provided-service | ||
Description: A user provided service | ||
UUID: new-class | ||
Status: Active | ||
Tags: | ||
Broker: ups-broker |
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.
FYI, there will need to be a follow-on PR to make this work for namespaced classes. That feature is super new so it doesn't need to be in this PR. Just giving you a heads up in case you want to pick up more related stuff after this PR is merged! 😀