Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove tenant_service.json fallback from tenant service lookup #281

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 1 addition & 32 deletions src/api/deploy/helpers/lookup-tenant-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,7 @@ async function lookupTenantService(service, environment, ref = 'main') {
Joi.assert(service, schema) // Check file in correct format
return service
} catch (error) {
logger.error(
`Error attempting to retrieve ${filePath} from GitHub - Falling back to tenant_services.json, ${error}`
)
return await lookupLegacyTenantService(service, environment, org, repo, ref)
}
}

/**
* Get service from the legacy single-file version of tenants.json
* @param {string} service
* @param {string} environment
* @param {string} owner
* @param {string} repo
* @param {string} ref
* @returns {Promise<undefined|*>}
*/
async function lookupLegacyTenantService(
service,
environment,
owner,
repo,
ref
) {
const filePath = `environments/${environment}/resources/tenant_services.json`
logger.info(`Getting legacy tenant file ${filePath}`)
try {
const data = await getContent(owner, repo, filePath, ref)
const services = JSON.parse(data)
return services[0][service]
} catch (error) {
logger.error(error, `Error attempting to retrieve ${filePath} from GitHub`)
return undefined
logger.error(`Error attempting to retrieve ${filePath} from GitHub`)
}
}

Expand Down
99 changes: 1 addition & 98 deletions src/api/deploy/helpers/lookup-tenant-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,101 +36,12 @@ describe('lookupTenantService', () => {
expect(result).toEqual(tenant)
})

test('should fallback to legacy tenant json when not found', async () => {
const tenants = [
{
someService: {
zone: 'public',
mongo: false,
redis: true,
service_code: 'ABC'
}
}
]

getContent
.mockReturnValueOnce(null)
.mockResolvedValue(JSON.stringify(tenants))

const result = await lookupTenantService('someService', 'dev')

expect(mockGetContent).toHaveBeenNthCalledWith(
1,
org,
repo,
'environments/dev/tenants/someService.json',
'main'
)

expect(mockGetContent).toHaveBeenNthCalledWith(
2,
org,
repo,
'environments/dev/resources/tenant_services.json',
'main'
)

expect(result).toEqual({
zone: 'public',
mongo: false,
redis: true,
service_code: 'ABC'
})
})

test('should fallback to legacy tenant json when service_code is missing', async () => {
const missingServiceCode = {
zone: 'public',
mongo: false,
redis: true
}
const tenants = [
{
someService: {
zone: 'public',
mongo: false,
redis: true,
service_code: 'ABC'
}
}
]

getContent
.mockReturnValueOnce(missingServiceCode)
.mockResolvedValue(JSON.stringify(tenants))

const result = await lookupTenantService('someService', 'dev')

expect(mockGetContent).toHaveBeenNthCalledWith(
1,
org,
repo,
'environments/dev/tenants/someService.json',
'main'
)

expect(mockGetContent).toHaveBeenNthCalledWith(
2,
org,
repo,
'environments/dev/resources/tenant_services.json',
'main'
)

expect(result).toEqual({
zone: 'public',
mongo: false,
redis: true,
service_code: 'ABC'
})
})

test('should return nothing when the service doesnt exist', async () => {
getContent.mockResolvedValue(null)

const result = await lookupTenantService('someService', 'dev')

expect(mockGetContent).toHaveBeenCalledTimes(2)
expect(mockGetContent).toHaveBeenCalledTimes(1)
expect(result).toBeUndefined()
})

Expand Down Expand Up @@ -159,14 +70,6 @@ describe('lookupTenantService', () => {
'environments/dev/tenants/someService.json',
'87428fc5'
)

expect(mockGetContent).toHaveBeenNthCalledWith(
2,
org,
repo,
'environments/dev/resources/tenant_services.json',
'87428fc5'
)
})

test('should get the tenant service from the single file version when present even if it has extra fields', async () => {
Expand Down