Skip to content

Commit

Permalink
revert slot_repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
korsvanloon committed Oct 5, 2022
1 parent 356a54f commit 606cce8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/plugin-terraform/examples/codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ generates:
content_repositories:
website1: var.variables["CONTENT_REPO1_ID"]
website2: var.variables["CONTENT_REPO2_ID"]
slot_repositories:
slot1: var.variables["SLOT_REPO1_ID"]
slot2: var.variables["SLOT_REPO2_ID"]
9 changes: 9 additions & 0 deletions packages/plugin-terraform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const plugin: PluginFunction<PluginConfig> = (
hostname = 'https://schema-examples.com',
visualization,
content_repositories,
slot_repositories,
schemaSuffix,
}
) => {
Expand All @@ -40,13 +41,21 @@ export const plugin: PluginFunction<PluginConfig> = (
})
)
: undefined
const slotRepositories = slot_repositories
? Object.entries(slot_repositories).map(([name, value]) =>
tfg.data('amplience_content_repository', snakeCase(name), {
id: maybeArg(value),
})
)
: undefined

// For each GraphQl object type, add corresponding resources to the terraform generator.
visit(astNode, {
ObjectTypeDefinition: {
leave: createObjectTypeVisitor({
tfg,
contentRepositories,
slotRepositories,
hostname,
visualization,
schemaSuffix,
Expand Down
13 changes: 12 additions & 1 deletion packages/plugin-terraform/src/lib/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ export const createObjectTypeVisitor =
hostname,
visualization,
contentRepositories,
slotRepositories,
schemaSuffix,
}: {
tfg: TerraformGenerator
hostname: string
visualization?: VisualizationType[]
contentRepositories?: Data[]
slotRepositories?: Data[]
schemaSuffix?: string
}) =>
(node: ObjectTypeDefinitionNode) => {
Expand Down Expand Up @@ -88,7 +90,7 @@ export const createObjectTypeVisitor =
'repository'
)?.value

if (contentRepositories) {
if (contentRepositories && !isSlot) {
tfg.resource('amplience_content_type_assignment', name, {
content_type_id: contentType.id,
repository_id: (
Expand All @@ -97,6 +99,15 @@ export const createObjectTypeVisitor =
).id,
})
}
if (slotRepositories && isSlot) {
tfg.resource('amplience_content_type_assignment', name, {
content_type_id: contentType.id,
repository_id: (
slotRepositories.find((r) => r.name === repositoryName) ??
slotRepositories[0]
).id,
})
}
return null
}

Expand Down
7 changes: 7 additions & 0 deletions packages/plugin-terraform/test/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ it.each([{ graphqlFile: 'base' }])(
content_repositories: {
website1: 'var.variables["CONTENT_REPO1_ID"]',
website2: 'var.variables["CONTENT_REPO2_ID"]',
},
slot_repositories: {
slot1: 'var.variables["SLOT_REPO1_ID"]',
slot2: 'var.variables["SLOT_REPO2_ID"]',
},
})

// Note: the terraform generator does output the terraform code with ugly formatting.
// Normally we can deal with this by configuring "prettier" in the codegen config.
// But in this unittest we test the raw result.
// Therefore the expected output is also formatted ugly.

expect(terraformResult).toEqual(expected + '\n')
}
)
2 changes: 1 addition & 1 deletion packages/plugin-terraform/test/testdata/expected/base.tf
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ status = "ACTIVE"

resource "amplience_content_type_assignment" "test_slot"{
content_type_id = amplience_content_type.test_slot.id
repository_id = data.amplience_content_repository.website1.id
repository_id = data.amplience_content_repository.slot1.id
}

resource "amplience_content_type_schema" "test_visualizations"{
Expand Down

0 comments on commit 606cce8

Please sign in to comment.