5
5
from ucloud .client import Client
6
6
from ucloud .helpers import wait , utils
7
7
8
- logger = logging .getLogger (' ucloud' )
8
+ logger = logging .getLogger (" ucloud" )
9
9
logger .setLevel (logging .DEBUG )
10
10
11
11
# NOTE: find your public/private key at
12
12
# website `UAPI <https://console.ucloud.cn/uapi/apikey>`_
13
- client = Client ({
14
- "region" : "cn-bj2" ,
15
- "project_id" : os .getenv ("UCLOUD_PROJECT_ID" ),
16
- "public_key" : os .getenv ("UCLOUD_PUBLIC_KEY" ),
17
- "private_key" : os .getenv ("UCLOUD_PRIVATE_KEY" ),
18
- })
13
+ client = Client (
14
+ {
15
+ "region" : "cn-bj2" ,
16
+ "project_id" : os .getenv ("UCLOUD_PROJECT_ID" ),
17
+ "public_key" : os .getenv ("UCLOUD_PUBLIC_KEY" ),
18
+ "private_key" : os .getenv ("UCLOUD_PRIVATE_KEY" ),
19
+ }
20
+ )
19
21
20
22
21
23
def main ():
@@ -39,98 +41,111 @@ def main():
39
41
40
42
41
43
def describe_image ():
42
- images = client .uhost ().describe_image ({'ImageType' : 'Base' }).get ('ImageSet' , [])
44
+ images = (
45
+ client .uhost ().describe_image ({"ImageType" : "Base" }).get ("ImageSet" , [])
46
+ )
43
47
if not images :
44
48
return
45
49
image = random .choice (images )
46
- return image .get (' ImageId' )
50
+ return image .get (" ImageId" )
47
51
48
52
49
53
def mget_uhost_states (uhost_ids ):
50
- resp = client .uhost ().describe_uhost_instance ({' UHostIds' : uhost_ids })
51
- return [inst .get (' State' ) for inst in resp .get (' UHostSet' )]
54
+ resp = client .uhost ().describe_uhost_instance ({" UHostIds" : uhost_ids })
55
+ return [inst .get (" State" ) for inst in resp .get (" UHostSet" )]
52
56
53
57
54
58
def create_uhost_batch (image_id , count ):
55
- resp = client .uhost ().create_uhost_instance ({
56
- 'Name' : 'sdk-python3-example-two-tier' ,
57
- 'Zone' : 'cn-bj2-05' ,
58
- 'ImageId' : image_id ,
59
- 'LoginMode' : "Password" ,
60
- 'Password' : utils .gen_password (20 ),
61
- 'CPU' : 1 ,
62
- 'Memory' : 1024 ,
63
- 'MaxCount' : count ,
64
- })
65
- uhost_ids = resp .get ('UHostIds' , [])
59
+ resp = client .uhost ().create_uhost_instance (
60
+ {
61
+ "Name" : "sdk-python3-example-two-tier" ,
62
+ "Zone" : "cn-bj2-05" ,
63
+ "ImageId" : image_id ,
64
+ "LoginMode" : "Password" ,
65
+ "Password" : utils .gen_password (20 ),
66
+ "CPU" : 1 ,
67
+ "Memory" : 1024 ,
68
+ "MaxCount" : count ,
69
+ }
70
+ )
71
+ uhost_ids = resp .get ("UHostIds" , [])
66
72
wait .wait_for_state (
67
- target = ['running' ], pending = ['pending' ], timeout = 300 ,
73
+ target = ["running" ],
74
+ pending = ["pending" ],
75
+ timeout = 300 ,
68
76
refresh = lambda : (
69
- 'running' if all ([state == 'Running' for state in mget_uhost_states (uhost_ids )]) else 'pending'
77
+ "running"
78
+ if all (
79
+ [state == "Running" for state in mget_uhost_states (uhost_ids )]
80
+ )
81
+ else "pending"
70
82
),
71
83
)
72
84
return uhost_ids
73
85
74
86
75
87
def create_ulb ():
76
- resp = client .ulb ().create_ulb ({
77
- 'Name' : 'sdk-python3-example-two-tier' ,
78
- })
79
- return resp .get ('ULBId' )
88
+ resp = client .ulb ().create_ulb ({"Name" : "sdk-python3-example-two-tier" })
89
+ return resp .get ("ULBId" )
80
90
81
91
82
92
def create_vserver (ulb_id ):
83
- resp = client .ulb ().create_vserver ({
84
- 'Name' : 'sdk-python3-example-two-tier' ,
85
- 'ULBId' : ulb_id ,
86
- })
87
- return resp .get ('VServerId' )
93
+ resp = client .ulb ().create_vserver (
94
+ {"Name" : "sdk-python3-example-two-tier" , "ULBId" : ulb_id }
95
+ )
96
+ return resp .get ("VServerId" )
88
97
89
98
90
99
def allocate_backend_batch (ulb_id , vserver_id , uhost_ids ):
91
100
backend_ids = []
92
101
for uhost_id in uhost_ids :
93
- resp = client .ulb ().allocate_backend ({
94
- 'ULBId' : ulb_id ,
95
- 'VServerId' : vserver_id ,
96
- 'ResourceId' : uhost_id ,
97
- 'ResourceType' : 'UHost' ,
98
- })
99
- backend_ids .append (resp .get ('BackendId' ))
102
+ resp = client .ulb ().allocate_backend (
103
+ {
104
+ "ULBId" : ulb_id ,
105
+ "VServerId" : vserver_id ,
106
+ "ResourceId" : uhost_id ,
107
+ "ResourceType" : "UHost" ,
108
+ }
109
+ )
110
+ backend_ids .append (resp .get ("BackendId" ))
100
111
return backend_ids
101
112
102
113
103
114
def release_backend_batch (ulb_id , vserver_id , backend_ids ):
104
115
for backend_id in backend_ids :
105
- client .ulb ().release_backend ({
106
- 'ULBId' : ulb_id ,
107
- 'VServerId' : vserver_id ,
108
- 'BackendId' : backend_id ,
109
- })
116
+ client .ulb ().release_backend (
117
+ {"ULBId" : ulb_id , "VServerId" : vserver_id , "BackendId" : backend_id }
118
+ )
110
119
111
120
112
121
def delete_vserver (ulb_id , vserver_id ):
113
- client .ulb ().delete_vserver ({' ULBId' : ulb_id , ' VServerId' : vserver_id })
122
+ client .ulb ().delete_vserver ({" ULBId" : ulb_id , " VServerId" : vserver_id })
114
123
115
124
116
125
def delete_ulb (ulb_id ):
117
- client .ulb ().delete_ulb ({' ULBId' : ulb_id })
126
+ client .ulb ().delete_ulb ({" ULBId" : ulb_id })
118
127
119
128
120
129
def delete_uhost_batch (uhost_ids ):
121
130
for uhost_id in uhost_ids :
122
- client .uhost ().stop_uhost_instance ({' UHostId' : uhost_id })
131
+ client .uhost ().stop_uhost_instance ({" UHostId" : uhost_id })
123
132
124
133
wait .wait_for_state (
125
- target = ['stopped' ], pending = ['pending' ], timeout = 300 ,
134
+ target = ["stopped" ],
135
+ pending = ["pending" ],
136
+ timeout = 300 ,
126
137
refresh = lambda : (
127
- 'stopped' if all ([state == 'Stopped' for state in mget_uhost_states (uhost_ids )]) else 'pending'
138
+ "stopped"
139
+ if all (
140
+ [state == "Stopped" for state in mget_uhost_states (uhost_ids )]
141
+ )
142
+ else "pending"
128
143
),
129
144
)
130
145
131
146
for uhost_id in uhost_ids :
132
- client .uhost ().terminate_uhost_instance ({' UHostId' : uhost_id })
147
+ client .uhost ().terminate_uhost_instance ({" UHostId" : uhost_id })
133
148
134
149
135
- if __name__ == ' __main__' :
150
+ if __name__ == " __main__" :
136
151
main ()
0 commit comments