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

     1  package organizations
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strings"
     7  
     8  	"code.cloudfoundry.org/cli/cf/api/resources"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/errors"
    11  	"code.cloudfoundry.org/cli/cf/models"
    12  	"code.cloudfoundry.org/cli/cf/net"
    13  )
    14  
    15  //go:generate counterfeiter . OrganizationRepository
    16  
    17  type OrganizationRepository interface {
    18  	ListOrgs(limit int) ([]models.Organization, error)
    19  	GetManyOrgsByGUID(orgGUIDs []string) (orgs []models.Organization, apiErr error)
    20  	FindByName(name string) (org models.Organization, apiErr error)
    21  	Create(org models.Organization) (apiErr error)
    22  	Rename(orgGUID string, name string) (apiErr error)
    23  	Delete(orgGUID string) (apiErr error)
    24  	SharePrivateDomain(orgGUID string, domainGUID string) (apiErr error)
    25  	UnsharePrivateDomain(orgGUID string, domainGUID string) (apiErr error)
    26  }
    27  
    28  type CloudControllerOrganizationRepository struct {
    29  	config  coreconfig.Reader
    30  	gateway net.Gateway
    31  }
    32  
    33  func NewCloudControllerOrganizationRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerOrganizationRepository) {
    34  	repo.config = config
    35  	repo.gateway = gateway
    36  	return
    37  }
    38  
    39  func (repo CloudControllerOrganizationRepository) ListOrgs(limit int) ([]models.Organization, error) {
    40  	orgs := []models.Organization{}
    41  	err := repo.gateway.ListPaginatedResources(
    42  		repo.config.APIEndpoint(),
    43  		"/v2/organizations?order-by=name",
    44  		resources.OrganizationResource{},
    45  		func(resource interface{}) bool {
    46  			if orgResource, ok := resource.(resources.OrganizationResource); ok {
    47  				orgs = append(orgs, orgResource.ToModel())
    48  				return limit == 0 || len(orgs) < limit
    49  			}
    50  			return false
    51  		},
    52  	)
    53  	return orgs, err
    54  }
    55  
    56  func (repo CloudControllerOrganizationRepository) GetManyOrgsByGUID(orgGUIDs []string) (orgs []models.Organization, err error) {
    57  	for _, orgGUID := range orgGUIDs {
    58  		url := fmt.Sprintf("%s/v2/organizations/%s", repo.config.APIEndpoint(), orgGUID)
    59  		orgResource := resources.OrganizationResource{}
    60  		err = repo.gateway.GetResource(url, &orgResource)
    61  		if err != nil {
    62  			return nil, err
    63  		}
    64  		orgs = append(orgs, orgResource.ToModel())
    65  	}
    66  	return
    67  }
    68  
    69  func (repo CloudControllerOrganizationRepository) FindByName(name string) (org models.Organization, apiErr error) {
    70  	found := false
    71  	apiErr = repo.gateway.ListPaginatedResources(
    72  		repo.config.APIEndpoint(),
    73  		fmt.Sprintf("/v2/organizations?q=%s&inline-relations-depth=1", url.QueryEscape("name:"+strings.ToLower(name))),
    74  		resources.OrganizationResource{},
    75  		func(resource interface{}) bool {
    76  			org = resource.(resources.OrganizationResource).ToModel()
    77  			found = true
    78  			return false
    79  		})
    80  
    81  	if apiErr == nil && !found {
    82  		apiErr = errors.NewModelNotFoundError("Organization", name)
    83  	}
    84  
    85  	return
    86  }
    87  
    88  func (repo CloudControllerOrganizationRepository) Create(org models.Organization) (apiErr error) {
    89  	data := fmt.Sprintf(`{"name":"%s"`, org.Name)
    90  	if org.QuotaDefinition.GUID != "" {
    91  		data = data + fmt.Sprintf(`, "quota_definition_guid":"%s"`, org.QuotaDefinition.GUID)
    92  	}
    93  	data = data + "}"
    94  	return repo.gateway.CreateResource(repo.config.APIEndpoint(), "/v2/organizations", strings.NewReader(data))
    95  }
    96  
    97  func (repo CloudControllerOrganizationRepository) Rename(orgGUID string, name string) (apiErr error) {
    98  	url := fmt.Sprintf("/v2/organizations/%s", orgGUID)
    99  	data := fmt.Sprintf(`{"name":"%s"}`, name)
   100  	return repo.gateway.UpdateResource(repo.config.APIEndpoint(), url, strings.NewReader(data))
   101  }
   102  
   103  func (repo CloudControllerOrganizationRepository) Delete(orgGUID string) (apiErr error) {
   104  	url := fmt.Sprintf("/v2/organizations/%s?recursive=true", orgGUID)
   105  	return repo.gateway.DeleteResource(repo.config.APIEndpoint(), url)
   106  }
   107  
   108  func (repo CloudControllerOrganizationRepository) SharePrivateDomain(orgGUID string, domainGUID string) error {
   109  	url := fmt.Sprintf("/v2/organizations/%s/private_domains/%s", orgGUID, domainGUID)
   110  	return repo.gateway.UpdateResource(repo.config.APIEndpoint(), url, nil)
   111  }
   112  
   113  func (repo CloudControllerOrganizationRepository) UnsharePrivateDomain(orgGUID string, domainGUID string) error {
   114  	url := fmt.Sprintf("/v2/organizations/%s/private_domains/%s", orgGUID, domainGUID)
   115  	return repo.gateway.DeleteResource(repo.config.APIEndpoint(), url)
   116  }