github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+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  )
     7  
     8  // Organization represents a V7 actor organization.
     9  type Organization struct {
    10  	// GUID is the unique organization identifier.
    11  	GUID string
    12  	// Name is the name of the organization.
    13  	Name string
    14  
    15  	// Metadata is used for custom tagging of API resources
    16  	Metadata *Metadata
    17  }
    18  
    19  func (actor Actor) GetOrganizations(labelSelector string) ([]Organization, Warnings, error) {
    20  	queries := []ccv3.Query{
    21  		ccv3.Query{Key: ccv3.OrderBy, Values: []string{ccv3.NameOrder}},
    22  	}
    23  	if len(labelSelector) > 0 {
    24  		queries = append(queries, ccv3.Query{Key: ccv3.LabelSelectorFilter, Values: []string{labelSelector}})
    25  	}
    26  	ccOrgs, warnings, err := actor.CloudControllerClient.GetOrganizations(queries...)
    27  	if err != nil {
    28  		return []Organization{}, Warnings(warnings), err
    29  	}
    30  
    31  	orgs := make([]Organization, len(ccOrgs))
    32  	for i, ccOrg := range ccOrgs {
    33  		orgs[i] = actor.convertCCToActorOrganization(ccOrg)
    34  	}
    35  
    36  	return orgs, Warnings(warnings), nil
    37  }
    38  
    39  // GetOrganizationByGUID returns the organization with the given guid.
    40  func (actor Actor) GetOrganizationByGUID(orgGUID string) (Organization, Warnings, error) {
    41  	ccOrg, warnings, err := actor.CloudControllerClient.GetOrganization(orgGUID)
    42  	if err != nil {
    43  		return Organization{}, Warnings(warnings), err
    44  	}
    45  
    46  	return actor.convertCCToActorOrganization(ccOrg), Warnings(warnings), err
    47  }
    48  
    49  // GetOrganizationByName returns the organization with the given name.
    50  func (actor Actor) GetOrganizationByName(name string) (Organization, Warnings, error) {
    51  	orgs, warnings, err := actor.CloudControllerClient.GetOrganizations(
    52  		ccv3.Query{Key: ccv3.NameFilter, Values: []string{name}},
    53  	)
    54  	if err != nil {
    55  		return Organization{}, Warnings(warnings), err
    56  	}
    57  
    58  	if len(orgs) == 0 {
    59  		return Organization{}, Warnings(warnings), actionerror.OrganizationNotFoundError{Name: name}
    60  	}
    61  
    62  	return actor.convertCCToActorOrganization(orgs[0]), Warnings(warnings), nil
    63  }
    64  
    65  // CreateOrganization creates a new organization with the given name
    66  func (actor Actor) CreateOrganization(orgName string) (Organization, Warnings, error) {
    67  	allWarnings := Warnings{}
    68  
    69  	organization, apiWarnings, err := actor.CloudControllerClient.CreateOrganization(orgName)
    70  	allWarnings = append(allWarnings, apiWarnings...)
    71  
    72  	return actor.convertCCToActorOrganization(organization), allWarnings, err
    73  }
    74  
    75  // updateOrganization updates the name and/or labels of an organization
    76  func (actor Actor) updateOrganization(org Organization) (Organization, Warnings, error) {
    77  	ccOrg := ccv3.Organization{
    78  		GUID:     org.GUID,
    79  		Name:     org.Name,
    80  		Metadata: (*ccv3.Metadata)(org.Metadata),
    81  	}
    82  
    83  	updatedOrg, warnings, err := actor.CloudControllerClient.UpdateOrganization(ccOrg)
    84  	if err != nil {
    85  		return Organization{}, Warnings(warnings), err
    86  	}
    87  
    88  	return actor.convertCCToActorOrganization(updatedOrg), Warnings(warnings), nil
    89  }
    90  
    91  func (actor Actor) RenameOrganization(oldOrgName, newOrgName string) (Organization, Warnings, error) {
    92  	var allWarnings Warnings
    93  
    94  	org, warnings, err := actor.GetOrganizationByName(oldOrgName)
    95  	allWarnings = append(allWarnings, warnings...)
    96  	if err != nil {
    97  		return Organization{}, allWarnings, err
    98  	}
    99  
   100  	org.Name = newOrgName
   101  	org, warnings, err = actor.updateOrganization(org)
   102  	allWarnings = append(allWarnings, warnings...)
   103  	if err != nil {
   104  		return Organization{}, allWarnings, err
   105  	}
   106  	return org, allWarnings, nil
   107  }
   108  
   109  func (actor Actor) DeleteOrganization(name string) (Warnings, error) {
   110  	var allWarnings Warnings
   111  
   112  	org, warnings, err := actor.GetOrganizationByName(name)
   113  	allWarnings = append(allWarnings, warnings...)
   114  	if err != nil {
   115  		return allWarnings, err
   116  	}
   117  
   118  	jobURL, deleteWarnings, err := actor.CloudControllerClient.DeleteOrganization(org.GUID)
   119  	allWarnings = append(allWarnings, Warnings(deleteWarnings)...)
   120  	if err != nil {
   121  		return allWarnings, err
   122  	}
   123  
   124  	ccWarnings, err := actor.CloudControllerClient.PollJob(jobURL)
   125  	allWarnings = append(allWarnings, Warnings(ccWarnings)...)
   126  
   127  	return allWarnings, err
   128  }
   129  
   130  func (actor Actor) convertCCToActorOrganization(org ccv3.Organization) Organization {
   131  	return Organization{
   132  		GUID:     org.GUID,
   133  		Name:     org.Name,
   134  		Metadata: (*Metadata)(org.Metadata),
   135  	}
   136  }
   137  
   138  func (actor Actor) GetDefaultDomain(orgGUID string) (Domain, Warnings, error) {
   139  	domain, warnings, err := actor.CloudControllerClient.GetDefaultDomain(orgGUID)
   140  
   141  	return Domain(domain), Warnings(warnings), err
   142  }