docker/ts/docker.classes.secret.ts

83 lines
2.5 KiB
TypeScript
Raw Normal View History

2019-09-12 12:45:36 +00:00
import * as plugins from './docker.plugins';
import { DockerHost } from './docker.classes.host';
// interfaces
import * as interfaces from './interfaces';
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;
}
public static async getSecretByID (dockerHostArg: DockerHost, idArg: string) {
const secrets = await this.getSecrets(dockerHostArg);
return secrets.find(secret => secret.ID === idArg);
}
public static async getSecretByName (dockerHostArg: DockerHost, nameArg: string) {
const secrets = await this.getSecrets(dockerHostArg);
return secrets.find(secret => secret.Spec.Name === nameArg);
}
public static async createSecret(dockerHostArg: DockerHost, secretDescriptor: interfaces.ISecretCreationDescriptor) {
2019-09-13 12:40:38 +00:00
const labels: interfaces.TLabels = {
...secretDescriptor.labels,
version: secretDescriptor.version
};
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,
2019-09-12 12:45:36 +00:00
Data: plugins.smartstring.base64.encode(secretDescriptor.contentArg)
});
const newSecretInstance = new DockerSecret(dockerHostArg);
Object.assign(newSecretInstance, response.body);
Object.assign (newSecretInstance, await DockerSecret.getSecretByID(dockerHostArg, newSecretInstance.ID));
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-12 12:45:36 +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
*/
public async update (contentArg: string) {
const route = `/secrets/${this.ID}/update?=version=${this.Version.Index}`;
const response = await this.dockerHost.request('POST', `/secrets/${this.ID}/update?version=${this.Version.Index}`, {
Name: this.Spec.Name,
Labels: this.Spec.Labels,
Data: plugins.smartstring.base64.encode(contentArg)
});
}
public async remove () {
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-12 12:45:36 +00:00
}