Skip to content

Commit

Permalink
Create namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahhenderson committed Jul 20, 2018
1 parent 6d28439 commit 97d51ac
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 1 deletion.
50 changes: 50 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ type PublishOrbResponse struct {
GQLResponseErrors
}

// CreateNamepsaceResponse type matches the data shape of the GQL response for
// creating a namespace
type CreateNamespaceResponse struct {
Namespace struct {
CreatedAt string
Id string
}

GQLResponseErrors
}

// ToError returns all GraphQL errors for a single response concatenated, or
// nil.
func (response GQLResponseErrors) ToError() error {
Expand Down Expand Up @@ -164,3 +175,42 @@ func OrbPublish(ctx context.Context, logger *logger.Logger,
}
return &response.PublishOrb.PublishOrbResponse, err
}

func OrbCreateNamespace(ctx context.Context, logger *logger.Logger, name string, ownerId string) (*CreateNamespaceResponse, error) {
var response struct {
CreateNamespace struct {
CreateNamespaceResponse
}
}

query := `
mutation($name: String!, $organizationId: UUID!) {
createNamespace(
name: $name,
organizationId: $organizationId
) {
namespace {
createdAt
id
}
errors {
message
type
}
}
}`

request := client.NewAuthorizedRequest(viper.GetString("token"), query)
request.Var("name", name)
request.Var("organizationId", ownerId) // TODO: In GraphQL, organizationId should really be ownerId

graphQLclient := client.NewClient(viper.GetString("endpoint"), logger)

err := graphQLclient.Run(ctx, request, &response)

if err != nil {
err = errors.Wrap(err, "Unable to create namespace")
}

return &response.CreateNamespace.CreateNamespaceResponse, err
}
29 changes: 29 additions & 0 deletions cmd/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
var orbPath string
var orbVersion string
var orbID string
var name string
var ownerId string

func newOrbCommand() *cobra.Command {

Expand Down Expand Up @@ -48,6 +50,12 @@ func newOrbCommand() *cobra.Command {
orbPublishCommand.PersistentFlags().StringVarP(&orbVersion, "orb-version", "o", "", "version of orb to publish")
orbPublishCommand.PersistentFlags().StringVarP(&orbID, "orb-id", "i", "", "id of orb to publish")

orbCreateNamespace := &cobra.Command{
Use: "createns",
Short: "create an orb namespace",
RunE: createOrbNamespace,
}

orbCommand := &cobra.Command{
Use: "orb",
Short: "Operate on orbs",
Expand All @@ -63,6 +71,10 @@ func newOrbCommand() *cobra.Command {

orbCommand.AddCommand(orbPublishCommand)

orbCreateNamespace.PersistentFlags().StringVarP(&name, "name", "n", "", "name of namespace")
orbCreateNamespace.PersistentFlags().StringVarP(&ownerId, "owner-id", "i", "", "owner id")
orbCommand.AddCommand(orbCreateNamespace)

return orbCommand
}

Expand Down Expand Up @@ -229,3 +241,20 @@ func publishOrb(cmd *cobra.Command, args []string) error {
Logger.Info("Orb published")
return nil
}

func createOrbNamespace(cmd *cobra.Command, args []string) error {
ctx := context.Background()

response, err := api.OrbCreateNamespace(ctx, Logger, name, ownerId)

if err != nil {
return err
}

if len(response.Errors) > 0 {
return response.ToError()
}

Logger.Info("Namespace created")
return nil
}
76 changes: 75 additions & 1 deletion cmd/orb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,79 @@ var _ = Describe("Orb integration tests", func() {

})
})

Describe("when creating / reserving a namespace", func() {
BeforeEach(func() {
command = exec.Command(pathCLI,
"orb", "createns",
"-t", token,
"-e", testServer.URL(),
"--name", "foo-ns",
"--owner-id", "70cb2cb6-f960-4135-84a9-db88221c6573",
)
})

It("works", func() {
By("setting up a mock server")

gqlResponse := `{
"createNamespace": {
"errors": [],
"namespace": {
"createdAt": "2018-07-16T18:03:18.961Z",
"id": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}
}`

expectedRequestJson := `{
"query": "\n\t\t\tmutation($name: String!, $organizationId: UUID!) {\n\t\t\t\tcreateNamespace(\n\t\t\t\t\tname: $name,\n\t\t\t\t\torganizationId: $organizationId\n\t\t\t\t) {\n\t\t\t\t\tnamespace {\n\t\t\t\t\t\tcreatedAt\n\t\t\t\t\t\tid\n\t\t\t\t\t}\n\t\t\t\t\terrors {\n\t\t\t\t\t\tmessage\n\t\t\t\t\t\ttype\t\t\t\t\t\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}",
"variables": {
"name": "foo-ns",
"organizationId": "70cb2cb6-f960-4135-84a9-db88221c6573"
}
}`

appendPostHandler(testServer, token, http.StatusOK, expectedRequestJson, gqlResponse)

By("running the command")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)

Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Out).Should(gbytes.Say("Namespace created"))
Eventually(session).Should(gexec.Exit(0))
})

It("prints all errors returned by the GraphQL API", func() {
By("setting up a mock server")

gqlResponse := `{
"createNamespace": {
"errors": [
{"message": "error1"},
{"message": "error2"}
],
"namespace": null
}
}`

expectedRequestJson := `{
"query": "\n\t\t\tmutation($name: String!, $organizationId: UUID!) {\n\t\t\t\tcreateNamespace(\n\t\t\t\t\tname: $name,\n\t\t\t\t\torganizationId: $organizationId\n\t\t\t\t) {\n\t\t\t\t\tnamespace {\n\t\t\t\t\t\tcreatedAt\n\t\t\t\t\t\tid\n\t\t\t\t\t}\n\t\t\t\t\terrors {\n\t\t\t\t\t\tmessage\n\t\t\t\t\t\ttype\t\t\t\t\t\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}",
"variables": {
"name": "foo-ns",
"organizationId": "70cb2cb6-f960-4135-84a9-db88221c6573"
}
}`

appendPostHandler(testServer, token, http.StatusOK, expectedRequestJson, gqlResponse)

By("running the command")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)

Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err).Should(gbytes.Say("Error: error1: error2"))
Eventually(session).ShouldNot(gexec.Exit(0))
})
})
})
})
})

0 comments on commit 97d51ac

Please sign in to comment.