2021-09-09 08:38:34 +00:00
|
|
|
import { PortainerEndpointTypes } from '@/portainer/models/endpoint/models';
|
2022-12-21 00:18:51 +00:00
|
|
|
import { EditorType } from '@/react/edge/edge-stacks/types';
|
2023-01-26 03:03:44 +00:00
|
|
|
import { getValidEditorTypes } from '@/react/edge/edge-stacks/utils';
|
2020-07-06 07:35:13 +00:00
|
|
|
export class EditEdgeStackFormController {
|
2020-08-10 21:51:21 +00:00
|
|
|
/* @ngInject */
|
2022-09-21 07:10:58 +00:00
|
|
|
constructor($scope) {
|
|
|
|
this.$scope = $scope;
|
2021-09-09 08:38:34 +00:00
|
|
|
this.state = {
|
|
|
|
endpointTypes: [],
|
2023-01-30 21:03:21 +00:00
|
|
|
readOnlyCompose: false,
|
2021-09-09 08:38:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.fileContents = {
|
|
|
|
0: '',
|
|
|
|
1: '',
|
|
|
|
};
|
|
|
|
|
2022-12-21 00:18:51 +00:00
|
|
|
this.EditorType = EditorType;
|
|
|
|
|
2021-09-09 08:38:34 +00:00
|
|
|
this.onChangeGroups = this.onChangeGroups.bind(this);
|
|
|
|
this.onChangeFileContent = this.onChangeFileContent.bind(this);
|
|
|
|
this.onChangeComposeConfig = this.onChangeComposeConfig.bind(this);
|
|
|
|
this.onChangeKubeManifest = this.onChangeKubeManifest.bind(this);
|
|
|
|
this.hasDockerEndpoint = this.hasDockerEndpoint.bind(this);
|
|
|
|
this.hasKubeEndpoint = this.hasKubeEndpoint.bind(this);
|
|
|
|
this.onChangeDeploymentType = this.onChangeDeploymentType.bind(this);
|
|
|
|
this.removeLineBreaks = this.removeLineBreaks.bind(this);
|
|
|
|
this.onChangeFileContent = this.onChangeFileContent.bind(this);
|
2022-12-13 20:56:47 +00:00
|
|
|
this.onChangeUseManifestNamespaces = this.onChangeUseManifestNamespaces.bind(this);
|
2023-01-30 21:03:21 +00:00
|
|
|
this.selectValidDeploymentType = this.selectValidDeploymentType.bind(this);
|
2022-12-13 20:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onChangeUseManifestNamespaces(value) {
|
|
|
|
this.$scope.$evalAsync(() => {
|
|
|
|
this.model.UseManifestNamespaces = value;
|
|
|
|
});
|
2021-09-09 08:38:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hasKubeEndpoint() {
|
|
|
|
return this.state.endpointTypes.includes(PortainerEndpointTypes.EdgeAgentOnKubernetesEnvironment);
|
|
|
|
}
|
|
|
|
|
|
|
|
hasDockerEndpoint() {
|
|
|
|
return this.state.endpointTypes.includes(PortainerEndpointTypes.EdgeAgentOnDockerEnvironment);
|
|
|
|
}
|
|
|
|
|
|
|
|
onChangeGroups(groups) {
|
2022-09-21 07:10:58 +00:00
|
|
|
return this.$scope.$evalAsync(() => {
|
|
|
|
this.model.EdgeGroups = groups;
|
2023-01-30 21:03:21 +00:00
|
|
|
this.setEnvironmentTypesInSelection(groups);
|
|
|
|
this.selectValidDeploymentType();
|
|
|
|
this.state.readOnlyCompose = this.hasKubeEndpoint();
|
2022-09-21 07:10:58 +00:00
|
|
|
});
|
2021-09-09 08:38:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isFormValid() {
|
|
|
|
return this.model.EdgeGroups.length && this.model.StackFileContent && this.validateEndpointsForDeployment();
|
|
|
|
}
|
|
|
|
|
2023-01-30 21:03:21 +00:00
|
|
|
setEnvironmentTypesInSelection(groups) {
|
2021-09-09 08:38:34 +00:00
|
|
|
const edgeGroups = groups.map((id) => this.edgeGroups.find((e) => e.Id === id));
|
|
|
|
this.state.endpointTypes = edgeGroups.flatMap((group) => group.EndpointTypes);
|
2023-01-26 03:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
selectValidDeploymentType() {
|
2023-01-30 21:03:21 +00:00
|
|
|
const validTypes = getValidEditorTypes(this.state.endpointTypes, this.allowKubeToSelectCompose);
|
2023-01-26 03:03:44 +00:00
|
|
|
|
|
|
|
if (!validTypes.includes(this.model.DeploymentType)) {
|
|
|
|
this.onChangeDeploymentType(validTypes[0]);
|
|
|
|
}
|
2020-05-14 02:14:28 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 08:38:34 +00:00
|
|
|
removeLineBreaks(value) {
|
|
|
|
return value.replace(/(\r\n|\n|\r)/gm, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
onChangeFileContent(type, value) {
|
|
|
|
const oldValue = this.fileContents[type];
|
|
|
|
if (this.removeLineBreaks(oldValue) !== this.removeLineBreaks(value)) {
|
|
|
|
this.model.StackFileContent = value;
|
|
|
|
this.fileContents[type] = value;
|
2021-05-19 22:22:07 +00:00
|
|
|
this.isEditorDirty = true;
|
|
|
|
}
|
2020-05-14 02:14:28 +00:00
|
|
|
}
|
2021-09-09 08:38:34 +00:00
|
|
|
|
|
|
|
onChangeKubeManifest(value) {
|
|
|
|
this.onChangeFileContent(1, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
onChangeComposeConfig(value) {
|
|
|
|
this.onChangeFileContent(0, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
onChangeDeploymentType(deploymentType) {
|
2023-01-26 03:03:44 +00:00
|
|
|
return this.$scope.$evalAsync(() => {
|
|
|
|
this.model.DeploymentType = deploymentType;
|
|
|
|
this.model.StackFileContent = this.fileContents[deploymentType];
|
|
|
|
});
|
2021-09-09 08:38:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
validateEndpointsForDeployment() {
|
|
|
|
return this.model.DeploymentType == 0 || !this.hasDockerEndpoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
$onInit() {
|
2023-01-30 21:03:21 +00:00
|
|
|
this.setEnvironmentTypesInSelection(this.model.EdgeGroups);
|
2023-01-26 03:03:44 +00:00
|
|
|
this.fileContents[this.model.DeploymentType] = this.model.StackFileContent;
|
2023-01-30 21:03:21 +00:00
|
|
|
|
|
|
|
// allow kube to view compose if it's an existing kube compose stack
|
|
|
|
const initiallyContainsKubeEnv = this.hasKubeEndpoint();
|
|
|
|
const isComposeStack = this.model.DeploymentType === 0;
|
|
|
|
this.allowKubeToSelectCompose = initiallyContainsKubeEnv && isComposeStack;
|
|
|
|
this.state.readOnlyCompose = this.allowKubeToSelectCompose;
|
|
|
|
this.selectValidDeploymentType();
|
2021-09-09 08:38:34 +00:00
|
|
|
}
|
2020-05-14 02:14:28 +00:00
|
|
|
}
|