Skip to content
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
42 changes: 42 additions & 0 deletions src/project/utils/__tests__/getAutoGeneratedAttributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { getAwsDatabaseProjectMock } from '@tests/mocks/project'
import { PROVIDER, SERVICE_TYPE } from '@src/constants'
import { ENVIRONMENT } from '@src/project/constants'
import type { ProjectConfiguration } from '@src/project/types'
import { getAutoGeneratedAttributes } from '../getAutoGeneratedAttributes'

describe('Auto generated attributes for services', () => {
describe('state attributes', () => {
let project: ProjectConfiguration

beforeEach(() => {
project = getAwsDatabaseProjectMock()
})

it('provides all the attributes needed', () => {
const stateAttributes = getAutoGeneratedAttributes(
SERVICE_TYPE.STATE,
project,
ENVIRONMENT.PRODUCTION,
)

expect(stateAttributes).toEqual(
expect.objectContaining({
bucket: project.state.bucket,
statePath: project.state.statePath,
lockTable: project.state.lockTable,
type: SERVICE_TYPE.STATE,
provider: PROVIDER.AWS,
name: expect.stringContaining('state'),
}),
)
})
})

describe('cluster attributes', () => {
it.todo('is pending')
})

describe('dns attributes', () => {
it.todo('is pending')
})
})
17 changes: 16 additions & 1 deletion src/project/utils/getAutoGeneratedAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type AttributesGenerator = (
project: ProjectConfiguration,
environment?: string,
associated?: BaseServiceAttributes,
) => BaseServiceAttributes
) => BaseServiceAttributes | null

const ATTRIBUTE_GENERATOR: Partial<Record<ServiceTypeChoice, AttributesGenerator>> = {
[SERVICE_TYPE.STATE]: (project) => ({
Expand All @@ -27,6 +27,21 @@ const ATTRIBUTE_GENERATOR: Partial<Record<ServiceTypeChoice, AttributesGenerator
`${project.name || path.basename(process.cwd()) || 'stackmate-app'}-${environment}`,
),
}),
[SERVICE_TYPE.DNS]: (project, environment, associated) => {
if (!associated || !('domain' in associated) || !associated.domain) {
return null
}

return {
type: SERVICE_TYPE.DNS,
provider: associated.provider || project.provider || DEFAULT_PROVIDER,
region: associated.region || project.region,
domain: associated.domain,
name: kebabCase(
`${project.name || path.basename(process.cwd()) || 'stackmate-app'}-${environment}`,
),
}
},
}

export const getAutoGeneratedAttributes = (
Expand Down
36 changes: 36 additions & 0 deletions tests/mocks/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import path from 'node:path'
import { faker } from '@faker-js/faker/locale/af_ZA'
import { PROVIDER } from '@src/constants'
import { ENVIRONMENT } from '@src/project/constants'
import type { ProjectConfiguration } from '@src/project'

export const getAwsDatabaseProjectMock = (): ProjectConfiguration => ({
name: faker.internet.domainWord(),
state: {
provider: PROVIDER.AWS,
bucket: faker.internet.domainWord(),
lockTable: faker.internet.domainWord(),
statePath: path.join(faker.system.directoryPath(), `${faker.internet.domainWord()}.tfstate`),
},
environments: {
[ENVIRONMENT.PRODUCTION]: {
mysqlDb: {
type: 'mysql',
},
},
},
})

// TODO
export const getFullStackProject = (): ProjectConfiguration => {
const project = getAwsDatabaseProjectMock()

return {
...project,
environments: {
[ENVIRONMENT.PRODUCTION]: {
...project.environments.production,
},
},
}
}