|
| 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) |
0 commit comments