-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsessionext.go
157 lines (143 loc) · 3.93 KB
/
sessionext.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 fetcher
import (
"encoding/json"
"fmt"
"net/http"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
clients "github.com/cloudfoundry-community/go-cf-clients-helper/v2"
"github.com/cloudfoundry/cf_exporter/models"
log "github.com/sirupsen/logrus"
)
type SessionExt struct {
clients.Session
}
func NewSessionExt(config *CFConfig) (*SessionExt, error) {
conf := clients.Config{
Endpoint: config.URL,
SkipSslValidation: config.SkipSSLValidation,
CFClientID: config.ClientID,
CFClientSecret: config.ClientSecret,
User: config.Username,
Password: config.Password,
BinName: "cf_exporter",
}
session, err := clients.NewSession(conf)
if err != nil {
log.Errorf("unable to create cf client: %s", err)
return nil, err
}
return &SessionExt{
Session: *session,
}, nil
}
func (s SessionExt) GetInfo() (models.Info, error) {
responseBody := models.Info{}
res, httpres, err := s.V3().MakeRequestSendReceiveRaw(
"GET",
fmt.Sprintf("%s/v3/info", s.V3().CloudControllerURL),
http.Header{},
nil,
)
if err != nil {
return responseBody, err
}
defer httpres.Body.Close()
if httpres.StatusCode != http.StatusOK {
return responseBody, fmt.Errorf("http error")
}
if err = json.Unmarshal(res, &responseBody); err != nil {
return responseBody, err
}
return responseBody, nil
}
func (s SessionExt) GetApplications() ([]models.Application, error) {
res := []models.Application{}
_, _, err := s.V3().MakeListRequest(ccv3.RequestParams{
RequestName: "GetApplications",
Query: []ccv3.Query{LargeQuery},
ResponseBody: models.Application{},
AppendToList: func(item interface{}) error {
res = append(res, item.(models.Application))
return nil
},
})
return res, err
}
func (s SessionExt) GetTasks() ([]models.Task, error) {
res := []models.Task{}
_, _, err := s.V3().MakeListRequest(ccv3.RequestParams{
RequestName: "GetTasks",
Query: []ccv3.Query{LargeQuery, TaskActiveStates},
ResponseBody: models.Task{},
AppendToList: func(item interface{}) error {
res = append(res, item.(models.Task))
return nil
},
})
return res, err
}
func (s SessionExt) GetOrganizationQuotas() ([]models.Quota, error) {
res := []models.Quota{}
_, _, err := s.V3().MakeListRequest(ccv3.RequestParams{
RequestName: "GetOrganizationQuotas",
Query: []ccv3.Query{LargeQuery},
ResponseBody: models.Quota{},
AppendToList: func(item interface{}) error {
res = append(res, item.(models.Quota))
return nil
},
})
return res, err
}
func (s SessionExt) GetSpaceQuotas() ([]models.Quota, error) {
res := []models.Quota{}
_, _, err := s.V3().MakeListRequest(ccv3.RequestParams{
RequestName: "GetSpaceQuotas",
Query: []ccv3.Query{LargeQuery},
ResponseBody: models.Quota{},
AppendToList: func(item interface{}) error {
res = append(res, item.(models.Quota))
return nil
},
})
return res, err
}
func (s SessionExt) GetEvents(query ...ccv3.Query) ([]models.Event, error) {
res := []models.Event{}
_, _, err := s.V3().MakeListRequest(ccv3.RequestParams{
RequestName: "GetEvents",
Query: query,
ResponseBody: models.Event{},
AppendToList: func(item interface{}) error {
res = append(res, item.(models.Event))
return nil
},
})
return res, err
}
func (s SessionExt) GetSpaceSummary(guid string) (*models.SpaceSummary, error) {
client := s.Raw()
url := fmt.Sprintf("/v2/spaces/%s/summary", guid)
req, err := client.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %d on request %s", resp.StatusCode, url)
}
res := models.SpaceSummary{}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&res)
if err != nil {
return nil, err
}
return &res, nil
}
// Local Variables:
// ispell-local-dictionary: "american"
// End: