Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
fix: Database connection issues
Browse files Browse the repository at this point in the history
  • Loading branch information
simenandre committed Feb 16, 2021
1 parent 3719df9 commit dd28fcb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/components/sql.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as pulumi from '@pulumi/pulumi';
import * as gcp from '@pulumi/gcp';
import { escapeName } from '../libs/utils';
import { RandomPassword } from '@pulumi/random';

export interface MySQLConfig {
/**
Expand Down Expand Up @@ -55,6 +56,7 @@ export class MySQLComponent extends pulumi.ComponentResource {
readonly instance: gcp.sql.DatabaseInstance;
readonly database: gcp.sql.Database;
readonly user: gcp.sql.User;
readonly password: pulumi.Output<string>;

constructor(
name: string,
Expand Down Expand Up @@ -92,11 +94,19 @@ export class MySQLComponent extends pulumi.ComponentResource {
{ parent: this },
);

const password = new RandomPassword(name, {
length: 20,
}, { parent: this });

this.password = password.result;

this.user = new gcp.sql.User(
name,
{
instance: this.instance.name,
name: escapeName(`${name}-user`, 5, 16),
host: '%',
password: this.password,
},
{ parent: this },
);
Expand Down
2 changes: 1 addition & 1 deletion src/libs/env-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function createEnvironmentVariables(config: CreateEnvVarsProps) {
return [
{
name: `${prefix}DB_HOST`,
value: pulumi.interpolate`localhost:/cloudsql/${instance.connectionName}`,
value: pulumi.interpolate`:/cloudsql/${instance.connectionName}`,
},
{
name: `${prefix}DB_NAME`,
Expand Down
2 changes: 1 addition & 1 deletion src/wordpress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class Wordpress extends pulumi.ComponentResource {
domain,
dbEnvironmentPrefix: envPrefix[preset],
databaseSettings: {
connectionName: this.database.database.instance,
connectionName: this.database.instance.connectionName,
databaseName: this.database.database.name,
databaseUsername: this.database.user.name,
databasePassword: this.database.user.password,
Expand Down

0 comments on commit dd28fcb

Please sign in to comment.