Search in sources :

Example 1 with Builder

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

the class GcpSubnetResourceBuilder method build.

@Override
public CloudResource build(GcpContext context, AuthenticatedContext auth, Network network, Security security, CloudResource resource) throws Exception {
    if (isNewNetworkAndSubnet(network) || isNewSubnetInExistingNetwork(network)) {
        Compute compute = context.getCompute();
        String projectId = context.getProjectId();
        Subnetwork gcpSubnet = new Subnetwork();
        gcpSubnet.setName(resource.getName());
        gcpSubnet.setIpCidrRange(network.getSubnet().getCidr());
        String networkName = context.getStringParameter(GcpNetworkResourceBuilder.NETWORK_NAME);
        gcpSubnet.setNetwork(String.format("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", projectId, networkName));
        Insert snInsert = compute.subnetworks().insert(projectId, auth.getCloudContext().getLocation().getRegion().value(), gcpSubnet);
        try {
            Operation operation = snInsert.execute();
            if (operation.getHttpErrorStatusCode() != null) {
                throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), resource.getName());
            }
            context.putParameter(SUBNET_NAME, resource.getName());
            return createOperationAwareCloudResource(resource, operation);
        } catch (GoogleJsonResponseException e) {
            throw new GcpResourceException(checkException(e), resourceType(), resource.getName());
        }
    }
    context.putParameter(SUBNET_NAME, resource.getName());
    return new Builder().cloudResource(resource).persistent(false).build();
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Subnetwork(com.google.api.services.compute.model.Subnetwork) Compute(com.google.api.services.compute.Compute) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) Operation(com.google.api.services.compute.model.Operation) Insert(com.google.api.services.compute.Compute.Subnetworks.Insert)

Example 2 with Builder

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

the class GcpInstanceResourceBuilderTest method setUp.

@Before
public void setUp() {
    privateId = 0L;
    name = "master";
    flavor = "m1.medium";
    instanceId = "SOME_ID";
    volumes = Arrays.asList(new Volume("/hadoop/fs1", "HDD", 1), new Volume("/hadoop/fs2", "HDD", 1));
    List<SecurityRule> rules = Collections.singletonList(new SecurityRule("0.0.0.0/0", new PortDefinition[] { new PortDefinition("22", "22"), new PortDefinition("443", "443") }, "tcp"));
    security = new Security(rules, null);
    Location location = Location.location(Region.region("region"), AvailabilityZone.availabilityZone("az"));
    Map<InstanceGroupType, String> userData = ImmutableMap.of(InstanceGroupType.CORE, "CORE", InstanceGroupType.GATEWAY, "GATEWAY");
    image = new Image("cb-centos66-amb200-2015-05-25", userData, "redhat6", "", "default", "default-id");
    CloudContext cloudContext = new CloudContext(privateId, "testname", "GCP", "owner");
    CloudCredential cloudCredential = new CloudCredential(privateId, "credentialname");
    cloudCredential.putParameter("projectId", "projectId");
    String projectId = GcpStackUtil.getProjectId(cloudCredential);
    String serviceAccountId = GcpStackUtil.getServiceAccountId(cloudCredential);
    authenticatedContext = new AuthenticatedContext(cloudContext, cloudCredential);
    context = new GcpContext(cloudContext.getName(), location, projectId, serviceAccountId, compute, false, 30, false);
    List<CloudResource> networkResources = Arrays.asList(new Builder().type(ResourceType.GCP_NETWORK).name("network-test").build());
    context.addNetworkResources(networkResources);
    operation = new Operation();
    operation.setName("operation");
    operation.setHttpErrorStatusCode(null);
    GcpResourceNameService resourceNameService = new GcpResourceNameService();
    ReflectionTestUtils.setField(resourceNameService, "maxResourceNameLength", 50);
    ReflectionTestUtils.setField(builder, "resourceNameService", resourceNameService);
}
Also used : PortDefinition(com.sequenceiq.cloudbreak.cloud.model.PortDefinition) InstanceGroupType(com.sequenceiq.cloudbreak.api.model.InstanceGroupType) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) GcpResourceNameService(com.sequenceiq.cloudbreak.cloud.gcp.service.GcpResourceNameService) SecurityRule(com.sequenceiq.cloudbreak.cloud.model.SecurityRule) Matchers.anyString(org.mockito.Matchers.anyString) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Operation(com.google.api.services.compute.model.Operation) Security(com.sequenceiq.cloudbreak.cloud.model.Security) Image(com.sequenceiq.cloudbreak.cloud.model.Image) GcpContext(com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext) Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Location(com.sequenceiq.cloudbreak.cloud.model.Location) Before(org.junit.Before)

Example 3 with Builder

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

the class OpenStackResourceConnector method launch.

@SuppressWarnings("unchecked")
@Override
public List<CloudResourceStatus> launch(AuthenticatedContext authenticatedContext, CloudStack stack, PersistenceNotifier notifier, AdjustmentType adjustmentType, Long threshold) {
    String stackName = utils.getStackName(authenticatedContext);
    NeutronNetworkView neutronNetworkView = new NeutronNetworkView(stack.getNetwork());
    boolean existingNetwork = neutronNetworkView.isExistingNetwork();
    String existingSubnetCidr = getExistingSubnetCidr(authenticatedContext, stack);
    ModelContext modelContext = new ModelContext();
    modelContext.withExistingNetwork(existingNetwork);
    modelContext.withExistingSubnet(existingSubnetCidr != null);
    modelContext.withGroups(stack.getGroups());
    modelContext.withInstanceUserData(stack.getImage());
    modelContext.withLocation(authenticatedContext.getCloudContext().getLocation());
    modelContext.withStackName(stackName);
    modelContext.withNeutronNetworkView(neutronNetworkView);
    modelContext.withTemplateString(stack.getTemplate());
    modelContext.withTags(stack.getTags());
    String heatTemplate = heatTemplateBuilder.build(modelContext);
    Map<String, String> parameters = heatTemplateBuilder.buildParameters(authenticatedContext, stack, existingNetwork, existingSubnetCidr);
    OSClient<?> client = openStackClient.createOSClient(authenticatedContext);
    List<CloudResourceStatus> resources;
    Stack existingStack = client.heat().stacks().getStackByName(stackName);
    if (existingStack == null) {
        if (stack.getInstanceAuthentication().getPublicKeyId() == null) {
            createKeyPair(authenticatedContext, stack, client);
        }
        Stack heatStack = client.heat().stacks().create(Builders.stack().name(stackName).template(heatTemplate).disableRollback(false).parameters(parameters).timeoutMins(OPERATION_TIMEOUT).build());
        List<CloudResource> cloudResources = collectResources(authenticatedContext, notifier, heatStack, stack, neutronNetworkView);
        resources = check(authenticatedContext, cloudResources);
    } else {
        LOGGER.info("Heat stack already exists: {}", existingStack.getName());
        CloudResource cloudResource = new Builder().type(ResourceType.HEAT_STACK).name(existingStack.getId()).build();
        resources = Collections.singletonList(new CloudResourceStatus(cloudResource, ResourceStatus.CREATED));
    }
    LOGGER.debug("Launched resources: {}", resources);
    return resources;
}
Also used : ModelContext(com.sequenceiq.cloudbreak.cloud.openstack.heat.HeatTemplateBuilder.ModelContext) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) NeutronNetworkView(com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView) Stack(org.openstack4j.model.heat.Stack) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack)

Example 4 with Builder

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

the class MockResourceConnector method launch.

@Override
public List<CloudResourceStatus> launch(AuthenticatedContext authenticatedContext, CloudStack stack, PersistenceNotifier persistenceNotifier, AdjustmentType adjustmentType, Long threshold) {
    List<CloudResourceStatus> cloudResourceStatuses = new ArrayList<>();
    for (Group group : stack.getGroups()) {
        for (int i = 0; i < group.getInstancesSize(); i++) {
            CloudResource cloudResource = new Builder().type(ResourceType.MOCK_INSTANCE).status(CommonStatus.CREATED).name("cloudinstance" + cloudResourceStatuses.size()).reference("").persistent(true).build();
            cloudResourceStatuses.add(new CloudResourceStatus(cloudResource, CREATED));
        }
    }
    return cloudResourceStatuses;
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) ArrayList(java.util.ArrayList) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 5 with Builder

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

the class TestApplicationContext method cloudConnectors.

@Bean
public CloudConnector cloudConnectors() throws Exception {
    CloudResource resource = new Builder().type(ResourceType.HEAT_STACK).name("ref").build();
    when(cloudConnector.authentication()).thenReturn(authenticator);
    when(cloudConnector.credentials()).thenReturn(credentialConnector);
    when(credentialConnector.create(any(AuthenticatedContext.class))).thenReturn(new CloudCredentialStatus(null, CredentialStatus.CREATED));
    when(credentialConnector.delete(any(AuthenticatedContext.class))).thenReturn(new CloudCredentialStatus(null, CredentialStatus.DELETED));
    when(authenticator.authenticate(any(), any())).thenReturn(g.createAuthenticatedContext());
    when(cloudConnector.platform()).thenReturn(Platform.platform("TESTCONNECTOR"));
    when(cloudConnector.variant()).thenReturn(Variant.variant("TESTVARIANT"));
    when(cloudConnector.resources()).thenReturn(resourceConnector);
    when(cloudConnector.instances()).thenReturn(instanceConnector);
    when(cloudConnector.metadata()).thenReturn(collector);
    when(resourceConnector.launch(any(), any(), any(), any(), anyLong())).thenReturn(Collections.singletonList(new CloudResourceStatus(resource, ResourceStatus.CREATED)));
    when(resourceConnector.terminate(any(), any(), any())).thenReturn(Collections.singletonList(new CloudResourceStatus(resource, ResourceStatus.DELETED)));
    when(resourceConnector.update(any(), any(), any())).thenReturn(Collections.singletonList(new CloudResourceStatus(resource, ResourceStatus.UPDATED)));
    when(resourceConnector.upscale(any(), any(), any())).thenReturn(Collections.singletonList(new CloudResourceStatus(resource, ResourceStatus.UPDATED)));
    when(resourceConnector.downscale(any(), any(), any(), anyList(), anyMap())).thenReturn(Collections.singletonList(new CloudResourceStatus(resource, ResourceStatus.UPDATED)));
    when(instanceConnector.check(any(), any())).thenReturn(Collections.singletonList(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.STARTED)));
    CloudVmInstanceStatus collectInstanceStatus = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.IN_PROGRESS);
    when(collector.collect(any(), any(), any())).thenReturn(Collections.singletonList(new CloudVmMetaDataStatus(collectInstanceStatus, new CloudInstanceMetaData("privateIp", "publicIp", "hypervisor"))));
    when(instanceConnector.start(any(), any(), any())).thenReturn(Collections.singletonList(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.STARTED)));
    when(instanceConnector.stop(any(), any(), any())).thenReturn(Collections.singletonList(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.STOPPED)));
    when(instanceConnector.getConsoleOutput(any(), eq(cloudInstance))).thenReturn(g.getSshFingerprint() + "    RSA/n-----END SSH HOST KEY FINGERPRINTS-----");
    when(instanceConnector.getConsoleOutput(any(), eq(cloudInstanceBad))).thenReturn("XYZ    RSA/n-----END SSH HOST KEY FINGERPRINTS-----");
    return cloudConnector;
}
Also used : CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudCredentialStatus(com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus) Bean(org.springframework.context.annotation.Bean)

Aggregations

Builder (com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder)14 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)12 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)5 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)4 Operation (com.google.api.services.compute.model.Operation)3 ArrayList (java.util.ArrayList)3 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)2 Compute (com.google.api.services.compute.Compute)2 AwsInstanceProfileView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView)2 AwsNetworkView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView)2 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)2 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)2 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)2 Group (com.sequenceiq.cloudbreak.cloud.model.Group)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)1 AmazonCloudFormationClient (com.amazonaws.services.cloudformation.AmazonCloudFormationClient)1 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)1 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)1 Insert (com.google.api.services.compute.Compute.Networks.Insert)1