@@ -191,19 +191,12 @@ func (m *appMonitor) spaceApps(guid string) ([]cfclient.App, error) {
191191}
192192
193193func (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