com.google.api.services.compute.model.AccessConfig Java Examples

The following examples show how to use com.google.api.services.compute.model.AccessConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GoogleProviderUtils.java    From halyard with Apache License 2.0 6 votes vote down vote up
static String getInstanceIp(
    AccountDeploymentDetails<GoogleAccount> details, String instanceName) {
  Compute compute = getCompute(details);
  Instance instance = null;
  try {
    instance =
        compute
            .instances()
            .get(details.getAccount().getProject(), "us-central1-f", instanceName)
            .execute();
  } catch (IOException e) {
    throw new HalException(FATAL, "Unable to get instance " + instanceName);
  }

  return instance.getNetworkInterfaces().stream()
      .map(
          i ->
              i.getAccessConfigs().stream()
                  .map(AccessConfig::getNatIP)
                  .filter(ip -> !StringUtils.isEmpty(ip))
                  .findFirst())
      .filter(Optional::isPresent)
      .map(Optional::get)
      .findFirst()
      .orElseThrow(() -> new HalException(FATAL, "No public IP associated with" + instanceName));
}
 
Example #2
Source File: GcpInstanceResourceBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private List<NetworkInterface> getNetworkInterface(GcpContext context, Iterable<CloudResource> computeResources, Group group, CloudStack stack)
        throws IOException {
    boolean noPublicIp = context.getNoPublicIp();
    String projectId = context.getProjectId();
    Location location = context.getLocation();
    Compute compute = context.getCompute();

    NetworkInterface networkInterface = new NetworkInterface();
    List<CloudResource> subnet = filterResourcesByType(context.getNetworkResources(), ResourceType.GCP_SUBNET);
    String networkName = subnet.isEmpty() ? filterResourcesByType(context.getNetworkResources(),
            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, location.getRegion().value(), reservedIp.get(0).getName());
            accessConfig.setNatIP(getReservedIp.execute().getAddress());
        }
        networkInterface.setAccessConfigs(singletonList(accessConfig));
    }
    prepareNetworkAndSubnet(projectId, location.getRegion(), stack.getNetwork(), networkInterface, subnet, networkName);
    return singletonList(networkInterface);
}
 
Example #3
Source File: GcpMetadataCollectorTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private Optional<NetworkInterface> createNetworkInterface() {
    NetworkInterface networkInterface = new NetworkInterface();
    AccessConfig accessConfig = new AccessConfig();
    networkInterface.setNetworkIP(PRIVATE_IP);
    accessConfig.setNatIP(PUBLIC_IP);
    networkInterface.setAccessConfigs(List.of(accessConfig));
    return Optional.of(networkInterface);
}
 
Example #4
Source File: ComputeEngineSample.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
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(
      String.format(
          "https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/e2-standard-1",
          PROJECT_ID, ZONE_NAME));
  // Add Network Interface to be used by VM Instance.
  NetworkInterface ifc = new NetworkInterface();
  ifc.setNetwork(
      String.format(
          "https://www.googleapis.com/compute/v1/projects/%s/global/networks/default",
          PROJECT_ID));
  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(
      String.format(
          "https://www.googleapis.com/compute/v1/projects/%s/zones/%s/diskTypes/pd-standard",
          PROJECT_ID, ZONE_NAME));
  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(String.format("gs://%s/vm-startup.sh", PROJECT_ID));
  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();
}