1+ /*
2+ * Copyright 2022 Google LLC
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package compute .customhostname ;
18+
19+ // [START compute_instances_create_custom_hostname]
20+
21+ import com .google .cloud .compute .v1 .AttachedDisk ;
22+ import com .google .cloud .compute .v1 .AttachedDiskInitializeParams ;
23+ import com .google .cloud .compute .v1 .InsertInstanceRequest ;
24+ import com .google .cloud .compute .v1 .Instance ;
25+ import com .google .cloud .compute .v1 .InstancesClient ;
26+ import com .google .cloud .compute .v1 .NetworkInterface ;
27+ import com .google .cloud .compute .v1 .Operation ;
28+ import java .io .IOException ;
29+ import java .util .concurrent .ExecutionException ;
30+
31+ public class CreateInstanceWithCustomHostname {
32+
33+ public static void main (String [] args )
34+ throws IOException , ExecutionException , InterruptedException {
35+ // TODO(developer): Replace these variables before running the sample.
36+ // hostName: Custom hostname of the new VM instance.
37+ // * Custom hostnames must conform to RFC 1035 requirements for valid hostnames.
38+ String project = "your-project-id" ;
39+ String zone = "zone-name" ; // eg: "us-central1-a"
40+ String instanceName = "instance-name" ;
41+ String hostName = "host.example.com" ;
42+ createInstanceWithCustomHostname (project , zone , instanceName , hostName );
43+ }
44+
45+ // Creates an instance with custom hostname.
46+ public static void createInstanceWithCustomHostname (String projectId , String zone ,
47+ String instanceName , String hostName )
48+ throws IOException , ExecutionException , InterruptedException {
49+ // machineType - Machine type for the VM instance specified in the following format:
50+ // * "zones/{zone}/machineTypes/{type_name}". For example:
51+ // * "zones/europe-west3-c/machineTypes/f1-micro"
52+ // * You can find the list of available machine types by using this gcloud command:
53+ // * $ gcloud compute machine-types list
54+ // sourceImage - Path of the disk image you want to use for your boot
55+ // * disk. This image can be one of the public images
56+ // * eg: "projects/...
57+ // * or a private image you have access to.
58+ // * You can check the list of available public images using:
59+ // * $ gcloud compute images list
60+ // networkName - Name of the network you want the new instance to use.
61+ // * For example: global/networks/default - if you want to use the default network.
62+ String machineType = "n1-standard-1" ;
63+ String sourceImage = String .format ("projects/%s/global/images/family/%s" , "debian-cloud" ,
64+ "debian-11" );
65+ String networkName = "global/networks/default" ;
66+
67+ try (InstancesClient instancesClient = InstancesClient .create ()) {
68+ System .out .printf ("Creating the %s instance in %s with hostname %s..." , instanceName , zone ,
69+ hostName );
70+
71+ AttachedDisk disk =
72+ AttachedDisk .newBuilder ()
73+ .setBoot (true )
74+ .setAutoDelete (true )
75+ .setType (AttachedDisk .Type .PERSISTENT .toString ())
76+ .setInitializeParams (
77+ // Describe the size and source image of the boot disk to attach to the instance.
78+ AttachedDiskInitializeParams .newBuilder ()
79+ .setSourceImage (sourceImage )
80+ .setDiskSizeGb (10 ).build ())
81+ .build ();
82+
83+ // Use the network interface provided in the networkName argument.
84+ NetworkInterface networkInterface = NetworkInterface .newBuilder ()
85+ .setName (networkName )
86+ .build ();
87+
88+ Instance instanceResource = Instance .newBuilder ()
89+ // Custom hostnames are not resolved by the automatically created records
90+ // provided by Compute Engine internal DNS.
91+ // You must manually configure the DNS record for your custom hostname.
92+ .setName (instanceName )
93+ .setHostname (hostName )
94+ .addDisks (disk )
95+ .setMachineType (String .format ("zones/%s/machineTypes/%s" , zone , machineType ))
96+ .addNetworkInterfaces (networkInterface ).build ();
97+
98+ InsertInstanceRequest request = InsertInstanceRequest .newBuilder ()
99+ .setProject (projectId )
100+ .setZone (zone )
101+ .setInstanceResource (instanceResource ).build ();
102+
103+ // Wait for the create operation to complete.
104+ Operation response = instancesClient .insertAsync (request ).get ();
105+
106+ if (response .hasError ()) {
107+ System .out .printf ("Instance creation failed for instance: %s ; Response: %s ! ! " ,
108+ instanceName , response );
109+ return ;
110+ }
111+ System .out .printf ("Instance created : %s" , instanceName );
112+ System .out .printf ("Operation Status for instance %s is %s: " , instanceName ,
113+ response .getStatus ());
114+ }
115+
116+ }
117+
118+ }
119+ // [END compute_instances_create_custom_hostname]
0 commit comments