Search in sources :

Example 6 with CloudCredential

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

the class GcpProvisionSetup method checkImageStatus.

@Override
public ImageStatusResult checkImageStatus(AuthenticatedContext authenticatedContext, CloudStack stack, com.sequenceiq.cloudbreak.cloud.model.Image image) {
    CloudCredential credential = authenticatedContext.getCloudCredential();
    String projectId = getProjectId(credential);
    String imageName = image.getImageName();
    try {
        Image gcpApiImage = new Image();
        gcpApiImage.setName(getImageName(imageName));
        Compute compute = buildCompute(credential);
        Get getImages = compute.images().get(projectId, gcpApiImage.getName());
        String status = getImages.execute().getStatus();
        LOGGER.info("Status of image {} copy: {}", gcpApiImage.getName(), status);
        if (READY.equals(status)) {
            return new ImageStatusResult(ImageStatus.CREATE_FINISHED, ImageStatusResult.COMPLETED);
        }
    } catch (IOException e) {
        LOGGER.warn("Failed to retrieve image copy status", e);
        return new ImageStatusResult(ImageStatus.CREATE_FAILED, 0);
    }
    return new ImageStatusResult(ImageStatus.IN_PROGRESS, ImageStatusResult.HALF);
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) GcpStackUtil.buildCompute(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil.buildCompute) Compute(com.google.api.services.compute.Compute) Get(com.google.api.services.compute.Compute.Images.Get) IOException(java.io.IOException) Image(com.google.api.services.compute.model.Image) ImageStatusResult(com.sequenceiq.cloudbreak.common.type.ImageStatusResult)

Example 7 with CloudCredential

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

the class GcpProvisionSetup method prepareImage.

@Override
public void prepareImage(AuthenticatedContext authenticatedContext, CloudStack stack, com.sequenceiq.cloudbreak.cloud.model.Image image) {
    CloudCredential credential = authenticatedContext.getCloudCredential();
    CloudContext cloudContext = authenticatedContext.getCloudContext();
    try {
        String projectId = getProjectId(credential);
        String imageName = image.getImageName();
        Compute compute = buildCompute(credential);
        ImageList list = compute.images().list(projectId).execute();
        if (!containsSpecificImage(list, imageName)) {
            Storage storage = buildStorage(credential, cloudContext.getName());
            Bucket bucket = new Bucket();
            bucket.setName(String.format("%s-%s-%d", projectId, cloudContext.getName(), cloudContext.getId()));
            bucket.setStorageClass("STANDARD");
            try {
                Buckets.Insert ins = storage.buckets().insert(projectId, bucket);
                ins.execute();
            } catch (GoogleJsonResponseException ex) {
                if (ex.getStatusCode() != HttpStatus.SC_CONFLICT) {
                    throw ex;
                }
            }
            String tarName = getTarName(imageName);
            Copy copy = storage.objects().copy(getBucket(imageName), tarName, bucket.getName(), tarName, new StorageObject());
            copy.execute();
            Image gcpApiImage = new Image();
            gcpApiImage.setName(getImageName(imageName));
            RawDisk rawDisk = new RawDisk();
            rawDisk.setSource(String.format("http://storage.googleapis.com/%s/%s", bucket.getName(), tarName));
            gcpApiImage.setRawDisk(rawDisk);
            Insert ins = compute.images().insert(projectId, gcpApiImage);
            ins.execute();
        }
    } catch (Exception e) {
        Long stackId = cloudContext.getId();
        String msg = String.format("Error occurred on %s stack during the setup: %s", stackId, e.getMessage());
        LOGGER.error(msg, e);
        throw new CloudConnectorException(msg, e);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Image(com.google.api.services.compute.model.Image) Insert(com.google.api.services.compute.Compute.Images.Insert) Buckets(com.google.api.services.storage.Storage.Buckets) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) IOException(java.io.IOException) ImageList(com.google.api.services.compute.model.ImageList) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Storage(com.google.api.services.storage.Storage) GcpStackUtil.buildStorage(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil.buildStorage) GcpStackUtil.getBucket(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil.getBucket) Bucket(com.google.api.services.storage.model.Bucket) Copy(com.google.api.services.storage.Storage.Objects.Copy) GcpStackUtil.buildCompute(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil.buildCompute) Compute(com.google.api.services.compute.Compute) RawDisk(com.google.api.services.compute.model.Image.RawDisk)

Example 8 with CloudCredential

use of com.sequenceiq.cloudbreak.cloud.model.CloudCredential 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 9 with CloudCredential

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

the class GcpMetadataCollector method getCloudVmMetaDataStatus.

private CloudVmMetaDataStatus getCloudVmMetaDataStatus(AuthenticatedContext authenticatedContext, CloudResource cloudResource, CloudInstance matchedInstance) {
    CloudVmMetaDataStatus cloudVmMetaDataStatus;
    if (cloudResource != null) {
        CloudInstance cloudInstance = new CloudInstance(cloudResource.getName(), matchedInstance.getTemplate(), matchedInstance.getAuthentication());
        try {
            CloudCredential credential = authenticatedContext.getCloudCredential();
            CloudContext cloudContext = authenticatedContext.getCloudContext();
            Compute compute = GcpStackUtil.buildCompute(credential);
            Instance executeInstance = getInstance(cloudContext, credential, compute, cloudResource.getName());
            String privateIp = executeInstance.getNetworkInterfaces().get(0).getNetworkIP();
            String publicIp = null;
            List<AccessConfig> acl = executeInstance.getNetworkInterfaces().get(0).getAccessConfigs();
            if (acl != null && acl.get(0) != null) {
                publicIp = executeInstance.getNetworkInterfaces().get(0).getAccessConfigs().get(0).getNatIP();
            }
            CloudInstanceMetaData metaData = new CloudInstanceMetaData(privateIp, publicIp);
            CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
            cloudVmMetaDataStatus = new CloudVmMetaDataStatus(status, metaData);
        } catch (IOException e) {
            LOGGER.warn(String.format("Instance %s is not reachable", cloudResource.getName()), e);
            CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.UNKNOWN);
            cloudVmMetaDataStatus = new CloudVmMetaDataStatus(status, CloudInstanceMetaData.EMPTY_METADATA);
        }
    } else {
        CloudVmInstanceStatus status = new CloudVmInstanceStatus(matchedInstance, InstanceStatus.TERMINATED);
        cloudVmMetaDataStatus = new CloudVmMetaDataStatus(status, CloudInstanceMetaData.EMPTY_METADATA);
    }
    return cloudVmMetaDataStatus;
}
Also used : CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Instance(com.google.api.services.compute.model.Instance) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Compute(com.google.api.services.compute.Compute) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) IOException(java.io.IOException) AccessConfig(com.google.api.services.compute.model.AccessConfig)

Example 10 with CloudCredential

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

the class OpenStackPlatformResourcesTest method testVirtualMachines.

@Test
public void testVirtualMachines() {
    CloudCredential cloudCredential = new CloudCredential(0L, "name");
    OSClient osClient = mock(OSClient.class);
    String regionName = "region1";
    Set<String> regionsFromOpenStack = Sets.newHashSet(regionName);
    List<AvailabilityZone> availabilityZones = newArrayList(new AvailabilityZone("zone1"));
    Flavor flavor8GB = flavor(8192);
    when(openStackClient.createOSClient(cloudCredential)).thenReturn(osClient);
    when(openStackClient.getRegion(cloudCredential)).thenReturn(regionsFromOpenStack);
    when(openStackClient.getZones(osClient, regionName)).thenReturn(availabilityZones);
    when(openStackClient.getFlavors(osClient)).thenReturn((List) Collections.singletonList(flavor8GB));
    CloudVmTypes actual = underTest.virtualMachines(cloudCredential, null, null);
    Assert.assertEquals(1, actual.getCloudVmResponses().get("zone1").size());
    Assert.assertEquals(1, actual.getDefaultCloudVmResponses().size());
    Assert.assertEquals("8.0", actual.getCloudVmResponses().get("zone1").iterator().next().getMetaData().getProperties().get("Memory"));
    Assert.assertNotNull(actual.getDefaultCloudVmResponses().get(regionName));
}
Also used : CloudVmTypes(com.sequenceiq.cloudbreak.cloud.model.CloudVmTypes) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) OSClient(org.openstack4j.api.OSClient) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) Flavor(org.openstack4j.model.compute.Flavor) Test(org.junit.Test)

Aggregations

CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)47 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)27 Location (com.sequenceiq.cloudbreak.cloud.model.Location)18 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)10 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)9 OperationException (com.sequenceiq.cloudbreak.service.stack.connector.OperationException)9 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)8 Stack (com.sequenceiq.cloudbreak.domain.Stack)8 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)7 CloudAccessConfigs (com.sequenceiq.cloudbreak.cloud.model.CloudAccessConfigs)7 HashSet (java.util.HashSet)7 Compute (com.google.api.services.compute.Compute)6 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)6 InstanceMetaData (com.sequenceiq.cloudbreak.domain.InstanceMetaData)6 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)5 CloudRegions (com.sequenceiq.cloudbreak.cloud.model.CloudRegions)5 AvailabilityZone (com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone)4 CloudVmTypes (com.sequenceiq.cloudbreak.cloud.model.CloudVmTypes)4 Strings (com.google.common.base.Strings)3