github.com/cloudfoundry/cli@v7.1.0+incompatible/actor/v3action/organization.go (about)

     1  package v3action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     6  	"code.cloudfoundry.org/cli/resources"
     7  )
     8  
     9  // Organization represents a V3 actor organization.
    10  type Organization resources.Organization
    11  
    12  // GetOrganizationByName returns the organization with the given name.
    13  func (actor Actor) GetOrganizationByName(name string) (Organization, Warnings, error) {
    14  	orgs, warnings, err := actor.CloudControllerClient.GetOrganizations(
    15  		ccv3.Query{Key: ccv3.NameFilter, Values: []string{name}},
    16  	)
    17  	if err != nil {
    18  		return Organization{}, Warnings(warnings), err
    19  	}
    20  
    21  	if len(orgs) == 0 {
    22  		return Organization{}, Warnings(warnings), actionerror.OrganizationNotFoundError{Name: name}
    23  	}
    24  
    25  	return Organization(orgs[0]), Warnings(warnings), nil
    26  }
    27  
    28  func (actor Actor) GetOrganizationsByGUIDs(guids ...string) ([]Organization, Warnings, error) {
    29  	queries := []ccv3.Query{}
    30  	if len(guids) > 0 {
    31  		queries = []ccv3.Query{ccv3.Query{Key: ccv3.GUIDFilter, Values: guids}}
    32  	}
    33  
    34  	orgs, warnings, err := actor.CloudControllerClient.GetOrganizations(queries...)
    35  	if err != nil {
    36  		return []Organization{}, Warnings(warnings), err
    37  	}
    38  
    39  	guidToOrg := make(map[string]resources.Organization)
    40  	for _, org := range orgs {
    41  		guidToOrg[org.GUID] = org
    42  	}
    43  
    44  	filteredOrgs := make([]resources.Organization, 0)
    45  	for _, guid := range guids {
    46  		filteredOrgs = append(filteredOrgs, guidToOrg[guid])
    47  	}
    48  	orgs = filteredOrgs
    49  
    50  	return convertCCToActorOrganizations(orgs), Warnings(warnings), nil
    51  }
    52  
    53  func (actor Actor) GetOrganizations() ([]Organization, Warnings, error) {
    54  	orderBy := ccv3.Query{
    55  		Key:    "order_by",
    56  		Values: []string{"name"},
    57  	}
    58  	orgs, warnings, err := actor.CloudControllerClient.GetOrganizations(orderBy)
    59  	if err != nil {
    60  		return []Organization{}, Warnings(warnings), err
    61  	}
    62  	return convertCCToActorOrganizations(orgs), Warnings(warnings), nil
    63  }
    64  
    65  func convertCCToActorOrganizations(v3orgs []resources.Organization) []Organization {
    66  	orgs := make([]Organization, len(v3orgs))
    67  	for i := range v3orgs {
    68  		orgs[i] = Organization(v3orgs[i])
    69  	}
    70  	return orgs
    71  }