github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/actor/v3action/organization.go (about)

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