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);
}
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;
}
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;
}
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());
}
}
Aggregations