Search in sources :

Example 6 with CloudInstance

use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.

the class AwsMetaDataCollectorTest method collectNewOldIsTagged.

@Test
public void collectNewOldIsTagged() {
    List<CloudInstance> vms = new ArrayList<>();
    List<Volume> volumes = new ArrayList<>();
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    vms.add(new CloudInstance(null, new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication));
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonCFClient);
    when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonASClient);
    when(awsClient.createAccess(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonEC2Client);
    when(cloudFormationStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("cbgateway"))).thenReturn("cbgateway-AAA");
    List<String> gatewayIds = Arrays.asList("i-1", "i-new");
    when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq("cbgateway-AAA"))).thenReturn(gatewayIds);
    when(cloudFormationStackUtil.createDescribeInstancesRequest(eq(gatewayIds))).thenReturn(describeInstancesRequestGw);
    when(amazonEC2Client.describeInstances(describeInstancesRequestGw)).thenReturn(describeInstancesResultGw);
    Instance instance1 = Mockito.mock(Instance.class);
    when(instance1.getInstanceId()).thenReturn("i-1");
    when(instance1.getPrivateIpAddress()).thenReturn("privateIp1");
    when(instance1.getPublicIpAddress()).thenReturn("publicIp1");
    Tag tag = new Tag();
    tag.setKey("cbname");
    tag.setValue("somevalue");
    when(instance1.getTags()).thenReturn(Collections.singletonList(tag));
    Instance instance2 = Mockito.mock(Instance.class);
    when(instance2.getInstanceId()).thenReturn("i-new");
    when(instance2.getPrivateIpAddress()).thenReturn("privateIp2");
    when(instance2.getPublicIpAddress()).thenReturn("publicIp2");
    List<Reservation> gatewayReservations = Collections.singletonList(getReservation(instance1, instance2));
    when(describeInstancesResultGw.getReservations()).thenReturn(gatewayReservations);
    AuthenticatedContext ac = authenticatedContext();
    List<CloudVmMetaDataStatus> statuses = awsMetadataCollector.collect(ac, null, vms);
    verify(amazonEC2Client, times(1)).createTags(any(CreateTagsRequest.class));
    Assert.assertEquals(1, statuses.size());
    Assert.assertEquals("i-new", statuses.get(0).getCloudVmInstanceStatus().getCloudInstance().getInstanceId());
    Assert.assertEquals("privateIp2", statuses.get(0).getMetaData().getPrivateIp());
    Assert.assertEquals("publicIp2", statuses.get(0).getMetaData().getPublicIp());
}
Also used : InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) Reservation(com.amazonaws.services.ec2.model.Reservation) Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) Tag(com.amazonaws.services.ec2.model.Tag) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.Test)

Example 7 with CloudInstance

use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.

the class CloudFormationTemplateBuilderTest method setUp.

@Before
public void setUp() throws Exception {
    initMocks(this);
    FreeMarkerConfigurationFactoryBean factoryBean = new FreeMarkerConfigurationFactoryBean();
    factoryBean.setPreferFileSystemAccess(false);
    factoryBean.setTemplateLoaderPath("classpath:/");
    factoryBean.afterPropertiesSet();
    Configuration configuration = factoryBean.getObject();
    ReflectionTestUtils.setField(cloudFormationTemplateBuilder, "freemarkerConfiguration", configuration);
    awsCloudFormationTemplate = configuration.getTemplate(templatePath, "UTF-8").toString();
    authenticatedContext = authenticatedContext();
    existingSubnetCidr = "testSubnet";
    name = "master";
    List<Volume> volumes = Arrays.asList(new Volume("/hadoop/fs1", "HDD", 1), new Volume("/hadoop/fs2", "HDD", 1));
    InstanceTemplate instanceTemplate = new InstanceTemplate("m1.medium", name, 0L, volumes, InstanceStatus.CREATE_REQUESTED, new HashMap<>(), 0L);
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    CloudInstance instance = new CloudInstance("SOME_ID", instanceTemplate, instanceAuthentication);
    List<SecurityRule> rules = Collections.singletonList(new SecurityRule("0.0.0.0/0", new PortDefinition[] { new PortDefinition("22", "22"), new PortDefinition("443", "443") }, "tcp"));
    Security security = new Security(rules, null);
    Map<InstanceGroupType, String> userData = ImmutableMap.of(InstanceGroupType.CORE, "CORE", InstanceGroupType.GATEWAY, "GATEWAY");
    Image image = new Image("cb-centos66-amb200-2015-05-25", userData, "redhat6", "", "default", "default-id");
    List<Group> groups = new ArrayList<>();
    groups.add(new Group(name, InstanceGroupType.CORE, Collections.singletonList(instance), security, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey()));
    groups.add(new Group(name, InstanceGroupType.GATEWAY, Collections.singletonList(instance), security, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey()));
    Network network = new Network(new Subnet("testSubnet"));
    Map<String, String> parameters = new HashMap<>();
    parameters.put("persistentStorage", "persistentStorageTest");
    parameters.put("attachedStorageOption", "attachedStorageOptionTest");
    Map<String, String> tags = new HashMap<>();
    tags.put("testtagkey", "testtagvalue");
    defaultTags.put(CloudbreakResourceType.DISK.templateVariable(), CloudbreakResourceType.DISK.key());
    defaultTags.put(CloudbreakResourceType.INSTANCE.templateVariable(), CloudbreakResourceType.INSTANCE.key());
    defaultTags.put(CloudbreakResourceType.IP.templateVariable(), CloudbreakResourceType.IP.key());
    defaultTags.put(CloudbreakResourceType.NETWORK.templateVariable(), CloudbreakResourceType.NETWORK.key());
    defaultTags.put(CloudbreakResourceType.SECURITY.templateVariable(), CloudbreakResourceType.SECURITY.key());
    defaultTags.put(CloudbreakResourceType.STORAGE.templateVariable(), CloudbreakResourceType.STORAGE.key());
    defaultTags.put(CloudbreakResourceType.TEMPLATE.templateVariable(), CloudbreakResourceType.TEMPLATE.key());
    cloudStack = new CloudStack(groups, network, image, parameters, tags, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey());
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) PortDefinition(com.sequenceiq.cloudbreak.cloud.model.PortDefinition) FreeMarkerConfigurationFactoryBean(org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean) Configuration(freemarker.template.Configuration) InstanceGroupType(com.sequenceiq.cloudbreak.api.model.InstanceGroupType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) SecurityRule(com.sequenceiq.cloudbreak.cloud.model.SecurityRule) Matchers.containsString(org.hamcrest.Matchers.containsString) Security(com.sequenceiq.cloudbreak.cloud.model.Security) Image(com.sequenceiq.cloudbreak.cloud.model.Image) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) Network(com.sequenceiq.cloudbreak.cloud.model.Network) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Before(org.junit.Before)

Example 8 with CloudInstance

use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.

the class AzureInstanceConnector method stop.

@Override
public List<CloudVmInstanceStatus> stop(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms) {
    String stackName = armTemplateUtils.getStackName(ac.getCloudContext());
    List<CloudVmInstanceStatus> statuses = new ArrayList<>();
    for (CloudInstance vm : vms) {
        try {
            AzureClient azureClient = ac.getParameter(AzureClient.class);
            azureClient.deallocateVirtualMachine(stackName, vm.getInstanceId());
            statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.IN_PROGRESS));
        } catch (RuntimeException e) {
            statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.FAILED, e.getMessage()));
        }
    }
    return statuses;
}
Also used : AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance)

Example 9 with CloudInstance

use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.

the class AzureMetadataCollector method collect.

@Override
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext authenticatedContext, List<CloudResource> resources, List<CloudInstance> vms) {
    CloudResource resource = azureUtils.getTemplateResource(resources);
    List<CloudVmMetaDataStatus> results = new ArrayList<>();
    List<InstanceTemplate> templates = Lists.transform(vms, CloudInstance::getTemplate);
    String resourceName = resource.getName();
    Map<String, InstanceTemplate> templateMap = Maps.uniqueIndex(templates, from -> azureUtils.getPrivateInstanceId(resourceName, from.getGroupName(), Long.toString(from.getPrivateId())));
    try {
        for (Entry<String, InstanceTemplate> instance : templateMap.entrySet()) {
            AzureClient azureClient = authenticatedContext.getParameter(AzureClient.class);
            VirtualMachine vm = azureClient.getVirtualMachine(resourceName, instance.getKey());
            String subnetId = vm.getPrimaryNetworkInterface().primaryIPConfiguration().subnetName();
            String privateIp = null;
            String publicIp = null;
            Integer faultDomainCount = azureClient.getFaultDomainNumber(resourceName, vm.name());
            String platform = authenticatedContext.getCloudContext().getPlatform().value();
            String location = authenticatedContext.getCloudContext().getLocation().getRegion().value();
            String hostgroupNm = instance.getValue().getGroupName();
            StringBuilder localityIndicatorBuilder = new StringBuilder();
            localityIndicatorBuilder.append(LOCALITY_SEPARATOR).append(platform).append(LOCALITY_SEPARATOR).append(location).append(LOCALITY_SEPARATOR).append(resourceName).append(LOCALITY_SEPARATOR).append(hostgroupNm).append(LOCALITY_SEPARATOR).append(faultDomainCount);
            AzureUtils.removeBlankSpace(localityIndicatorBuilder);
            List<String> networkInterfaceIdList = vm.networkInterfaceIds();
            for (String networkInterfaceId : networkInterfaceIdList) {
                NetworkInterface networkInterface = azureClient.getNetworkInterfaceById(networkInterfaceId);
                privateIp = networkInterface.primaryPrivateIP();
                PublicIPAddress publicIpAddress = networkInterface.primaryIPConfiguration().getPublicIPAddress();
                List<LoadBalancerBackend> backends = networkInterface.primaryIPConfiguration().listAssociatedLoadBalancerBackends();
                List<LoadBalancerInboundNatRule> inboundNatRules = networkInterface.primaryIPConfiguration().listAssociatedLoadBalancerInboundNatRules();
                if (!backends.isEmpty() || !inboundNatRules.isEmpty()) {
                    publicIp = azureClient.getLoadBalancerIps(resource.getName(), azureUtils.getLoadBalancerId(resource.getName())).get(0);
                }
                if (publicIpAddress != null && publicIpAddress.ipAddress() != null) {
                    publicIp = publicIpAddress.ipAddress();
                }
            }
            String instanceId = instance.getKey();
            CloudInstanceMetaData md = new CloudInstanceMetaData(privateIp, publicIp, faultDomainCount == null ? null : localityIndicatorBuilder.toString());
            InstanceTemplate template = templateMap.get(instanceId);
            if (template != null) {
                Map<String, Object> params = new HashMap<>(1);
                params.put(CloudInstance.SUBNET_ID, subnetId);
                // TODO use shhkey here
                CloudInstance cloudInstance = new CloudInstance(instanceId, template, null, params);
                CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
                results.add(new CloudVmMetaDataStatus(status, md));
            }
        }
    } catch (RuntimeException e) {
        throw new CloudConnectorException(e.getMessage(), e);
    }
    return results;
}
Also used : HashMap(java.util.HashMap) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) PublicIPAddress(com.microsoft.azure.management.network.PublicIPAddress) LoadBalancerInboundNatRule(com.microsoft.azure.management.network.LoadBalancerInboundNatRule) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) NetworkInterface(com.microsoft.azure.management.network.NetworkInterface) LoadBalancerBackend(com.microsoft.azure.management.network.LoadBalancerBackend) AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 10 with CloudInstance

use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.

the class AzureResourceConnector method collectResourcesToRemove.

@Override
public Map<String, Map<String, Object>> collectResourcesToRemove(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources, List<CloudInstance> vms) {
    AzureClient client = ac.getParameter(AzureClient.class);
    AzureCredentialView azureCredentialView = new AzureCredentialView(ac.getCloudCredential());
    String stackName = azureUtils.getStackName(ac.getCloudContext());
    Map<String, Map<String, Object>> resp = new HashMap<>(vms.size());
    for (CloudInstance instance : vms) {
        resp.put(instance.getInstanceId(), collectInstanceResourcesToRemove(ac, stack, client, azureCredentialView, stackName, instance));
    }
    return resp;
}
Also used : AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) AzureCredentialView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView) HashMap(java.util.HashMap) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)75 ArrayList (java.util.ArrayList)41 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)32 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)25 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)20 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)17 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)16 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)14 InstanceMetaData (com.sequenceiq.cloudbreak.domain.InstanceMetaData)12 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)10 Group (com.sequenceiq.cloudbreak.cloud.model.Group)10 Volume (com.sequenceiq.cloudbreak.cloud.model.Volume)10 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)9 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)9 CloudInstanceMetaData (com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData)9 InstanceGroup (com.sequenceiq.cloudbreak.domain.InstanceGroup)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)7 Instance (com.amazonaws.services.ec2.model.Instance)7