github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/v7action/organization.go (about)

     1  package v7action
     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  func (actor Actor) GetOrganizations(labelSelector string) ([]resources.Organization, Warnings, error) {
    10  	queries := []ccv3.Query{
    11  		ccv3.Query{Key: ccv3.OrderBy, Values: []string{ccv3.NameOrder}},
    12  	}
    13  	if len(labelSelector) > 0 {
    14  		queries = append(queries, ccv3.Query{Key: ccv3.LabelSelectorFilter, Values: []string{labelSelector}})
    15  	}
    16  	orgs, warnings, err := actor.CloudControllerClient.GetOrganizations(queries...)
    17  	if err != nil {
    18  		return []resources.Organization{}, Warnings(warnings), err
    19  	}
    20  
    21  	return orgs, Warnings(warnings), nil
    22  }
    23  
    24  // GetOrganizationByGUID returns the organization with the given guid.
    25  func (actor Actor) GetOrganizationByGUID(orgGUID string) (resources.Organization, Warnings, error) {
    26  	ccOrg, warnings, err := actor.CloudControllerClient.GetOrganization(orgGUID)
    27  	if err != nil {
    28  		return resources.Organization{}, Warnings(warnings), err
    29  	}
    30  
    31  	return resources.Organization(ccOrg), Warnings(warnings), err
    32  }
    33  
    34  // GetOrganizationByName returns the organization with the given name.
    35  func (actor Actor) GetOrganizationByName(name string) (resources.Organization, Warnings, error) {
    36  	orgs, warnings, err := actor.CloudControllerClient.GetOrganizations(
    37  		ccv3.Query{Key: ccv3.NameFilter, Values: []string{name}},
    38  	)
    39  	if err != nil {
    40  		return resources.Organization{}, Warnings(warnings), err
    41  	}
    42  
    43  	if len(orgs) == 0 {
    44  		return resources.Organization{}, Warnings(warnings), actionerror.OrganizationNotFoundError{Name: name}
    45  	}
    46  
    47  	return resources.Organization(orgs[0]), Warnings(warnings), nil
    48  }
    49  
    50  // CreateOrganization creates a new organization with the given name
    51  func (actor Actor) CreateOrganization(orgName string) (resources.Organization, Warnings, error) {
    52  	allWarnings := Warnings{}
    53  
    54  	organization, apiWarnings, err := actor.CloudControllerClient.CreateOrganization(orgName)
    55  	allWarnings = append(allWarnings, apiWarnings...)
    56  
    57  	return organization, allWarnings, err
    58  }
    59  
    60  // updateOrganization updates the name and/or labels of an organization
    61  func (actor Actor) updateOrganization(org resources.Organization) (resources.Organization, Warnings, error) {
    62  	updatedOrg, warnings, err := actor.CloudControllerClient.UpdateOrganization(org)
    63  	if err != nil {
    64  		return resources.Organization{}, Warnings(warnings), err
    65  	}
    66  
    67  	return updatedOrg, Warnings(warnings), nil
    68  }
    69  
    70  func (actor Actor) RenameOrganization(oldOrgName, newOrgName string) (resources.Organization, Warnings, error) {
    71  	var allWarnings Warnings
    72  
    73  	org, warnings, err := actor.GetOrganizationByName(oldOrgName)
    74  	allWarnings = append(allWarnings, warnings...)
    75  	if err != nil {
    76  		return resources.Organization{}, allWarnings, err
    77  	}
    78  
    79  	org.Name = newOrgName
    80  	org, warnings, err = actor.updateOrganization(org)
    81  	allWarnings = append(allWarnings, warnings...)
    82  	if err != nil {
    83  		return resources.Organization{}, allWarnings, err
    84  	}
    85  	return org, allWarnings, nil
    86  }
    87  
    88  func (actor Actor) DeleteOrganization(name string) (Warnings, error) {
    89  	var allWarnings Warnings
    90  
    91  	org, warnings, err := actor.GetOrganizationByName(name)
    92  	allWarnings = append(allWarnings, warnings...)
    93  	if err != nil {
    94  		return allWarnings, err
    95  	}
    96  
    97  	jobURL, deleteWarnings, err := actor.CloudControllerClient.DeleteOrganization(org.GUID)
    98  	allWarnings = append(allWarnings, Warnings(deleteWarnings)...)
    99  	if err != nil {
   100  		return allWarnings, err
   101  	}
   102  
   103  	ccWarnings, err := actor.CloudControllerClient.PollJob(jobURL)
   104  	allWarnings = append(allWarnings, Warnings(ccWarnings)...)
   105  
   106  	return allWarnings, err
   107  }
   108  
   109  func (actor Actor) GetDefaultDomain(orgGUID string) (resources.Domain, Warnings, error) {
   110  	domain, warnings, err := actor.CloudControllerClient.GetDefaultDomain(orgGUID)
   111  
   112  	return resources.Domain(domain), Warnings(warnings), err
   113  }