github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/spaces/spaces.go (about)

     1  package spaces
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/url"
     7  	"strings"
     8  
     9  	"code.cloudfoundry.org/cli/cf/api/resources"
    10  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    11  	"code.cloudfoundry.org/cli/cf/errors"
    12  	"code.cloudfoundry.org/cli/cf/models"
    13  	"code.cloudfoundry.org/cli/cf/net"
    14  )
    15  
    16  //go:generate counterfeiter . SpaceRepository
    17  
    18  type SpaceRepository interface {
    19  	ListSpaces(func(models.Space) bool) error
    20  	ListSpacesFromOrg(orgGUID string, spaceFunc func(models.Space) bool) error
    21  	FindByName(name string) (space models.Space, apiErr error)
    22  	FindByNameInOrg(name, orgGUID string) (space models.Space, apiErr error)
    23  	Create(name string, orgGUID string, spaceQuotaGUID string) (space models.Space, apiErr error)
    24  	Rename(spaceGUID, newName string) (apiErr error)
    25  	SetAllowSSH(spaceGUID string, allow bool) (apiErr error)
    26  	Delete(spaceGUID string) (apiErr error)
    27  }
    28  
    29  type CloudControllerSpaceRepository struct {
    30  	config  coreconfig.Reader
    31  	gateway net.Gateway
    32  }
    33  
    34  func NewCloudControllerSpaceRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerSpaceRepository) {
    35  	repo.config = config
    36  	repo.gateway = gateway
    37  	return
    38  }
    39  
    40  func (repo CloudControllerSpaceRepository) ListSpaces(callback func(models.Space) bool) error {
    41  	return repo.gateway.ListPaginatedResources(
    42  		repo.config.APIEndpoint(),
    43  		fmt.Sprintf("/v2/organizations/%s/spaces?order-by=name&inline-relations-depth=1", repo.config.OrganizationFields().GUID),
    44  		resources.SpaceResource{},
    45  		func(resource interface{}) bool {
    46  			return callback(resource.(resources.SpaceResource).ToModel())
    47  		})
    48  }
    49  
    50  func (repo CloudControllerSpaceRepository) ListSpacesFromOrg(orgGUID string, callback func(models.Space) bool) error {
    51  	return repo.gateway.ListPaginatedResources(
    52  		repo.config.APIEndpoint(),
    53  		fmt.Sprintf("/v2/organizations/%s/spaces?order-by=name&inline-relations-depth=1", orgGUID),
    54  		resources.SpaceResource{},
    55  		func(resource interface{}) bool {
    56  			return callback(resource.(resources.SpaceResource).ToModel())
    57  		})
    58  }
    59  
    60  func (repo CloudControllerSpaceRepository) FindByName(name string) (space models.Space, apiErr error) {
    61  	return repo.FindByNameInOrg(name, repo.config.OrganizationFields().GUID)
    62  }
    63  
    64  func (repo CloudControllerSpaceRepository) FindByNameInOrg(name, orgGUID string) (space models.Space, apiErr error) {
    65  	foundSpace := false
    66  	apiErr = repo.gateway.ListPaginatedResources(
    67  		repo.config.APIEndpoint(),
    68  		fmt.Sprintf("/v2/organizations/%s/spaces?q=%s&inline-relations-depth=1", orgGUID, url.QueryEscape("name:"+strings.ToLower(name))),
    69  		resources.SpaceResource{},
    70  		func(resource interface{}) bool {
    71  			space = resource.(resources.SpaceResource).ToModel()
    72  			foundSpace = true
    73  			return false
    74  		})
    75  
    76  	if !foundSpace {
    77  		apiErr = errors.NewModelNotFoundError("Space", name)
    78  	}
    79  
    80  	return
    81  }
    82  
    83  func (repo CloudControllerSpaceRepository) Create(name, orgGUID, spaceQuotaGUID string) (models.Space, error) {
    84  	var space models.Space
    85  	path := "/v2/spaces?inline-relations-depth=1"
    86  
    87  	bodyMap := map[string]string{"name": name, "organization_guid": orgGUID}
    88  	if spaceQuotaGUID != "" {
    89  		bodyMap["space_quota_definition_guid"] = spaceQuotaGUID
    90  	}
    91  
    92  	body, err := json.Marshal(bodyMap)
    93  	if err != nil {
    94  		return models.Space{}, err
    95  	}
    96  
    97  	resource := new(resources.SpaceResource)
    98  	err = repo.gateway.CreateResource(repo.config.APIEndpoint(), path, strings.NewReader(string(body)), resource)
    99  	if err != nil {
   100  		return models.Space{}, err
   101  	}
   102  	space = resource.ToModel()
   103  	return space, nil
   104  }
   105  
   106  func (repo CloudControllerSpaceRepository) Rename(spaceGUID, newName string) (apiErr error) {
   107  	path := fmt.Sprintf("/v2/spaces/%s", spaceGUID)
   108  	body := fmt.Sprintf(`{"name":"%s"}`, newName)
   109  	return repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, strings.NewReader(body))
   110  }
   111  
   112  func (repo CloudControllerSpaceRepository) SetAllowSSH(spaceGUID string, allow bool) (apiErr error) {
   113  	path := fmt.Sprintf("/v2/spaces/%s", spaceGUID)
   114  	body := fmt.Sprintf(`{"allow_ssh":%t}`, allow)
   115  	return repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, strings.NewReader(body))
   116  }
   117  
   118  func (repo CloudControllerSpaceRepository) Delete(spaceGUID string) (apiErr error) {
   119  	path := fmt.Sprintf("/v2/spaces/%s?recursive=true", spaceGUID)
   120  	return repo.gateway.DeleteResource(repo.config.APIEndpoint(), path)
   121  }