forked from concourse/concourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
105 lines (85 loc) · 2.57 KB
/
set.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
package teamserver
import (
"encoding/json"
"net/http"
"code.cloudfoundry.org/lager"
"github.com/pf-qiu/concourse/v6/atc"
"github.com/pf-qiu/concourse/v6/atc/api/accessor"
"github.com/pf-qiu/concourse/v6/atc/api/present"
)
type SetTeamResponse struct {
Errors []string `json:"errors,omitempty"`
Warnings []atc.ConfigWarning `json:"warnings,omitempty"`
Team atc.Team `json:"team"`
}
func (s *Server) SetTeam(w http.ResponseWriter, r *http.Request) {
hLog := s.logger.Session("set-team")
hLog.Debug("setting-team")
acc := accessor.GetAccessor(r)
teamName := r.FormValue(":team_name")
var atcTeam atc.Team
err := json.NewDecoder(r.Body).Decode(&atcTeam)
if err != nil {
hLog.Error("malformed-request", err)
w.WriteHeader(http.StatusBadRequest)
return
}
if err := atcTeam.Validate(); err != nil {
hLog.Error("malformed-auth-config", err)
w.WriteHeader(http.StatusBadRequest)
return
}
atcTeam.Name = teamName
if !acc.IsAdmin() && !acc.IsAuthorized(teamName) {
hLog.Debug("not-allowed")
w.WriteHeader(http.StatusForbidden)
return
}
team, found, err := s.teamFactory.FindTeam(teamName)
if err != nil {
hLog.Error("failed-to-lookup-team", err, lager.Data{"teamName": teamName})
w.WriteHeader(http.StatusInternalServerError)
return
}
response := SetTeamResponse{}
if found {
hLog.Debug("updating-credentials")
err = team.UpdateProviderAuth(atcTeam.Auth)
if err != nil {
hLog.Error("failed-to-update-team", err, lager.Data{"teamName": teamName})
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
} else if acc.IsAdmin() {
hLog.Debug("creating team")
warning := atc.ValidateIdentifier(atcTeam.Name, "team")
if warning != nil {
response.Warnings = append(response.Warnings, *warning)
}
team, err = s.teamFactory.CreateTeam(atcTeam)
if err != nil {
hLog.Error("failed-to-save-team", err, lager.Data{"teamName": teamName})
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
} else {
w.WriteHeader(http.StatusForbidden)
return
}
err = s.teamFactory.NotifyCacher()
if err != nil {
hLog.Error("failed-to-notify-cacher", err, lager.Data{"teamName": teamName})
w.WriteHeader(http.StatusInternalServerError)
return
}
response.Team = present.Team(team)
err = json.NewEncoder(w).Encode(response)
if err != nil {
hLog.Error("failed-to-encode-team", err)
w.WriteHeader(http.StatusInternalServerError)
}
}