|
| 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 ClusterAction(object): |
| 22 | + |
| 23 | + def __init__(self, conn): |
| 24 | + self.conn = conn |
| 25 | + |
| 26 | + def start_clusters(self, clusters, |
| 27 | + **ignore): |
| 28 | + """ Start one or more clusters. |
| 29 | + @param clusters: the array of clusters IDs. |
| 30 | + """ |
| 31 | + action = const.ACTION_START_CLUSTERS |
| 32 | + body = {'clusters': clusters} |
| 33 | + if not self.conn.req_checker.check_params(body, |
| 34 | + required_params=['clusters'], |
| 35 | + integer_params=[], |
| 36 | + list_params=['clusters'] |
| 37 | + ): |
| 38 | + return None |
| 39 | + |
| 40 | + return self.conn.send_request(action, body) |
| 41 | + |
| 42 | + def stop_clusters(self, clusters, |
| 43 | + **ignore): |
| 44 | + """ Stop one or more clusters. |
| 45 | + @param clusters: the array of clusters IDs. |
| 46 | + """ |
| 47 | + action = const.ACTION_STOP_CLUSTERS |
| 48 | + body = {'clusters': clusters} |
| 49 | + if not self.conn.req_checker.check_params(body, |
| 50 | + required_params=['clusters'], |
| 51 | + integer_params=[], |
| 52 | + list_params=['clusters'] |
| 53 | + ): |
| 54 | + return None |
| 55 | + |
| 56 | + return self.conn.send_request(action, body) |
| 57 | + |
| 58 | + def resize_cluster(self, cluster, |
| 59 | + node_role=None, |
| 60 | + cpu=None, |
| 61 | + memory=None, |
| 62 | + storage_size=None, |
| 63 | + **ignore): |
| 64 | + """ Resize cluster |
| 65 | + @param cluster: the ID of the cluster you want to resize. |
| 66 | + @param cpu: cpu core number. |
| 67 | + @param memory: memory size in MB. |
| 68 | + @param storage_size: The new larger size of the storage_size, unit is GB. |
| 69 | + """ |
| 70 | + action = const.ACTION_RESIZE_CLUSTER |
| 71 | + valid_keys = ['cluster', 'node_role', 'cpu', 'memory', 'storage_size'] |
| 72 | + body = filter_out_none(locals(), valid_keys) |
| 73 | + if not self.conn.req_checker.check_params(body, |
| 74 | + required_params=['cluster'], |
| 75 | + integer_params=['cpu', 'memory'] |
| 76 | + ): |
| 77 | + return None |
| 78 | + |
| 79 | + return self.conn.send_request(action, body) |
| 80 | + |
| 81 | + def describe_clusters(self, clusters=None, |
| 82 | + role=None, |
| 83 | + status=None, |
| 84 | + verbose=1, |
| 85 | + search_word=None, |
| 86 | + owner=None, |
| 87 | + offset=None, |
| 88 | + limit=None, |
| 89 | + tags=None, |
| 90 | + **ignore): |
| 91 | + """ Describe clusters filtered by condition. |
| 92 | + @param clusters: the array of cluster IDs. |
| 93 | + @param status: pending, active, stopped, deleted, suspended, ceased |
| 94 | + @param verbose: the number to specify the verbose level, larger the number, the more detailed information will be returned. |
| 95 | + @param search_word: search word column. |
| 96 | + @param offset: the starting offset of the returning results. |
| 97 | + @param limit: specify the number of the returning results. |
| 98 | + @param tags : the array of IDs of tags. |
| 99 | + """ |
| 100 | + action = const.ACTION_DESCRIBE_CLUSTERS |
| 101 | + valid_keys = ['clusters', 'status', 'verbose', 'search_word', |
| 102 | + 'owner', 'offset', 'limit', 'tags', 'role'] |
| 103 | + body = filter_out_none(locals(), valid_keys) |
| 104 | + if not self.conn.req_checker.check_params(body, |
| 105 | + required_params=[], |
| 106 | + integer_params=[ |
| 107 | + 'offset', 'limit'], |
| 108 | + list_params=[ |
| 109 | + 'clusters', 'status', 'tags'] |
| 110 | + ): |
| 111 | + return None |
| 112 | + |
| 113 | + return self.conn.send_request(action, body) |
| 114 | + |
| 115 | + def add_cluster_nodes(self, cluster, node_count, owner=None, node_name=None, node_role=None, resource_conf=None, **params): |
| 116 | + """ Add one or more cluster nodes |
| 117 | + """ |
| 118 | + action = const.ACTION_ADD_CLUSTER_NODES |
| 119 | + valid_keys = ['cluster', 'node_count', 'owner', 'node_name', 'node_role', 'resource_conf'] |
| 120 | + body = filter_out_none(locals(), valid_keys) |
| 121 | + if not self.conn.req_checker.check_params(body, |
| 122 | + required_params=[ |
| 123 | + 'cluster', 'node_count'], |
| 124 | + integer_params=['node_count'] |
| 125 | + ): |
| 126 | + return None |
| 127 | + |
| 128 | + return self.conn.send_request(action, body) |
| 129 | + |
| 130 | + def delete_cluster_nodes(self, cluster, nodes, owner=None): |
| 131 | + """ Delete one or more cluster nodes |
| 132 | + """ |
| 133 | + action = const.ACTION_DELETE_CLUSTER_NODES |
| 134 | + valid_keys = ['cluster', 'nodes', 'owner'] |
| 135 | + body = filter_out_none(locals(), valid_keys) |
| 136 | + if not self.conn.req_checker.check_params(body, |
| 137 | + required_params=[ |
| 138 | + 'cluster', 'nodes'], |
| 139 | + list_params=['nodes']): |
| 140 | + return None |
| 141 | + |
| 142 | + return self.conn.send_request(action, body) |
| 143 | + |
| 144 | + def delete_clusters(self, clusters, direct_cease=0): |
| 145 | + """ Delete one or more clusters. |
| 146 | + @param clusters: the array of clusters IDs. |
| 147 | + @param direct_cease: whether to keep deleted resource in recycle bin (direct_cease=0) or not (direct_cease=1). |
| 148 | + """ |
| 149 | + action = const.ACTION_DELETE_CLUSTERS |
| 150 | + valid_keys = ['clusters', 'direct_cease'] |
| 151 | + body = filter_out_none(locals(), valid_keys) |
| 152 | + if not self.conn.req_checker.check_params(body, |
| 153 | + required_params=['clusters'], |
| 154 | + integer_params=['direct_cease'], |
| 155 | + list_params=['clusters'] |
| 156 | + ): |
| 157 | + return None |
| 158 | + |
| 159 | + return self.conn.send_request(action, body) |
0 commit comments