|
| 1 | +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package software.aws.toolkits.jetbrains.services.lambda.deploy |
| 4 | + |
| 5 | +import com.intellij.execution.util.EnvVariablesTable |
| 6 | +import com.intellij.execution.util.EnvironmentVariable |
| 7 | +import com.intellij.openapi.application.ApplicationManager |
| 8 | +import com.intellij.openapi.project.Project |
| 9 | +import com.intellij.ui.AnActionButton |
| 10 | +import com.intellij.ui.IdeBorderFactory |
| 11 | +import com.intellij.ui.SimpleListCellRenderer |
| 12 | +import com.intellij.ui.ToolbarDecorator |
| 13 | +import com.intellij.ui.components.panels.Wrapper |
| 14 | +import software.aws.toolkits.jetbrains.services.cloudformation.Parameter |
| 15 | +import software.aws.toolkits.jetbrains.services.ecr.resources.EcrResources.LIST_REPOS |
| 16 | +import software.aws.toolkits.jetbrains.services.ecr.resources.Repository |
| 17 | +import software.aws.toolkits.jetbrains.services.s3.resources.S3Resources.listBucketNamesByActiveRegion |
| 18 | +import software.aws.toolkits.jetbrains.ui.ResourceSelector |
| 19 | +import software.aws.toolkits.resources.message |
| 20 | +import javax.swing.JButton |
| 21 | +import javax.swing.JCheckBox |
| 22 | +import javax.swing.JLabel |
| 23 | +import javax.swing.JPanel |
| 24 | +import javax.swing.JRadioButton |
| 25 | +import javax.swing.JTextField |
| 26 | + |
| 27 | +class DeployServerlessApplicationPanel(private val project: Project) { |
| 28 | + lateinit var newStackName: JTextField |
| 29 | + private set |
| 30 | + lateinit var createS3BucketButton: JButton |
| 31 | + private set |
| 32 | + lateinit var content: JPanel |
| 33 | + private set |
| 34 | + lateinit var s3Bucket: ResourceSelector<String> |
| 35 | + private set |
| 36 | + lateinit var stacks: ResourceSelector<Stack> |
| 37 | + private set |
| 38 | + lateinit var updateStack: JRadioButton |
| 39 | + private set |
| 40 | + lateinit var createStack: JRadioButton |
| 41 | + private set |
| 42 | + lateinit var requireReview: JCheckBox |
| 43 | + private set |
| 44 | + lateinit var useContainer: JCheckBox |
| 45 | + private set |
| 46 | + lateinit var ecrRepo: ResourceSelector<Repository> |
| 47 | + private set |
| 48 | + lateinit var createEcrRepoButton: JButton |
| 49 | + private set |
| 50 | + private lateinit var environmentVariablesTable: EnvVariablesTable |
| 51 | + private lateinit var ecrLabel: JLabel |
| 52 | + private lateinit var stackParameters: Wrapper |
| 53 | + private lateinit var parametersPanel: JPanel |
| 54 | + private lateinit var capabilitiesPanel: JPanel |
| 55 | + val capabilities: CapabilitiesEnumCheckBoxes = CapabilitiesEnumCheckBoxes() |
| 56 | + |
| 57 | + private fun createUIComponents() { |
| 58 | + environmentVariablesTable = EnvVariablesTable() |
| 59 | + stackParameters = Wrapper() |
| 60 | + stacks = ResourceSelector.builder() |
| 61 | + .resource(DeployServerlessApplicationDialog.ACTIVE_STACKS) |
| 62 | + .awsConnection(project) |
| 63 | + .build() |
| 64 | + s3Bucket = ResourceSelector.builder() |
| 65 | + .resource(listBucketNamesByActiveRegion(project)) |
| 66 | + .awsConnection(project) |
| 67 | + .build() |
| 68 | + ecrRepo = ResourceSelector.builder() |
| 69 | + .resource(LIST_REPOS) |
| 70 | + .awsConnection(project) |
| 71 | + .customRenderer(SimpleListCellRenderer.create("", Repository::repositoryName)) |
| 72 | + .build() |
| 73 | + if (!ApplicationManager.getApplication().isUnitTestMode) { |
| 74 | + val tableComponent = environmentVariablesTable.component |
| 75 | + hideActionButton(ToolbarDecorator.findAddButton(tableComponent)) |
| 76 | + hideActionButton(ToolbarDecorator.findRemoveButton(tableComponent)) |
| 77 | + hideActionButton(ToolbarDecorator.findEditButton(tableComponent)) |
| 78 | + stackParameters.setContent(tableComponent) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + init { |
| 83 | + capabilities.checkboxes.forEach { capabilitiesPanel.add(it) } |
| 84 | + showImageOptions(false) |
| 85 | + } |
| 86 | + |
| 87 | + fun withTemplateParameters(parameters: List<Parameter>): DeployServerlessApplicationPanel { |
| 88 | + parametersPanel.border = IdeBorderFactory.createTitledBorder(message("serverless.application.deploy.template.parameters"), false) |
| 89 | + environmentVariablesTable.setValues( |
| 90 | + parameters.map { parameter: Parameter -> |
| 91 | + object : EnvironmentVariable( |
| 92 | + parameter.logicalName, |
| 93 | + parameter.defaultValue(), |
| 94 | + false |
| 95 | + ) { |
| 96 | + override fun getNameIsWriteable(): Boolean = false |
| 97 | + override fun getDescription(): String? = parameter.description() |
| 98 | + } |
| 99 | + }.toList() |
| 100 | + ) |
| 101 | + return this |
| 102 | + } |
| 103 | + |
| 104 | + val templateParameters: Map<String, String> |
| 105 | + get() { |
| 106 | + environmentVariablesTable.stopEditing() |
| 107 | + return environmentVariablesTable.environmentVariables.map { envVar -> envVar.name to envVar.value }.toMap() |
| 108 | + } |
| 109 | + |
| 110 | + fun showImageOptions(hasImages: Boolean) { |
| 111 | + ecrLabel.isVisible = hasImages |
| 112 | + ecrRepo.isVisible = hasImages |
| 113 | + createEcrRepoButton.isVisible = hasImages |
| 114 | + } |
| 115 | + |
| 116 | + private fun hideActionButton(actionButton: AnActionButton?) { |
| 117 | + if (actionButton != null) { |
| 118 | + actionButton.isEnabled = false |
| 119 | + actionButton.isVisible = false |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments