Skip to content
This repository has been archived by the owner on Apr 24, 2021. It is now read-only.

add create-apikey command #6

Merged
merged 3 commits into from
Oct 21, 2020
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
63 changes: 63 additions & 0 deletions src/commands/create-apikey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {BaseCommand} from '../lib'
import {getEnvironment} from '../lib/environments'
import {flags} from '@oclif/command'
import fetch from 'node-fetch'
import {cli} from 'cli-ux'

export default class CreateApikey extends BaseCommand {
static description = 'Create an API key for a Backend by id'
nelsonpecora marked this conversation as resolved.
Show resolved Hide resolved
static hidden = true

static examples = [
'$ slash-graphql create-apikey "0xid"',
]

static flags = {
...BaseCommand.commonFlags,
name: flags.string({char: 'n', description: 'Client name', default: 'slash-graphql-cli'}),
role: flags.string({char: 'r', description: 'Client role', default: 'client', options: ['admin', 'client']})
}

static args = [{name: 'id', description: 'Backend id', required: true}]

async run() {
const opts = this.parse(CreateApikey)
const {apiServer, authFile} = getEnvironment(opts.flags.environment)

const id = opts.args.id

if (!id.match(/0x[0-9a-f]+/)) {
this.error(`Invalid id ${id}`)
}

const token = await this.getAccessToken(apiServer, authFile)
if (!token) {
this.error('Please login with `slash-graphql login`')
}

const backend = this.findBackendByUid(apiServer, token, id)

if (!backend) {
this.error('Cannot find the backend that you are trying to create an API key for. Please run `slash-graphql list-backends` to get a list of backends')
}

const response = await fetch(`${apiServer}/deployment/${id}/api-keys`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
name: opts.args.name,
role: opts.args.role
})
})
if (response.status !== 200) {
this.error(`Unable to create API key. Try logging in again\n${await response.text()}`)
}
const apiKey = await response.json() as APIKey

if (!opts.flags.quiet) {
this.log(apiKey.key)
}
}
}
7 changes: 7 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ interface APIBackend {
owner: string;
}

interface APIKey {
key: string;
name: string;
role: string;
uid: string;
}

interface AuthConfig {
apiTime: number;
access_token: string;
Expand Down