Skip to content

Commit

Permalink
feat: add list of spaces for an organization
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosvega91 committed Dec 4, 2020
1 parent 2360d98 commit b2142f8
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 8 deletions.
28 changes: 28 additions & 0 deletions src/__tests__/spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,31 @@ test('should pass query paramiters to the API', async () => {
},
})
})

test('should get list of spaces for an organization', async () => {
const cfclient = await setup()
const list = await cfclient.organizations.spaces('real_organization')

expect(list).toMatchObject({
total_results: 2,
total_pages: 2,
prev_url: null,
next_url:
'/v2/organizations/real_organization/spaces?order-direction=asc&page=2&results-per-page=1',
})
})

test('should filter spaces for an organization', async () => {
const cfclient = await setup()
const list = await cfclient.organizations.spaces('real_organization', {
page: 2,
})

expect(list).toMatchObject({
total_results: 2,
total_pages: 2,
prev_url:
'/v2/organizations/real_organization/spaces?order-direction=asc&page=1&results-per-page=1',
next_url: null,
})
})
29 changes: 24 additions & 5 deletions src/apis/organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import fetch from 'node-fetch'
import { URLSearchParams } from 'url'
import { Login } from '../internalTypes'
import { PaginatedV3, PaginatedV2, APIError } from './types'
import { SpaceV2 } from './spaces'

type Organization = {
type OrganizationV3 = {
guid: string
created_at: string
updated_at: string
name: string
suspended: boolean
}

type OrganizationUser = {
type OrganizationUserV2 = {
metadata: {
guid: string
url: string
Expand Down Expand Up @@ -43,13 +44,31 @@ function organizations(auth: Login, endpoint: string) {
})

if (listResponse.ok && listResponse.status === 200) {
const list = (await listResponse.json()) as PaginatedV3<Organization>
const list = (await listResponse.json()) as PaginatedV3<OrganizationV3>

return list
}
const error = (await listResponse.json()) as APIError
return error
},
spaces: async (organization: string, qs?: any) => {
const params = new URLSearchParams(qs)
const usersResponse = await fetch(
`${endpoint}/v2/organizations/${organization}/spaces?${params}`,
{
headers: {
Authorization: `Bearer ${auth.access_token}`,
},
},
)
if (usersResponse.ok && usersResponse.status === 200) {
const list = (await usersResponse.json()) as PaginatedV2<SpaceV2>

return list
}
const error = (await usersResponse.json()) as APIError
return error
},
users: {
all: async (organization: string, qs?: any) => {
const params = new URLSearchParams(qs)
Expand All @@ -62,7 +81,7 @@ function organizations(auth: Login, endpoint: string) {
},
)
if (usersResponse.ok && usersResponse.status === 200) {
const list = (await usersResponse.json()) as PaginatedV2<OrganizationUser>
const list = (await usersResponse.json()) as PaginatedV2<OrganizationUserV2>

return list
}
Expand All @@ -73,4 +92,4 @@ function organizations(auth: Login, endpoint: string) {
}
}

export { organizations, Organization, OrganizationUser }
export { organizations, OrganizationV3, OrganizationUserV2 }
34 changes: 31 additions & 3 deletions src/apis/spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,35 @@ import { URLSearchParams } from 'url'
import { Login } from '../internalTypes'
import { PaginatedV3, APIError } from './types'

type Space = {
type SpaceV2 = {
metadata: {
guid: string
created_at: string
updated_at: string
name: string
}
entity: {
name: string
organization_guid: string
space_quota_definition_guid: null | string
isolation_segment_guid: null | string
allow_ssh: boolean
organization_url: string
developers_url: string
managers_url: string
auditors_url: string
apps_url: string
routes_url: string
domains_url: string
service_instances_url: string
app_events_url: string
events_url: string
security_groups_url: string
staging_security_groups_url: string
}
}

type SpaceV3 = {
guid: string
created_at: string
updated_at: string
Expand All @@ -21,7 +49,7 @@ function spaces(auth: Login, endpoint: string) {
})

if (listResponse.ok && listResponse.status === 200) {
const list = (await listResponse.json()) as PaginatedV3<Space>
const list = (await listResponse.json()) as PaginatedV3<SpaceV2>

return list
}
Expand All @@ -31,4 +59,4 @@ function spaces(auth: Login, endpoint: string) {
}
}

export { spaces, Space }
export { spaces, SpaceV3, SpaceV2 }
60 changes: 60 additions & 0 deletions src/test/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,66 @@ const handlers = [
)
},
),
rest.get(
'https://api.cf.eu10.hana.ondemand.com/v2/organizations/:organization/spaces',
(req, res, ctx) => {
const page = parseInt(req.url.searchParams.get('page') ?? '1', 10)
const { organization } = req.params
if (organization !== 'real_organization') {
return res(
ctx.status(400),
ctx.json({
description: `organization doesn't exist`,
error_code: 'not_exist',
code: 9000,
}),
)
}
const space = {
metadata: {
guid: 'test',
url: '/v2/spaces/test',
created_at: '2017-05-12T09:24:41Z',
updated_at: '2019-06-06T07:56:04Z',
},
entity: {
name: 'test',
organization_guid: 'real_organization',
space_quota_definition_guid: null,
isolation_segment_guid: null,
allow_ssh: true,
organization_url: '/v2/organizations/real_organization',
developers_url: '/v2/spaces/test/developers',
managers_url: '/v2/spaces/test/managers',
auditors_url: '/v2/spaces/test/auditors',
apps_url: '/v2/spaces/test/apps',
routes_url: '/v2/spaces/test/routes',
domains_url: '/v2/spaces/test/domains',
service_instances_url: '/v2/spaces/test/service_instances',
app_events_url: '/v2/spaces/test/app_events',
events_url: '/v2/spaces/test/events',
security_groups_url: '/v2/spaces/test/security_groups',
staging_security_groups_url: '/v2/spaces/test/staging_security_groups',
},
}
return res(
ctx.status(200),
ctx.json({
total_results: 2,
total_pages: 2,
prev_url:
page === 1
? null
: `/v2/organizations/${organization}/spaces?order-direction=asc&page=1&results-per-page=1`,
next_url:
page === 2
? null
: `/v2/organizations/${organization}/spaces?order-direction=asc&page=2&results-per-page=1`,
resources: [space],
}),
)
},
),
]

export { handlers }

0 comments on commit b2142f8

Please sign in to comment.