java—是否有可能用机器映像启动compute engine中的示例?

vm0i2vca  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(165)

我试着用下面的例子:
https://github.com/google/google-api-java-client-samples/blob/master/compute-engine-cmdline-sample/src/main/java/com/google/api/services/samples/computeengine/cmdline/computeenginesample.java
在web界面上创建示例时,要根据可用的rest从机器映像初始化示例,应该如下所示:
“sourcemachineimage”:xx
但是对于客户端库,我没有找到任何信息/解决方案:

// [START create_instances]
    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/n1-standard-8", PROJECT_ID, ZONE_NAME ) );

// trying to add here the Machine Image but no results until now

        // 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 );
//        params.setSourceImage("projects/online-school-labs/global/machineImages/e3-template-florian");
        // 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();
    }
    // [END create_instances]

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题