github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/actor/v2action/organization.go (about)

     1  package v2action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     8  )
     9  
    10  // Organization represents a CLI Organization.
    11  type Organization ccv2.Organization
    12  
    13  // GetOrganization returns an Organization based on the provided guid.
    14  func (actor Actor) GetOrganization(guid string) (Organization, Warnings, error) {
    15  	org, warnings, err := actor.CloudControllerClient.GetOrganization(guid)
    16  
    17  	if _, ok := err.(ccerror.ResourceNotFoundError); ok {
    18  		return Organization{}, Warnings(warnings), actionerror.OrganizationNotFoundError{GUID: guid}
    19  	}
    20  
    21  	return Organization(org), Warnings(warnings), err
    22  }
    23  
    24  // GetOrganizationByName returns an Organization based off of the name given.
    25  func (actor Actor) GetOrganizationByName(orgName string) (Organization, Warnings, error) {
    26  	orgs, warnings, err := actor.CloudControllerClient.GetOrganizations(ccv2.Filter{
    27  		Type:     constant.NameFilter,
    28  		Operator: constant.EqualOperator,
    29  		Values:   []string{orgName},
    30  	})
    31  	if err != nil {
    32  		return Organization{}, Warnings(warnings), err
    33  	}
    34  
    35  	if len(orgs) == 0 {
    36  		return Organization{}, Warnings(warnings), actionerror.OrganizationNotFoundError{Name: orgName}
    37  	}
    38  
    39  	if len(orgs) > 1 {
    40  		var guids []string
    41  		for _, org := range orgs {
    42  			guids = append(guids, org.GUID)
    43  		}
    44  		return Organization{}, Warnings(warnings), actionerror.MultipleOrganizationsFoundError{Name: orgName, GUIDs: guids}
    45  	}
    46  
    47  	return Organization(orgs[0]), Warnings(warnings), nil
    48  }
    49  
    50  // DeleteOrganization deletes the Organization associated with the provided
    51  // GUID. Once the deletion request is sent, it polls the deletion job until
    52  // it's finished.
    53  func (actor Actor) DeleteOrganization(orgName string) (Warnings, error) {
    54  	var allWarnings Warnings
    55  
    56  	org, warnings, err := actor.GetOrganizationByName(orgName)
    57  	allWarnings = append(allWarnings, warnings...)
    58  	if err != nil {
    59  		return allWarnings, err
    60  	}
    61  
    62  	job, deleteWarnings, err := actor.CloudControllerClient.DeleteOrganizationJob(org.GUID)
    63  	allWarnings = append(allWarnings, deleteWarnings...)
    64  	if err != nil {
    65  		return allWarnings, err
    66  	}
    67  
    68  	ccWarnings, err := actor.CloudControllerClient.PollJob(job)
    69  	for _, warning := range ccWarnings {
    70  		allWarnings = append(allWarnings, warning)
    71  	}
    72  
    73  	return allWarnings, err
    74  }
    75  
    76  func (actor Actor) GetOrganizations() ([]Organization, Warnings, error) {
    77  	var returnedOrgs []Organization
    78  	orgs, warnings, err := actor.CloudControllerClient.GetOrganizations()
    79  	for _, org := range orgs {
    80  		returnedOrgs = append(returnedOrgs, Organization(org))
    81  	}
    82  	return returnedOrgs, Warnings(warnings), err
    83  }