Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CR-D collaborators management #113

Merged
merged 1 commit into from
May 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions api/collaborators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package api

import "gopkg.in/errgo.v1"

type Collaborator struct {
ID string `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Status string `json:"status"`
}

type CollaboratorsRes struct {
Collaborators []Collaborator `json:"collaborators"`
}

type CollaboratorRes struct {
Collaborator Collaborator `json:"collaborator"`
}

func CollaboratorsList(app string) ([]Collaborator, error) {
var collaboratorsRes CollaboratorsRes
err := subresourceList(app, "collaborators", nil, &collaboratorsRes)
if err != nil {
return nil, errgo.Mask(err)
}
return collaboratorsRes.Collaborators, nil
}

func CollaboratorAdd(app string, email string) (Collaborator, error) {
var collaboratorRes CollaboratorRes
err := subresourceAdd(app, "collaborators", CollaboratorRes{
Collaborator: Collaborator{Email: email},
}, &collaboratorRes)
if err != nil {
return Collaborator{}, errgo.Mask(err)
}
return collaboratorRes.Collaborator, nil
}

func CollaboratorRemove(app string, id string) error {
return subresourceDelete(app, "collaborators", id)
}
79 changes: 79 additions & 0 deletions api/subresources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package api

import "gopkg.in/errgo.v1"

func subresourceList(app, subresource string, payload, data interface{}) error {
req := &APIRequest{
Endpoint: "/apps/" + app + "/" + subresource,
Params: payload,
}
res, err := req.Do()
if err != nil {
return errgo.Mask(err, errgo.Any)
}
defer res.Body.Close()

err = ParseJSON(res, data)
if err != nil {
return errgo.Mask(err, errgo.Any)
}

return nil
}

func subresourceAdd(app, subresource string, payload, data interface{}) error {
req := &APIRequest{
Method: "POST",
Endpoint: "/apps/" + app + "/" + subresource,
Expected: Statuses{201},
Params: payload,
}

res, err := req.Do()
if err != nil {
return errgo.Mask(err, errgo.Any)
}
defer res.Body.Close()

err = ParseJSON(res, data)
if err != nil {
return errgo.Mask(err, errgo.Any)
}

return nil
}

func subresourceDelete(app string, subresource string, id string) error {
req := &APIRequest{
Method: "DELETE",
Endpoint: "/apps/" + app + "/" + subresource + "/" + id,
Expected: Statuses{204},
}

res, err := req.Do()
if err != nil {
return errgo.Mask(err, errgo.Any)
}
res.Body.Close()
return nil
}

func subresourceUpdate(app, subresource, id string, payload, data interface{}) error {
req := &APIRequest{
Method: "PATCH",
Endpoint: "/apps/" + app + "/" + subresource + "/" + id,
Params: payload,
}

res, err := req.Do()
if err != nil {
return errgo.Mask(err, errgo.Any)
}

err = ParseJSON(res, data)
if err != nil {
return errgo.Mask(err, errgo.Any)
}

return nil
}
63 changes: 63 additions & 0 deletions cmd/collaborators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmd

import (
"github.com/Scalingo/cli/Godeps/_workspace/src/github.com/Scalingo/codegangsta-cli"
"github.com/Scalingo/cli/appdetect"
"github.com/Scalingo/cli/collaborators"
)

var (
CollaboratorsListCommand = cli.Command{
Name: "collaborators",
Category: "Collaborators",
Usage: "List the collaborators of an application",
Description: "List all the collaborator of an application and display information about them.",
Action: func(c *cli.Context) {
currentApp := appdetect.CurrentApp(c)
if len(c.Args()) != 0 {
cli.ShowCommandHelp(c, "collaborators")
} else {
err := collaborators.List(currentApp)
if err != nil {
errorQuit(err)
}
}
},
}

CollaboratorsAddCommand = cli.Command{
Name: "collaborators-add",
Category: "Collaborators",
Usage: "Invite someone to work on an application",
Description: "Invite someone to collaborate on an application, an invitation will be sent to the given email\n scalingo -a myapp collaborators-add user@example.com",
Action: func(c *cli.Context) {
currentApp := appdetect.CurrentApp(c)
if len(c.Args()) != 1 {
cli.ShowCommandHelp(c, "collaborators-add")
} else {
err := collaborators.Add(currentApp, c.Args()[0])
if err != nil {
errorQuit(err)
}
}
},
}

CollaboratorsRemoveCommand = cli.Command{
Name: "collaborators-remove",
Category: "Collaborators",
Usage: "Revoke permission to collaborate on an application",
Description: "Revoke the invitation of collaboration to the given email\n scalingo -a myapp collaborators-remove user@example.com",
Action: func(c *cli.Context) {
currentApp := appdetect.CurrentApp(c)
if len(c.Args()) != 1 {
cli.ShowCommandHelp(c, "collaborators-remove")
} else {
err := collaborators.Remove(currentApp, c.Args()[0])
if err != nil {
errorQuit(err)
}
}
},
}
)
17 changes: 17 additions & 0 deletions collaborators/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package collaborators

import (
"github.com/Scalingo/cli/api"
"github.com/Scalingo/cli/io"
"gopkg.in/errgo.v1"
)

func Add(app, email string) error {
collaborator, err := api.CollaboratorAdd(app, email)
if err != nil {
return errgo.Mask(err, errgo.Any)
}

io.Status(collaborator.Email, "has been invited to collaborate to", app)
return nil
}
25 changes: 25 additions & 0 deletions collaborators/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package collaborators

import (
"os"

"github.com/Scalingo/cli/api"
"github.com/olekukonko/tablewriter"
"gopkg.in/errgo.v1"
)

func List(app string) error {
collaborators, err := api.CollaboratorsList(app)
if err != nil {
return errgo.Mask(err, errgo.Any)
}

t := tablewriter.NewWriter(os.Stdout)
t.SetHeader([]string{"Email", "Username", "Status"})

for _, collaborator := range collaborators {
t.Append([]string{collaborator.Email, collaborator.Username, collaborator.Status})
}
t.Render()
return nil
}
46 changes: 46 additions & 0 deletions collaborators/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package collaborators

import (
"errors"

"github.com/Scalingo/cli/api"
"github.com/Scalingo/cli/io"
"gopkg.in/errgo.v1"
)

var (
notFound = errors.New("collaborator not found")
)

func Remove(app, email string) error {
collaborator, err := getFromEmail(app, email)
if err != nil {
if err == notFound {
io.Error(email + " is not a collaborator of " + app + ".")
return nil
} else {
return errgo.Mask(err, errgo.Any)
}
}

err = api.CollaboratorRemove(app, collaborator.ID)
if err != nil {
return errgo.Mask(err, errgo.Any)
}

io.Status(email, "has been removed from the collaborators of", app)
return nil
}

func getFromEmail(app, email string) (api.Collaborator, error) {
collaborators, err := api.CollaboratorsList(app)
if err != nil {
return api.Collaborator{}, errgo.Mask(err, errgo.Any)
}
for _, collaborator := range collaborators {
if collaborator.Email == email {
return collaborator, nil
}
}
return api.Collaborator{}, notFound
}
5 changes: 5 additions & 0 deletions scalingo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func main() {
cmd.DomainsRemoveCommand,
cmd.DomainsSSLCommand,

// Collaborators
cmd.CollaboratorsListCommand,
cmd.CollaboratorsAddCommand,
cmd.CollaboratorsRemoveCommand,

// Addons
cmd.AddonProvidersListCommand,
cmd.AddonProvidersPlansCommand,
Expand Down