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

feat: Enable passing admin username for Postgres #2078

Merged
merged 2 commits into from
Sep 2, 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
5 changes: 5 additions & 0 deletions aws-postgresql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ provision:
If you do, the encryption is done using an AWS KMS customer managed key.
This property if provided, will set the customer managed key to use for encrypting the Cloudwatch log group
created for the RDS PostgreSQL and upgrade logs.
- field_name: admin_username
type: string
details: The username to use for the admin user of the database. When not specified a random username will be generated. This property should only be used when migrating data.
default: ""
prohibit_update: true
computed_inputs:
- name: labels
default: ${json.marshal(request.default_labels)}
Expand Down
3 changes: 3 additions & 0 deletions integration-tests/postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ var _ = Describe("Postgresql", Label("Postgresql"), func() {
"enable_export_upgrade_logs": true,
"cloudwatch_upgrade_log_group_retention_in_days": 1,
"cloudwatch_log_groups_kms_key_id": "arn:aws:kms:us-west-2:xxxxxxxxxxxx:key/xxxxxxxx-80b9-4afd-98c0-xxxxxxxxxxxx",
"admin_username": "some-other-username",
})
Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -255,6 +256,7 @@ var _ = Describe("Postgresql", Label("Postgresql"), func() {
HaveKeyWithValue("enable_export_upgrade_logs", true),
HaveKeyWithValue("cloudwatch_upgrade_log_group_retention_in_days", BeNumerically("==", 1)),
HaveKeyWithValue("cloudwatch_log_groups_kms_key_id", "arn:aws:kms:us-west-2:xxxxxxxxxxxx:key/xxxxxxxx-80b9-4afd-98c0-xxxxxxxxxxxx"),
HaveKeyWithValue("admin_username", "some-other-username"),
),
)
})
Expand Down Expand Up @@ -301,6 +303,7 @@ var _ = Describe("Postgresql", Label("Postgresql"), func() {
const initialProvisionInvocation = 1
Expect(mockTerraform.ApplyInvocations()).To(HaveLen(initialProvisionInvocation))
},
Entry("admin_username", "admin_username", "new-username"),
Entry("update region", "region", "no-matter-what-region"),
Entry("update kms_key_id", "kms_key_id", "no-matter-what-key"),
Entry("update db_name", "db_name", "no-matter-what-name"),
Expand Down
45 changes: 45 additions & 0 deletions terraform-tests/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var _ = Describe("postgres", Label("postgres-terraform"), Ordered, func() {
"enable_export_upgrade_logs": false,
"cloudwatch_upgrade_log_group_retention_in_days": 30,
"cloudwatch_log_groups_kms_key_id": "",
"admin_username": "",
}
})

Expand All @@ -75,6 +76,50 @@ var _ = Describe("postgres", Label("postgres-terraform"), Ordered, func() {
Init(terraformProvisionDir)
})

Context("admin username", func() {
When("admin username has been passed", func() {
BeforeAll(func() {
plan = ShowPlan(terraformProvisionDir, buildVars(defaultVars, map[string]any{
"admin_username": "test-name",
}))
})

It("should use that admin username", func() {
Expect(ResourceChangesTypes(plan)).To(ConsistOf(
"aws_db_instance",
"random_password",
"aws_db_parameter_group",
"aws_db_subnet_group",
"aws_security_group",
"aws_security_group_rule",
))

Expect(AfterValuesForType(plan, "aws_db_instance")).To(
MatchKeys(IgnoreExtras, Keys{
"username": Equal("test-name"),
}))
})
})

When("admin username has not been passed", func() {
BeforeAll(func() {
plan = ShowPlan(terraformProvisionDir, buildVars(defaultVars, map[string]any{}))
})

It("should create a new random admin username", func() {
Expect(ResourceChangesTypes(plan)).To(ConsistOf(
"aws_db_instance",
"random_password",
"random_string",
"aws_db_parameter_group",
"aws_db_subnet_group",
"aws_security_group",
"aws_security_group_rule",
))
})
})
})

Context("cloud watch log groups", func() {
When("no parameters passed", func() {
BeforeAll(func() {
Expand Down
3 changes: 2 additions & 1 deletion terraform/postgresql/provision/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ resource "random_string" "username" {
length = 16
special = false
numeric = false
count = length(var.admin_username) == 0 ? 1 : 0
}

resource "random_password" "password" {
Expand All @@ -57,7 +58,7 @@ resource "aws_db_instance" "db_instance" {
instance_class = local.instance_class
identifier = var.instance_name
db_name = var.db_name
username = random_string.username.result
username = length(var.admin_username) == 0 ? random_string.username[0].result : var.admin_username
password = random_password.password.result
parameter_group_name = length(var.parameter_group_name) == 0 ? aws_db_parameter_group.db_parameter_group[0].name : var.parameter_group_name
tags = var.labels
Expand Down
3 changes: 2 additions & 1 deletion terraform/postgresql/provision/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ variable "enable_export_postgresql_logs" { type = bool }
variable "enable_export_upgrade_logs" { type = bool }
variable "cloudwatch_postgresql_log_group_retention_in_days" { type = number }
variable "cloudwatch_upgrade_log_group_retention_in_days" { type = number }
variable "cloudwatch_log_groups_kms_key_id" { type = string }
variable "cloudwatch_log_groups_kms_key_id" { type = string }
variable "admin_username" { type = string }
Loading