github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/actor/v3action/organization.go (about)

     1  package v3action
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     8  )
     9  
    10  // Organization represents a V3 actor organization.
    11  type Organization ccv3.Organization
    12  
    13  // OrganizationNotFoundError represents the error that occurs when the
    14  // organization is not found.
    15  type OrganizationNotFoundError struct {
    16  	Name string
    17  }
    18  
    19  func (e OrganizationNotFoundError) Error() string {
    20  	return fmt.Sprintf("Organization '%s' not found.", e.Name)
    21  }
    22  
    23  // GetOrganizationByName returns the organization with the given name.
    24  func (actor Actor) GetOrganizationByName(name string) (Organization, Warnings, error) {
    25  	orgs, warnings, err := actor.CloudControllerClient.GetOrganizations(url.Values{
    26  		ccv3.NameFilter: []string{name},
    27  	})
    28  	if err != nil {
    29  		return Organization{}, Warnings(warnings), err
    30  	}
    31  
    32  	if len(orgs) == 0 {
    33  		return Organization{}, Warnings(warnings), OrganizationNotFoundError{Name: name}
    34  	}
    35  
    36  	return Organization(orgs[0]), Warnings(warnings), nil
    37  }