Skip to content

Commit cb28ed3

Browse files
committed
Search for specified org using the CF API
Not by getting all orgs and iterating over the returned array. Also, the Cloud Foundry client has a bug - if the response is paginated, it returns only the results that are returned with the first page.
1 parent ccda3bb commit cb28ed3

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

monitor.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,12 @@ func (m *appMonitor) spaceApps(guid string) ([]cfclient.App, error) {
191191
}
192192

193193
func (m *appMonitor) space(orgName, spaceName string) (cfclient.Space, error) {
194-
var targetOrg cfclient.Org
195-
orgs, err := m.cloudFoundryClient.ListOrgs()
194+
org, err := m.org(orgName)
196195
if err != nil {
197196
return cfclient.Space{}, err
198197
}
199-
for _, org := range orgs {
200-
if org.Name == orgName {
201-
targetOrg = org
202-
break
203-
}
204-
}
205198

206-
spaces, err := m.cloudFoundryClient.OrgSpaces(targetOrg.Guid)
199+
spaces, err := m.cloudFoundryClient.OrgSpaces(org.Guid)
207200
if err != nil {
208201
return cfclient.Space{}, err
209202
}
@@ -215,3 +208,33 @@ func (m *appMonitor) space(orgName, spaceName string) (cfclient.Space, error) {
215208
}
216209
return cfclient.Space{}, fmt.Errorf("space %s not found", spaceName)
217210
}
211+
212+
func (m *appMonitor) org(name string) (cfclient.Org, error) {
213+
cf := m.cloudFoundryClient
214+
215+
path := fmt.Sprintf("/v2/organizations?q=name:%s", name)
216+
resp, err := cf.DoRequest(cf.NewRequest("GET", path))
217+
if err != nil {
218+
return cfclient.Org{}, fmt.Errorf("error requesting organizations: %v", err)
219+
}
220+
defer resp.Body.Close()
221+
222+
var orgResp cfclient.OrgResponse
223+
d := json.NewDecoder(resp.Body)
224+
if err := d.Decode(&orgResp); err != nil {
225+
return cfclient.Org{}, fmt.Errorf("error decoding response: %v", err)
226+
}
227+
println(orgResp.Count)
228+
if orgResp.Count == 0 {
229+
return cfclient.Org{}, fmt.Errorf("org %q not found", name)
230+
}
231+
if orgResp.Count > 1 {
232+
return cfclient.Org{}, fmt.Errorf("name %q does not refer to a single org", name)
233+
}
234+
235+
return cfclient.Org{
236+
Guid: orgResp.Resources[0].Meta.Guid,
237+
Name: orgResp.Resources[0].Entity.Name,
238+
}, nil
239+
240+
}

0 commit comments

Comments
 (0)