1+ import logging
2+ import sys
3+ import unittest
4+ from scaleway .vpc .v2 import VpcV2API
5+ from scaleway_core .api import ScalewayException
6+ from scaleway_core .client import Client
7+ from scaleway_core .utils import random_name
8+
9+ logger = logging .getLogger ()
10+ logger .level = logging .DEBUG
11+ stream_handler = logging .StreamHandler (sys .stdout )
12+ logger .addHandler (stream_handler )
13+
14+ region = "fr-par"
15+ tags = ["sdk-python" , "regression-test" ]
16+
17+ class TestScalewayVPCV2 (unittest .TestCase ):
18+ @classmethod
19+ def setUpClass (self ):
20+ self .client = Client .from_config_file_and_env ()
21+ self .vpcAPI = VpcV2API (self .client )
22+ self .project_id = self .client .default_project_id
23+ self .region = region
24+ self ._vpc = None
25+ self ._pns_to_cleanup = []
26+
27+ self ._vpc = self .vpcAPI .create_vpc (
28+ enable_routing = True ,
29+ region = self .region ,
30+ project_id = self .project_id ,
31+ name = random_name ("vpc-test-sdk-python" )
32+ )
33+ logger .info (f"✅ VPC { self ._vpc .id } has been created" )
34+
35+ @classmethod
36+ def tearDownClass (self ):
37+ for pn in self ._pns_to_cleanup :
38+ self .vpcAPI .delete_private_network (private_network_id = pn .id )
39+ logger .info (f"🧹 Deleted Private Network { pn .id } " )
40+
41+ if self ._vpc is not None :
42+ self .vpcAPI .delete_vpc (vpc_id = self ._vpc .id , region = self .region )
43+ logger .info (f"🧹 Deleted VPC { self ._vpc .id } " )
44+
45+ def test_delete_vpc (self ):
46+ vpc = self .vpcAPI .create_vpc (
47+ enable_routing = True ,
48+ region = self .region ,
49+ project_id = self .project_id ,
50+ name = random_name ("vpc-test-sdk-python" )
51+ )
52+ logger .info (f"✅ VPC { vpc .id } has been created" )
53+ self .assertIsNotNone (vpc .id )
54+ self .assertEqual (vpc .region , self .region )
55+
56+ self .vpcAPI .delete_vpc (vpc_id = vpc .id )
57+ logger .info (f"🗑️ VPC { vpc .id } deletion requested" )
58+
59+ with self .assertRaises (ScalewayException ):
60+ self .vpcAPI .get_vpc (vpc_id = vpc .id )
61+ logger .info (f"✅ VPC { vpc .id } has been deleted successfully" )
62+
63+ def test_list_vpcs (self ):
64+ vpcs = self .vpcAPI .list_vp_cs (region = self .region ).vpcs
65+ logger .info (f"🔎 Listed { len (vpcs )} VPC(s) in region: { self .region } " )
66+ self .assertIsInstance (vpcs , list )
67+
68+ def test_create_private_network (self ):
69+ for i in range (5 ):
70+ pn = self .vpcAPI .create_private_network (
71+ vpc_id = self ._vpc .id ,
72+ default_route_propagation_enabled = True ,
73+ project_id = self .project_id ,
74+ name = random_name (f"pn-{ i } " )
75+ )
76+ self ._pns_to_cleanup .append (pn )
77+ logger .info (f"✅ PN { i + 1 } /5: { pn .id } created in VPC { self ._vpc .id } " )
78+ self .assertEqual (pn .vpc_id , self ._vpc .id )
79+
80+ def test_list_private_network (self ):
81+ networks = self .vpcAPI .list_private_networks (region = self .region )
82+ logger .info (f"🔎 Listed { networks .total_count } private network(s) in region: { self .region } " )
83+ self .assertIsInstance (networks .private_networks , list )
84+
85+ def test_get_vpc (self ):
86+ vpc = self .vpcAPI .get_vpc (vpc_id = self ._vpc .id , region = self .region )
87+ logger .info (f"📥 Retrieved VPC { vpc .id } " )
88+ self .assertIsNotNone (vpc )
89+ self .assertEqual (self ._vpc .id , vpc .id )
90+
91+ def test_update_vpc (self ):
92+ vpc = self .vpcAPI .update_vpc (vpc_id = self ._vpc .id , tags = tags )
93+ logger .info (f"🛠️ Updated VPC { vpc .id } with tags: { tags } " )
94+ self .assertEqual (vpc .tags , tags )
95+ self .assertEqual (self ._vpc .id , vpc .id )
96+
97+ def test_list_vpc_all (self ):
98+ vpcs = self .vpcAPI .list_vp_cs_all ()
99+ logger .info (f"📥 Retrieved total of { len (vpcs )} VPC(s) across all regions" )
100+ self .assertIsInstance (vpcs , list )
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
0 commit comments