github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+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 "github.com/cloudfoundry/cli/cf/api/resources" 10 "github.com/cloudfoundry/cli/cf/configuration/core_config" 11 "github.com/cloudfoundry/cli/cf/errors" 12 "github.com/cloudfoundry/cli/cf/models" 13 "github.com/cloudfoundry/cli/cf/net" 14 ) 15 16 type SpaceRepository interface { 17 ListSpaces(func(models.Space) bool) error 18 FindByName(name string) (space models.Space, apiErr error) 19 FindByNameInOrg(name, orgGuid string) (space models.Space, apiErr error) 20 Create(name string, orgGuid string, spaceQuotaGuid string) (space models.Space, apiErr error) 21 Rename(spaceGuid, newName string) (apiErr error) 22 Delete(spaceGuid string) (apiErr error) 23 } 24 25 type CloudControllerSpaceRepository struct { 26 config core_config.Reader 27 gateway net.Gateway 28 } 29 30 func NewCloudControllerSpaceRepository(config core_config.Reader, gateway net.Gateway) (repo CloudControllerSpaceRepository) { 31 repo.config = config 32 repo.gateway = gateway 33 return 34 } 35 36 func (repo CloudControllerSpaceRepository) ListSpaces(callback func(models.Space) bool) error { 37 return repo.gateway.ListPaginatedResources( 38 repo.config.ApiEndpoint(), 39 fmt.Sprintf("/v2/organizations/%s/spaces?inline-relations-depth=1", repo.config.OrganizationFields().Guid), 40 resources.SpaceResource{}, 41 func(resource interface{}) bool { 42 return callback(resource.(resources.SpaceResource).ToModel()) 43 }) 44 } 45 46 func (repo CloudControllerSpaceRepository) FindByName(name string) (space models.Space, apiErr error) { 47 return repo.FindByNameInOrg(name, repo.config.OrganizationFields().Guid) 48 } 49 50 func (repo CloudControllerSpaceRepository) FindByNameInOrg(name, orgGuid string) (space models.Space, apiErr error) { 51 foundSpace := false 52 apiErr = repo.gateway.ListPaginatedResources( 53 repo.config.ApiEndpoint(), 54 fmt.Sprintf("/v2/organizations/%s/spaces?q=%s&inline-relations-depth=1", orgGuid, url.QueryEscape("name:"+strings.ToLower(name))), 55 resources.SpaceResource{}, 56 func(resource interface{}) bool { 57 space = resource.(resources.SpaceResource).ToModel() 58 foundSpace = true 59 return false 60 }) 61 62 if !foundSpace { 63 apiErr = errors.NewModelNotFoundError("Space", name) 64 } 65 66 return 67 } 68 69 func (repo CloudControllerSpaceRepository) Create(name, orgGuid, spaceQuotaGuid string) (space models.Space, apiErr error) { 70 path := "/v2/spaces?inline-relations-depth=1" 71 72 bodyMap := map[string]string{"name": name, "organization_guid": orgGuid} 73 if spaceQuotaGuid != "" { 74 bodyMap["space_quota_definition_guid"] = spaceQuotaGuid 75 } 76 77 body, apiErr := json.Marshal(bodyMap) 78 if apiErr != nil { 79 return 80 } 81 82 resource := new(resources.SpaceResource) 83 apiErr = repo.gateway.CreateResource(repo.config.ApiEndpoint(), path, strings.NewReader(string(body)), resource) 84 if apiErr != nil { 85 return 86 } 87 space = resource.ToModel() 88 return 89 } 90 91 func (repo CloudControllerSpaceRepository) Rename(spaceGuid, newName string) (apiErr error) { 92 path := fmt.Sprintf("/v2/spaces/%s", spaceGuid) 93 body := fmt.Sprintf(`{"name":"%s"}`, newName) 94 return repo.gateway.UpdateResource(repo.config.ApiEndpoint(), path, strings.NewReader(body)) 95 } 96 97 func (repo CloudControllerSpaceRepository) Delete(spaceGuid string) (apiErr error) { 98 path := fmt.Sprintf("/v2/spaces/%s?recursive=true", spaceGuid) 99 return repo.gateway.DeleteResource(repo.config.ApiEndpoint(), path) 100 }