Skip to content
Closed
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
25 changes: 25 additions & 0 deletions src/api/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,28 @@ export async function removeAPIKey(id) {
const { data } = await axios.delete(`${baseUrl}/apikeys/${id}/`);
return data;
}

// ssh key api functions
export async function fetchSSHKeys(params = {}) {
try {
const { data } = await axios.get(`${baseUrl}/sshkeys/`, { params: params });
return data;
} catch (e) {
console.error(e);
}
}

export async function saveSSHKey(sshKey) {
const { data } = await axios.post(`${baseUrl}/sshkeys/`, sshKey);
return data;
}

export async function editSSHKey(id, sshKey) {
const { data } = await axios.put(`${baseUrl}/sshkeys/${id}/`, sshKey);
return data;
}

export async function removeSSHKey(id) {
const { data } = await axios.delete(`${baseUrl}/sshkeys/${id}/`);
return data;
}
114 changes: 114 additions & 0 deletions src/components/core/SSHKeysForm.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<template>
<q-dialog ref="dialogRef" @hide="onDialogHide">
<q-card class="q-dialog-plugin" style="width: 60vw">
<q-bar>
{{ title }}
<q-space />
<q-btn dense flat icon="close" v-close-popup>
<q-tooltip class="bg-white text-primary">Close</q-tooltip>
</q-btn>
</q-bar>
<q-form @submit.prevent="submitForm">
<q-card-section>
<span v-if="!SSHKey">Add a public key for SSH access to agents</span>
</q-card-section>

<q-card-section>
<q-input
label="Name"
outlined
dense
v-model="localKey.name"
:rules="[(val) => !!val || '*Required']"
/>
</q-card-section>

<q-card-section v-if="!SSHKey">
<q-input
label="Public Key"
outlined
dense
type="textarea"
v-model="localKey.public_key"
:rules="[(val) => !!val || '*Required']"
placeholder="ssh-ed25519 AAAAC3... your@email.com"
/>
</q-card-section>

<q-card-section v-if="SSHKey">
<q-input
label="Comment"
outlined
dense
v-model="localKey.comment"
/>
</q-card-section>

<q-card-actions align="right">
<q-btn flat label="Cancel" v-close-popup />
<q-btn
flat
label="Submit"
color="primary"
type="submit"
:loading="loading"
/>
</q-card-actions>
</q-form>
</q-card>
</q-dialog>
</template>

<script>
import { ref, computed } from "vue";
import { useDialogPluginComponent } from "quasar";
import { saveSSHKey, editSSHKey } from "@/api/accounts";
import { notifySuccess } from "@/utils/notify";

export default {
name: "SSHKeysForm",
emits: [...useDialogPluginComponent.emits],
props: { SSHKey: Object },
setup(props) {
const { dialogRef, onDialogHide, onDialogOK } = useDialogPluginComponent();

const key = props.SSHKey
? ref(Object.assign({}, props.SSHKey))
: ref({ name: "", public_key: "" });
const loading = ref(false);

const title = computed(() =>
props.SSHKey ? "Edit SSH Key" : "Add SSH Key"
);

async function submitForm() {
loading.value = true;

const data = {
...key.value,
};

try {
const result = props.SSHKey
? await editSSHKey(data.id, data)
: await saveSSHKey(data);
onDialogOK();
notifySuccess(result);
loading.value = false;
} catch (e) {
loading.value = false;
}
}

return {
localKey: key,
loading,
title,
submitForm,
dialogRef,
onDialogHide,
onDialogOK,
};
},
};
</script>
203 changes: 203 additions & 0 deletions src/components/core/SSHKeysTable.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<template>
<div>
<div class="row">
<div class="text-subtitle2">SSH Keys</div>
<q-space />
<q-btn
size="sm"
color="grey-5"
icon="fas fa-plus"
text-color="black"
label="Add key"
@click="addSSHKey"
/>
</div>
<q-separator />
<q-table
dense
:rows="keys"
:columns="columns"
v-model:pagination="pagination"
row-key="id"
binary-state-sort
hide-pagination
virtual-scroll
:rows-per-page-options="[0]"
no-data-label="No SSH keys added yet"
>
<template v-slot:header-cell-actions="props">
<q-th :props="props" auto-width> </q-th>
</template>

<template v-slot:body="props">
<q-tr
:props="props"
class="cursor-pointer"
@dblclick="editSSHKey(props.row)"
>
<q-menu context-menu>
<q-list dense style="min-width: 200px">
<q-item clickable v-close-popup @click="editSSHKey(props.row)">
<q-item-section side>
<q-icon name="edit" />
</q-item-section>
<q-item-section>Edit</q-item-section>
</q-item>
<q-item clickable v-close-popup @click="deleteSSHKey(props.row)">
<q-item-section side>
<q-icon name="delete" />
</q-item-section>
<q-item-section>Delete</q-item-section>
</q-item>

<q-separator></q-separator>

<q-item clickable v-close-popup>
<q-item-section>Close</q-item-section>
</q-item>
</q-list>
</q-menu>
<q-td>
{{ props.row.name }}
</q-td>
<q-td>
{{ props.row.key_type }}
</q-td>
<q-td>
<code class="text-caption">{{ props.row.fingerprint }}</code>
</q-td>
<q-td>
{{ props.row.comment }}
</q-td>
<q-td>
{{ formatDate(props.row.created_time) }}
</q-td>
</q-tr>
</template>
</q-table>
</div>
</template>

<script>
import { ref, computed, onMounted } from "vue";
import { useStore } from "vuex";
import { fetchSSHKeys, removeSSHKey } from "@/api/accounts";
import { useQuasar } from "quasar";
import { notifySuccess } from "@/utils/notify";
import SSHKeysForm from "@/components/core/SSHKeysForm.vue";

const columns = [
{
name: "name",
label: "Name",
field: "name",
align: "left",
sortable: true,
},
{
name: "key_type",
label: "Type",
field: "key_type",
align: "left",
sortable: true,
},
{
name: "fingerprint",
label: "Fingerprint",
field: "fingerprint",
align: "left",
sortable: true,
},
{
name: "comment",
label: "Comment",
field: "comment",
align: "left",
sortable: true,
},
{
name: "created_time",
label: "Created",
field: "created_time",
align: "left",
sortable: true,
},
{
name: "actions",
label: "",
field: "actions",
},
];

export default {
name: "SSHKeysTable",
setup() {
const $q = useQuasar();
const store = useStore();
const formatDate = computed(() => store.getters.formatDate);

const keys = ref([]);
const loading = ref(false);

const pagination = ref({
rowsPerPage: 0,
sortBy: "name",
descending: true,
});

async function getSSHKeys() {
loading.value = true;
keys.value = await fetchSSHKeys();
loading.value = false;
}

async function deleteSSHKey(key) {
$q.dialog({
title: `Delete SSH key: ${key.name}?`,
cancel: true,
ok: { label: "Delete", color: "negative" },
}).onOk(async () => {
loading.value = true;
try {
const result = await removeSSHKey(key.id);
notifySuccess(result);
getSSHKeys();
loading.value = false;
} catch (e) {
console.error(e);
loading.value = false;
}
});
}

function editSSHKey(key) {
$q.dialog({
component: SSHKeysForm,
componentProps: {
SSHKey: key,
},
}).onOk(() => getSSHKeys());
}

function addSSHKey() {
$q.dialog({
component: SSHKeysForm,
}).onOk(() => getSSHKeys());
}

onMounted(getSSHKeys);

return {
keys,
loading,
pagination,
columns,
getSSHKeys,
deleteSSHKey,
formatDate,
editSSHKey,
addSSHKey,
};
},
};
</script>
Loading
Loading