【发布时间】:2016-09-22 03:31:20
【问题描述】:
我使用以下代码使用 Google Cloud java(https://github.com/GoogleCloudPlatform/google-cloud-java) 从 Java 创建实例
public class CreateInstance {
public static void main(String... args) throws InterruptedException, TimeoutException {
Compute compute = ComputeOptions.defaultInstance().service();
ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329");
NetworkId networkId = NetworkId.of("default");
AttachedDisk attachedDisk = AttachedDisk.of(AttachedDisk.CreateDiskConfiguration.of(imageId));
NetworkInterface networkInterface = NetworkInterface.of(networkId);
InstanceId instanceId = InstanceId.of("us-central1-a", "instance-name");
MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1");
Operation operation =
compute.create(InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface));
operation = operation.waitFor();
if (operation.errors() == null) {
// use instance
Instance instance = compute.getInstance(instanceId);
}
}
}
Evrything 工作正常,但所有创建的实例默认都禁用 HTTP 和 HTTPS,我如何创建默认启用 HTTP 的实例。
我已经有这种方法了:
Firewall firewall = new Firewall();
firewall.setName(name);
Firewall.Allowed allowedTCP8080 = new Firewall.Allowed();
allowedTCP8080.setIPProtocol("tcp");
List<String> ports = new ArrayList<>();
ports.add("80");
ports.add("8080");
ports.add("8280");
ports.add("8180");
ports.add("9610");
allowedTCP8080.setPorts(ports);
List<Firewall.Allowed> allowedPorts = new ArrayList<>();
allowedPorts.add(allowedTCP8080);
firewall.setAllowed(allowedPorts);
firewall.setDescription("httpports");
firewall.setNetwork(googleCloudUtils.networkId.network());
但我不知道如何将防火墙附加到网络配置和/或实例。
【问题讨论】:
-
您使用的是什么版本的 API?当我查看源代码时,我在任何地方都没有看到 Firewall 类。
-
@WillHayworth 0.3 版:
就是在这个包包com.google.api.services.compute.model;com.google.cloud google-cloud-compute 0.3.0
标签: java http google-cloud-platform