-
Notifications
You must be signed in to change notification settings - Fork 24
/
create.go
109 lines (94 loc) · 2.61 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package notifiers
import (
"github.com/Scalingo/cli/config"
"github.com/Scalingo/go-scalingo/debug"
"github.com/Scalingo/cli/io"
scalingo "github.com/Scalingo/go-scalingo"
"gopkg.in/errgo.v1"
)
type ProvisionParams struct {
CollaboratorUsernames []string
SelectedEventNames []string
scalingo.NotifierParams
}
func Provision(app, platformName string, params ProvisionParams) error {
c, err := config.ScalingoClient()
if err != nil {
return errgo.Notef(err, "fail to get Scalingo client")
}
debug.Printf("[Provision] params: %+v", params)
if app == "" {
return errgo.New("no app defined")
}
if platformName == "" {
return errgo.New("no platform defined")
}
eventTypes, err := c.EventTypesList()
if err != nil {
return errgo.Notef(err, "fail to list event types")
}
for _, name := range params.SelectedEventNames {
for _, t := range eventTypes {
if t.Name == name {
params.SelectedEventIDs = append(params.SelectedEventIDs, t.ID)
break
}
}
}
if len(params.CollaboratorUsernames) > 0 {
params.UserIDs, err = collaboratorUserIDs(c, app, params.CollaboratorUsernames)
if err != nil {
return errgo.Notef(err, "invalid collaborator usernames")
}
}
platforms, err := c.NotificationPlatformByName(platformName)
if err != nil {
return errgo.Mask(err, errgo.Any)
}
if len(platforms) <= 0 {
return errgo.Newf("notification platform \"%s\" has not been found", platformName)
}
params.PlatformID = platforms[0].ID
baseNotifier, err := c.NotifierProvision(app, params.NotifierParams)
if err != nil {
return errgo.Mask(err, errgo.Any)
}
notifier := baseNotifier.Specialize()
displayDetails(notifier, eventTypes)
io.Info()
io.Status("Notifier have been created.")
return nil
}
func collaboratorUserIDs(c *scalingo.Client, app string, usernames []string) ([]string, error) {
ids := make([]string, 0, len(usernames))
collaborators, err := c.CollaboratorsList(app)
if err != nil {
return nil, errgo.Notef(err, "fail to list collaborators")
}
scapp, err := c.AppsShow(app)
if err != nil {
return nil, errgo.Notef(err, "fail to get application information")
}
var id string
for _, u := range usernames {
id = ""
if u == scapp.Owner.Username {
id = scapp.Owner.ID
} else {
for _, c := range collaborators {
if c.Username == u && c.Status == "pending" {
return nil, errgo.Newf("%v is a collaborator but their invitation is still pending", c.Username)
} else if c.Username == u {
id = c.UserID
break
}
}
}
if id == "" {
return nil, errgo.Newf("no such collaborator: %v", u)
} else {
ids = append(ids, id)
}
}
return ids, nil
}