Skip to content

Commit

Permalink
Ask to include API key for Postgres commands
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineleclair committed Jun 9, 2024
1 parent 8945e9f commit 4f7525a
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 14 deletions.
10 changes: 10 additions & 0 deletions src/auth-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ export function request({
discoConfig,
body,
expectedStatuses = [200],
extraHeaders,
bodyStream,
}: {
method: string
url: string
discoConfig: DiscoConfig
body?: unknown
expectedStatuses?: number[]
extraHeaders?: Record<string, string>,
bodyStream?: Readable
}) {
const params: RequestInit = {
Expand All @@ -60,9 +62,17 @@ export function request({
...params.headers,
'Content-Type': 'application/json',
}

params.body = JSON.stringify(body)
}

if (extraHeaders !== undefined) {
params.headers = {
...params.headers,
...extraHeaders,
}
}

if (bodyStream) {
params.body = bodyStream
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/postgres/addon/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class PostgresAddonInstall extends Command {
const {flags} = await this.parse(PostgresAddonInstall)
const discoConfig = getDisco(flags.disco || null)
const url = `https://${discoConfig.host}/api/projects`
const project = 'postgres-addon';
const project = 'postgres-addon'
const body = {
name: project,
githubRepo: 'letsdiscodev/disco-addon-postgres',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/postgres/addon/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class PostgresAddonRemove extends Command {
public async run(): Promise<void> {
const {flags} = await this.parse(PostgresAddonRemove)
const discoConfig = getDisco(flags.disco || null)
const project = 'postgres-addon';
const project = 'postgres-addon'
const url = `https://${discoConfig.host}/api/projects/${project}`
try {
await request({method: 'DELETE', url, discoConfig, expectedStatuses: [200, 204]})
Expand Down
2 changes: 1 addition & 1 deletion src/commands/postgres/addon/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class PostgresAddonUpdate extends Command {
public async run(): Promise<void> {
const {flags} = await this.parse(PostgresAddonUpdate)
const discoConfig = getDisco(flags.disco || null)
const project = 'postgres-addon';
const project = 'postgres-addon'
const url = `https://${discoConfig.host}/api/projects/${project}/deployments`
const res = await request({method: 'POST', url, body: {}, discoConfig, expectedStatuses: [201]})
const data = (await res.json()) as any
Expand Down
10 changes: 9 additions & 1 deletion src/commands/postgres/databases/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ export default class PostgresDatabasesAdd extends Command {
const {flags} = await this.parse(PostgresDatabasesAdd)
const discoConfig = getDisco(flags.disco || null)
const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances/${flags.instance}/databases`
const res = await request({method: 'POST', url, discoConfig, expectedStatuses: [201]})
const res = await request({
method: 'POST',
url,
discoConfig,
expectedStatuses: [201],
extraHeaders: {
'X-Disco-Include-API-Key': 'true',
},
})
const respBody = (await res.json()) as {database: PostgresDatabase}
this.log(respBody.database.name)
}
Expand Down
11 changes: 10 additions & 1 deletion src/commands/postgres/databases/attach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ export default class PostgresDatabasesAttach extends Command {
const discoConfig = getDisco(flags.disco || null)
const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances/${flags.instance}/databases/${flags.database}/attach`
const reqBody = {envVar: flags['env-var'], project: flags.project}
await request({method: 'POST', url, discoConfig, body: reqBody, expectedStatuses: [200]})
await request({
method: 'POST',
url,
discoConfig,
body: reqBody,
expectedStatuses: [200],
extraHeaders: {
'X-Disco-Include-API-Key': 'true',
},
})
}
}
11 changes: 10 additions & 1 deletion src/commands/postgres/databases/detach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ export default class PostgresDatabasesDetach extends Command {
const discoConfig = getDisco(flags.disco || null)
const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances/${flags.instance}/databases/${flags.database}/detach`
const reqBody = {envVar: flags['env-var'], project: flags.project}
await request({method: 'POST', url, discoConfig, body: reqBody, expectedStatuses: [200]})
await request({
method: 'POST',
url,
discoConfig,
body: reqBody,
expectedStatuses: [200],
extraHeaders: {
'X-Disco-Include-API-Key': 'true',
},
})
}
}
14 changes: 11 additions & 3 deletions src/commands/postgres/databases/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {getDisco} from '../../../config.js'
import {request} from '../../../auth-request.js'

type PostgresDatabase = {
created: string;
name: string;
created: string
name: string
}

export default class PostgresDatabasesList extends Command {
Expand All @@ -21,7 +21,15 @@ export default class PostgresDatabasesList extends Command {
const {flags} = await this.parse(PostgresDatabasesList)
const discoConfig = getDisco(flags.disco || null)
const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances/${flags.instance}/databases`
const res = await request({method: 'GET', url, discoConfig, expectedStatuses: [200]})
const res = await request({
method: 'GET',
url,
discoConfig,
expectedStatuses: [200],
extraHeaders: {
'X-Disco-Include-API-Key': 'true',
},
})
const respBody = (await res.json()) as {databases: PostgresDatabase[]}
for (const database of respBody.databases) {
this.log(database.name)
Expand Down
10 changes: 9 additions & 1 deletion src/commands/postgres/databases/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ export default class PostgresDatabasesRemove extends Command {
const {flags} = await this.parse(PostgresDatabasesRemove)
const discoConfig = getDisco(flags.disco || null)
const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances/${flags.instance}/databases/${flags.database}`
await request({method: 'DELETE', url, discoConfig, expectedStatuses: [200]})
await request({
method: 'DELETE',
url,
discoConfig,
expectedStatuses: [200],
extraHeaders: {
'X-Disco-Include-API-Key': 'true',
},
})
}
}
10 changes: 9 additions & 1 deletion src/commands/postgres/instances/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ export default class PostgresInstancesAdd extends Command {
const {flags} = await this.parse(PostgresInstancesAdd)
const discoConfig = getDisco(flags.disco || null)
const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances`
const res = await request({method: 'POST', url, discoConfig, expectedStatuses: [201]})
const res = await request({
method: 'POST',
url,
discoConfig,
expectedStatuses: [201],
extraHeaders: {
'X-Disco-Include-API-Key': 'true',
},
})
const respBody = (await res.json()) as {project: {name: string}; deployment: {number: number}}
this.log(`Added instance ${respBody.project.name}`)
const deploymentUrl = `https://${discoConfig.host}/api/projects/${respBody.project.name}/deployments/${respBody.deployment.number}/output`
Expand Down
12 changes: 10 additions & 2 deletions src/commands/postgres/instances/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {getDisco} from '../../../config.js'
import {request} from '../../../auth-request.js'

type PostgresInstance = {
name: string;
name: string
}

export default class PostgresInstancesList extends Command {
Expand All @@ -19,7 +19,15 @@ export default class PostgresInstancesList extends Command {
const {flags} = await this.parse(PostgresInstancesList)
const discoConfig = getDisco(flags.disco || null)
const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances`
const res = await request({method: 'GET', url, discoConfig, expectedStatuses: [200]})
const res = await request({
method: 'GET',
url,
discoConfig,
expectedStatuses: [200],
extraHeaders: {
'X-Disco-Include-API-Key': 'true',
},
})
const respBody = (await res.json()) as {instances: PostgresInstance[]}
for (const instance of respBody.instances) {
this.log(instance.name)
Expand Down
10 changes: 9 additions & 1 deletion src/commands/postgres/instances/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export default class PostgresInstancesRemove extends Command {
const {flags} = await this.parse(PostgresInstancesRemove)
const discoConfig = getDisco(flags.disco || null)
const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances/${flags.instance}`
await request({method: 'DELETE', url, discoConfig, expectedStatuses: [200]})
await request({
method: 'DELETE',
url,
discoConfig,
expectedStatuses: [200],
extraHeaders: {
'X-Disco-Include-API-Key': 'true',
},
})
}
}

0 comments on commit 4f7525a

Please sign in to comment.