Search in sources :

Example 6 with CloudGcsView

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

the class GcpInstanceResourceBuilderTest method extraxtServiceAccountWhenServiceEmailNotEmpty.

@Test
public void extraxtServiceAccountWhenServiceEmailNotEmpty() throws Exception {
    // GIVEN
    String email = "service@email.com";
    CloudGcsView cloudGcsView = new CloudGcsView(CloudIdentityType.LOG);
    cloudGcsView.setServiceAccountEmail(email);
    CloudStack cloudStack = new CloudStack(Collections.emptyList(), new Network(null), image, emptyMap(), emptyMap(), null, null, null, null, new SpiFileSystem("test", FileSystemType.GCS, List.of(cloudGcsView)));
    Group group = newGroupWithParams(ImmutableMap.of(), cloudGcsView);
    List<CloudResource> buildableResources = builder.create(context, group.getInstances().get(0), privateId, authenticatedContext, group, image);
    context.addComputeResources(0L, buildableResources);
    // WHEN
    when(compute.instances()).thenReturn(instances);
    when(instances.insert(anyString(), anyString(), any(Instance.class))).thenReturn(insert);
    when(insert.setPrettyPrint(anyBoolean())).thenReturn(insert);
    when(insert.execute()).thenReturn(operation);
    builder.build(context, group.getInstances().get(0), privateId, authenticatedContext, group, buildableResources, cloudStack);
    // THEN
    verify(compute).instances();
    verify(instances).insert(anyString(), anyString(), instanceArg.capture());
    assertEquals(instanceArg.getValue().getServiceAccounts().get(0).getEmail(), email);
}
Also used : InstanceGroup(com.google.api.services.compute.model.InstanceGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) CloudGcsView(com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudGcsView) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.google.api.services.compute.model.Instance) GroupNetwork(com.sequenceiq.cloudbreak.cloud.model.GroupNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SpiFileSystem(com.sequenceiq.cloudbreak.cloud.model.SpiFileSystem) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Test(org.junit.Test)

Example 7 with CloudGcsView

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

the class GcpServiceAccountObjectStorageValidator method validateObjectStorage.

public ValidationResultBuilder validateObjectStorage(CloudCredential cloudCredential, SpiFileSystem spiFileSystem, ValidationResultBuilder resultBuilder) throws IOException {
    LOGGER.info("Validating Gcp identities...");
    Iam iam = gcpIamFactory.buildIam(cloudCredential);
    List<CloudFileSystemView> cloudFileSystems = spiFileSystem.getCloudFileSystems();
    if (Objects.nonNull(cloudFileSystems) && cloudFileSystems.size() > 0) {
        String projectId = gcpStackUtil.getProjectId(cloudCredential);
        Set<String> serviceAccountEmailsToFind = cloudFileSystems.stream().map(cloudFileSystemView -> ((CloudGcsView) cloudFileSystemView).getServiceAccountEmail()).collect(Collectors.toSet());
        Iam.Projects.ServiceAccounts.List listServiceAccountEmailsRequest = iam.projects().serviceAccounts().list("projects/" + projectId).setPageSize(DEFAULT_PAGE_SIZE);
        ListServiceAccountsResponse response;
        do {
            response = listServiceAccountEmailsRequest.execute();
            response.getAccounts().forEach(serviceAccount -> serviceAccountEmailsToFind.remove(serviceAccount.getEmail()));
            listServiceAccountEmailsRequest.setPageToken(response.getNextPageToken());
        } while (response.getNextPageToken() != null && !serviceAccountEmailsToFind.isEmpty());
        if (!serviceAccountEmailsToFind.isEmpty()) {
            addError(resultBuilder, String.format("Service Account with email(s) '%s' could not be found in the configured Google Cloud project '%s'.", String.join(", ", serviceAccountEmailsToFind), projectId));
        }
    }
    return resultBuilder;
}
Also used : Logger(org.slf4j.Logger) ListServiceAccountsResponse(com.google.api.services.iam.v1.model.ListServiceAccountsResponse) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) SpiFileSystem(com.sequenceiq.cloudbreak.cloud.model.SpiFileSystem) IOException(java.io.IOException) CloudFileSystemView(com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudFileSystemView) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Inject(javax.inject.Inject) List(java.util.List) Component(org.springframework.stereotype.Component) ValidationResultBuilder(com.sequenceiq.cloudbreak.validation.ValidationResult.ValidationResultBuilder) Iam(com.google.api.services.iam.v1.Iam) GcpIamFactory(com.sequenceiq.cloudbreak.cloud.gcp.client.GcpIamFactory) CloudGcsView(com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudGcsView) GcpStackUtil(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil) ListServiceAccountsResponse(com.google.api.services.iam.v1.model.ListServiceAccountsResponse) CloudGcsView(com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudGcsView) Iam(com.google.api.services.iam.v1.Iam) CloudFileSystemView(com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudFileSystemView)

Example 8 with CloudGcsView

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

the class CloudStorageParametersConverter method gcsToCloudView.

public CloudGcsView gcsToCloudView(StorageIdentityBase source) {
    CloudGcsView cloudGcsView = new CloudGcsView(source.getType());
    cloudGcsView.setServiceAccountEmail(source.getGcs().getServiceAccountEmail());
    return cloudGcsView;
}
Also used : CloudGcsView(com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudGcsView)

Example 9 with CloudGcsView

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

the class GcpInstanceResourceBuilder method build.

@Override
public List<CloudResource> build(GcpContext context, CloudInstance cloudInstance, long privateId, AuthenticatedContext auth, Group group, List<CloudResource> buildableResource, CloudStack cloudStack) throws Exception {
    InstanceTemplate template = group.getReferenceInstanceTemplate();
    String projectId = context.getProjectId();
    String location = cloudInstance.getAvailabilityZone();
    Compute compute = context.getCompute();
    List<CloudResource> computeResources = context.getComputeResources(privateId);
    List<AttachedDisk> listOfDisks = new ArrayList<>();
    listOfDisks.addAll(getBootDiskList(computeResources, projectId, cloudInstance.getAvailabilityZone()));
    listOfDisks.addAll(getAttachedDisks(computeResources, projectId));
    listOfDisks.forEach(disk -> customGcpDiskEncryptionService.addEncryptionKeyToDisk(template, disk));
    Instance instance = new Instance();
    instance.setMachineType(String.format("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/%s", projectId, location, template.getFlavor()));
    instance.setDescription(description());
    instance.setName(buildableResource.get(0).getName());
    Optional<CloudFileSystemView> cloudFileSystemView = group.getIdentity();
    if (cloudFileSystemView.isPresent()) {
        CloudGcsView gcsView = (CloudGcsView) cloudFileSystemView.get();
        ServiceAccount serviceAccount = new ServiceAccount();
        serviceAccount.setEmail(gcsView.getServiceAccountEmail());
        serviceAccount.setScopes(Arrays.asList(GCP_CLOUD_STORAGE_RW_SCOPE));
        instance.setServiceAccounts(Arrays.asList(serviceAccount));
    }
    // For FreeIPA hosts set the hostname during creation to avoid Google Network Manager overriding it with internal hostnames
    if (cloudStack.getParameters() != null && cloudStack.getParameters().getOrDefault(CLOUD_STACK_TYPE_PARAMETER, "").equals(FREEIPA_STACK_TYPE)) {
        String hostname = getHostname(group, privateId);
        if (hostname != null) {
            instance.setHostname(hostname);
        }
    }
    instance.setCanIpForward(Boolean.TRUE);
    instance.setNetworkInterfaces(getNetworkInterface(context, computeResources, group, cloudStack, cloudInstance));
    instance.setDisks(listOfDisks);
    instance.setServiceAccounts(extractServiceAccounts(group));
    Scheduling scheduling = new Scheduling();
    boolean preemptible = false;
    if (template.getParameter(PREEMPTIBLE, Boolean.class) != null) {
        preemptible = template.getParameter(PREEMPTIBLE, Boolean.class);
    }
    scheduling.setPreemptible(preemptible);
    instance.setScheduling(scheduling);
    configureTagsOnInstance(auth, group, instance);
    configureLabelsOnInstance(cloudStack, instance);
    Metadata metadata = new Metadata();
    metadata.setItems(new ArrayList<>());
    Items sshMetaData = new Items();
    sshMetaData.setKey("ssh-keys");
    sshMetaData.setValue(getPublicKey(group.getPublicKey(), group.getLoginUserName()));
    Items blockProjectWideSsh = new Items();
    blockProjectWideSsh.setKey("block-project-ssh-keys");
    blockProjectWideSsh.setValue("TRUE");
    Items startupScript = new Items();
    startupScript.setKey("startup-script");
    startupScript.setValue(cloudStack.getImage().getUserDataByType(group.getType()));
    metadata.getItems().add(sshMetaData);
    metadata.getItems().add(startupScript);
    metadata.getItems().add(blockProjectWideSsh);
    instance.setMetadata(metadata);
    Insert insert = compute.instances().insert(projectId, cloudInstance.getAvailabilityZone(), instance);
    insert.setPrettyPrint(Boolean.TRUE);
    try {
        Operation operation = insert.execute();
        verifyOperation(operation, buildableResource);
        updateDiskSetWithInstanceName(auth, computeResources, instance);
        assignToExistingInstanceGroup(context, group, instance, buildableResource);
        return singletonList(createOperationAwareCloudResource(buildableResource.get(0), operation));
    } catch (GoogleJsonResponseException e) {
        throw new GcpResourceException(checkException(e), resourceType(), buildableResource.get(0).getName());
    }
}
Also used : ServiceAccount(com.google.api.services.compute.model.ServiceAccount) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.google.api.services.compute.model.Instance) ArrayList(java.util.ArrayList) Metadata(com.google.api.services.compute.model.Metadata) AttachedDisk(com.google.api.services.compute.model.AttachedDisk) Scheduling(com.google.api.services.compute.model.Scheduling) Operation(com.google.api.services.compute.model.Operation) Insert(com.google.api.services.compute.Compute.Instances.Insert) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) CloudGcsView(com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudGcsView) Compute(com.google.api.services.compute.Compute) CloudFileSystemView(com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudFileSystemView) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) Items(com.google.api.services.compute.model.Metadata.Items) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Aggregations

CloudGcsView (com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudGcsView)9 Instance (com.google.api.services.compute.model.Instance)2 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)2 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)2 SpiFileSystem (com.sequenceiq.cloudbreak.cloud.model.SpiFileSystem)2 CloudAdlsGen2View (com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudAdlsGen2View)2 CloudFileSystemView (com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudFileSystemView)2 CloudS3View (com.sequenceiq.cloudbreak.cloud.model.filesystem.CloudS3View)2 AdlsGen2CloudStorageV1Parameters (com.sequenceiq.common.api.cloudstorage.old.AdlsGen2CloudStorageV1Parameters)2 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 Compute (com.google.api.services.compute.Compute)1 Insert (com.google.api.services.compute.Compute.Instances.Insert)1 AttachedDisk (com.google.api.services.compute.model.AttachedDisk)1 InstanceGroup (com.google.api.services.compute.model.InstanceGroup)1 Metadata (com.google.api.services.compute.model.Metadata)1 Items (com.google.api.services.compute.model.Metadata.Items)1 Operation (com.google.api.services.compute.model.Operation)1 Scheduling (com.google.api.services.compute.model.Scheduling)1 ServiceAccount (com.google.api.services.compute.model.ServiceAccount)1 Iam (com.google.api.services.iam.v1.Iam)1