forked from vmware-archive/tsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retirer.go
67 lines (53 loc) · 1.39 KB
/
retirer.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
package tsa
import (
"net/http"
"net/http/httputil"
"fmt"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/tedsuo/rata"
)
type Retirer struct {
ATCEndpoint *rata.RequestGenerator
TokenGenerator TokenGenerator
}
func (l *Retirer) Retire(logger lager.Logger, worker atc.Worker) error {
logger.Info("start")
defer logger.Info("end")
request, err := l.ATCEndpoint.CreateRequest(atc.RetireWorker, rata.Params{
"worker_name": worker.Name,
}, nil)
if err != nil {
logger.Error("failed-to-construct-request", err)
return err
}
var jwtToken string
if worker.Team != "" {
jwtToken, err = l.TokenGenerator.GenerateTeamToken(worker.Team)
} else {
jwtToken, err = l.TokenGenerator.GenerateSystemToken()
}
if err != nil {
logger.Error("failed-to-generate-token", err)
return err
}
request.Header.Add("Authorization", "Bearer "+jwtToken)
response, err := http.DefaultClient.Do(request)
if err != nil {
logger.Error("failed-to-retire", err)
return err
}
defer response.Body.Close()
if response.StatusCode == http.StatusNotFound {
logger.Info("worker-not-found")
return nil
}
if response.StatusCode != http.StatusOK {
logger.Error("bad-response", nil, lager.Data{
"status-code": response.StatusCode,
})
b, _ := httputil.DumpResponse(response, true)
return fmt.Errorf("bad-response (%d): %s", response.StatusCode, string(b))
}
return nil
}