Skip to content

Commit 322055e

Browse files
docs(samples): Adding Windows server samples (#274)
* docs(samples): Adding Windows instance related sampels Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent e4077ae commit 322055e

File tree

19 files changed

+1469
-0
lines changed

19 files changed

+1469
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
20+
from google.cloud import compute_v1
21+
22+
23+
# <INGREDIENT create_firewall_rule_for_windows_activation_host>
24+
def create_firewall_rule_for_windows_activation_host(
25+
project_id: str, firewall_rule_name: str, network: str = "global/networks/default"
26+
) -> compute_v1.Firewall:
27+
"""
28+
Creates an egress firewall rule with the highest priority for host
29+
kms.windows.googlecloud.com (35.190.247.13) for Windows activation.
30+
31+
Args:
32+
project_id: project ID or project number of the Cloud project you want to use.
33+
firewall_rule_name: name of the rule that is created.
34+
network: name of the network the rule will be applied to. Available name formats:
35+
* https://www.googleapis.com/compute/v1/projects/{project_id}/global/networks/{network}
36+
* projects/{project_id}/global/networks/{network}
37+
* global/networks/{network}
38+
39+
Returns:
40+
A Firewall object.
41+
"""
42+
firewall_rule = compute_v1.Firewall()
43+
firewall_rule.name = firewall_rule_name
44+
firewall_rule.network = network
45+
46+
allowed = compute_v1.Allowed()
47+
allowed.ports = ['1688']
48+
allowed.I_p_protocol = 'tcp'
49+
50+
firewall_rule.allowed = [allowed]
51+
firewall_rule.destination_ranges = ["35.190.247.13/32"]
52+
firewall_rule.direction = compute_v1.Firewall.Direction.EGRESS.name
53+
firewall_rule.priority = 0
54+
55+
firewall_client = compute_v1.FirewallsClient()
56+
operation = firewall_client.insert(project=project_id, firewall_resource=firewall_rule)
57+
58+
wait_for_extended_operation(operation, "windows KSM firewall rule creation")
59+
60+
return firewall_client.get(project=project_id, firewall=firewall_rule_name)
61+
# </INGREDIENT>
62+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
from typing import Optional
20+
21+
from google.cloud import compute_v1
22+
23+
24+
# <INGREDIENT create_windows_instance>
25+
def create_windows_instance(project_id: str, zone: str, instance_name: str,
26+
machine_type: str, source_image_family: str = "windows-2022",
27+
network_link: str = "global/networks/default",
28+
subnetwork_link: Optional[str] = None) -> compute_v1.Instance:
29+
"""
30+
Creates a new Windows Server instance that has only an internal IP address.
31+
32+
Args:
33+
project_id: project ID or project number of the Cloud project you want to use.
34+
zone: name of the zone to create the instance in. For example: "us-west3-b"
35+
instance_name: name of the new virtual machine (VM) instance.
36+
machine_type: machine type you want to create in following format:
37+
"zones/{zone}/machineTypes/{type_name}". For example:
38+
"zones/europe-west3-c/machineTypes/f1-micro"
39+
You can find the list of available machine types using:
40+
https://cloud.google.com/sdk/gcloud/reference/compute/machine-types/list
41+
source_image_family: name of the public image family for Windows Server or SQL Server images.
42+
https://cloud.google.com/compute/docs/images#os-compute-support
43+
network_link: name of the network you want the new instance to use.
44+
For example: "global/networks/default" represents the network
45+
named "default", which is created automatically for each project.
46+
subnetwork_link: name of the subnetwork you want the new instance to use.
47+
This value uses the following format:
48+
"regions/{region}/subnetworks/{subnetwork_name}"
49+
50+
Returns:
51+
Instance object.
52+
"""
53+
if subnetwork_link is None:
54+
subnetwork_link = f'regions/{zone}/subnetworks/default'
55+
56+
base_image = get_image_from_family(
57+
project="windows-cloud", family=source_image_family
58+
)
59+
disk_type = f"zones/{zone}/diskTypes/pd-standard"
60+
disks = [disk_from_image(disk_type, 100, True, base_image.self_link, True)]
61+
62+
# You must verify or configure routes and firewall rules in your VPC network
63+
# to allow access to kms.windows.googlecloud.com.
64+
# More information about access to kms.windows.googlecloud.com: https://cloud.google.com/compute/docs/instances/windows/creating-managing-windows-instances#kms-server
65+
66+
# Additionally, you must enable Private Google Access for subnets in your VPC network
67+
# that contain Windows instances with only internal IP addresses.
68+
# More information about Private Google Access: https://cloud.google.com/vpc/docs/configure-private-google-access#enabling
69+
70+
instance = create_instance(project_id, zone, instance_name, disks,
71+
machine_type=machine_type, network_link=network_link,
72+
subnetwork_link=subnetwork_link, external_access=True,
73+
)
74+
return instance
75+
# </INGREDIENT>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
from google.cloud import compute_v1
20+
21+
22+
# <INGREDIENT get_instance_serial_port_output>
23+
def get_instance_serial_port_output(project_id: str, zone: str, instance_name: str) -> compute_v1.SerialPortOutput:
24+
"""
25+
Returns the last 1 MB of serial port output from the specified instance.
26+
27+
Args:
28+
project_id: project ID or project number of the Cloud project you want to use.
29+
zone: name of the zone you want to use. For example: “us-west3-b”
30+
instance_name: name of the VM instance you want to query.
31+
Returns:
32+
Content of the serial port output of an instance inside a compute_v1.SerialPortOutput object.
33+
More about this type: https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.types.SerialPortOutput
34+
35+
"""
36+
instance_client = compute_v1.InstancesClient()
37+
return instance_client.get_serial_port_output(project=project_id, zone=zone, instance=instance_name)
38+
# </INGREDIENT>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
from typing import Optional
20+
21+
from google.cloud import compute_v1
22+
23+
24+
# <INGREDIENT create_route>
25+
def create_route(project_id: str, network: str, route_name: str, destination_range: str, *,
26+
next_hop_gateway: Optional[str] = None,
27+
next_hop_ip: Optional[str] = None, next_hop_instance: Optional[str] = None,
28+
next_hop_vpn_tunnel: Optional[str] = None, next_hop_ilb: Optional[str] = None) -> compute_v1.Route:
29+
"""
30+
Create a new route in selected network by providing a destination and next hop name.
31+
32+
Note: The set of {next_hop_gateway, next_hop_ip, next_hop_instance, next_hop_vpn_tunnel,
33+
next_hop_ilb} is exclusive, you and only specify one of those parameters.
34+
35+
Args:
36+
project_id: project ID or project number of the Cloud project you want to use.
37+
network: name of the network the route will be created in. Available name formats:
38+
* https://www.googleapis.com/compute/v1/projects/{project_id}/global/networks/{network}
39+
* projects/{project_id}/global/networks/{network}
40+
* global/networks/{network}
41+
route_name: name of the new route.
42+
destination_range: range of destination IPs this route should be applied to. E.g. 10.0.0.0/16.
43+
next_hop_gateway: name of the gateway the traffic should be directed to.
44+
next_hop_ip: IP address the traffic should be directed to.
45+
next_hop_instance: name of the instance the traffic should be directed to. Name format:
46+
"projects/{project}/zones/{zone}/instances/{instance_name}"
47+
next_hop_vpn_tunnel: name of the VPN tunnel the traffic should be directed to. Name format:
48+
"projects/{project}/regions/{region}/vpnTunnels/{vpn_tunnel_name}"
49+
next_hop_ilb: name of a forwarding rule of the Internal Load Balancer the traffic
50+
should be directed to. Name format:
51+
"projects/{project}/regions/{region}/forwardingRules/{forwarding_rule_region}"
52+
53+
Returns:
54+
A new compute_v1.Route object.
55+
"""
56+
excl_args = {next_hop_instance, next_hop_ilb, next_hop_vpn_tunnel, next_hop_gateway, next_hop_ip}
57+
args_set = sum(1 if arg is not None else 0 for arg in excl_args)
58+
59+
if args_set != 1:
60+
raise RuntimeError("You must specify exactly one next_hop_* parameter.")
61+
62+
route = compute_v1.Route()
63+
route.name = route_name
64+
route.network = network
65+
route.dest_range = destination_range
66+
67+
if next_hop_gateway:
68+
route.next_hop_gateway = next_hop_gateway
69+
elif next_hop_ip:
70+
route.next_hop_ip = next_hop_ip
71+
elif next_hop_instance:
72+
route.next_hop_instance = next_hop_instance
73+
elif next_hop_vpn_tunnel:
74+
route.next_hop_vpn_tunnel = next_hop_vpn_tunnel
75+
elif next_hop_ilb:
76+
route.next_hop_ilb = next_hop_ilb
77+
78+
route_client = compute_v1.RoutesClient()
79+
operation = route_client.insert(project=project_id, route_resource=route)
80+
81+
wait_for_extended_operation(operation, "route creation")
82+
83+
return route_client.get(project=project_id, route=route_name)
84+
# </INGREDIENT>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
from typing import NoReturn
20+
21+
from google.cloud import compute_v1
22+
23+
24+
# <INGREDIENT delete_route>
25+
def delete_route(project_id: str, route_name: str) -> NoReturn:
26+
"""
27+
Delete a route in project.
28+
29+
Args:
30+
project_id: project ID or project number of the Cloud project you want to use.
31+
route_name: name of the route to delete.
32+
"""
33+
34+
route_client = compute_v1.RoutesClient()
35+
operation = route_client.delete(project=project_id, route=route_name)
36+
37+
wait_for_extended_operation(operation, "route deletion")
38+
39+
return
40+
# </INGREDIENT>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
16+
# folder for complete code samples that are ready to be used.
17+
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18+
# flake8: noqa
19+
from typing import Iterable
20+
21+
from google.cloud import compute_v1
22+
23+
24+
# <INGREDIENT list_routes>
25+
def list_routes(project_id: str, ) -> Iterable[compute_v1.Route]:
26+
"""
27+
Lists routes in project.
28+
29+
Args:
30+
project_id: project ID or project number of the Cloud project you want to use.
31+
32+
Returns:
33+
An iterable collection of routes found in given project.
34+
"""
35+
36+
route_client = compute_v1.RoutesClient()
37+
return route_client.list(project=project_id)
38+
# </INGREDIENT>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# flake8: noqa
15+
16+
# <REGION compute_create_egress_rule_windows_activation>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT wait_for_extended_operation />
20+
21+
# <INGREDIENT create_firewall_rule_for_windows_activation_host />
22+
23+
# </REGION compute_create_egress_rule_windows_activation>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# flake8: noqa
15+
16+
# <REGION compute_create_windows_instance_internal_ip>
17+
# <IMPORTS/>
18+
19+
# <INGREDIENT get_image_from_family />
20+
21+
22+
# <INGREDIENT disk_from_image />
23+
24+
# <INGREDIENT wait_for_extended_operation />
25+
26+
27+
# <INGREDIENT create_instance />
28+
29+
30+
# <INGREDIENT create_windows_instance />
31+
# </REGION compute_create_windows_instance_internal_ip>

0 commit comments

Comments
 (0)