Skip to content

Commit ddd6413

Browse files
authored
Merge pull request #58 from yunify/refactor
Refactor actions
2 parents 8d17225 + 9993ae3 commit ddd6413

File tree

15 files changed

+3371
-3092
lines changed

15 files changed

+3371
-3092
lines changed

qingcloud/iaas/actions/alarm_policy.py

Lines changed: 329 additions & 0 deletions
Large diffs are not rendered by default.

qingcloud/iaas/actions/eip.py

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# =========================================================================
2+
# Copyright 2012-present Yunify, Inc.
3+
# -------------------------------------------------------------------------
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this work except in compliance with the License.
6+
# You may obtain a copy of the License in the LICENSE file, or 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+
from qingcloud.iaas import constants as const
18+
from qingcloud.misc.utils import filter_out_none
19+
20+
21+
class EipAction(object):
22+
23+
def __init__(self, conn):
24+
self.conn = conn
25+
26+
def describe_eips(self, eips=None,
27+
status=None,
28+
instance_id=None,
29+
search_word=None,
30+
owner=None,
31+
offset=None,
32+
limit=None,
33+
tags=None,
34+
**ignore):
35+
""" Describe eips filtered by condition.
36+
@param eips: IDs of the eip you want describe.
37+
@param status: filter eips by status
38+
@param instance_id: filter eips by instance.
39+
@param search_word: search word column.
40+
@param offset: the starting offset of the returning results.
41+
@param limit: specify the number of the returning results.
42+
@param tags : the array of IDs of tags.
43+
"""
44+
action = const.ACTION_DESCRIBE_EIPS
45+
valid_keys = ['eips', 'status', 'instance_id', 'search_word',
46+
'offset', 'limit', 'tags', 'owner']
47+
body = filter_out_none(locals(), valid_keys)
48+
if not self.conn.req_checker.check_params(body,
49+
required_params=[],
50+
integer_params=[
51+
'offset', 'limit'],
52+
list_params=[
53+
'status', 'eips', 'tags']
54+
):
55+
return None
56+
57+
return self.conn.send_request(action, body)
58+
59+
def associate_eip(self, eip,
60+
instance,
61+
**ignore):
62+
""" Associate an eip on an instance.
63+
@param eip: The id of eip you want to associate with instance.
64+
@param instance: the id of instance you want to associate eip.
65+
"""
66+
action = const.ACTION_ASSOCIATE_EIP
67+
body = {'eip': eip, 'instance': instance}
68+
if not self.conn.req_checker.check_params(body,
69+
required_params=[
70+
'eip', 'instance'],
71+
integer_params=[],
72+
list_params=[]
73+
):
74+
return None
75+
76+
return self.conn.send_request(action, body)
77+
78+
def dissociate_eips(self, eips,
79+
**ignore):
80+
""" Dissociate one or more eips.
81+
@param eips: The ids of eips you want to dissociate with instance.
82+
"""
83+
action = const.ACTION_DISSOCIATE_EIPS
84+
body = {'eips': eips}
85+
if not self.conn.req_checker.check_params(body,
86+
required_params=['eips'],
87+
integer_params=[],
88+
list_params=['eips']
89+
):
90+
return None
91+
92+
return self.conn.send_request(action, body)
93+
94+
def allocate_eips(self, bandwidth,
95+
billing_mode=const.EIP_BILLING_MODE_BANDWIDTH,
96+
count=1,
97+
need_icp=0,
98+
eip_name='',
99+
target_user=None,
100+
associate_mode=0,
101+
**ignore):
102+
""" Allocate one or more eips.
103+
@param count: the number of eips you want to allocate.
104+
@param bandwidth: the bandwidth of the eip in Mbps.
105+
@param need_icp: 0 - no need, 1 - need
106+
@param eip_name : the short name of eip
107+
@param target_user: ID of user who will own this resource, should be one of your sub-accounts
108+
@param associate_mode: 0 - associate ip addr to virtual gateway, 1 - associate ip addr to vm
109+
"""
110+
action = const.ACTION_ALLOCATE_EIPS
111+
valid_keys = ['bandwidth', 'billing_mode',
112+
'count', 'need_icp', 'eip_name',
113+
'target_user', 'associate_mode']
114+
body = filter_out_none(locals(), valid_keys)
115+
if not self.conn.req_checker.check_params(body,
116+
required_params=['bandwidth'],
117+
integer_params=['bandwidth', 'count',
118+
'need_icp', 'associate_mode'],
119+
list_params=[]
120+
):
121+
return None
122+
123+
return self.conn.send_request(action, body)
124+
125+
def release_eips(self, eips,
126+
force=0,
127+
**ignore):
128+
""" Release one or more eips.
129+
@param eips : The ids of eips that you want to release
130+
@param force : Whether to force release the eip that needs icp codes.
131+
"""
132+
action = const.ACTION_RELEASE_EIPS
133+
body = {'eips': eips, 'force': int(force != 0)}
134+
if not self.conn.req_checker.check_params(body,
135+
required_params=['eips'],
136+
integer_params=['force'],
137+
list_params=['eips']
138+
):
139+
return None
140+
141+
return self.conn.send_request(action, body)
142+
143+
def change_eips_bandwidth(self, eips,
144+
bandwidth,
145+
**ignore):
146+
""" Change one or more eips bandwidth.
147+
@param eips: The IDs of the eips whose bandwidth you want to change.
148+
@param bandwidth: the new bandwidth of the eip in MB.
149+
"""
150+
action = const.ACTION_CHANGE_EIPS_BANDWIDTH
151+
body = {'eips': eips, 'bandwidth': bandwidth}
152+
if not self.conn.req_checker.check_params(body,
153+
required_params=[
154+
'eips', 'bandwidth'],
155+
integer_params=['bandwidth'],
156+
list_params=['eips']
157+
):
158+
return None
159+
160+
return self.conn.send_request(action, body)
161+
162+
def change_eips_billing_mode(self, eips,
163+
billing_mode,
164+
**ignore):
165+
""" Change one or more eips billing mode.
166+
@param eips: The IDs of the eips whose billing mode you want to change.
167+
@param billing_mode: the new billing mode, "bandwidth" or "traffic".
168+
"""
169+
action = const.ACTION_CHANGE_EIPS_BILLING_MODE
170+
body = {'eips': eips, 'billing_mode': billing_mode}
171+
if not self.conn.req_checker.check_params(body,
172+
required_params=[
173+
'eips', 'billing_mode'],
174+
list_params=['eips']
175+
):
176+
return None
177+
178+
return self.conn.send_request(action, body)
179+
180+
def modify_eip_attributes(self, eip,
181+
eip_name=None,
182+
description=None,
183+
**ignore):
184+
""" Modify eip attributes.
185+
If you want to modify eip's bandwidth, use `change_eips_bandwidth`.
186+
@param eip : the ID of eip that you want to modify
187+
@param eip_name : the name of eip
188+
@param description : the eip description
189+
"""
190+
action = const.ACTION_MODIFY_EIP_ATTRIBUTES
191+
valid_keys = ['eip', 'eip_name', 'description']
192+
body = filter_out_none(locals(), valid_keys)
193+
if not self.conn.req_checker.check_params(body,
194+
required_params=['eip'],
195+
integer_params=[],
196+
list_params=[]
197+
):
198+
return None
199+
200+
return self.conn.send_request(action, body)

qingcloud/iaas/actions/image.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# =========================================================================
2+
# Copyright 2012-present Yunify, Inc.
3+
# -------------------------------------------------------------------------
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this work except in compliance with the License.
6+
# You may obtain a copy of the License in the LICENSE file, or 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+
from qingcloud.iaas import constants as const
18+
from qingcloud.misc.utils import filter_out_none
19+
20+
21+
class ImageAction(object):
22+
23+
def __init__(self, conn):
24+
self.conn = conn
25+
26+
def describe_images(self, images=None,
27+
tags=None,
28+
os_family=None,
29+
processor_type=None,
30+
status=None,
31+
visibility=None,
32+
provider=None,
33+
verbose=0,
34+
search_word=None,
35+
owner=None,
36+
offset=None,
37+
limit=None,
38+
**ignore):
39+
""" Describe images filtered by condition.
40+
@param images: an array including IDs of the images you want to list.
41+
No ID specified means list all.
42+
@param tags: the array of IDs of tags.
43+
@param os_family: os family, windows/debian/centos/ubuntu.
44+
@param processor_type: supported processor types are `64bit` and `32bit`.
45+
@param status: valid values include pending, available, deleted, ceased.
46+
@param visibility: who can see and use this image. Valid values include public, private.
47+
@param provider: who provide this image, self, system.
48+
@param verbose: the number to specify the verbose level,
49+
larger the number, the more detailed information will be returned.
50+
@param search_word: the search word.
51+
@param offset: the starting offset of the returning results.
52+
@param limit: specify the number of the returning results.
53+
"""
54+
55+
action = const.ACTION_DESCRIBE_IMAGES
56+
valid_keys = ['images', 'os_family', 'processor_type', 'status', 'visibility',
57+
'provider', 'verbose', 'search_word', 'offset', 'limit', 'owner',
58+
'tags']
59+
body = filter_out_none(locals(), valid_keys)
60+
if not self.conn.req_checker.check_params(body,
61+
required_params=[],
62+
integer_params=[
63+
"offset", "limit", "verbose"],
64+
list_params=["images", "tags"]
65+
):
66+
return None
67+
68+
return self.conn.send_request(action, body)
69+
70+
def capture_instance(self, instance,
71+
image_name="",
72+
**ignore):
73+
""" Capture an instance and make it available as an image for reuse.
74+
@param instance: ID of the instance you want to capture.
75+
@param image_name: short name of the image.
76+
"""
77+
action = const.ACTION_CAPTURE_INSTANCE
78+
valid_keys = ['instance', 'image_name']
79+
body = filter_out_none(locals(), valid_keys)
80+
if not self.conn.req_checker.check_params(body,
81+
required_params=['instance'],
82+
integer_params=[],
83+
list_params=[]
84+
):
85+
return None
86+
87+
return self.conn.send_request(action, body)
88+
89+
def delete_images(self, images,
90+
**ignore):
91+
""" Delete one or more images whose provider is `self`.
92+
@param images: ID of the images you want to delete.
93+
"""
94+
action = const.ACTION_DELETE_IMAGES
95+
body = {'images': images}
96+
if not self.conn.req_checker.check_params(body,
97+
required_params=['images'],
98+
integer_params=[],
99+
list_params=[]
100+
):
101+
return None
102+
103+
return self.conn.send_request(action, body)
104+
105+
def modify_image_attributes(self, image,
106+
image_name=None,
107+
description=None,
108+
**ignore):
109+
""" Modify image attributes.
110+
@param image: the ID of image whose attributes you want to modify.
111+
@param image_name: Name of the image. It's a short name for the image
112+
that more meaningful than image id.
113+
@param description: The detailed description of the image.
114+
"""
115+
action = const.ACTION_MODIFY_IMAGE_ATTRIBUTES
116+
valid_keys = ['image', 'image_name', 'description']
117+
body = filter_out_none(locals(), valid_keys)
118+
if not self.conn.req_checker.check_params(body,
119+
required_params=['image'],
120+
integer_params=[],
121+
list_params=[]
122+
):
123+
return None
124+
125+
return self.conn.send_request(action, body)

0 commit comments

Comments
 (0)