balena-sdk
Version:
The Balena JavaScript SDK
130 lines (126 loc) • 3.37 kB
JavaScript
;
/*
Copyright 2016 Balena
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const errors = tslib_1.__importStar(require("balena-errors"));
const getKeyModel = function (deps) {
const { pine,
// Do not destructure sub-modules, to allow lazy loading only when needed.
sdkInstance, } = deps;
/**
* @summary Get all ssh keys
* @name getAll
* @public
* @function
* @memberof balena.models.key
*
* @param {Object} [options={}] - extra pine options to use
* @fulfil {Object[]} - ssh keys
* @returns {Promise}
*
* @example
* balena.models.key.getAll().then(function(keys) {
* console.log(keys);
* });
*/
function getAll(options) {
return pine.get({
resource: 'user__has__public_key',
options,
});
}
/**
* @summary Get a single ssh key
* @name get
* @public
* @function
* @memberof balena.models.key
*
* @param {Number} id - key id
* @fulfil {Object} - ssh key
* @returns {Promise}
*
* @example
* balena.models.key.get(51).then(function(key) {
* console.log(key);
* });
*/
async function get(id) {
const key = await pine.get({
resource: 'user__has__public_key',
id,
});
if (key == null) {
throw new errors.BalenaKeyNotFound(id);
}
return key;
}
/**
* @summary Remove ssh key
* @name remove
* @public
* @function
* @memberof balena.models.key
*
* @param {Number} id - key id
* @returns {Promise}
*
* @example
* balena.models.key.remove(51);
*/
function remove(id) {
return pine.delete({
resource: 'user__has__public_key',
id,
});
}
/**
* @summary Create a ssh key
* @name create
* @public
* @function
* @memberof balena.models.key
*
* @param {String} title - key title
* @param {String} key - the public ssh key
*
* @fulfil {Object} - ssh key
* @returns {Promise}
*
* @example
* balena.models.key.create('Main', 'ssh-rsa AAAAB....').then(function(key) {
* console.log(key);
* });
*/
async function create(title, key) {
// Avoid ugly whitespaces
key = key.trim();
const { id: userId } = await sdkInstance.auth.getUserInfo();
return await pine.post({
resource: 'user__has__public_key',
body: {
title,
public_key: key,
user: userId,
},
});
}
return {
getAll,
get,
remove,
create,
};
};
exports.default = getKeyModel;