Skip to content

Commit e6517d8

Browse files
[snipets][compute_ip_address_reserve_new_external] (#11578)
* [snipets][compute_ip_address_reserve_new_external] * [snipets][compute_ip_address_reserve_new_external] fix sgs * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * [snipets][compute_ip_address_reserve_new_external] fix PR comments * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent f2ba608 commit e6517d8

File tree

4 files changed

+333
-1
lines changed

4 files changed

+333
-1
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright 2024 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+
from typing import Optional
16+
17+
from google.cloud.compute_v1.types import Address
18+
from google.cloud.compute_v1.services.addresses.client import AddressesClient
19+
from google.cloud.compute_v1.services.global_addresses import GlobalAddressesClient
20+
21+
22+
# <INGREDIENT reserve_new_external_ip_address>
23+
def reserve_new_external_ip_address(
24+
project_id: str,
25+
address_name: str,
26+
is_v6: bool = False,
27+
is_premium: bool = False,
28+
region: Optional[str] = None,
29+
):
30+
"""
31+
Reserves a new external IP address in the specified project and region.
32+
33+
Args:
34+
project_id (str): Your Google Cloud project ID.
35+
address_name (str): The name for the new IP address.
36+
is_v6 (bool): 'IPV4' or 'IPV6' depending on the IP version. IPV6 if True. Option only for global regions.
37+
is_premium (bool): 'STANDARD' or 'PREMIUM' network tier. Standard option available only in regional ip.
38+
region (Optional[str]): The region to reserve the IP address in, if regional. Must be None if global.
39+
40+
Returns:
41+
None
42+
"""
43+
44+
ip_version = "IPV6" if is_v6 else "IPV4"
45+
network_tier = "STANDARD" if not is_premium and region else "PREMIUM"
46+
47+
address = Address(
48+
name=address_name,
49+
address_type="EXTERNAL",
50+
network_tier=network_tier,
51+
)
52+
if not region: # global IP address
53+
client = GlobalAddressesClient()
54+
address.ip_version = ip_version
55+
operation = client.insert(project=project_id, address_resource=address)
56+
else: # regional IP address
57+
address.region = region
58+
client = AddressesClient()
59+
operation = client.insert(
60+
project=project_id, region=region, address_resource=address
61+
)
62+
63+
operation.result()
64+
65+
print(f"External IP address '{address_name}' reserved successfully.")
66+
67+
68+
# </INGREDIENT>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2024 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+
17+
# <REGION compute_ip_address_reserve_new_external>
18+
# <IMPORTS/>
19+
# <INGREDIENT reserve_new_external_ip_address />
20+
21+
# </REGION compute_ip_address_reserve_new_external>
22+
23+
if __name__ == "__main__":
24+
import google.auth
25+
26+
PROJECT = google.auth.default()[1]
27+
region = "us-central1"
28+
address_name = "my-new-external-ip"
29+
30+
# ip4 global
31+
reserve_new_external_ip_address(PROJECT, address_name + "ip4-global")
32+
# ip4 regional premium
33+
reserve_new_external_ip_address(
34+
PROJECT,
35+
address_name + "ip4-regional-premium",
36+
region=region,
37+
is_premium=True,
38+
)
39+
# ip4 regional
40+
reserve_new_external_ip_address(
41+
PROJECT, address_name + "ip4-regional", region=region
42+
)
43+
# ip6 global
44+
reserve_new_external_ip_address(PROJECT, address_name + "ip6-global", is_v6=True)
45+
# ip6 regional
46+
reserve_new_external_ip_address(
47+
PROJECT, address_name + "ip6-regional", is_v6=True, region=region
48+
)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Copyright 2024 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+
17+
# This file is automatically generated. Please do not modify it directly.
18+
# Find the relevant recipe file in the samples/recipes or samples/ingredients
19+
# directory and apply your changes there.
20+
21+
22+
# [START compute_ip_address_reserve_new_external]
23+
from typing import Optional
24+
25+
from google.cloud.compute_v1.services.addresses.client import AddressesClient
26+
from google.cloud.compute_v1.services.global_addresses import GlobalAddressesClient
27+
from google.cloud.compute_v1.types import Address
28+
29+
30+
def reserve_new_external_ip_address(
31+
project_id: str,
32+
address_name: str,
33+
is_v6: bool = False,
34+
is_premium: bool = False,
35+
region: Optional[str] = None,
36+
):
37+
"""
38+
Reserves a new external IP address in the specified project and region.
39+
40+
Args:
41+
project_id (str): Your Google Cloud project ID.
42+
address_name (str): The name for the new IP address.
43+
is_v6 (bool): 'IPV4' or 'IPV6' depending on the IP version. IPV6 if True. Option only for global regions.
44+
is_premium (bool): 'STANDARD' or 'PREMIUM' network tier. Standard option available only in regional ip.
45+
region (Optional[str]): The region to reserve the IP address in, if regional. Must be None if global.
46+
47+
Returns:
48+
None
49+
"""
50+
51+
ip_version = "IPV6" if is_v6 else "IPV4"
52+
network_tier = "STANDARD" if not is_premium and region else "PREMIUM"
53+
54+
address = Address(
55+
name=address_name,
56+
address_type="EXTERNAL",
57+
network_tier=network_tier,
58+
)
59+
if not region: # global IP address
60+
client = GlobalAddressesClient()
61+
address.ip_version = ip_version
62+
operation = client.insert(project=project_id, address_resource=address)
63+
else: # regional IP address
64+
address.region = region
65+
client = AddressesClient()
66+
operation = client.insert(
67+
project=project_id, region=region, address_resource=address
68+
)
69+
70+
operation.result()
71+
72+
print(f"External IP address '{address_name}' reserved successfully.")
73+
74+
75+
# [END compute_ip_address_reserve_new_external]
76+
77+
if __name__ == "__main__":
78+
import google.auth
79+
80+
PROJECT = google.auth.default()[1]
81+
region = "us-central1"
82+
address_name = "my-new-external-ip"
83+
84+
# ip4 global
85+
reserve_new_external_ip_address(PROJECT, address_name + "ip4-global")
86+
# ip4 regional premium
87+
reserve_new_external_ip_address(
88+
PROJECT,
89+
address_name + "ip4-regional-premium",
90+
region=region,
91+
is_premium=True,
92+
)
93+
# ip4 regional
94+
reserve_new_external_ip_address(
95+
PROJECT, address_name + "ip4-regional", region=region
96+
)
97+
# ip6 global
98+
reserve_new_external_ip_address(PROJECT, address_name + "ip6-global", is_v6=True)
99+
# ip6 regional
100+
reserve_new_external_ip_address(
101+
PROJECT, address_name + "ip6-regional", is_v6=True, region=region
102+
)

compute/client_library/snippets/tests/test_ip_address.py

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from typing import List, Optional, Union
1516
import uuid
1617

1718
import google.auth
@@ -24,10 +25,12 @@
2425
disk_from_image,
2526
get_image_from_family,
2627
)
27-
2828
from ..instances.delete import delete_instance
2929
from ..instances.ip_address.get_static_ip_address import get_static_ip_address
3030
from ..instances.ip_address.get_vm_address import get_instance_ip_address, IPType
31+
from ..instances.ip_address.reserve_new_external_ip_address import (
32+
reserve_new_external_ip_address,
33+
)
3134

3235
PROJECT = google.auth.default()[1]
3336
REGION = "us-central1"
@@ -101,6 +104,56 @@ def test_get_static_ip(static_ip: Address):
101104
assert static_ip.name == actual_address.name
102105

103106

107+
def delete_ip_address(
108+
client: Union[AddressesClient, GlobalAddressesClient],
109+
project_id: str,
110+
address_name: str,
111+
region: Optional[str] = None,
112+
):
113+
"""
114+
Deletes ip address with given parameters.
115+
Args:
116+
client (Union[AddressesClient, GlobalAddressesClient]): global or regional address client
117+
project_id (str): project id
118+
address_name (str): ip address name to delete
119+
region (Optional[str]): region of ip address. Marker to choose between clients (GlobalAddressesClient when None)
120+
"""
121+
try:
122+
if region:
123+
operation = client.delete(
124+
project=project_id, region=region, address=address_name
125+
)
126+
else:
127+
operation = client.delete(project=project_id, address=address_name)
128+
operation.result()
129+
except Exception as e:
130+
print(
131+
f"Error deleting ip address: {e}"
132+
) # suppress potential errors during deletions
133+
134+
135+
def list_ip_addresses(
136+
client: Union[AddressesClient, GlobalAddressesClient],
137+
project_id: str,
138+
region: Optional[str] = None,
139+
) -> List[str]:
140+
"""
141+
Retrieves ip address names of project (global) or region.
142+
Args:
143+
client (Union[AddressesClient, GlobalAddressesClient]): global or regional address client
144+
project_id (str): project id
145+
region (Optional[str]): region of ip address. Marker to choose between clients (GlobalAddressesClient when None)
146+
147+
Returns:
148+
list of ip address names as strings
149+
"""
150+
if region:
151+
return [
152+
address.name for address in client.list(project=project_id, region=region)
153+
]
154+
return [address.name for address in client.list(project=project_id)]
155+
156+
104157
def test_get_instance_external_ip_address(instance_with_ips):
105158
# Internal IP check
106159
internal_ips = get_instance_ip_address(instance_with_ips, ip_type=IPType.INTERNAL)
@@ -128,3 +181,64 @@ def test_get_instance_external_ip_address(instance_with_ips):
128181
if ipv6_config.type_ == "DIRECT_IPV6"
129182
}
130183
assert set(ipv6_ips) == expected_ipv6_ips, "IPv6 IPs do not match"
184+
185+
186+
def test_reserve_new_external_ip_address_global():
187+
global_client = GlobalAddressesClient()
188+
unique_string = uuid.uuid4()
189+
ip_4_global = f"ip4-global-{unique_string}"
190+
ip_6_global = f"ip6-global-{unique_string}"
191+
192+
expected_ips = {ip_4_global, ip_6_global}
193+
try:
194+
# ip4 global
195+
reserve_new_external_ip_address(PROJECT, ip_4_global)
196+
# ip6 global
197+
reserve_new_external_ip_address(PROJECT, ip_6_global, is_v6=True)
198+
199+
ips = list_ip_addresses(global_client, PROJECT)
200+
assert set(ips).issuperset(expected_ips)
201+
finally:
202+
# cleanup
203+
for address in expected_ips:
204+
delete_ip_address(global_client, PROJECT, address)
205+
206+
207+
def test_reserve_new_external_ip_address_regional():
208+
regional_client = AddressesClient()
209+
unique_string = uuid.uuid4()
210+
region = "us-central1"
211+
212+
ip_4_regional = f"ip4-regional-{unique_string}"
213+
ip_4_regional_premium = f"ip4-regional-premium-{unique_string}"
214+
ip_6_regional = f"ip6-regional-{unique_string}"
215+
ip_6_regional_premium = f"ip6-regional-premium-{unique_string}"
216+
217+
expected_ips = {
218+
ip_4_regional,
219+
ip_4_regional_premium,
220+
ip_6_regional,
221+
ip_6_regional_premium,
222+
}
223+
try:
224+
# ip4 regional standard
225+
reserve_new_external_ip_address(PROJECT, ip_4_regional, region=region)
226+
# ip4 regional premium
227+
reserve_new_external_ip_address(
228+
PROJECT, ip_4_regional_premium, region=region, is_premium=True
229+
)
230+
# ip6 regional standard
231+
reserve_new_external_ip_address(
232+
PROJECT, ip_6_regional, region=region, is_v6=True
233+
)
234+
# ip6 regional premium
235+
reserve_new_external_ip_address(
236+
PROJECT, ip_6_regional_premium, region=region, is_premium=True, is_v6=True
237+
)
238+
239+
ips = list_ip_addresses(regional_client, PROJECT, region=region)
240+
assert set(ips).issuperset(expected_ips)
241+
finally:
242+
# cleanup
243+
for address in expected_ips:
244+
delete_ip_address(regional_client, PROJECT, address, region=region)

0 commit comments

Comments
 (0)