1
+ /* ******************************************************************************
2
+ * Copyright (c) 2023 Red Hat, Inc.
3
+ * Distributed under license by Red Hat, Inc. All rights reserved.
4
+ * This program is made available under the terms of the
5
+ * Eclipse Public License v2.0 which accompanies this distribution,
6
+ * and is available at http://www.eclipse.org/legal/epl-v20.html
7
+ *
8
+ * Contributors:
9
+ * Red Hat, Inc. - initial API and implementation
10
+ ******************************************************************************/
11
+ package com.redhat.devtools.intellij.kubernetes.actions
12
+
13
+ import com.intellij.openapi.actionSystem.AnActionEvent
14
+ import com.redhat.devtools.intellij.common.actions.StructureTreeAction
15
+ import com.redhat.devtools.intellij.kubernetes.dialogs.ScaleReplicaDialog
16
+ import com.redhat.devtools.intellij.kubernetes.model.Notification
17
+ import com.redhat.devtools.intellij.kubernetes.model.util.toMessage
18
+ import io.fabric8.kubernetes.api.model.HasMetadata
19
+ import io.fabric8.kubernetes.api.model.Pod
20
+ import io.fabric8.kubernetes.api.model.ReplicationController
21
+ import io.fabric8.kubernetes.api.model.apps.Deployment
22
+ import io.fabric8.kubernetes.api.model.apps.ReplicaSet
23
+ import io.fabric8.kubernetes.api.model.apps.StatefulSet
24
+ import io.fabric8.openshift.api.model.DeploymentConfig
25
+ import java.awt.event.MouseEvent
26
+ import javax.swing.tree.TreePath
27
+
28
+ class ScaleReplicaAction : StructureTreeAction () {
29
+
30
+ override fun actionPerformed (event : AnActionEvent ? , path : TreePath ? , selected : Any? ) {
31
+ // not called
32
+ }
33
+
34
+ override fun actionPerformed (event : AnActionEvent ? , path : Array <out TreePath >? , selected : Array <out Any >? ) {
35
+ val project = getEventProject(event) ? : return
36
+ val toScale = selected?.firstOrNull()?.getElement<HasMetadata >() ? : return
37
+ val model = getResourceModel() ? : return
38
+ try {
39
+ val replicator = model.getReplicas(toScale)
40
+ val replicas = replicator?.replicas
41
+ if (replicator == null
42
+ || replicas == null ) {
43
+ Notification ().error(
44
+ " Error Scaling" ,
45
+ " Could not scale ${toScale.kind} '${toScale.metadata.name} : unsupported resource"
46
+ )
47
+ return
48
+ }
49
+ val resourceLabel = " ${replicator.resource.kind} ${replicator.resource.metadata.name} "
50
+ ScaleReplicaDialog (
51
+ project,
52
+ resourceLabel,
53
+ replicas,
54
+ { replicas: Int -> model.setReplicas(replicas, replicator)},
55
+ (event?.inputEvent as ? MouseEvent )?.locationOnScreen
56
+ ).show()
57
+ } catch (e: RuntimeException ) {
58
+ Notification ().error(
59
+ " Error Scaling" ,
60
+ " Could not scale ${toScale.kind} '${toScale.metadata.name} ': ${toMessage(e)} "
61
+ )
62
+ }
63
+ }
64
+
65
+ override fun isVisible (selected : Array <out Any >? ): Boolean {
66
+ return selected?.size == 1
67
+ && isVisible(selected.firstOrNull())
68
+ }
69
+
70
+ override fun isVisible (selected : Any? ): Boolean {
71
+ val element = selected?.getElement<HasMetadata >()
72
+ return element is Deployment
73
+ || element is DeploymentConfig
74
+ || element is ReplicationController
75
+ || element is ReplicaSet
76
+ || element is StatefulSet
77
+ || element is Pod
78
+ }
79
+ }
0 commit comments