use of org.opentosca.toscana.plugins.cloudformation.mapper.CapabilityMapper in project TOSCAna by StuPro-TOSCAna.
the class TransformModelNodeVisitor method visit.
@Override
public void visit(Compute node) {
try {
if (cfnModule.checkComputeToEc2(node)) {
logger.debug("Compute '{}' will be transformed to EC2", node.getEntityName());
String nodeName = toAlphanumerical(node.getEntityName());
// default security group the EC2 Instance
SecurityGroup webServerSecurityGroup = cfnModule.resource(SecurityGroup.class, nodeName + SECURITY_GROUP).groupDescription("Enables ports for " + nodeName + ".");
// open endpoint port
node.getEndpoint().getPort().ifPresent(port -> webServerSecurityGroup.ingress(ingress -> ingress.cidrIp(IP_OPEN), PROTOCOL_TCP, port.port));
// check what image id should be taken
CapabilityMapper capabilityMapper = createCapabilityMapper();
OsCapability computeOs = node.getOs();
String imageId = capabilityMapper.mapOsCapabilityToImageId(computeOs);
ComputeCapability computeCompute = node.getHost();
String instanceType = capabilityMapper.mapComputeCapabilityToInstanceType(computeCompute, CapabilityMapper.EC2_DISTINCTION);
// create CFN init and store it
CFNInit init = new CFNInit(CONFIG_SETS);
cfnModule.putCFNInit(nodeName, init);
cfnModule.resource(Instance.class, nodeName).securityGroupIds(webServerSecurityGroup).imageId(imageId).instanceType(instanceType);
capabilityMapper.mapDiskSize(computeCompute, cfnModule, nodeName);
// Add Reference to keyName if KeyPair needed and open Port 22 (Allows SSH access)
if (cfnModule.hasKeyPair()) {
Instance instance = (Instance) cfnModule.getResource(nodeName);
instance.keyName(cfnModule.getKeyNameVar());
webServerSecurityGroup.ingress(ingress -> ingress.cidrIp(IP_OPEN), PROTOCOL_TCP, 22);
}
} else {
logger.debug("Compute '{}' will not be transformed to EC2", node.getEntityName());
}
} catch (SdkClientException se) {
logger.error("SDKClient failed, no valid credentials or no internet connection");
throw new TransformationFailureException("Failed", se);
} catch (Exception e) {
logger.error("Error while creating EC2Instance resource.");
throw new TransformationFailureException("Failed at Compute node " + node.getEntityName(), e);
}
}
use of org.opentosca.toscana.plugins.cloudformation.mapper.CapabilityMapper in project TOSCAna by StuPro-TOSCAna.
the class CloudFormationPluginTest method setUp.
@Before
public void setUp() throws Exception {
lamp = new EffectiveModelFactory().create(TestCsars.VALID_LAMP_NO_INPUT_TEMPLATE, logMock());
fileAccess = new PluginFileAccess(new File("src/test/resources/csars/yaml/valid/lamp-input/"), tmpdir, logMock());
cfnModule = new CloudFormationModule(fileAccess, "us-west-2", new BasicAWSCredentials("", ""));
TransformationContext context = mock(TransformationContext.class);
when(context.getModel()).thenReturn(lamp);
when(context.getLogger((Class<?>) any(Class.class))).thenReturn(LoggerFactory.getLogger("Dummy Logger"));
TransformModelNodeVisitor cfnNodeVisitorL = new TransformModelNodeVisitor(context, cfnModule);
cfnNodeVisitor = spy(cfnNodeVisitorL);
CapabilityMapper capabilityMapper = mock(CapabilityMapper.class);
when(capabilityMapper.mapOsCapabilityToImageId(any(OsCapability.class))).thenReturn("ami-testami");
when(cfnNodeVisitor.createCapabilityMapper()).thenReturn(capabilityMapper);
}
use of org.opentosca.toscana.plugins.cloudformation.mapper.CapabilityMapper in project TOSCAna by StuPro-TOSCAna.
the class TransformModelNodeVisitor method visit.
@Override
public void visit(MysqlDatabase node) {
try {
String nodeName = toAlphanumerical(node.getEntityName());
// get the compute where the dbms this node is hosted on, is hosted on
Compute compute = getCompute(node);
String serverName = toAlphanumerical(compute.getEntityName());
String dbName = node.getDatabaseName();
String masterUser = node.getUser().orElseThrow(() -> new IllegalArgumentException("Database user not set"));
String masterPassword = node.getPassword().orElseThrow(() -> new IllegalArgumentException("Database " + "password not set"));
Integer port = node.getPort().orElseThrow(() -> new IllegalArgumentException("Database port not set"));
// check what values should be taken
ComputeCapability hostedOnComputeCapability = compute.getHost();
CapabilityMapper capabilityMapper = createCapabilityMapper();
String dBInstanceClass = capabilityMapper.mapComputeCapabilityToInstanceType(hostedOnComputeCapability, CapabilityMapper.RDS_DISTINCTION);
Integer allocatedStorage = capabilityMapper.mapComputeCapabilityToRDSAllocatedStorage(hostedOnComputeCapability);
// SSD
String storageType = "gp2";
String securityGroupName = nodeName + SECURITY_GROUP;
SecurityGroup securityGroup = cfnModule.resource(SecurityGroup.class, securityGroupName).groupDescription("Open database " + dbName + " for access to group " + serverName + SECURITY_GROUP);
Set<Compute> hostsOfConnectedTo = getHostsOfConnectedTo(node);
for (Compute hostOfConnectedTo : hostsOfConnectedTo) {
securityGroup.ingress(ingress -> ingress.sourceSecurityGroupName(cfnModule.ref(toAlphanumerical(hostOfConnectedTo.getEntityName()) + SECURITY_GROUP)), PROTOCOL_TCP, port);
}
cfnModule.resource(DBInstance.class, nodeName).engine("MySQL").dBName(dbName).masterUsername(masterUser).masterUserPassword(masterPassword).dBInstanceClass(dBInstanceClass).allocatedStorage(allocatedStorage).storageType(storageType).vPCSecurityGroups(cfnModule.fnGetAtt(securityGroupName, "GroupId"));
// handle sql artifact
for (Artifact artifact : node.getArtifacts()) {
String relPath = artifact.getFilePath();
if (relPath.endsWith(".sql")) {
String sql = cfnModule.getFileAccess().read(artifact.getFilePath());
String computeName = createSqlCompute(node, sql);
securityGroup.ingress(ingress -> ingress.sourceSecurityGroupName(cfnModule.ref(toAlphanumerical(computeName) + SECURITY_GROUP)), PROTOCOL_TCP, port);
}
}
} catch (Exception e) {
logger.error("Error while creating MysqlDatabase resource.");
throw new TransformationFailureException("Failed at MysqlDatabase node " + node.getEntityName(), e);
}
}
Aggregations