Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consolidate all models into models package #57

Merged
merged 9 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 27 additions & 64 deletions apihelper/apihelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/url"
"strconv"

"github.com/aegershman/cf-usage-report-plugin/models"

"github.com/aegershman/cf-usage-report-plugin/cfcurl"
"github.com/cloudfoundry/cli/plugin"
)
Expand All @@ -15,54 +17,15 @@ var (
ErrOrgNotFound = errors.New("organization not found")
)

// Organization -
type Organization struct {
URL string
Name string
QuotaURL string
SpacesURL string
}

// Space -
type Space struct {
Name string
SummaryURL string
}

// App -
type App struct {
Actual float64
Desire float64
RAM float64
}

// Service -
type Service struct {
Label string
ServicePlan string
}

// Orgs -
type Orgs []Organization

// Spaces -
type Spaces []Space

// Apps -
type Apps []App

// Services -
type Services []Service

// CFAPIHelper wraps cf curl results
type CFAPIHelper interface {
GetTarget() string
GetOrgs() (Orgs, error)
GetOrg(string) (Organization, error)
GetOrgs() (models.Orgs, error)
GetOrg(string) (models.Org, error)
GetQuotaMemoryLimit(string) (float64, error)
GetOrgMemoryUsage(Organization) (float64, error)
GetOrgSpaces(string) (Spaces, error)
GetSpaceAppsAndServices(string) (Apps, Services, error)
GetOrgMemoryUsage(models.Org) (float64, error)
GetOrgSpaces(string) (models.Spaces, error)
GetSpaceAppsAndServices(string) (models.Apps, models.Services, error)
}

// APIHelper -
Expand Down Expand Up @@ -91,13 +54,13 @@ func (api *APIHelper) GetTarget() string {
}

// GetOrgs -
func (api *APIHelper) GetOrgs() (Orgs, error) {
func (api *APIHelper) GetOrgs() (models.Orgs, error) {
orgsJSON, err := cfcurl.Curl(api.cli, "/v2/organizations")
if nil != err {
return nil, err
}
pages := int(orgsJSON["total_pages"].(float64))
orgs := []Organization{}
orgs := models.Orgs{}
for i := 1; i <= pages; i++ {
if 1 != i {
orgsJSON, err = cfcurl.Curl(api.cli, "/v2/organizations?page="+strconv.Itoa(i))
Expand All @@ -111,7 +74,7 @@ func (api *APIHelper) GetOrgs() (Orgs, error) {
}
metadata := theOrg["metadata"].(map[string]interface{})
orgs = append(orgs,
Organization{
models.Org{
Name: name,
URL: metadata["url"].(string),
QuotaURL: entity["quota_definition_url"].(string),
Expand All @@ -123,17 +86,17 @@ func (api *APIHelper) GetOrgs() (Orgs, error) {
}

// GetOrg -
func (api *APIHelper) GetOrg(name string) (Organization, error) {
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 {
return Organization{}, err
return models.Org{}, err
}

results := int(orgsJSON["total_results"].(float64))
if results == 0 {
return Organization{}, ErrOrgNotFound
return models.Org{}, ErrOrgNotFound
}

orgResource := orgsJSON["resources"].([]interface{})[0]
Expand All @@ -142,11 +105,11 @@ func (api *APIHelper) GetOrg(name string) (Organization, error) {
return org, nil
}

func (api *APIHelper) orgResourceToOrg(o interface{}) Organization {
func (api *APIHelper) orgResourceToOrg(o interface{}) models.Org {
theOrg := o.(map[string]interface{})
entity := theOrg["entity"].(map[string]interface{})
metadata := theOrg["metadata"].(map[string]interface{})
return Organization{
return models.Org{
Name: entity["name"].(string),
URL: metadata["url"].(string),
QuotaURL: entity["quota_definition_url"].(string),
Expand All @@ -164,7 +127,7 @@ func (api *APIHelper) GetQuotaMemoryLimit(quotaURL string) (float64, error) {
}

// GetOrgMemoryUsage returns amount of memory (in MB) a given org is currently using
func (api *APIHelper) GetOrgMemoryUsage(org Organization) (float64, error) {
func (api *APIHelper) GetOrgMemoryUsage(org models.Org) (float64, error) {
usageJSON, err := cfcurl.Curl(api.cli, org.URL+"/memory_usage")
if nil != err {
return 0, err
Expand All @@ -173,9 +136,9 @@ func (api *APIHelper) GetOrgMemoryUsage(org Organization) (float64, error) {
}

// GetOrgSpaces returns the spaces in an org
func (api *APIHelper) GetOrgSpaces(spacesURL string) (Spaces, error) {
func (api *APIHelper) GetOrgSpaces(spacesURL string) (models.Spaces, error) {
nextURL := spacesURL
spaces := []Space{}
spaces := models.Spaces{}
for nextURL != "" {
spacesJSON, err := cfcurl.Curl(api.cli, nextURL)
if nil != err {
Expand All @@ -186,7 +149,7 @@ func (api *APIHelper) GetOrgSpaces(spacesURL string) (Spaces, error) {
metadata := theSpace["metadata"].(map[string]interface{})
entity := theSpace["entity"].(map[string]interface{})
spaces = append(spaces,
Space{
models.Space{
Name: entity["name"].(string),
SummaryURL: metadata["url"].(string) + "/summary",
})
Expand All @@ -212,9 +175,9 @@ func (api *APIHelper) GetOrgSpaces(spacesURL string) (Spaces, error) {
// Granted, on the other hand, this isn't the "cf go library" so if it makes opinionated
// decisions about what to return it's not the end of the world. But even still, we probably
// don't want to make those decisions here. We'll want to make them in a specific view.
func (api *APIHelper) GetSpaceAppsAndServices(summaryURL string) (Apps, Services, error) {
apps := []App{}
services := []Service{}
func (api *APIHelper) GetSpaceAppsAndServices(summaryURL string) (models.Apps, models.Services, error) {
apps := models.Apps{}
services := models.Services{}
summaryJSON, err := cfcurl.Curl(api.cli, summaryURL)
if nil != err {
return nil, nil, err
Expand All @@ -223,10 +186,10 @@ func (api *APIHelper) GetSpaceAppsAndServices(summaryURL string) (Apps, Services
for _, a := range summaryJSON["apps"].([]interface{}) {
theApp := a.(map[string]interface{})
apps = append(apps,
App{
Actual: theApp["running_instances"].(float64),
Desire: theApp["instances"].(float64),
RAM: theApp["memory"].(float64),
models.App{
RunningInstances: int(theApp["running_instances"].(float64)),
Instances: int(theApp["instances"].(float64)),
Memory: int(theApp["memory"].(float64)),
})
}
}
Expand All @@ -248,7 +211,7 @@ func (api *APIHelper) GetSpaceAppsAndServices(summaryURL string) (Apps, Services
// and then act as though the only services that exist in
// a space are the ones that have passed the filter
services = append(services,
Service{
models.Service{
Label: label,
ServicePlan: servicePlan["name"].(string),
})
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ require (
github.com/onsi/ginkgo v1.10.3 // indirect
github.com/onsi/gomega v1.7.1 // indirect
github.com/sirupsen/logrus v1.4.2
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 // indirect
golang.org/x/sys v0.0.0-20191008105621-543471e840be // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
14 changes: 3 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (cmd *UsageReportCmd) GetMetadata() plugin.PluginMetadata {
Version: plugin.VersionType{
Major: 2,
Minor: 8,
Build: 1,
Build: 2,
},
Commands: []plugin.Command{
{
Expand Down Expand Up @@ -133,7 +133,7 @@ func (cmd *UsageReportCmd) getOrg(name string) (models.Org, error) {
return cmd.getOrgDetails(rawOrg)
}

func (cmd *UsageReportCmd) getOrgDetails(o apihelper.Organization) (models.Org, error) {
func (cmd *UsageReportCmd) getOrgDetails(o models.Org) (models.Org, error) {
usage, err := cmd.apiHelper.GetOrgMemoryUsage(o)
if nil != err {
return models.Org{}, err
Expand Down Expand Up @@ -178,19 +178,11 @@ func (cmd *UsageReportCmd) getSpaces(spaceURL string) ([]models.Space, error) {
}

func (cmd *UsageReportCmd) getAppsAndServices(summaryURL string) ([]models.App, []models.Service, error) {
rawApps, rawServices, err := cmd.apiHelper.GetSpaceAppsAndServices(summaryURL)
apps, rawServices, err := cmd.apiHelper.GetSpaceAppsAndServices(summaryURL)
if nil != err {
return nil, nil, err
}
var apps = []models.App{}
var services = []models.Service{}
for _, a := range rawApps {
apps = append(apps, models.App{
Actual: int(a.Actual),
Desire: int(a.Desire),
RAM: int(a.RAM),
})
}
for _, s := range rawServices {
services = append(services, models.Service{
Label: string(s.Label),
Expand Down
11 changes: 11 additions & 0 deletions models/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package models

// App -
type App struct {
RunningInstances int
Instances int
Memory int
}

// Apps -
type Apps []App
3 changes: 3 additions & 0 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ type Org struct {
MemoryQuota int
MemoryUsage int
Spaces Spaces
QuotaURL string
SpacesURL string
URL string
}

// Orgs -
Expand Down
10 changes: 10 additions & 0 deletions models/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

// Service -
type Service struct {
Label string
ServicePlan string
}

// Services -
type Services []Service
34 changes: 8 additions & 26 deletions models/space.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
package models

// App -
type App struct {
Actual int
Desire int
RAM int
}

// Apps -
type Apps []App

// Service -
type Service struct {
Label string
ServicePlan string
}

// Services -
type Services []Service

// Space -
type Space struct {
Name string
Apps Apps
Services Services
Name string
Apps Apps
Services Services
SummaryURL string
}

// Spaces -
Expand All @@ -34,7 +16,7 @@ type Spaces []Space
func (space *Space) ConsumedMemory() int {
count := 0
for _, app := range space.Apps {
count += int(app.Actual * app.RAM)
count += int(app.RunningInstances * app.Memory)
}
return count
}
Expand Down Expand Up @@ -67,7 +49,7 @@ func (space *Space) AppsCount() int {
func (space *Space) RunningAppsCount() int {
count := 0
for _, app := range space.Apps {
if app.Actual > 0 {
if app.RunningInstances > 0 {
count++
}
}
Expand All @@ -87,7 +69,7 @@ func (space *Space) RunningAppsCount() int {
func (space *Space) AppInstancesCount() int {
count := 0
for _, app := range space.Apps {
count += int(app.Desire)
count += int(app.Instances)
}
return count
}
Expand All @@ -105,7 +87,7 @@ func (space *Space) AppInstancesCount() int {
func (space *Space) RunningAppInstancesCount() int {
count := 0
for _, app := range space.Apps {
count += int(app.Actual)
count += int(app.RunningInstances)
}
return count
}
Expand Down