use of com.google.api.services.compute.model.Operation in project halyard by spinnaker.
the class GoogleProviderUtils method ensureSpinnakerNetworkExists.
static String ensureSpinnakerNetworkExists(AccountDeploymentDetails<GoogleAccount> details) {
String networkName = getNetworkName();
String project = details.getAccount().getProject();
Compute compute = getCompute(details);
boolean exists = true;
try {
compute.networks().get(project, networkName).execute();
} catch (GoogleJsonResponseException e) {
if (e.getStatusCode() == 404) {
exists = false;
} else {
throw new HalException(FATAL, "Google error encountered retrieving network: " + e.getMessage(), e);
}
} catch (IOException e) {
throw new HalException(FATAL, "Failed to check if spinnaker network exists: " + e.getMessage(), e);
}
if (!exists) {
String networkUrl;
Network network = new Network().setAutoCreateSubnetworks(true).setName(networkName).setDescription("Spinnaker network auto-created by Halyard");
try {
DaemonTaskHandler.message("Creating a spinnaker network...");
Operation operation = compute.networks().insert(project, network).execute();
networkUrl = operation.getTargetLink();
GoogleProviderUtils.waitOnGlobalOperation(compute, project, operation);
} catch (IOException e) {
throw new HalException(FATAL, "Failed to create Spinnaker network: " + e.getMessage(), e);
}
Firewall.Allowed allowSsh = new Firewall.Allowed().setPorts(Collections.singletonList("22")).setIPProtocol("tcp");
Firewall firewallSsh = new Firewall().setNetwork(networkUrl).setAllowed(Collections.singletonList(allowSsh)).setName(networkName + "-allow-ssh").setSourceRanges(Collections.singletonList("0.0.0.0/0"));
Firewall.Allowed allowInternalTcp = new Firewall.Allowed().setPorts(Collections.singletonList("1-65535")).setIPProtocol("tcp");
Firewall.Allowed allowInternalUdp = new Firewall.Allowed().setPorts(Collections.singletonList("1-65535")).setIPProtocol("udp");
Firewall.Allowed allowInternalIcmp = new Firewall.Allowed().setIPProtocol("icmp");
List<Firewall.Allowed> allowInteral = new ArrayList<>();
allowInteral.add(allowInternalTcp);
allowInteral.add(allowInternalUdp);
allowInteral.add(allowInternalIcmp);
Firewall firewallInternal = new Firewall().setNetwork(networkUrl).setAllowed(allowInteral).setName(networkName + "-allow-internal").setSourceRanges(Collections.singletonList("10.0.0.0/8"));
try {
DaemonTaskHandler.message("Adding firewall rules...");
compute.firewalls().insert(project, firewallSsh).execute();
compute.firewalls().insert(project, firewallInternal).execute();
} catch (IOException e) {
throw new HalException(FATAL, "Failed to create Firewall rule network: " + e.getMessage(), e);
}
}
return String.format("projects/%s/global/networks/%s", project, networkName);
}
use of com.google.api.services.compute.model.Operation in project halyard by spinnaker.
the class GoogleProviderUtils method waitOnOperation.
private static void waitOnOperation(Supplier<Operation> operationSupplier) {
Operation operation = operationSupplier.get();
while (!operation.getStatus().equals("DONE")) {
if (operation.getError() != null) {
throw new HalException(FATAL, String.join("\n", operation.getError().getErrors().stream().map(e -> e.getCode() + ": " + e.getMessage()).collect(Collectors.toList())));
}
operation = operationSupplier.get();
DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(1));
}
}
use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.
the class GcpAttachedDiskResourceBuilderTest method setUpBuild.
@BeforeEach
void setUpBuild() throws Exception {
privateCrn = "crn";
CloudContext cloudContext = CloudContext.Builder.builder().withId(privateId).withName("testname").withCrn("crn").withPlatform("GCP").withWorkspaceId(WORKSPACE_ID).build();
CloudCredential cloudCredential = new CloudCredential(privateCrn, "credentialname", "account");
cloudCredential.putParameter("projectId", "projectId");
cloudInstance = new CloudInstance(instanceId, new InstanceTemplate("flavor", "group", 1L, new ArrayList<>(), InstanceStatus.CREATE_REQUESTED, new HashMap<>(), 1L, "img", TemporaryStorage.ATTACHED_VOLUMES, 0L), new InstanceAuthentication("pub", "pub", "cb"), "subnet1", "az1");
Location location = Location.location(Region.region("region"), AvailabilityZone.availabilityZone("az"));
context = new GcpContext(cloudContext.getName(), location, "projectId", "serviceAccountId", compute, false, 30, false);
List<CloudResource> networkResources = Collections.singletonList(new Builder().type(ResourceType.GCP_NETWORK).name("network-test").build());
context.addNetworkResources(networkResources);
privateId = 1L;
name = "master";
flavor = "m1.medium";
instanceId = "SOME_ID";
auth = new AuthenticatedContext(cloudContext, cloudCredential);
params = Map.of();
volumes = Arrays.asList(new Volume("/hadoop/fs1", "HDD", 1, CloudVolumeUsageType.GENERAL), new Volume("/hadoop/fs2", "HDD", 1, CloudVolumeUsageType.GENERAL));
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, emptyList());
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
InstanceTemplate instanceTemplate = new InstanceTemplate(flavor, name, privateId, volumes, InstanceStatus.CREATE_REQUESTED, params, 0L, "cb-centos66-amb200-2015-05-25", TemporaryStorage.ATTACHED_VOLUMES, 0L);
CloudInstance cloudInstance = new CloudInstance(instanceId, instanceTemplate, instanceAuthentication, "subnet-1", "az1");
group = new Group(name, InstanceGroupType.CORE, Collections.singletonList(cloudInstance), security, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), 50, Optional.empty(), createGroupNetwork(), emptyMap());
List<VolumeSetAttributes.Volume> volumes = new ArrayList<>();
volumes.add(new VolumeSetAttributes.Volume("1234", "noop", 0, "eph", CloudVolumeUsageType.GENERAL));
VolumeSetAttributes attributes = new VolumeSetAttributes("Ireland", true, "", volumes, 0, "eph");
Map<String, Object> params = new HashMap<>();
params.put(CloudResource.ATTRIBUTES, attributes);
buildableResource = List.of(CloudResource.builder().type(ResourceType.GCP_DISK).status(CommonStatus.REQUESTED).name("disk").params(params).build());
Map<InstanceGroupType, String> userData = ImmutableMap.of(InstanceGroupType.CORE, "CORE", InstanceGroupType.GATEWAY, "GATEWAY");
Image image = new Image("cb-centos66-amb200-2015-05-25", userData, "redhat6", "redhat6", "", "default", "default-id", new HashMap<>());
cloudStack = new CloudStack(Collections.emptyList(), null, image, emptyMap(), emptyMap(), null, null, null, null, null);
when(intermediateBuilderExecutor.submit(any(Callable.class))).thenAnswer(invocation -> {
Callable<Void> callable = invocation.getArgument(0);
return new MockFuture(callable);
});
operation = new Operation();
operation.setName("operation");
operation.setHttpErrorStatusCode(null);
when(compute.disks()).thenReturn(disks);
when(disks.insert(anyString(), anyString(), any(Disk.class))).thenReturn(insert);
when(insert.execute()).thenReturn(operation);
}
use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.
the class GcpInstanceResourceBuilderTest method addToInstanceGroupFailsAuth.
@Test
public void addToInstanceGroupFailsAuth() throws Exception {
// GIVEN
Group group = newGroupWithParams(ImmutableMap.of());
List<CloudResource> buildableResources = builder.create(context, group.getInstances().get(0), privateId, authenticatedContext, group, image);
List<CloudResource> resourcesWithGroup = buildableResources.stream().map(b -> CloudResource.builder().cloudResource(b).group(group.getName()).build()).collect(Collectors.toList());
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);
Operation addOperation = new Operation();
addOperation.setName("operation");
addOperation.setHttpErrorStatusCode(401);
addOperation.setHttpErrorMessage("Not Authorized");
addOperation.setError(new Operation.Error());
CloudResource instanceGroup = CloudResource.builder().type(ResourceType.GCP_INSTANCE_GROUP).status(CommonStatus.CREATED).name(group.getName()).group(group.getName()).build();
context.addGroupResources(group.getName(), Collections.singletonList(instanceGroup));
when(compute.instanceGroups()).thenReturn(instanceGroups);
when(instanceGroups.addInstances(anyString(), anyString(), anyString(), any())).thenReturn(addInstances);
InstanceGroups.List list = mock(InstanceGroups.List.class);
when(instanceGroups.list(anyString(), anyString())).thenReturn(list);
InstanceGroupList instanceGroupList = new InstanceGroupList();
instanceGroupList.setItems(singletonList(new InstanceGroup().setName(group.getName())));
when(list.execute()).thenReturn(instanceGroupList);
when(addInstances.execute()).thenReturn(addOperation);
Assert.assertThrows("Not Authorized", GcpResourceException.class, () -> builder.build(context, group.getInstances().get(0), privateId, authenticatedContext, group, resourcesWithGroup, cloudStack));
// THEN
verify(compute).instances();
verify(instances).insert(anyString(), anyString(), instanceArg.capture());
assertNull(instanceArg.getValue().getHostname());
}
use of com.google.api.services.compute.model.Operation in project cloudbreak by hortonworks.
the class GcpInstanceResourceBuilderTest method addInstanceGroupFromUpscale.
@Test
public void addInstanceGroupFromUpscale() throws Exception {
// GIVEN
Group group = newGroupWithParams(ImmutableMap.of());
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);
Operation addOperation = new Operation();
addOperation.setName("operation");
CloudResource instanceGroup = CloudResource.builder().type(ResourceType.GCP_INSTANCE_GROUP).status(CommonStatus.CREATED).name(group.getName()).group(group.getName()).build();
CloudResource instanceGroup2 = CloudResource.builder().type(ResourceType.GCP_INSTANCE_GROUP).status(CommonStatus.CREATED).name("gateway").build();
CloudResource instanceGroup3 = CloudResource.builder().type(ResourceType.GCP_INSTANCE_GROUP).status(CommonStatus.CREATED).name("idbroker").build();
CloudResource instanceGroup4 = CloudResource.builder().type(ResourceType.GCP_INSTANCE_GROUP).status(CommonStatus.CREATED).name("free-master0").build();
context.addGroupResources(group.getName(), List.of(instanceGroup4, instanceGroup2, instanceGroup, instanceGroup3));
when(compute.instanceGroups()).thenReturn(instanceGroups);
ArgumentCaptor<String> groupName = ArgumentCaptor.forClass(String.class);
when(instanceGroups.addInstances(anyString(), anyString(), groupName.capture(), any())).thenReturn(addInstances);
InstanceGroups.List list = mock(InstanceGroups.List.class);
when(instanceGroups.list(anyString(), anyString())).thenReturn(list);
InstanceGroupList instanceGroupList = new InstanceGroupList();
instanceGroupList.setItems(singletonList(new InstanceGroup().setName(group.getName())));
when(list.execute()).thenReturn(instanceGroupList);
when(addInstances.execute()).thenReturn(addOperation);
builder.build(context, group.getInstances().get(0), privateId, authenticatedContext, group, buildableResources, cloudStack);
// THEN
verify(compute).instances();
assertEquals("master", groupName.getValue());
verify(instances).insert(anyString(), anyString(), instanceArg.capture());
assertNull(instanceArg.getValue().getHostname());
}
Aggregations