/* * Copyright 2010-2016 Amazon Technologies, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://aws.amazon.com/apache2.0 * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES * OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and * limitations under the License. */ package com.amazonaws.eclipse.elasticbeanstalk.server.ui; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import org.eclipse.core.databinding.DataBindingContext; import org.eclipse.core.databinding.beans.PojoObservables; import org.eclipse.core.databinding.observable.value.IObservableValue; import org.eclipse.jface.databinding.swt.SWTObservables; import org.eclipse.jface.viewers.ColumnWeightData; import org.eclipse.jface.viewers.TableLayout; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.TableEditor; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.TableItem; import org.eclipse.wst.server.ui.wizard.IWizardHandle; import com.amazonaws.eclipse.core.AwsToolkitCore; import com.amazonaws.eclipse.core.regions.ServiceAbbreviations; import com.amazonaws.eclipse.elasticbeanstalk.ConfigurationOptionConstants; import com.amazonaws.eclipse.elasticbeanstalk.deploy.DeployWizardDataModel; import com.amazonaws.eclipse.elasticbeanstalk.jobs.LoadResourcesCallback; import com.amazonaws.eclipse.elasticbeanstalk.jobs.LoadVpcsJob; import com.amazonaws.eclipse.elasticbeanstalk.util.BeanstalkConstants; import com.amazonaws.eclipse.elasticbeanstalk.util.OnUiThreadProxyFactory; import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest; import com.amazonaws.services.ec2.model.DescribeSubnetsRequest; import com.amazonaws.services.ec2.model.Filter; import com.amazonaws.services.ec2.model.SecurityGroup; import com.amazonaws.services.ec2.model.Subnet; import com.amazonaws.services.ec2.model.Tag; import com.amazonaws.services.ec2.model.Vpc; import com.amazonaws.util.StringUtils; public class DeployWizardVpcConfigurationPage extends AbstractDeployWizardPage { private final VpcWidgetBuilder vpcWidgetBuilder = new VpcWidgetBuilder(); private static final String VPC_CONFIGURATION_DOC_URL = "https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/vpc.html"; private Composite wizardPageRoot; private final LoadResourcesCallback loadVpcCallback; protected DeployWizardVpcConfigurationPage( DeployWizardDataModel wizardDataModel) { super(wizardDataModel); setComplete(false); this.loadVpcCallback = OnUiThreadProxyFactory.getProxy( LoadResourcesCallback.class, new LoadVpcsCallback()); } @Override public String getPageTitle() { return "VPC Configuration"; } @Override public String getPageDescription() { return "Configure VPC and subnets for your EC2 instances, and specify VPC security group."; } // When entering this page, according to the setup from the previous page, // such as region and whether using non-default VPC, // the VPC list will be refreshed, and the availability for the UI // components will be refreshed as well. @Override public void enter() { super.enter(); if (wizardDataModel.isUseNonDefaultVpc()) { new LoadVpcsJob(wizardDataModel.getRegion(), loadVpcCallback).schedule(); } else { wizardDataModel.setVpcId(null); } // Set complete true since all the resources are loaded and selected default values. setComplete(true); } @Override public Composite createComposite(Composite parent, IWizardHandle handle) { wizardHandle = handle; setDefaultsInDataModel(); handle.setImageDescriptor(AwsToolkitCore.getDefault() .getImageRegistry() .getDescriptor(AwsToolkitCore.IMAGE_AWS_LOGO)); this.wizardPageRoot = new Composite(parent, SWT.NONE); wizardPageRoot.setLayout(new GridLayout(1, false)); initializeValidators(); vpcWidgetBuilder.buildVpcUiSection(wizardPageRoot); return wizardPageRoot; } private class LoadVpcsCallback implements LoadResourcesCallback { @Override public void onSuccess(List vpcs) { createVpcConfigurationSection(vpcs); } @Override public void onFailure() { onInsufficientPermissions(); } @Override public void onInsufficientPermissions() { // currently do nothing, and let the caller handle the failure. } private void createVpcConfigurationSection(List vpcs) { vpcWidgetBuilder.refreshVpcConfigurationSection(vpcs); vpcWidgetBuilder.refreshVpcSectionAvailability(); } } /** * Set the default values for the roles and vpc in the data model to be * reflected in the UI when the model is bound to a control */ private void setDefaultsInDataModel() { wizardDataModel.setAssociatePublicIpAddress(false); if (StringUtils.isNullOrEmpty(wizardDataModel.getElbScheme())) { wizardDataModel .setElbScheme(BeanstalkConstants.ELB_SCHEME_EXTERNAL); } } private enum CheckboxType { ELB, EC2 } private class VpcWidgetBuilder { private final String[] SUBNET_TABLE_TITLES = { "Availability Zone", "Subnet ID", "Cidr Block", "ELB", "EC2" }; private final String[] ELB_SCHEMES = { BeanstalkConstants.ELB_SCHEME_EXTERNAL, BeanstalkConstants.ELB_SCHEME_INTERNAL }; private Combo vpcCombo; private Table subnetsTable; private Button apiaButton;// Associate Public Ip Address private Combo securityGroupCombo; private Combo elbSchemeCombo; private List