Skip to content

Commit af7aadf

Browse files
authored
Swap over to prefer usage of GUIDs for making calls (#110)
1 parent fc561b3 commit af7aadf

File tree

4 files changed

+85
-105
lines changed

4 files changed

+85
-105
lines changed

internal/report/client.go

+17-26
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (r *Client) GetSummaryReportByOrgNames(orgNames ...string) (*SummaryReport,
3232
var orgReports []OrgReport
3333
for _, org := range populatedOrgs {
3434
spaceReports := r.getSpaceReportsByOrg(org)
35-
orgQuota, _ := r.client.OrgQuotas.GetOrgQuota(org.QuotaURL)
35+
orgQuota, _ := r.client.OrgQuotas.GetOrgQuotaByOrgGUID(org.GUID)
3636
orgReport := *NewOrgReport(orgQuota, org, spaceReports...)
3737
orgReports = append(orgReports, orgReport)
3838
}
@@ -54,7 +54,7 @@ func (r *Client) getOrgs(orgNames ...string) ([]v2client.Org, error) {
5454

5555
if len(orgNames) > 0 {
5656
for _, orgName := range orgNames {
57-
rawOrg, err := r.client.Orgs.GetOrg(orgName)
57+
rawOrg, err := r.client.Orgs.GetOrgByName(orgName)
5858
if err != nil {
5959
return nil, err
6060
}
@@ -81,59 +81,50 @@ func (r *Client) getOrgs(orgNames ...string) ([]v2client.Org, error) {
8181
}
8282

8383
func (r *Client) getOrgDetails(o v2client.Org) (v2client.Org, error) {
84-
usage, err := r.client.Orgs.GetOrgMemoryUsage(o)
84+
usage, err := r.client.Orgs.GetOrgMemoryUsageByOrgGUID(o.GUID)
8585
if err != nil {
8686
return v2client.Org{}, err
8787
}
8888

89-
// TODO teeing up to swap out for 'quota' being it's own managed entity
90-
// for time being, going to simply modify it _here_ to not break anything obvious
91-
quota, err := r.client.OrgQuotas.GetOrgQuota(o.QuotaURL)
89+
quota, err := r.client.OrgQuotas.GetOrgQuotaByOrgGUID(o.GUID)
9290
if err != nil {
9391
return v2client.Org{}, err
9492
}
95-
spaces, err := r.getSpaces(o.SpacesURL)
93+
spaces, err := r.getGetSpacesByOrgGUID(o.GUID)
9694
if err != nil {
9795
return v2client.Org{}, err
9896
}
9997

10098
return v2client.Org{
101-
MemoryQuota: quota.MemoryLimit,
102-
MemoryUsage: int(usage),
103-
Name: o.Name,
104-
QuotaURL: o.QuotaURL,
105-
Spaces: spaces,
106-
SpacesURL: o.SpacesURL,
107-
URL: o.URL,
99+
GUID: o.GUID,
100+
MemoryQuota: quota.MemoryLimit,
101+
MemoryUsage: int(usage),
102+
Name: o.Name,
103+
QuotaDefinitionGUID: o.QuotaDefinitionGUID,
104+
Spaces: spaces,
105+
SpacesURL: o.SpacesURL,
108106
}, nil
109107
}
110108

111-
func (r *Client) getSpaces(spaceURL string) ([]v2client.Space, error) {
112-
rawSpaces, err := r.client.Orgs.GetOrgSpaces(spaceURL)
109+
func (r *Client) getGetSpacesByOrgGUID(orgGUID string) ([]v2client.Space, error) {
110+
rawSpaces, err := r.client.Orgs.GetOrgSpacesByOrgGUID(orgGUID)
113111
if err != nil {
114112
return nil, err
115113
}
116114
var spaces = []v2client.Space{}
117115
for _, s := range rawSpaces {
118-
apps, services, err := r.getAppsAndServices(s.SummaryURL)
116+
apps, services, err := r.client.Spaces.GetSpaceAppsAndServicesBySpaceGUID(s.GUID)
119117
if err != nil {
120118
return nil, err
121119
}
122120
spaces = append(spaces,
123121
v2client.Space{
124-
Name: s.Name,
125122
Apps: apps,
123+
GUID: s.GUID,
124+
Name: s.Name,
126125
Services: services,
127126
},
128127
)
129128
}
130129
return spaces, nil
131130
}
132-
133-
func (r *Client) getAppsAndServices(summaryURL string) ([]v2client.App, []v2client.Service, error) {
134-
apps, services, err := r.client.Spaces.GetSpaceAppsAndServices(summaryURL)
135-
if err != nil {
136-
return nil, nil, err
137-
}
138-
return apps, services, nil
139-
}

internal/v2client/org_quotas.go

+21-22
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ package v2client
44
// A space quota looks very similar but it uses a different (v2) API endpoint
55
// just to be safe, going to explicitly reference this as a way to get quota of an Org
66
type OrgQuota struct {
7-
AppInstanceLimit int `json:"app_instance_limit"`
8-
AppTaskLimit int `json:"app_task_limit"`
9-
GUID string
7+
AppInstanceLimit int `json:"app_instance_limit"`
8+
AppTaskLimit int `json:"app_task_limit"`
9+
GUID string `json:"guid"`
1010
InstanceMemoryLimit int `json:"instance_memory_limit"`
1111
MemoryLimit int `json:"memory_limit"`
1212
Name string `json:"name"`
@@ -20,30 +20,29 @@ type OrgQuota struct {
2020
// OrgQuotasService -
2121
type OrgQuotasService service
2222

23-
// GetOrgQuota returns an org's quota. A space quota looks very similar
24-
// but it uses a different (v2) API endpoint, so just to be safe, going to explicitly
25-
// reference this as a way to get quota of an Org
26-
func (o *OrgQuotasService) GetOrgQuota(quotaURL string) (OrgQuota, error) {
27-
quotaJSON, err := o.client.Curl(quotaURL)
23+
// GetOrgQuotaByOrgGUID -
24+
func (o *OrgQuotasService) GetOrgQuotaByOrgGUID(orgGUID string) (OrgQuota, error) {
25+
org, err := o.client.cfc.GetOrgByGuid(orgGUID)
2826
if err != nil {
2927
return OrgQuota{}, err
3028
}
3129

32-
metadata := quotaJSON["metadata"].(map[string]interface{})
33-
guid := metadata["guid"].(string)
30+
quota, err := org.Quota()
31+
if err != nil {
32+
return OrgQuota{}, err
33+
}
3434

35-
quota := quotaJSON["entity"].(map[string]interface{})
3635
return OrgQuota{
37-
AppInstanceLimit: int(quota["app_instance_limit"].(float64)),
38-
AppTaskLimit: int(quota["app_task_limit"].(float64)),
39-
GUID: guid,
40-
InstanceMemoryLimit: int(quota["instance_memory_limit"].(float64)),
41-
MemoryLimit: int(quota["memory_limit"].(float64)),
42-
Name: quota["name"].(string),
43-
TotalPrivateDomains: int(quota["total_private_domains"].(float64)),
44-
TotalReservedRoutePorts: int(quota["total_service_keys"].(float64)),
45-
TotalRoutes: int(quota["total_routes"].(float64)),
46-
TotalServiceKeys: int(quota["total_service_keys"].(float64)),
47-
TotalServices: int(quota["total_services"].(float64)),
36+
AppInstanceLimit: quota.AppInstanceLimit,
37+
AppTaskLimit: quota.AppTaskLimit,
38+
GUID: quota.Guid,
39+
InstanceMemoryLimit: quota.InstanceMemoryLimit,
40+
MemoryLimit: quota.MemoryLimit,
41+
Name: quota.Name,
42+
TotalPrivateDomains: quota.TotalPrivateDomains,
43+
TotalReservedRoutePorts: quota.TotalReservedRoutePorts,
44+
TotalRoutes: quota.TotalRoutes,
45+
TotalServiceKeys: quota.TotalServiceKeys,
46+
TotalServices: quota.TotalServices,
4847
}, nil
4948
}

internal/v2client/orgs.go

+41-54
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,30 @@ import (
66

77
// Org -
88
type Org struct {
9-
GUID string
10-
MemoryQuota int
11-
MemoryUsage int
12-
Name string
13-
QuotaURL string
14-
Spaces []Space
15-
SpacesURL string
16-
URL string
9+
GUID string
10+
MemoryQuota int
11+
MemoryUsage int
12+
Name string
13+
QuotaDefinitionGUID string
14+
Spaces []Space
15+
SpacesURL string
1716
}
1817

1918
// OrgsService -
2019
type OrgsService service
2120

22-
// GetOrg -
23-
func (o *OrgsService) GetOrg(name string) (Org, error) {
21+
// GetOrgByName -
22+
func (o *OrgsService) GetOrgByName(name string) (Org, error) {
2423
org, err := o.client.cfc.GetOrgByName(name)
2524
if err != nil {
2625
return Org{}, err
2726
}
28-
29-
quotaURL := fmt.Sprintf("/v2/quota_definitions/%s", org.QuotaDefinitionGuid)
3027
spacesURL := fmt.Sprintf("/v2/organizations/%s/spaces", org.Guid)
31-
url := fmt.Sprintf("/v2/organizations/%s", org.Guid)
32-
3328
return Org{
34-
GUID: org.Guid,
35-
Name: org.Name,
36-
QuotaURL: quotaURL,
37-
SpacesURL: spacesURL,
38-
URL: url,
29+
GUID: org.Guid,
30+
Name: org.Name,
31+
QuotaDefinitionGUID: org.QuotaDefinitionGuid,
32+
SpacesURL: spacesURL,
3933
}, nil
4034
}
4135

@@ -48,54 +42,47 @@ func (o *OrgsService) GetOrgs() ([]Org, error) {
4842

4943
orgs := []Org{}
5044
for _, org := range listedOrgs {
51-
quotaURL := fmt.Sprintf("/v2/quota_definitions/%s", org.QuotaDefinitionGuid)
5245
spacesURL := fmt.Sprintf("/v2/organizations/%s/spaces", org.Guid)
53-
url := fmt.Sprintf("/v2/organizations/%s", org.Guid)
5446
orgs = append(orgs,
5547
Org{
56-
GUID: org.Guid,
57-
Name: org.Name,
58-
QuotaURL: quotaURL,
59-
SpacesURL: spacesURL,
60-
URL: url,
48+
GUID: org.Guid,
49+
Name: org.Name,
50+
QuotaDefinitionGUID: org.QuotaDefinitionGuid,
51+
SpacesURL: spacesURL,
6152
})
6253
}
63-
6454
return orgs, nil
6555
}
6656

67-
// GetOrgSpaces returns the spaces in an org
68-
func (o *OrgsService) GetOrgSpaces(spacesURL string) ([]Space, error) {
69-
nextURL := spacesURL
57+
// GetOrgSpacesByOrgGUID returns the spaces in an org using the org's GUID
58+
func (o *OrgsService) GetOrgSpacesByOrgGUID(orgGUID string) ([]Space, error) {
59+
org, err := o.client.cfc.GetOrgByGuid(orgGUID)
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
orgSummary, err := org.Summary()
65+
if err != nil {
66+
return nil, err
67+
}
68+
7069
spaces := []Space{}
71-
for nextURL != "" {
72-
spacesJSON, err := o.client.Curl(nextURL)
73-
if err != nil {
74-
return nil, err
75-
}
76-
for _, s := range spacesJSON["resources"].([]interface{}) {
77-
theSpace := s.(map[string]interface{})
78-
metadata := theSpace["metadata"].(map[string]interface{})
79-
entity := theSpace["entity"].(map[string]interface{})
80-
spaces = append(spaces,
81-
Space{
82-
GUID: metadata["guid"].(string),
83-
Name: entity["name"].(string),
84-
SummaryURL: metadata["url"].(string) + "/summary",
85-
})
86-
}
87-
if next, ok := spacesJSON["next_url"].(string); ok {
88-
nextURL = next
89-
} else {
90-
nextURL = ""
91-
}
70+
for _, space := range orgSummary.Spaces {
71+
summaryURL := fmt.Sprintf("/v2/space/%s/summary", space.Guid)
72+
spaces = append(spaces,
73+
Space{
74+
GUID: space.Guid,
75+
Name: space.Name,
76+
SummaryURL: summaryURL,
77+
})
9278
}
9379
return spaces, nil
9480
}
9581

96-
// GetOrgMemoryUsage returns amount of memory (in MB) a given org is currently using
97-
func (o *OrgsService) GetOrgMemoryUsage(org Org) (float64, error) {
98-
usageJSON, err := o.client.Curl(org.URL + "/memory_usage")
82+
// GetOrgMemoryUsageByOrgGUID returns amount of memory (in MB) a given org is currently using
83+
func (o *OrgsService) GetOrgMemoryUsageByOrgGUID(orgGUID string) (float64, error) {
84+
path := fmt.Sprintf("/v2/organizations/%s/memory_usage", orgGUID)
85+
usageJSON, err := o.client.Curl(path)
9986
if err != nil {
10087
return 0, err
10188
}

internal/v2client/spaces.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package v2client
22

3+
import "fmt"
4+
35
// Space -
46
type Space struct {
57
Apps []App
@@ -12,9 +14,10 @@ type Space struct {
1214
// SpacesService -
1315
type SpacesService service
1416

15-
// GetSpaceAppsAndServices returns the apps and the services from a space's /summary endpoint
16-
func (s *SpacesService) GetSpaceAppsAndServices(summaryURL string) ([]App, []Service, error) {
17-
summaryJSON, err := s.client.Curl(summaryURL)
17+
// GetSpaceAppsAndServicesBySpaceGUID returns the apps and the services from a space
18+
func (s *SpacesService) GetSpaceAppsAndServicesBySpaceGUID(spaceGUID string) ([]App, []Service, error) {
19+
path := fmt.Sprintf("/v2/spaces/%s/summary", spaceGUID)
20+
summaryJSON, err := s.client.Curl(path)
1821
if err != nil {
1922
return nil, nil, err
2023
}

0 commit comments

Comments
 (0)