github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/api/organizations/organizations.go (about) 1 package organizations 2 3 import ( 4 "fmt" 5 "net/url" 6 "strings" 7 8 "github.com/cloudfoundry/cli/cf/api/resources" 9 "github.com/cloudfoundry/cli/cf/configuration/core_config" 10 "github.com/cloudfoundry/cli/cf/errors" 11 "github.com/cloudfoundry/cli/cf/models" 12 "github.com/cloudfoundry/cli/cf/net" 13 ) 14 15 type OrganizationRepository interface { 16 ListOrgs() (orgs []models.Organization, apiErr error) 17 FindByName(name string) (org models.Organization, apiErr error) 18 Create(org models.Organization) (apiErr error) 19 Rename(orgGuid string, name string) (apiErr error) 20 Delete(orgGuid string) (apiErr error) 21 } 22 23 type CloudControllerOrganizationRepository struct { 24 config core_config.Reader 25 gateway net.Gateway 26 } 27 28 func NewCloudControllerOrganizationRepository(config core_config.Reader, gateway net.Gateway) (repo CloudControllerOrganizationRepository) { 29 repo.config = config 30 repo.gateway = gateway 31 return 32 } 33 34 func (repo CloudControllerOrganizationRepository) ListOrgs() ([]models.Organization, error) { 35 orgs := []models.Organization{} 36 err := repo.gateway.ListPaginatedResources( 37 repo.config.ApiEndpoint(), 38 "/v2/organizations", 39 resources.OrganizationResource{}, 40 func(resource interface{}) bool { 41 orgResource, ok := resource.(resources.OrganizationResource) 42 if ok { 43 orgs = append(orgs, orgResource.ToModel()) 44 return true 45 } else { 46 return false 47 } 48 }) 49 return orgs, err 50 } 51 52 func (repo CloudControllerOrganizationRepository) FindByName(name string) (org models.Organization, apiErr error) { 53 found := false 54 apiErr = repo.gateway.ListPaginatedResources( 55 repo.config.ApiEndpoint(), 56 fmt.Sprintf("/v2/organizations?q=%s&inline-relations-depth=1", url.QueryEscape("name:"+strings.ToLower(name))), 57 resources.OrganizationResource{}, 58 func(resource interface{}) bool { 59 org = resource.(resources.OrganizationResource).ToModel() 60 found = true 61 return false 62 }) 63 64 if apiErr == nil && !found { 65 apiErr = errors.NewModelNotFoundError("Organization", name) 66 } 67 68 return 69 } 70 71 func (repo CloudControllerOrganizationRepository) Create(org models.Organization) (apiErr error) { 72 data := fmt.Sprintf(`{"name":"%s"`, org.Name) 73 if org.QuotaDefinition.Guid != "" { 74 data = data + fmt.Sprintf(`, "quota_definition_guid":"%s"`, org.QuotaDefinition.Guid) 75 } 76 data = data + "}" 77 return repo.gateway.CreateResource(repo.config.ApiEndpoint(), "/v2/organizations", strings.NewReader(data)) 78 } 79 80 func (repo CloudControllerOrganizationRepository) Rename(orgGuid string, name string) (apiErr error) { 81 url := fmt.Sprintf("/v2/organizations/%s", orgGuid) 82 data := fmt.Sprintf(`{"name":"%s"}`, name) 83 return repo.gateway.UpdateResource(repo.config.ApiEndpoint(), url, strings.NewReader(data)) 84 } 85 86 func (repo CloudControllerOrganizationRepository) Delete(orgGuid string) (apiErr error) { 87 url := fmt.Sprintf("/v2/organizations/%s?recursive=true", orgGuid) 88 return repo.gateway.DeleteResource(repo.config.ApiEndpoint(), url) 89 }