forked from tsuru/tsuru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
174 lines (155 loc) · 3.7 KB
/
service.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2014 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package service
import (
"errors"
"regexp"
"github.com/tsuru/tsuru/auth"
"github.com/tsuru/tsuru/db"
"gopkg.in/mgo.v2/bson"
)
type Service struct {
Name string `bson:"_id"`
Username string
Password string
Endpoint map[string]string
OwnerTeams []string `bson:"owner_teams"`
Teams []string
Doc string
IsRestricted bool `bson:"is_restricted"`
}
var (
ErrServiceAlreadyExists = errors.New("Service already exists.")
)
func (s *Service) Get() error {
conn, err := db.Conn()
if err != nil {
return err
}
defer conn.Close()
query := bson.M{"_id": s.Name}
return conn.Services().Find(query).One(&s)
}
func (s *Service) Create() error {
conn, err := db.Conn()
if err != nil {
return err
}
defer conn.Close()
n, err := conn.Services().Find(bson.M{"_id": s.Name}).Count()
if err != nil {
return err
}
if n != 0 {
return ErrServiceAlreadyExists
}
return conn.Services().Insert(s)
}
func (s *Service) Update() error {
conn, err := db.Conn()
if err != nil {
return err
}
defer conn.Close()
return conn.Services().Update(bson.M{"_id": s.Name}, s)
}
func (s *Service) Delete() error {
conn, err := db.Conn()
if err != nil {
return err
}
defer conn.Close()
_, err = conn.Services().RemoveAll(bson.M{"_id": s.Name})
return err
}
func (s *Service) getClient(endpoint string) (cli *Client, err error) {
if e, ok := s.Endpoint[endpoint]; ok {
if p, _ := regexp.MatchString("^https?://", e); !p {
e = "http://" + e
}
cli = &Client{endpoint: e, username: s.GetUsername(), password: s.Password}
} else {
err = errors.New("Unknown endpoint: " + endpoint)
}
return
}
func (s *Service) GetUsername() string {
if s.Username != "" {
return s.Username
}
return s.Name
}
func (s *Service) findTeam(team *auth.Team) int {
for i, t := range s.Teams {
if team.Name == t {
return i
}
}
return -1
}
func (s *Service) HasTeam(team *auth.Team) bool {
return s.findTeam(team) > -1
}
func (s *Service) GrantAccess(team *auth.Team) error {
if s.HasTeam(team) {
return errors.New("This team already has access to this service")
}
s.Teams = append(s.Teams, team.Name)
return nil
}
func (s *Service) RevokeAccess(team *auth.Team) error {
index := s.findTeam(team)
if index < 0 {
return errors.New("This team does not have access to this service")
}
copy(s.Teams[index:], s.Teams[index+1:])
s.Teams = s.Teams[:len(s.Teams)-1]
return nil
}
func GetServicesNames(services []Service) []string {
sNames := make([]string, len(services))
for i, s := range services {
sNames[i] = s.Name
}
return sNames
}
func GetServicesByTeamKindAndNoRestriction(teamKind string, u *auth.User) ([]Service, error) {
teams, err := u.Teams()
if err != nil {
return nil, err
}
conn, err := db.Conn()
if err != nil {
return nil, err
}
defer conn.Close()
teamsNames := auth.GetTeamsNames(teams)
q := bson.M{"$or": []bson.M{
{teamKind: bson.M{"$in": teamsNames}},
{"is_restricted": false},
}}
var services []Service
err = conn.Services().Find(q).Select(bson.M{"name": 1}).All(&services)
return services, err
}
func GetServicesByOwnerTeams(teamKind string, u *auth.User) ([]Service, error) {
teams, err := u.Teams()
if err != nil {
return nil, err
}
conn, err := db.Conn()
if err != nil {
return nil, err
}
defer conn.Close()
teamsNames := auth.GetTeamsNames(teams)
q := bson.M{teamKind: bson.M{"$in": teamsNames}}
var services []Service
err = conn.Services().Find(q).All(&services)
return services, err
}
type ServiceModel struct {
Service string `json:"service"`
Instances []string `json:"instances"`
}