// SPDX-License-Identifier: Apache-2.0
import { StatusCodes } from 'http-status-codes';
import { V1ObjectMeta, V1PersistentVolumeClaim, V1PersistentVolumeClaimSpec, V1VolumeResourceRequirements, } from '@kubernetes/client-node';
import { Duration } from '../../../../../core/time/duration.js';
import { SoloError } from '../../../../../core/errors/solo-error.js';
import { KubeApiResponse } from '../../../kube-api-response.js';
import { ResourceOperation } from '../../../resources/resource-operation.js';
import { ResourceType } from '../../../resources/resource-type.js';
import { K8ClientPvc } from './k8-client-pvc.js';
export class K8ClientPvcs {
    kubeClient;
    constructor(kubeClient) {
        this.kubeClient = kubeClient;
    }
    async delete(pvcReference) {
        let resp;
        try {
            resp = await this.kubeClient.deleteNamespacedPersistentVolumeClaim(pvcReference.name.toString(), pvcReference.namespace.toString());
        }
        catch (error) {
            throw new SoloError(`Failed to delete pvc [pvc=${pvcReference.name.toString()}, ns=${pvcReference.namespace.toString()}], error: ${error.message}`);
        }
        KubeApiResponse.check(resp.response, ResourceOperation.DELETE, ResourceType.PERSISTENT_VOLUME_CLAIM, pvcReference.namespace, pvcReference.name.toString());
        return resp.response.statusCode === StatusCodes.OK;
    }
    async list(namespace, labels) {
        const pvcs = [];
        const labelSelector = labels ? labels.join(',') : undefined;
        let resp;
        try {
            resp = await this.kubeClient.listNamespacedPersistentVolumeClaim(namespace.name, undefined, undefined, undefined, undefined, labelSelector, undefined, undefined, undefined, undefined, Duration.ofMinutes(5).toMillis());
        }
        catch (error) {
            throw new SoloError('Failed to list pvcs', error);
        }
        KubeApiResponse.check(resp.response, ResourceOperation.LIST, ResourceType.PERSISTENT_VOLUME_CLAIM, namespace, '');
        for (const item of resp.body.items) {
            pvcs.push(item.metadata.name);
        }
        return pvcs;
    }
    async create(pvcReference, labels, accessModes) {
        const v1VolumeResourceRequirements = new V1VolumeResourceRequirements();
        v1VolumeResourceRequirements.requests = labels;
        const v1Spec = new V1PersistentVolumeClaimSpec();
        v1Spec.accessModes = accessModes;
        v1Spec.resources = v1VolumeResourceRequirements;
        const v1Metadata = new V1ObjectMeta();
        v1Metadata.name = pvcReference.name.toString();
        const v1Pvc = new V1PersistentVolumeClaim();
        v1Pvc.spec = v1Spec;
        v1Pvc.metadata = v1Metadata;
        let result;
        try {
            result = await this.kubeClient.createNamespacedPersistentVolumeClaim(pvcReference.namespace.toString(), v1Pvc);
        }
        catch (error) {
            throw new SoloError('Failed to create pvc', error);
        }
        KubeApiResponse.check(result.response, ResourceOperation.CREATE, ResourceType.PERSISTENT_VOLUME_CLAIM, pvcReference.namespace, pvcReference.name.toString());
        if (result?.body) {
            return new K8ClientPvc(pvcReference);
        }
        else {
            throw new SoloError('Failed to create pvc');
        }
    }
}
//# sourceMappingURL=k8-client-pvcs.js.map