frontend/src/components/PersistentVolumeClaimsEdit.vue
<!-- PersistentVolumeClaimsEdit.vue -->
<template>
<div v-if="show" class="modal-overlay" @click="closeModal">
<div class="modal-content yaml-editor" @click.stop>
<div class="modal-header">
<h3>Edit Persistent Volume Claim</h3>
<button class="close-button" @click="closeModal">×</button>
</div>
<div class="modal-body">
<div v-if="error" class="error-message">{{ error }}</div>
<div v-if="pvc" class="resource-info">
<div class="info-item">
<strong>Name:</strong> {{ pvc.metadata.name }}
</div>
<div class="info-item">
<strong>Namespace:</strong> {{ pvc.metadata.namespace }}
</div>
<div class="info-item">
<strong>Status:</strong> {{ pvc.status?.phase || 'Unknown' }}
</div>
</div>
<div class="yaml-container">
<textarea
v-model="yamlContent"
:disabled="isSubmitting"
class="yaml-textarea"
placeholder="Loading PVC YAML..."
></textarea>
</div>
<div class="warning-section">
<div class="warning-message">
<strong>⚠️ Warning:</strong> Editing a PVC may have limitations. Some fields like storage size can only be increased (if supported by storage class), and access modes cannot be changed after creation.
</div>
</div>
</div>
<div class="modal-footer">
<button
class="cancel-button"
@click="closeModal"
:disabled="isSubmitting"
>
Cancel
</button>
<button
class="update-button"
@click="updatePvc"
:disabled="isSubmitting || !yamlContent.trim() || !hasChanges"
>
{{ isSubmitting ? 'Updating...' : 'Update PVC' }}
</button>
</div>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent, ref, computed, watch, PropType } from 'vue';
import { KubernetesCluster, KubernetesPersistentVolumeClaim } from '../types/kubernetes';
import { PersistentVolumeClaimUpdateOptions } from '../types/custom';
export default defineComponent({
name: 'PersistentVolumeClaimsEdit',
props: {
show: {
type: Boolean,
required: true
},
pvc: {
type: Object as PropType<KubernetesPersistentVolumeClaim | null>,
required: false,
default: null
},
cluster: {
type: Object as PropType<KubernetesCluster | null>,
required: false,
default: null
}
},
emits: ['close', 'update-pvc'],
setup(props, { emit }) {
// State
const yamlContent = ref('');
const originalYaml = ref('');
const isSubmitting = ref(false);
const error = ref('');
// Computed
const hasChanges = computed(() => {
return yamlContent.value.trim() !== originalYaml.value.trim();
});
// Methods
const closeModal = () => {
if (!isSubmitting.value) {
resetForm();
emit('close');
}
};
const resetForm = () => {
yamlContent.value = '';
originalYaml.value = '';
error.value = '';
isSubmitting.value = false;
};
const convertToYaml = (obj: any): string => {
try {
// Simple YAML conversion - in a real app, you'd use a proper YAML library
const cleanObj = JSON.parse(JSON.stringify(obj));
// Remove fields that shouldn't be edited
if (cleanObj.metadata) {
delete cleanObj.metadata.resourceVersion;
delete cleanObj.metadata.uid;
delete cleanObj.metadata.selfLink;
delete cleanObj.metadata.creationTimestamp;
delete cleanObj.metadata.generation;
delete cleanObj.metadata.managedFields;
}
// Remove status as it's read-only
delete cleanObj.status;
return JSON.stringify(cleanObj, null, 2)
.replace(/"/g, '')
.replace(/,$/gm, '')
.replace(/\{/g, '')
.replace(/\}/g, '')
.replace(/\[/g, '')
.replace(/\]/g, '')
.replace(/^\s*$/gm, '')
.split('\n')
.filter(line => line.trim())
.map(line => {
const indent = ' '.repeat((line.match(/^\s*/)?.[0]?.length || 0) / 2);
const content = line.trim();
if (content.endsWith(':')) {
return `${indent}${content}`;
}
const [key, ...valueParts] = content.split(':');
const value = valueParts.join(':').trim();
return value ? `${indent}${key}: ${value}` : `${indent}${key}:`;
})
.join('\n');
} catch (err) {
console.error('Error converting to YAML:', err);
return JSON.stringify(obj, null, 2);
}
};
const loadPvcYaml = () => {
if (!props.pvc) {
yamlContent.value = '';
originalYaml.value = '';
return;
}
try {
const yaml = convertToYaml(props.pvc);
yamlContent.value = yaml;
originalYaml.value = yaml;
error.value = '';
} catch (err: any) {
error.value = `Failed to load PVC YAML: ${err.message || err}`;
yamlContent.value = '';
originalYaml.value = '';
}
};
const validateYaml = (yaml: string): boolean => {
try {
const trimmed = yaml.trim();
if (!trimmed) {
error.value = 'YAML content cannot be empty';
return false;
}
// Basic validation for PVC
if (!trimmed.includes('kind:') || !trimmed.includes('PersistentVolumeClaim')) {
error.value = 'YAML must contain "kind: PersistentVolumeClaim"';
return false;
}
if (!trimmed.includes('apiVersion:')) {
error.value = 'YAML must contain "apiVersion"';
return false;
}
if (!trimmed.includes('metadata:')) {
error.value = 'YAML must contain "metadata" section';
return false;
}
if (!trimmed.includes('spec:')) {
error.value = 'YAML must contain "spec" section';
return false;
}
return true;
} catch (err) {
error.value = 'Invalid YAML format';
return false;
}
};
const updatePvc = (): void => {
if (!props.pvc || !props.cluster) {
error.value = 'Missing PVC or cluster information';
return;
}
error.value = '';
if (!yamlContent.value.trim()) {
error.value = 'YAML content is required';
return;
}
if (!validateYaml(yamlContent.value)) {
return;
}
if (!hasChanges.value) {
error.value = 'No changes detected';
return;
}
isSubmitting.value = true;
try {
const opts: PersistentVolumeClaimUpdateOptions = {
context: props.cluster.contextName,
definition: yamlContent.value.trim(),
origNamespace: props.pvc.metadata.namespace,
origName: props.pvc.metadata.name
};
emit('update-pvc', opts);
closeModal();
} catch (err: any) {
error.value = `Failed to update PVC: ${err.message || err}`;
isSubmitting.value = false;
}
};
// Watchers
watch(() => props.show, (newShow) => {
if (newShow && props.pvc) {
loadPvcYaml();
}
});
watch(() => props.pvc, (newPvc) => {
if (newPvc && props.show) {
loadPvcYaml();
}
});
return {
yamlContent,
isSubmitting,
error,
hasChanges,
closeModal,
updatePvc
};
}
});
</script>
<style src="@/assets/css/MenuDialog.css" scoped></style>
<style scoped>
.resource-info {
background-color: #333;
border: 1px solid #444;
border-radius: 4px;
padding: 1rem;
margin-bottom: 1rem;
}
.info-item {
margin-bottom: 0.5rem;
color: #ccc;
}
.info-item:last-child {
margin-bottom: 0;
}
.info-item strong {
color: #fff;
margin-right: 0.5rem;
}
.yaml-container {
margin-bottom: 1rem;
}
.yaml-textarea {
width: 100%;
height: 400px;
padding: 1rem;
border: 1px solid #444;
border-radius: 4px;
background-color: #1e1e1e;
color: #ffffff;
font-family: 'Courier New', Consolas, monospace;
font-size: 14px;
line-height: 1.4;
resize: vertical;
min-height: 300px;
max-height: 600px;
}
.yaml-textarea:focus {
outline: none;
border-color: #007acc;
box-shadow: 0 0 0 2px rgba(0, 122, 204, 0.2);
}
.yaml-textarea:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.yaml-textarea::placeholder {
color: #666;
font-style: italic;
}
.warning-section {
margin-bottom: 1rem;
}
.warning-message {
background-color: #4a3a1a;
border: 1px solid #cc9944;
color: #ffcc66;
padding: 0.75rem;
border-radius: 4px;
font-size: 0.9rem;
line-height: 1.4;
}
.error-message {
background-color: #4a1a1a;
border: 1px solid #cc4444;
color: #ff6b6b;
padding: 0.75rem;
border-radius: 4px;
margin-bottom: 1rem;
font-size: 0.9rem;
}
.modal-footer {
display: flex;
justify-content: flex-end;
gap: 0.5rem;
padding: 1rem 1.5rem;
border-top: 1px solid #444;
}
.cancel-button {
padding: 0.5rem 1rem;
border: 1px solid #666;
background-color: transparent;
color: #ccc;
border-radius: 4px;
cursor: pointer;
transition: all 0.2s;
}
.cancel-button:hover:not(:disabled) {
background-color: #444;
border-color: #777;
}
.update-button {
padding: 0.5rem 1rem;
border: 1px solid #007acc;
background-color: #007acc;
color: white;
border-radius: 4px;
cursor: pointer;
transition: all 0.2s;
}
.update-button:hover:not(:disabled) {
background-color: #005a9e;
border-color: #005a9e;
}
.update-button:disabled {
opacity: 0.6;
cursor: not-allowed;
}
/* Responsive design */
@media (max-width: 768px) {
.yaml-textarea {
height: 300px;
font-size: 12px;
}
.resource-info {
padding: 0.75rem;
}
.info-item {
font-size: 0.9rem;
}
}
</style>