// SPDX-License-Identifier: Apache-2.0
import { K8ClientClusterRole } from './k8-client-cluster-role.js';
import { ResourceCreateError, ResourceDeleteError } from '../../../errors/resource-operation-errors.js';
import { ResourceType } from '../../../resources/resource-type.js';
import { KubeApiResponse } from '../../../kube-api-response.js';
import { ResourceOperation } from '../../../resources/resource-operation.js';
export class K8ClientRbacs {
    k8sRbacApi;
    constructor(k8sRbacApi) {
        this.k8sRbacApi = k8sRbacApi;
    }
    async createClusterRole(name, rules, labels) {
        const clusterRole = new K8ClientClusterRole(name, rules, labels);
        let result;
        try {
            result = await this.k8sRbacApi.createClusterRole(clusterRole.toV1ClusterRole());
        }
        catch (error) {
            throw new ResourceCreateError(ResourceType.RBAC, undefined, name, error);
        }
        KubeApiResponse.check(result.response, ResourceOperation.CREATE, ResourceType.RBAC, undefined, name);
    }
    async clusterRoleExists(name) {
        let result;
        try {
            result = await this.k8sRbacApi.readClusterRole(name);
        }
        catch {
            return false;
        }
        try {
            KubeApiResponse.check(result.response, ResourceOperation.READ, ResourceType.RBAC, undefined, name);
        }
        catch {
            return false;
        }
        return true;
    }
    async deleteClusterRole(name) {
        let result;
        try {
            result = await this.k8sRbacApi.deleteClusterRole(name);
        }
        catch (error) {
            throw new ResourceDeleteError(ResourceType.RBAC, undefined, name, error);
        }
        KubeApiResponse.check(result.response, ResourceOperation.DELETE, ResourceType.RBAC, undefined, name);
    }
}
//# sourceMappingURL=k8-client-rbacs.js.map