Skip to content

Commit

Permalink
inconspicuously leverage org_quota model (#88)
Browse files Browse the repository at this point in the history
* add quota model

* fixup quota json

* swap back nil and err ordering

* swap nil err to err nil, maybe its better the existing way just doing it for consistentcy

* swap out quota for org_quota, try to make change as minimally impactful as possible

* swap out getorgmemorylimit for just getorgquota

* rm unnecessary typecast
  • Loading branch information
aegershman authored Dec 5, 2019
1 parent 98e483e commit f984711
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 deletions.
39 changes: 27 additions & 12 deletions apihelper/apihelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type CFAPIHelper interface {
GetTarget() string
GetOrgs() ([]models.Org, error)
GetOrg(string) (models.Org, error)
GetQuotaMemoryLimit(string) (float64, error)
GetOrgQuota(string) (models.OrgQuota, error)
GetOrgMemoryUsage(models.Org) (float64, error)
GetOrgSpaces(string) ([]models.Space, error)
GetSpaceAppsAndServices(string) ([]models.App, []models.Service, error)
Expand All @@ -42,7 +42,7 @@ func New(cli plugin.CliConnection) CFAPIHelper {
// GetTarget -
func (api *APIHelper) GetTarget() string {
envInfo, err := cfcurl.Curl(api.cli, "/v2/info")
if nil != err {
if err != nil {
return ""
}
apiep, _ := envInfo["routing_endpoint"].(string)
Expand All @@ -57,7 +57,7 @@ func (api *APIHelper) GetTarget() string {
// GetOrgs -
func (api *APIHelper) GetOrgs() ([]models.Org, error) {
orgsJSON, err := cfcurl.Curl(api.cli, "/v2/organizations")
if nil != err {
if err != nil {
return nil, err
}
pages := int(orgsJSON["total_pages"].(float64))
Expand Down Expand Up @@ -91,7 +91,7 @@ func (api *APIHelper) GetOrg(name string) (models.Org, error) {
query := fmt.Sprintf("name:%s", name)
path := fmt.Sprintf("/v2/organizations?q=%s", url.QueryEscape(query))
orgsJSON, err := cfcurl.Curl(api.cli, path)
if nil != err {
if err != nil {
return models.Org{}, err
}

Expand All @@ -118,19 +118,34 @@ func (api *APIHelper) orgResourceToOrg(o interface{}) models.Org {
}
}

// GetQuotaMemoryLimit returns memory quota (in MB) of a given org
func (api *APIHelper) GetQuotaMemoryLimit(quotaURL string) (float64, error) {
// 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 (api *APIHelper) GetOrgQuota(quotaURL string) (models.OrgQuota, error) {
quotaJSON, err := cfcurl.Curl(api.cli, quotaURL)
if nil != err {
return 0, err
if err != nil {
return models.OrgQuota{}, err
}
return quotaJSON["entity"].(map[string]interface{})["memory_limit"].(float64), nil

quota := quotaJSON["entity"].(map[string]interface{})
return models.OrgQuota{
Name: quota["name"].(string),
TotalServices: int(quota["total_services"].(float64)),
TotalRoutes: int(quota["total_routes"].(float64)),
TotalPrivateDomains: int(quota["total_private_domains"].(float64)),
MemoryLimit: int(quota["memory_limit"].(float64)),
InstanceMemoryLimit: int(quota["instance_memory_limit"].(float64)),
AppInstanceLimit: int(quota["app_instance_limit"].(float64)),
AppTaskLimit: int(quota["app_task_limit"].(float64)),
TotalServiceKeys: int(quota["total_service_keys"].(float64)),
TotalReservedRoutePorts: int(quota["total_service_keys"].(float64)),
}, nil
}

// GetOrgMemoryUsage returns amount of memory (in MB) a given org is currently using
func (api *APIHelper) GetOrgMemoryUsage(org models.Org) (float64, error) {
usageJSON, err := cfcurl.Curl(api.cli, org.URL+"/memory_usage")
if nil != err {
if err != nil {
return 0, err
}
return usageJSON["memory_usage_in_mb"].(float64), nil
Expand All @@ -142,7 +157,7 @@ func (api *APIHelper) GetOrgSpaces(spacesURL string) ([]models.Space, error) {
spaces := []models.Space{}
for nextURL != "" {
spacesJSON, err := cfcurl.Curl(api.cli, nextURL)
if nil != err {
if err != nil {
return nil, err
}
for _, s := range spacesJSON["resources"].([]interface{}) {
Expand All @@ -167,7 +182,7 @@ func (api *APIHelper) GetOrgSpaces(spacesURL string) ([]models.Space, error) {
// GetSpaceAppsAndServices returns the apps and the services from a space's /summary endpoint
func (api *APIHelper) GetSpaceAppsAndServices(summaryURL string) ([]models.App, []models.Service, error) {
summaryJSON, err := cfcurl.Curl(api.cli, summaryURL)
if nil != err {
if err != nil {
return nil, nil, err
}

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion cfcurl/cfcurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func Curl(cli plugin.CliConnection, path string) (map[string]interface{}, error) {
log.Traceln(path)
output, err := cli.CliCommandWithoutTerminalOutput("curl", path)
if nil != err {
if err != nil {
return nil, err
}
log.Traceln(output)
Expand Down
21 changes: 12 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (cmd *UsageReportCmd) getOrgs(orgNames []string) ([]models.Org, error) {
}
} else {
extraRawOrgs, err := cmd.apiHelper.GetOrgs()
if nil != err {
if err != nil {
return nil, err
}
rawOrgs = extraRawOrgs
Expand All @@ -122,35 +122,38 @@ func (cmd *UsageReportCmd) getOrgs(orgNames []string) ([]models.Org, error) {

func (cmd *UsageReportCmd) getOrgDetails(o models.Org) (models.Org, error) {
usage, err := cmd.apiHelper.GetOrgMemoryUsage(o)
if nil != err {
if err != nil {
return models.Org{}, err
}
quota, err := cmd.apiHelper.GetQuotaMemoryLimit(o.QuotaURL)
if nil != 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 := cmd.apiHelper.GetOrgQuota(o.QuotaURL)
if err != nil {
return models.Org{}, err
}
spaces, err := cmd.getSpaces(o.SpacesURL)
if nil != err {
if err != nil {
return models.Org{}, err
}

return models.Org{
Name: o.Name,
MemoryQuota: int(quota),
MemoryQuota: quota.MemoryLimit,
MemoryUsage: int(usage),
Spaces: spaces,
}, nil
}

func (cmd *UsageReportCmd) getSpaces(spaceURL string) ([]models.Space, error) {
rawSpaces, err := cmd.apiHelper.GetOrgSpaces(spaceURL)
if nil != err {
if err != nil {
return nil, err
}
var spaces = []models.Space{}
for _, s := range rawSpaces {
apps, services, err := cmd.getAppsAndServices(s.SummaryURL)
if nil != err {
if err != nil {
return nil, err
}
spaces = append(spaces,
Expand All @@ -166,7 +169,7 @@ func (cmd *UsageReportCmd) getSpaces(spaceURL string) ([]models.Space, error) {

func (cmd *UsageReportCmd) getAppsAndServices(summaryURL string) ([]models.App, []models.Service, error) {
apps, services, err := cmd.apiHelper.GetSpaceAppsAndServices(summaryURL)
if nil != err {
if err != nil {
return nil, nil, err
}
return apps, services, nil
Expand Down
17 changes: 17 additions & 0 deletions models/org_quota.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package models

// OrgQuota -
// 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 {
Name string `json:"name"`
TotalServices int `json:"total_services"`
TotalRoutes int `json:"total_routes"`
TotalPrivateDomains int `json:"total_private_domains"`
MemoryLimit int `json:"memory_limit"`
InstanceMemoryLimit int `json:"instance_memory_limit"`
AppInstanceLimit int `json:"app_instance_limit"`
AppTaskLimit int `json:"app_task_limit"`
TotalServiceKeys int `json:"total_service_keys"`
TotalReservedRoutePorts int `json:"total_reserved_route_ports"`
}

0 comments on commit f984711

Please sign in to comment.