本文整理汇总了Java中com.google.api.services.compute.model.AccessConfig类的典型用法代码示例。如果您正苦于以下问题:Java AccessConfig类的具体用法?Java AccessConfig怎么用?Java AccessConfig使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AccessConfig类属于com.google.api.services.compute.model包,在下文中一共展示了AccessConfig类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createInstanceTemplate
import com.google.api.services.compute.model.AccessConfig; //导入依赖的package包/类
/**
* Create an instance template for later provisioning.
* @param userEmail The service account's client email.
* @param projectId The project id.
* @param zoneId The zone id.
* @param scopes The priority scopes.
* @return The instance template.
*/
private static Instance createInstanceTemplate(String userEmail, String projectId, String zoneId,
List<String> scopes) {
Instance instance = new Instance();
instance.setMachineType(String.format(ENUMERATION_TEST_MACHINE_TYPE, projectId, zoneId));
NetworkInterface ifc = new NetworkInterface();
ifc.setNetwork(String.format(NETWORK_INTERFACE, projectId));
List<AccessConfig> configs = new ArrayList<>();
AccessConfig config = new AccessConfig();
config.setType(NETWORK_INTERFACE_CONFIG);
config.setName(NETWORK_ACCESS_CONFIG);
configs.add(config);
ifc.setAccessConfigs(configs);
instance.setNetworkInterfaces(Collections.singletonList(ifc));
AttachedDisk disk = new AttachedDisk();
disk.setBoot(true);
disk.setAutoDelete(true);
disk.setType(DISK_TYPE_PERSISTENT);
AttachedDiskInitializeParams params = new AttachedDiskInitializeParams();
params.setSourceImage(SOURCE_IMAGE);
params.setDiskType(String.format(DISK_TYPE, projectId, zoneId));
disk.setInitializeParams(params);
instance.setDisks(Collections.singletonList(disk));
ServiceAccount account = new ServiceAccount();
account.setEmail(userEmail);
account.setScopes(scopes);
instance.setServiceAccounts(Collections.singletonList(account));
return instance;
}
示例2: defaultInstanceTemplate
import com.google.api.services.compute.model.AccessConfig; //导入依赖的package包/类
/**
* Creates the default instance template for each type. Each type only changes the name and
* startup script used.
*/
private InstanceTemplate defaultInstanceTemplate(String type) {
AccessConfig config = new AccessConfig();
config.setType("ONE_TO_ONE_NAT");
config.setName("External NAT");
return new InstanceTemplate()
.setName("cps-loadtest-" + type + "-" + cores)
.setProperties(new InstanceProperties()
.setMachineType(MACHINE_TYPE + cores)
.setDisks(Collections.singletonList(new AttachedDisk()
.setBoot(true)
.setAutoDelete(true)
.setInitializeParams(new AttachedDiskInitializeParams()
.setSourceImage(SOURCE_FAMILY))))
.setNetworkInterfaces(Collections.singletonList(new NetworkInterface()
.setNetwork("global/networks/default")
.setAccessConfigs(Collections.singletonList(config))))
.setMetadata(new Metadata()
.setItems(ImmutableList.of(
new Metadata.Items()
.setKey("startup-script-url")
.setValue("https://storage.googleapis.com/"
+ projectName
+ "-cloud-pubsub-loadtest/"
+ type
+ "_startup_script.sh"),
new Metadata.Items()
.setKey("bucket")
.setValue(projectName + "-cloud-pubsub-loadtest"))))
.setServiceAccounts(Collections.singletonList(new ServiceAccount().setScopes(
Collections.singletonList("https://www.googleapis.com/auth/cloud-platform")))));
}
示例3: getPropertyValue
import com.google.api.services.compute.model.AccessConfig; //导入依赖的package包/类
@Override
protected String getPropertyValue(Instance instance, Disk bootDisk) {
List<NetworkInterface> networkInterfaceList = instance.getNetworkInterfaces();
if (networkInterfaceList != null && networkInterfaceList.size() > 0) {
List<AccessConfig> accessConfigList = networkInterfaceList.get(0).getAccessConfigs();
if (accessConfigList != null && accessConfigList.size() > 0) {
return accessConfigList.get(0).getNatIP();
}
}
return null;
}
示例4: instance
import com.google.api.services.compute.model.AccessConfig; //导入依赖的package包/类
private Instance instance(String name, String status, String privateIp, String publicIp) {
String zone = region() + "-" + zoneQualfier();
List<NetworkInterface> networkInterfaces = Arrays.asList(new NetworkInterface().setName("nic0")
.setNetworkIP(privateIp).setAccessConfigs(Arrays.asList(new AccessConfig().setNatIP(publicIp))));
return new Instance().setName(name).setStatus(status).setZone(zoneUrl(zone))
.setCreationTimestamp(UtcTime.parse("2017-01-01T12:00:00.000Z").toString()).setKind("compute#instance")
.setMachineType(machineTypeUrl("n1-standard-1", zone)).setNetworkInterfaces(networkInterfaces)
.setSelfLink(instanceUrl(name, zone))//
.setMetadata(new Metadata().setItems(new ArrayList<>()));
}
示例5: instance
import com.google.api.services.compute.model.AccessConfig; //导入依赖的package包/类
private Instance instance(String name, String status, String privateIp, String publicIp) {
List<NetworkInterface> networkInterfaces = Arrays.asList(new NetworkInterface().setName("nic0")
.setNetworkIP(privateIp).setAccessConfigs(Arrays.asList(new AccessConfig().setNatIP(publicIp))));
return new Instance().setName(name).setStatus(status).setZone(zoneUrl())
.setCreationTimestamp(UtcTime.parse("2017-01-01T12:00:00.000Z").toString()).setKind("compute#instance")
.setMachineType(machineTypeUrl("n1-standard-1")).setNetworkInterfaces(networkInterfaces)
.setSelfLink(instanceUrl(name))//
.setMetadata(new Metadata().setItems(new ArrayList<>()));
}
示例6: getNetworkInterface
import com.google.api.services.compute.model.AccessConfig; //导入依赖的package包/类
private List<NetworkInterface> getNetworkInterface(List<CloudResource> networkResources, List<CloudResource> computeResources,
Region region, Group group, Compute compute, String projectId, boolean noPublicIp) throws IOException {
NetworkInterface networkInterface = new NetworkInterface();
List<CloudResource> subnet = filterResourcesByType(networkResources, ResourceType.GCP_SUBNET);
String networkName = subnet.isEmpty() ? filterResourcesByType(networkResources, ResourceType.GCP_NETWORK).get(0).getName() : subnet.get(0).getName();
networkInterface.setName(networkName);
if (!noPublicIp) {
AccessConfig accessConfig = new AccessConfig();
accessConfig.setName(networkName);
accessConfig.setType("ONE_TO_ONE_NAT");
List<CloudResource> reservedIp = filterResourcesByType(computeResources, ResourceType.GCP_RESERVED_IP);
if (InstanceGroupType.GATEWAY == group.getType() && !reservedIp.isEmpty()) {
Addresses.Get getReservedIp = compute.addresses().get(projectId, region.value(), reservedIp.get(0).getName());
accessConfig.setNatIP(getReservedIp.execute().getAddress());
}
networkInterface.setAccessConfigs(Collections.singletonList(accessConfig));
}
if (subnet.isEmpty()) {
networkInterface.setNetwork(
String.format("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", projectId, networkName));
} else {
networkInterface.setSubnetwork(
String.format("https://www.googleapis.com/compute/v1/projects/%s/regions/%s/subnetworks/%s", projectId, region.value(), networkName));
}
return Collections.singletonList(networkInterface);
}
示例7: startInstance
import com.google.api.services.compute.model.AccessConfig; //导入依赖的package包/类
public static Operation startInstance(Compute compute, String instanceName) throws IOException {
System.out.println("================== Starting New Instance ==================");
// Create VM Instance object with the required properties.
Instance instance = new Instance();
instance.setName(instanceName);
instance.setMachineType(
"https://www.googleapis.com/compute/v1/projects/"
+ PROJECT_ID + "/zones/" + ZONE_NAME + "/machineTypes/n1-standard-1");
// Add Network Interface to be used by VM Instance.
NetworkInterface ifc = new NetworkInterface();
ifc.setNetwork("https://www.googleapis.com/compute/v1/projects/" + PROJECT_ID + "/global/networks/default");
List<AccessConfig> configs = new ArrayList<>();
AccessConfig config = new AccessConfig();
config.setType(NETWORK_INTERFACE_CONFIG);
config.setName(NETWORK_ACCESS_CONFIG);
configs.add(config);
ifc.setAccessConfigs(configs);
instance.setNetworkInterfaces(Collections.singletonList(ifc));
// Add attached Persistent Disk to be used by VM Instance.
AttachedDisk disk = new AttachedDisk();
disk.setBoot(true);
disk.setAutoDelete(true);
disk.setType("PERSISTENT");
AttachedDiskInitializeParams params = new AttachedDiskInitializeParams();
// Assign the Persistent Disk the same name as the VM Instance.
params.setDiskName(instanceName);
// Specify the source operating system machine image to be used by the VM Instance.
params.setSourceImage(SOURCE_IMAGE_PREFIX + SOURCE_IMAGE_PATH);
// Specify the disk type as Standard Persistent Disk
params.setDiskType("https://www.googleapis.com/compute/v1/projects/" + PROJECT_ID + "/zones/"
+ ZONE_NAME + "/diskTypes/pd-standard");
disk.setInitializeParams(params);
instance.setDisks(Collections.singletonList(disk));
// Initialize the service account to be used by the VM Instance and set the API access scopes.
ServiceAccount account = new ServiceAccount();
account.setEmail("default");
List<String> scopes = new ArrayList<>();
scopes.add("https://www.googleapis.com/auth/devstorage.full_control");
scopes.add("https://www.googleapis.com/auth/compute");
account.setScopes(scopes);
instance.setServiceAccounts(Collections.singletonList(account));
// Optional - Add a startup script to be used by the VM Instance.
Metadata meta = new Metadata();
Metadata.Items item = new Metadata.Items();
item.setKey("startup-script-url");
// If you put a script called "vm-startup.sh" in this Google Cloud Storage
// bucket, it will execute on VM startup. This assumes you've created a
// bucket named the same as your PROJECT_ID.
// For info on creating buckets see: https://cloud.google.com/storage/docs/cloud-console#_creatingbuckets
item.setValue("gs://" + PROJECT_ID + "/vm-startup.sh");
meta.setItems(Collections.singletonList(item));
instance.setMetadata(meta);
System.out.println(instance.toPrettyString());
Compute.Instances.Insert insert = compute.instances().insert(PROJECT_ID, ZONE_NAME, instance);
return insert.execute();
}
示例8: createInstanceFromTemplate
import com.google.api.services.compute.model.AccessConfig; //导入依赖的package包/类
protected Instance createInstanceFromTemplate(InstanceTemplate template)
{
Instance instance = new Instance()
.setMachineType(template.getInstanceType().getName())
.setName(template.getInstanceName())
.setNetworkInterfaces(new ArrayList<NetworkInterface>());
instance.getNetworkInterfaces().add(new NetworkInterface().setAccessConfigs(new ArrayList<AccessConfig>()).setNetwork(
String.format("https://www.googleapis.com/compute/v1/projects/%s/global/networks/default", credentials_.getProject())));
instance.getNetworkInterfaces().get(0).getAccessConfigs().add(new AccessConfig().setName("External NAT").setType("ONE_TO_ONE_NAT"));
instance.setZone(template.getRegion().getName());
instance.setMachineType(getMachineType(instance.getZone(), template.getInstanceType().getName()).getSelfLink());
instance.setDisks(new ArrayList<AttachedDisk>());
instance.getDisks().add(new AttachedDisk()
.setType("PERSISTENT")
.setAutoDelete(false)
.setBoot(true)
.setMode("READ_WRITE")
.setInitializeParams(
new AttachedDiskInitializeParams()
.setDiskName(template.getInstanceName())
.setDiskSizeGb(SystemUtils2.getIntegerProperty("org.excalibur.default.disk.size", 10).longValue())
.setSourceImage(VM_IMAGES_NAME_MAPPING.get(template.getImageId()))));
List<Items> items = new ArrayList<Metadata.Items>();
Metadata metadata = new Metadata().setItems(items);
instance.setMetadata(metadata);
items.add
( new Items().setKey("sshKeys")
.setValue(String.format("%s:%s", template.getLoginCredentials().getUser(),
decrypt(template.getLoginCredentials().getPublicKey()).replaceAll("\n", "")))
);
items.add(new Items().setKey("image-id").setValue(template.getImageId()));
items.add(new Items().setKey("keyname").setValue(template.getKeyName().toLowerCase()));
items.add(new Items().setKey("zone").setValue(instance.getZone()));
items.add(new Items().setKey("platform").setValue(Platform.LINUX.name().toLowerCase()));
items.add(new Items().setKey("platform-username").setValue(template.getLoginCredentials().getUser()));
items.add(new Items().setKey("owner").setValue(template.getLoginCredentials().getUser()));
items.add(new Items().setKey("pem-key").setValue(Base64.encodeBase64URLSafeString(template.getLoginCredentials().getPrivateKey().getBytes())));
for (Tag tag: template.getTags())
{
items.add(new Items().setKey(tag.getName()).setValue(tag.getValue()));
}
return instance;
}
示例9: getInstancefromComputeInstance
import com.google.api.services.compute.model.AccessConfig; //导入依赖的package包/类
private VirtualMachine getInstancefromComputeInstance(Instance computeInstance)
{
MachineType machineType = getMachineType(splitAndGetLast("/", computeInstance.getZone()),
splitAndGetLast("/", computeInstance.getMachineType()));
NetworkInterface networkInterface = computeInstance.getNetworkInterfaces().get(0);
Map<String, String> metadata = METADATA_ITEMS_TO_MAP.apply(computeInstance.getMetadata().getItems());
checkState(metadata.size() >= 8);
AccessConfig accessConfig = Lists.newArrayList(Iterables.filter(networkInterface.getAccessConfigs(), new Predicate<AccessConfig>()
{
@Override
public boolean apply(@Nullable AccessConfig input)
{
return "ONE_TO_ONE_NAT".equals(input.getType());
}
})).get(0);
VirtualMachine instance = new VirtualMachine();
KeyPairs keypairs = new KeyPairs();
keypairs.setPrivateKey
(
new KeyPair().setKeyName(metadata.get("keyname"))
.setKeyMaterial(new String(Base64.decodeBase64(metadata.get("pem-key"))))
)
.setPublicKey
(
new KeyPair().setKeyName(metadata.get("keyname"))
.setKeyMaterial(cipher(metadata.get("sshKeys").split(":")[1]))
);
instance.setConfiguration(new VmConfiguration()
.setKeyName(metadata.get("keyname"))
.setPlatform(metadata.get("platform"))
.setPlatformUserName(metadata.get("platform-username"))
.setPrivateIpAddress(networkInterface.getNetworkIP())
.setPublicDnsName(accessConfig.getNatIP())
.setPublicIpAddress(accessConfig.getNatIP())
.setKeyPairs(keypairs));
instance.setImageId(metadata.get("image-id"))
.setLaunchTime(
new Date(DateTime.parseRfc3339(computeInstance.getCreationTimestamp()).getValue()))
.setName(computeInstance.getName())
.setPlacement(new Placement().setZone(metadata.get("zone")))
.setState(
new InstanceStateDetails()
//.setState(InstanceStateType.valueOf(computeInstance.getStatus()))
.setState(InstanceStateType.RUNNING)
.setTime(instance.getLaunchTime()))
.setType(new InstanceType().setName(machineType.getName())
.setId(machineType.getId().intValue())
.setProvider(this.credentials_.getProvider()))
.setLocation(new org.excalibur.core.cloud.api.domain.Zone().setName(metadata.get("zone")));
Disk disk = getBootDisk(computeInstance);
instance.getType().getConfiguration()
.setNumberOfCores(machineType.getGuestCpus())
.setRamMemorySizeGb
(
BigDecimal.valueOf(machineType.getMemoryMb().doubleValue() / 1024)
.setScale(2,java.math.RoundingMode.HALF_EVEN)
.doubleValue()
)
.setDiskSizeGb(disk.getSizeGb());
instance.setOwner(new User().setId(this.credentials_.getUserId()).setUsername(metadata.get("owner")));
for (String tagKey: metadata.keySet())
{
instance.getTags().add(new Tag().setName(tagKey).setValue(metadata.get(tagKey)));
}
return instance;
}
示例10: apply
import com.google.api.services.compute.model.AccessConfig; //导入依赖的package包/类
@Override
public Machine apply(Instance instance) {
Builder builder = Machine.builder();
// instance URL is machine id: For example,
// https://www.googleapis.com/compute/v1/projects/<project>/zones/europe-west1-d/instances/webservers-d58p
builder.id(instance.getSelfLink());
builder.cloudProvider(CloudProviders.GCE);
builder.region(ZoneUtils.regionName(ZoneUtils.zoneName(instance.getZone())));
builder.machineSize(UrlUtils.basename(instance.getMachineType()));
builder.machineState(new InstanceStatusToMachineStatus().apply(instance.getStatus()));
builder.launchTime(UtcTime.parse(instance.getCreationTimestamp()));
// only one network interface per instance appears to be supported
List<NetworkInterface> networkInterfaces = instance.getNetworkInterfaces();
if (networkInterfaces != null && !networkInterfaces.isEmpty()) {
NetworkInterface networkInterface = networkInterfaces.get(0);
String privateIp = networkInterface.getNetworkIP();
if (privateIp != null) {
builder.privateIp(privateIp);
}
List<AccessConfig> accessConfigs = networkInterface.getAccessConfigs();
if (accessConfigs != null && !accessConfigs.isEmpty()) {
String publicIp = accessConfigs.get(0).getNatIP();
if (publicIp != null) {
builder.publicIp(publicIp);
}
}
}
ServiceState serviceState = extractServiceState(instance);
if (serviceState != null) {
builder.serviceState(serviceState);
}
MembershipStatus membershipStatus = extractMembershipStatus(instance);
if (membershipStatus != null) {
builder.membershipStatus(membershipStatus);
}
return builder.build();
}
示例11: startInstance
import com.google.api.services.compute.model.AccessConfig; //导入依赖的package包/类
public static Operation startInstance(Compute compute, String instanceName) throws IOException {
System.out.println("================== Starting New Instance ==================");
// Create VM Instance object with the required properties.
Instance instance = new Instance();
instance.setName(instanceName);
instance.setMachineType("https://www.googleapis.com/compute/v1/projects/" + PROJECT_ID +
"/zones/" + ZONE_NAME + "/machineTypes/n1-standard-1");
// Add Network Interface to be used by VM Instance.
NetworkInterface ifc = new NetworkInterface();
ifc.setNetwork("https://www.googleapis.com/compute/v1/projects/" + PROJECT_ID + "/global/networks/default");
List<AccessConfig> configs = new ArrayList<>();
AccessConfig config = new AccessConfig();
config.setType(NETWORK_INTERFACE_CONFIG);
config.setName(NETWORK_ACCESS_CONFIG);
configs.add(config);
ifc.setAccessConfigs(configs);
instance.setNetworkInterfaces(Collections.singletonList(ifc));
// Add attached Persistent Disk to be used by VM Instance.
AttachedDisk disk = new AttachedDisk();
disk.setBoot(true);
disk.setAutoDelete(true);
disk.setType("PERSISTENT");
AttachedDiskInitializeParams params = new AttachedDiskInitializeParams();
// Assign the Persistent Disk the same name as the VM Instance.
params.setDiskName(instanceName);
// Specify the source operating system machine image to be used by the VM Instance.
params.setSourceImage(SOURCE_IMAGE_PREFIX + SOURCE_IMAGE_PATH);
// Specify the disk type as Standard Persistent Disk
params.setDiskType("https://www.googleapis.com/compute/v1/projects/" + PROJECT_ID + "/zones/"
+ ZONE_NAME + "/diskTypes/pd-standard");
disk.setInitializeParams(params);
instance.setDisks(Collections.singletonList(disk));
// Initialize the service account to be used by the VM Instance and set the API access scopes.
ServiceAccount account = new ServiceAccount();
account.setEmail("default");
List<String> scopes = new ArrayList<>();
scopes.add("https://www.googleapis.com/auth/devstorage.full_control");
scopes.add("https://www.googleapis.com/auth/compute");
account.setScopes(scopes);
instance.setServiceAccounts(Collections.singletonList(account));
// Optional - Add a startup script to be used by the VM Instance.
Metadata meta = new Metadata();
Metadata.Items item = new Metadata.Items();
item.setKey("startup-script-url");
// If you put a script called "vm-startup.sh" in this Google Cloud Storage bucket, it will execute on VM startup.
// This assumes you've created a bucket named the same as your PROJECT_ID
// For info on creating buckets see: https://cloud.google.com/storage/docs/cloud-console#_creatingbuckets
item.setValue("gs://" + PROJECT_ID + "/vm-startup.sh");
meta.setItems(Collections.singletonList(item));
instance.setMetadata(meta);
System.out.println(instance.toPrettyString());
Compute.Instances.Insert insert = compute.instances().insert(PROJECT_ID, ZONE_NAME, instance);
return insert.execute();
}