Skip to content

Commit

Permalink
Swap over to prefer usage of GUIDs for making calls (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
aegershman authored Dec 10, 2019
1 parent fc561b3 commit af7aadf
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 105 deletions.
43 changes: 17 additions & 26 deletions internal/report/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (r *Client) GetSummaryReportByOrgNames(orgNames ...string) (*SummaryReport,
var orgReports []OrgReport
for _, org := range populatedOrgs {
spaceReports := r.getSpaceReportsByOrg(org)
orgQuota, _ := r.client.OrgQuotas.GetOrgQuota(org.QuotaURL)
orgQuota, _ := r.client.OrgQuotas.GetOrgQuotaByOrgGUID(org.GUID)
orgReport := *NewOrgReport(orgQuota, org, spaceReports...)
orgReports = append(orgReports, orgReport)
}
Expand All @@ -54,7 +54,7 @@ func (r *Client) getOrgs(orgNames ...string) ([]v2client.Org, error) {

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

func (r *Client) getOrgDetails(o v2client.Org) (v2client.Org, error) {
usage, err := r.client.Orgs.GetOrgMemoryUsage(o)
usage, err := r.client.Orgs.GetOrgMemoryUsageByOrgGUID(o.GUID)
if err != nil {
return v2client.Org{}, err
}

// TODO teeing up to swap out for 'quota' being it's own managed entity
// for time being, going to simply modify it _here_ to not break anything obvious
quota, err := r.client.OrgQuotas.GetOrgQuota(o.QuotaURL)
quota, err := r.client.OrgQuotas.GetOrgQuotaByOrgGUID(o.GUID)
if err != nil {
return v2client.Org{}, err
}
spaces, err := r.getSpaces(o.SpacesURL)
spaces, err := r.getGetSpacesByOrgGUID(o.GUID)
if err != nil {
return v2client.Org{}, err
}

return v2client.Org{
MemoryQuota: quota.MemoryLimit,
MemoryUsage: int(usage),
Name: o.Name,
QuotaURL: o.QuotaURL,
Spaces: spaces,
SpacesURL: o.SpacesURL,
URL: o.URL,
GUID: o.GUID,
MemoryQuota: quota.MemoryLimit,
MemoryUsage: int(usage),
Name: o.Name,
QuotaDefinitionGUID: o.QuotaDefinitionGUID,
Spaces: spaces,
SpacesURL: o.SpacesURL,
}, nil
}

func (r *Client) getSpaces(spaceURL string) ([]v2client.Space, error) {
rawSpaces, err := r.client.Orgs.GetOrgSpaces(spaceURL)
func (r *Client) getGetSpacesByOrgGUID(orgGUID string) ([]v2client.Space, error) {
rawSpaces, err := r.client.Orgs.GetOrgSpacesByOrgGUID(orgGUID)
if err != nil {
return nil, err
}
var spaces = []v2client.Space{}
for _, s := range rawSpaces {
apps, services, err := r.getAppsAndServices(s.SummaryURL)
apps, services, err := r.client.Spaces.GetSpaceAppsAndServicesBySpaceGUID(s.GUID)
if err != nil {
return nil, err
}
spaces = append(spaces,
v2client.Space{
Name: s.Name,
Apps: apps,
GUID: s.GUID,
Name: s.Name,
Services: services,
},
)
}
return spaces, nil
}

func (r *Client) getAppsAndServices(summaryURL string) ([]v2client.App, []v2client.Service, error) {
apps, services, err := r.client.Spaces.GetSpaceAppsAndServices(summaryURL)
if err != nil {
return nil, nil, err
}
return apps, services, nil
}
43 changes: 21 additions & 22 deletions internal/v2client/org_quotas.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package v2client
// A space quota looks very similar but it uses a different (v2) API endpoint
// just to be safe, going to explicitly reference this as a way to get quota of an Org
type OrgQuota struct {
AppInstanceLimit int `json:"app_instance_limit"`
AppTaskLimit int `json:"app_task_limit"`
GUID string
AppInstanceLimit int `json:"app_instance_limit"`
AppTaskLimit int `json:"app_task_limit"`
GUID string `json:"guid"`
InstanceMemoryLimit int `json:"instance_memory_limit"`
MemoryLimit int `json:"memory_limit"`
Name string `json:"name"`
Expand All @@ -20,30 +20,29 @@ type OrgQuota struct {
// OrgQuotasService -
type OrgQuotasService service

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

metadata := quotaJSON["metadata"].(map[string]interface{})
guid := metadata["guid"].(string)
quota, err := org.Quota()
if err != nil {
return OrgQuota{}, err
}

quota := quotaJSON["entity"].(map[string]interface{})
return OrgQuota{
AppInstanceLimit: int(quota["app_instance_limit"].(float64)),
AppTaskLimit: int(quota["app_task_limit"].(float64)),
GUID: guid,
InstanceMemoryLimit: int(quota["instance_memory_limit"].(float64)),
MemoryLimit: int(quota["memory_limit"].(float64)),
Name: quota["name"].(string),
TotalPrivateDomains: int(quota["total_private_domains"].(float64)),
TotalReservedRoutePorts: int(quota["total_service_keys"].(float64)),
TotalRoutes: int(quota["total_routes"].(float64)),
TotalServiceKeys: int(quota["total_service_keys"].(float64)),
TotalServices: int(quota["total_services"].(float64)),
AppInstanceLimit: quota.AppInstanceLimit,
AppTaskLimit: quota.AppTaskLimit,
GUID: quota.Guid,
InstanceMemoryLimit: quota.InstanceMemoryLimit,
MemoryLimit: quota.MemoryLimit,
Name: quota.Name,
TotalPrivateDomains: quota.TotalPrivateDomains,
TotalReservedRoutePorts: quota.TotalReservedRoutePorts,
TotalRoutes: quota.TotalRoutes,
TotalServiceKeys: quota.TotalServiceKeys,
TotalServices: quota.TotalServices,
}, nil
}
95 changes: 41 additions & 54 deletions internal/v2client/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,30 @@ import (

// Org -
type Org struct {
GUID string
MemoryQuota int
MemoryUsage int
Name string
QuotaURL string
Spaces []Space
SpacesURL string
URL string
GUID string
MemoryQuota int
MemoryUsage int
Name string
QuotaDefinitionGUID string
Spaces []Space
SpacesURL string
}

// OrgsService -
type OrgsService service

// GetOrg -
func (o *OrgsService) GetOrg(name string) (Org, error) {
// GetOrgByName -
func (o *OrgsService) GetOrgByName(name string) (Org, error) {
org, err := o.client.cfc.GetOrgByName(name)
if err != nil {
return Org{}, err
}

quotaURL := fmt.Sprintf("/v2/quota_definitions/%s", org.QuotaDefinitionGuid)
spacesURL := fmt.Sprintf("/v2/organizations/%s/spaces", org.Guid)
url := fmt.Sprintf("/v2/organizations/%s", org.Guid)

return Org{
GUID: org.Guid,
Name: org.Name,
QuotaURL: quotaURL,
SpacesURL: spacesURL,
URL: url,
GUID: org.Guid,
Name: org.Name,
QuotaDefinitionGUID: org.QuotaDefinitionGuid,
SpacesURL: spacesURL,
}, nil
}

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

orgs := []Org{}
for _, org := range listedOrgs {
quotaURL := fmt.Sprintf("/v2/quota_definitions/%s", org.QuotaDefinitionGuid)
spacesURL := fmt.Sprintf("/v2/organizations/%s/spaces", org.Guid)
url := fmt.Sprintf("/v2/organizations/%s", org.Guid)
orgs = append(orgs,
Org{
GUID: org.Guid,
Name: org.Name,
QuotaURL: quotaURL,
SpacesURL: spacesURL,
URL: url,
GUID: org.Guid,
Name: org.Name,
QuotaDefinitionGUID: org.QuotaDefinitionGuid,
SpacesURL: spacesURL,
})
}

return orgs, nil
}

// GetOrgSpaces returns the spaces in an org
func (o *OrgsService) GetOrgSpaces(spacesURL string) ([]Space, error) {
nextURL := spacesURL
// GetOrgSpacesByOrgGUID returns the spaces in an org using the org's GUID
func (o *OrgsService) GetOrgSpacesByOrgGUID(orgGUID string) ([]Space, error) {
org, err := o.client.cfc.GetOrgByGuid(orgGUID)
if err != nil {
return nil, err
}

orgSummary, err := org.Summary()
if err != nil {
return nil, err
}

spaces := []Space{}
for nextURL != "" {
spacesJSON, err := o.client.Curl(nextURL)
if err != nil {
return nil, err
}
for _, s := range spacesJSON["resources"].([]interface{}) {
theSpace := s.(map[string]interface{})
metadata := theSpace["metadata"].(map[string]interface{})
entity := theSpace["entity"].(map[string]interface{})
spaces = append(spaces,
Space{
GUID: metadata["guid"].(string),
Name: entity["name"].(string),
SummaryURL: metadata["url"].(string) + "/summary",
})
}
if next, ok := spacesJSON["next_url"].(string); ok {
nextURL = next
} else {
nextURL = ""
}
for _, space := range orgSummary.Spaces {
summaryURL := fmt.Sprintf("/v2/space/%s/summary", space.Guid)
spaces = append(spaces,
Space{
GUID: space.Guid,
Name: space.Name,
SummaryURL: summaryURL,
})
}
return spaces, nil
}

// GetOrgMemoryUsage returns amount of memory (in MB) a given org is currently using
func (o *OrgsService) GetOrgMemoryUsage(org Org) (float64, error) {
usageJSON, err := o.client.Curl(org.URL + "/memory_usage")
// GetOrgMemoryUsageByOrgGUID returns amount of memory (in MB) a given org is currently using
func (o *OrgsService) GetOrgMemoryUsageByOrgGUID(orgGUID string) (float64, error) {
path := fmt.Sprintf("/v2/organizations/%s/memory_usage", orgGUID)
usageJSON, err := o.client.Curl(path)
if err != nil {
return 0, err
}
Expand Down
9 changes: 6 additions & 3 deletions internal/v2client/spaces.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package v2client

import "fmt"

// Space -
type Space struct {
Apps []App
Expand All @@ -12,9 +14,10 @@ type Space struct {
// SpacesService -
type SpacesService service

// GetSpaceAppsAndServices returns the apps and the services from a space's /summary endpoint
func (s *SpacesService) GetSpaceAppsAndServices(summaryURL string) ([]App, []Service, error) {
summaryJSON, err := s.client.Curl(summaryURL)
// GetSpaceAppsAndServicesBySpaceGUID returns the apps and the services from a space
func (s *SpacesService) GetSpaceAppsAndServicesBySpaceGUID(spaceGUID string) ([]App, []Service, error) {
path := fmt.Sprintf("/v2/spaces/%s/summary", spaceGUID)
summaryJSON, err := s.client.Curl(path)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit af7aadf

Please sign in to comment.