-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathondemand.go
157 lines (130 loc) · 3.77 KB
/
ondemand.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
package traefik_cf_containers_ondemand_plugin
import (
"context"
"fmt"
"log"
"net/http"
"time"
)
const defaultApiEndpoint = "https://api.run.pivotal.io"
// Config the plugin configuration
type Config struct {
Name string
ApiEndpoint string
OrgName string
SpaceName string
Username string
Password string
Apps string
Token string
}
// CreateConfig creates a config with its default values
func CreateConfig() *Config {
return &Config{
ApiEndpoint: defaultApiEndpoint,
OrgName: "TEST_ORG",
SpaceName: "TEST_SPACE",
Apps: "APPS_TO_BE_SCALED",
Name: "traefik_cf_containers_ondemand",
Username: "TEST_USER",
Password: "TEST_PASSWORD",
}
}
// Ondemand holds the request for the on demand service
type Ondemand struct {
name string
next http.Handler
config Config
// endpoint Endpoint
}
// New function creates the configuration and end points
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if len(config.ApiEndpoint) == 0 {
return nil, fmt.Errorf("ApiEndpoint cannot be null")
}
if len(config.OrgName) == 0 {
return nil, fmt.Errorf("OrgName cannot be null")
}
if len(config.SpaceName) == 0 {
return nil, fmt.Errorf("SpaceName cannot be null")
}
if len(config.Apps) == 0 {
return nil, fmt.Errorf("Apps cannot be null")
}
if len(config.Name) == 0 {
return nil, fmt.Errorf("Name cannot be null")
}
return &Ondemand{
next: next,
name: name,
config: *config,
// endpoint: endpoint,
}, nil
}
// ServeHTTP retrieve the service status
func (e *Ondemand) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Expectedly only the initial request would have the path as / and all
// subsequent requests would be different
if req.URL.Path == "/" {
status, err := getServiceStatus(/*&e.endpoint,*/ &e.config)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(err.Error()))
}
if status == "STARTED" {
// Service started forward request
e.next.ServeHTTP(rw, req)
} else if status == "STARTING" {
// Service starting, notify client
rw.WriteHeader(http.StatusAccepted)
rw.Write([]byte("Service is starting..."))
} else {
// Error
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte("Unexpected status answer from ondemand service"))
}
}else {
e.next.ServeHTTP(rw, req)
}
}
func getServiceStatus(/*endpoint *Endpoint,*/ config *Config) (string, error) {
start := time.Now()
endpoint, err := GetInfo(*config)
if err != nil {
return "error_starting", fmt.Errorf("Error while getting apiendpoint info")
}
log.Printf("%+v\n", endpoint)
// Get Access Token
loginResponse, err := GetToken(*config, endpoint.AuthorizationEndpoint)
if err != nil {
return "error_starting", fmt.Errorf("Error in getting token")
}
config.Token = loginResponse.AccessToken
// Get CF Space GUID
spaceGuid, err := GetSpace(*config)
if err != nil {
return "error_starting", fmt.Errorf("Error in getting space guid")
}
log.Printf("%s\n", spaceGuid)
// Get App GUID's
apps, err := GetAppIds(*config, spaceGuid)
if err != nil {
return "error_starting", fmt.Errorf("Error in getting app guid")
}
log.Printf("%+v\n", apps)
// Update environment variable with last request time
status, err := UpdateAppEnvironment(*config, apps)
if err != nil {
return "error_starting", fmt.Errorf("Error in updating app environment")
}
log.Printf("App environment status:%t\n", status)
// Start Apps
appResponses, err := StartApps(*config, apps)
if err != nil {
return "error_starting", fmt.Errorf("Error in starting app using guids")
}
log.Printf("%+v\n", appResponses)
duration := time.Since(start)
log.Printf("Time to process the middleware:%s", duration)
return appResponses[0].State, nil
}