问题
I am thinking to write a unix shell script to list orgs and spaces associated with it in Cloud Foundry. But had issues, as we can list spaces associated with an org only after targeting it.
I cannot target 2 Orgs at a time using : for i in Org1 Org2 ; do cf target -o $i ; done
Hence, if i put all the orgs in a text file and like this :
for i in cat Orgs.txt
; do cf spaces $i ; done
Please suggest your views. Thank you!
回答1:
The cf org
command will show information about an organization, including spaces, so it's unnecessary to target each org individually:
$ cf target
API endpoint: https://api.local.pcfdev.io
API version: 2.65.0
User: admin
No org or space targeted, use 'cf target -o ORG -s SPACE'
$ cf org pcfdev-org
Getting info for org pcfdev-org as admin...
OK
pcfdev-org:
domains: local.pcfdev.io, tcp.local.pcfdev.io
quota: default (102400M memory limit, unlimited instance memory limit, 100 routes, -1 services, paid services allowed, unlimited app instance limit, unlimited route ports)
spaces: pcfdev-space
space quotas:
I think you can simply iterate over the list of orgs, getting the spaces for each, and do a little string substitution to preface the spaces list with the appropriate org name:
$ for org in $( cf orgs | tail +4 ); do \
cf org "$org" | grep spaces | sed "s/spaces/$org/"; \
done
demos: spring-cloud, services, chaos-engineering
pcfdev-org: pcfdev-space
system: system
I looked through the list of CF CLI plugins, but I didn't find anything that didn't provide way more information than you're asking. Closest I could find quickly was the Usage Report plugin.
回答2:
In addition to the fine answer Clayton published, you can use cf curl
.
Ex: cf curl /v2/spaces | jq .resources[].entity.name
This will query the spaces API directly and does care if you are or are not targeted to an org. It will show all the spaces visible to your current user. You could optionally filter this more with the q
attribute. More on that in the docs.
http://apidocs.cloudfoundry.org/253/spaces/list_all_spaces.html
The jq bit is optional, but filters out just the space name for you.
Final note, the results are paginated so if you have more than 50 you either need to run multiple requests or increase the results-per-page
value.
来源:https://stackoverflow.com/questions/43487792/need-to-list-orgs-and-spaces-in-cloud-foundry