docker/ts/classes.secret.ts

93 lines
2.6 KiB
TypeScript
Raw Normal View History

import * as plugins from './plugins.js';
import { DockerHost } from './classes.host.js';
2019-09-12 12:45:36 +00:00
// interfaces
2022-10-17 07:36:35 +00:00
import * as interfaces from './interfaces/index.js';
2019-09-12 12:45:36 +00:00
export class DockerSecret {
// STATIC
public static async getSecrets(dockerHostArg: DockerHost) {
const response = await dockerHostArg.request('GET', '/secrets');
const secrets: DockerSecret[] = [];
for (const secret of response.body) {
const dockerSecretInstance = new DockerSecret(dockerHostArg);
Object.assign(dockerSecretInstance, secret);
secrets.push(dockerSecretInstance);
}
return secrets;
}
2019-09-13 16:20:12 +00:00
public static async getSecretByID(dockerHostArg: DockerHost, idArg: string) {
2019-09-12 12:45:36 +00:00
const secrets = await this.getSecrets(dockerHostArg);
2020-09-30 16:35:24 +00:00
return secrets.find((secret) => secret.ID === idArg);
2019-09-12 12:45:36 +00:00
}
2019-09-13 16:20:12 +00:00
public static async getSecretByName(dockerHostArg: DockerHost, nameArg: string) {
2019-09-12 12:45:36 +00:00
const secrets = await this.getSecrets(dockerHostArg);
2020-09-30 16:35:24 +00:00
return secrets.find((secret) => secret.Spec.Name === nameArg);
2019-09-12 12:45:36 +00:00
}
2019-09-13 16:20:12 +00:00
public static async createSecret(
dockerHostArg: DockerHost,
secretDescriptor: interfaces.ISecretCreationDescriptor
) {
2019-09-13 12:40:38 +00:00
const labels: interfaces.TLabels = {
...secretDescriptor.labels,
2020-09-30 16:35:24 +00:00
version: secretDescriptor.version,
2019-09-13 12:40:38 +00:00
};
2019-09-12 12:45:36 +00:00
const response = await dockerHostArg.request('POST', '/secrets/create', {
Name: secretDescriptor.name,
2019-09-13 12:40:38 +00:00
Labels: labels,
2020-09-30 16:35:24 +00:00
Data: plugins.smartstring.base64.encode(secretDescriptor.contentArg),
2019-09-12 12:45:36 +00:00
});
2019-09-13 16:20:12 +00:00
2019-09-12 12:45:36 +00:00
const newSecretInstance = new DockerSecret(dockerHostArg);
Object.assign(newSecretInstance, response.body);
2019-09-13 16:20:12 +00:00
Object.assign(
newSecretInstance,
await DockerSecret.getSecretByID(dockerHostArg, newSecretInstance.ID)
);
2019-09-12 12:45:36 +00:00
return newSecretInstance;
}
// INSTANCE
public ID: string;
public Spec: {
Name: string;
Labels: interfaces.TLabels;
};
2019-09-13 12:40:38 +00:00
public Version: {
2019-09-13 16:20:12 +00:00
Index: string;
2019-09-13 12:40:38 +00:00
};
2019-09-12 12:45:36 +00:00
public dockerHost: DockerHost;
constructor(dockerHostArg: DockerHost) {
this.dockerHost = dockerHostArg;
}
/**
* updates a secret
*/
2019-09-13 16:20:12 +00:00
public async update(contentArg: string) {
2019-09-12 12:45:36 +00:00
const route = `/secrets/${this.ID}/update?=version=${this.Version.Index}`;
2019-09-13 16:20:12 +00:00
const response = await this.dockerHost.request(
'POST',
`/secrets/${this.ID}/update?version=${this.Version.Index}`,
{
Name: this.Spec.Name,
Labels: this.Spec.Labels,
2020-09-30 16:35:24 +00:00
Data: plugins.smartstring.base64.encode(contentArg),
2019-09-13 16:20:12 +00:00
}
);
2019-09-12 12:45:36 +00:00
}
2019-09-13 16:20:12 +00:00
public async remove() {
2019-09-12 12:45:36 +00:00
await this.dockerHost.request('DELETE', `/secrets/${this.ID}`);
}
2019-09-13 12:40:38 +00:00
// get things
2019-09-13 12:45:35 +00:00
public async getVersion() {
2019-09-13 12:40:38 +00:00
return this.Spec.Labels.version;
}
2019-09-13 16:20:12 +00:00
}