//GetOrgs returns a struct that represents critical fields in the JSON
func (api *APIHelper) GetOrgs(cli plugin.CliConnection) ([]Organization, error) {
	orgsJSON, err := cfcurl.Curl(cli, "/v2/organizations")
	if nil != err {
		return nil, err
	}
	pages := int(orgsJSON["total_pages"].(float64))
	orgs := []Organization{}
	for i := 1; i <= pages; i++ {
		if 1 != i {
			orgsJSON, err = cfcurl.Curl(cli, "/v2/organizations?page="+strconv.Itoa(i))
		}
		for _, o := range orgsJSON["resources"].([]interface{}) {
			theOrg := o.(map[string]interface{})
			entity := theOrg["entity"].(map[string]interface{})
			metadata := theOrg["metadata"].(map[string]interface{})
			orgs = append(orgs,
				Organization{
					Name:      entity["name"].(string),
					URL:       metadata["url"].(string),
					QuotaURL:  entity["quota_definition_url"].(string),
					SpacesURL: entity["spaces_url"].(string),
				})
		}
	}
	return orgs, nil
}
//GetOrgMemoryUsage returns the amount of memory (in MB) that the org is consuming
func (api *APIHelper) GetOrgMemoryUsage(cli plugin.CliConnection, org Organization) (float64, error) {
	usageJSON, err := cfcurl.Curl(cli, org.URL+"/memory_usage")
	if nil != err {
		return 0, err
	}
	return usageJSON["memory_usage_in_mb"].(float64), nil
}
//GetQuotaMemoryLimit retruns the amount of memory (in MB) that the org is allowed
func (api *APIHelper) GetQuotaMemoryLimit(cli plugin.CliConnection, quotaURL string) (float64, error) {
	quotaJSON, err := cfcurl.Curl(cli, quotaURL)
	if nil != err {
		return 0, err
	}
	return quotaJSON["entity"].(map[string]interface{})["memory_limit"].(float64), nil
}
Exemple #4
0
//Run a command
func (cmd *TestCmd) Run(cliConnection plugin.CliConnection, args []string) {
	out, _ := cfcurl.Curl(cliConnection, "/v2/apps")
	fmt.Println(out)

	out, _ = cfcurl.CurlDepricated(cliConnection, "/v2/domains")
	fmt.Println(out)
}
//Base method to process paged results from API calls
func processPagedResults(cli plugin.CliConnection, url string, fn process) ([]interface{}, error) {

	theJSON, err := cfcurl.Curl(cli, url)
	if nil != err {
		return nil, err
	}

	pages := int(theJSON["total_pages"].(float64))
	var objects []interface{}
	for i := 1; i <= pages; i++ {
		if 1 != i {
			theJSON, err = cfcurl.Curl(cli, url+"?page="+strconv.Itoa(i))
		}
		for _, o := range theJSON["resources"].([]interface{}) {
			theObj := o.(map[string]interface{})
			entity := theObj["entity"].(map[string]interface{})
			metadata := theObj["metadata"].(map[string]interface{})
			objects = append(objects, fn(metadata, entity))
		}

	}

	return objects, nil
}
//GetSpace returns a struct that represents critical fields in the JSON
func (api *APIHelper) GetOrganization(cli plugin.CliConnection, organizationURL string) (Organization, error) {
	theJSON, err := cfcurl.Curl(cli, organizationURL)
	if nil != err {
		return Organization{}, err
	}

	entity := theJSON["entity"].(map[string]interface{})
	metadata := theJSON["metadata"].(map[string]interface{})

	organization := Organization{
		Name: entity["name"].(string),
		URL:  metadata["url"].(string),
	}

	return organization, nil
}
//GetOrgSpaces returns the spaces in an org.
func (api *APIHelper) GetOrgSpaces(cli plugin.CliConnection, spacesURL string) ([]Space, error) {
	spacesJSON, err := cfcurl.Curl(cli, spacesURL)
	if nil != err {
		return nil, err
	}
	spaces := []Space{}
	for _, s := range spacesJSON["resources"].([]interface{}) {
		theSpace := s.(map[string]interface{})
		entity := theSpace["entity"].(map[string]interface{})
		spaces = append(spaces,
			Space{
				AppsURL: entity["apps_url"].(string),
				Name:    entity["name"].(string),
			})
	}
	return spaces, nil
}
//GetSpaceApps returns the apps in a space
func (api *APIHelper) GetSpaceApps(cli plugin.CliConnection, appsURL string) ([]App, error) {
	appsJSON, err := cfcurl.Curl(cli, appsURL)
	if nil != err {
		return nil, err
	}
	apps := []App{}
	for _, a := range appsJSON["resources"].([]interface{}) {
		theApp := a.(map[string]interface{})
		entity := theApp["entity"].(map[string]interface{})
		apps = append(apps,
			App{
				Instances: entity["instances"].(float64),
				RAM:       entity["memory"].(float64),
			})
	}
	return apps, nil
}
//GetSpace returns a struct that represents critical fields in the JSON
func (api *APIHelper) GetSpace(cli plugin.CliConnection, spaceURL string) (Space, error) {
	theJSON, err := cfcurl.Curl(cli, spaceURL)
	if nil != err {
		return Space{}, err
	}

	entity := theJSON["entity"].(map[string]interface{})
	metadata := theJSON["metadata"].(map[string]interface{})

	space := Space{
		Name:            entity["name"].(string),
		URL:             metadata["url"].(string),
		OrganizationURL: entity["organization_url"].(string),
	}

	return space, nil
}
//GetOrg returns a struct that represents critical fields in the JSON
func (api *APIHelper) GetOrg(name string) (Organization, error) {
	query := fmt.Sprintf("name:%s", name)
	path := fmt.Sprintf("/v2/organizations?q=%s&inline-relations-depth=1", url.QueryEscape(query))
	orgsJSON, err := cfcurl.Curl(api.cli, path)
	if nil != err {
		return Organization{}, err
	}

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

	orgResource := orgsJSON["resources"].([]interface{})[0]
	org := api.orgResourceToOrg(orgResource)

	return org, nil
}
//GetOrgs returns a struct that represents critical fields in the JSON
func (api *APIHelper) GetOrgs(cli plugin.CliConnection) ([]Organization, error) {
	orgsJSON, err := cfcurl.Curl(cli, "/v2/organizations")
	if nil != err {
		return nil, err
	}

	orgs := []Organization{}
	for _, o := range orgsJSON["resources"].([]interface{}) {
		theOrg := o.(map[string]interface{})
		entity := theOrg["entity"].(map[string]interface{})
		metadata := theOrg["metadata"].(map[string]interface{})
		orgs = append(orgs,
			Organization{
				Name:      entity["name"].(string),
				URL:       metadata["url"].(string),
				QuotaURL:  entity["quota_definition_url"].(string),
				SpacesURL: entity["spaces_url"].(string),
			})
	}
	return orgs, nil
}
//GetSpaceApps returns the apps in a space
func (api *APIHelper) GetSpaceApps(cli plugin.CliConnection, appsURL string) ([]App, error) {
	appsJSON, err := cfcurl.Curl(cli, appsURL)
	if nil != err {
		return nil, err
	}

	apps := []App{}
	for _, a := range appsJSON["resources"].([]interface{}) {
		theApp := a.(map[string]interface{})
		entity := theApp["entity"].(map[string]interface{})

		bp := " "
		bpd := " "

		if entity["buildpack"] == nil {
			bp = "Not detected"
		} else {
			bp = entity["buildpack"].(string)
		}

		if entity["detected_buildpack"] == nil {
			bpd = "Not detected"
		} else {
			bpd = entity["detected_buildpack"].(string)
		}

		apps = append(apps,
			App{
				Instances:         entity["instances"].(float64),
				RAM:               entity["memory"].(float64),
				Running:           "STARTED" == entity["state"].(string),
				Name:              entity["name"].(string),
				Buildpack:         bp,
				BuildpackDetected: bpd,
			})
	}
	return apps, nil
}