Skip to content

Commit 70bf59b

Browse files
committed
Refactor cluster actions
1 parent d40036f commit 70bf59b

File tree

4 files changed

+164
-120
lines changed

4 files changed

+164
-120
lines changed

qingcloud/iaas/actions/cluster.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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)

qingcloud/iaas/connection.py

Lines changed: 3 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
import uuid
2020

2121
from qingcloud.iaas.actions.alarm_policy import AlarmPolicy
22+
from qingcloud.iaas.actions.cluster import ClusterAction
2223
from qingcloud.iaas.actions.nic import NicAction
24+
from qingcloud.iaas.actions.s2 import S2Action
2325

2426
from qingcloud.conn.auth import QuerySignatureAuthHandler
2527
from qingcloud.conn.connection import HttpConnection, HTTPRequest
26-
from qingcloud.iaas.actions.s2 import S2Action
2728
from qingcloud.misc.json_tool import json_load, json_dump
2829
from qingcloud.misc.utils import filter_out_none
2930
from . import constants as const
@@ -69,6 +70,7 @@ def __init__(self, qy_access_key_id, qy_secret_access_key, zone,
6970
NicAction(self),
7071
AlarmPolicy(self),
7172
S2Action(self),
73+
ClusterAction(self),
7274
]
7375

7476
def send_request(self, action, body, url="/iaas/", verb="GET"):
@@ -5134,121 +5136,3 @@ def verify_notification_item(self,
51345136
return None
51355137

51365138
return self.send_request(action, body)
5137-
5138-
def start_clusters(self, clusters,
5139-
**ignore):
5140-
""" Start one or more clusters.
5141-
@param clusters: the array of clusters IDs.
5142-
"""
5143-
action = const.ACTION_START_CLUSTERS
5144-
body = {'clusters': clusters}
5145-
if not self.req_checker.check_params(body,
5146-
required_params=['clusters'],
5147-
integer_params=[],
5148-
list_params=['clusters']
5149-
):
5150-
return None
5151-
5152-
return self.send_request(action, body)
5153-
5154-
def stop_clusters(self, clusters,
5155-
**ignore):
5156-
""" Stop one or more clusters.
5157-
@param clusters: the array of clusters IDs.
5158-
"""
5159-
action = const.ACTION_STOP_CLUSTERS
5160-
body = {'clusters': clusters}
5161-
if not self.req_checker.check_params(body,
5162-
required_params=['clusters'],
5163-
integer_params=[],
5164-
list_params=['clusters']
5165-
):
5166-
return None
5167-
5168-
return self.send_request(action, body)
5169-
5170-
def resize_cluster(self, cluster,
5171-
node_role=None,
5172-
cpu=None,
5173-
memory=None,
5174-
storage_size=None,
5175-
**ignore):
5176-
""" Resize cluster
5177-
@param cluster: the ID of the cluster you want to resize.
5178-
@param cpu: cpu core number.
5179-
@param memory: memory size in MB.
5180-
@param storage_size: The new larger size of the storage_size, unit is GB.
5181-
"""
5182-
action = const.ACTION_RESIZE_CLUSTER
5183-
valid_keys = ['cluster', 'node_role', 'cpu', 'memory', 'storage_size']
5184-
body = filter_out_none(locals(), valid_keys)
5185-
if not self.req_checker.check_params(body,
5186-
required_params=['cluster'],
5187-
integer_params=['cpu', 'memory']
5188-
):
5189-
return None
5190-
5191-
return self.send_request(action, body)
5192-
5193-
def describe_clusters(self, clusters=None,
5194-
role=None,
5195-
status=None,
5196-
verbose=1,
5197-
search_word=None,
5198-
owner=None,
5199-
offset=None,
5200-
limit=None,
5201-
tags=None,
5202-
**ignore):
5203-
""" Describe clusters filtered by condition.
5204-
@param clusters: the array of cluster IDs.
5205-
@param status: pending, active, stopped, deleted, suspended, ceased
5206-
@param verbose: the number to specify the verbose level, larger the number, the more detailed information will be returned.
5207-
@param search_word: search word column.
5208-
@param offset: the starting offset of the returning results.
5209-
@param limit: specify the number of the returning results.
5210-
@param tags : the array of IDs of tags.
5211-
"""
5212-
action = const.ACTION_DESCRIBE_CLUSTERS
5213-
valid_keys = ['clusters', 'status', 'verbose', 'search_word',
5214-
'owner', 'offset', 'limit', 'tags', 'role']
5215-
body = filter_out_none(locals(), valid_keys)
5216-
if not self.req_checker.check_params(body,
5217-
required_params=[],
5218-
integer_params=[
5219-
'offset', 'limit'],
5220-
list_params=[
5221-
'clusters', 'status', 'tags']
5222-
):
5223-
return None
5224-
5225-
return self.send_request(action, body)
5226-
5227-
def add_cluster_nodes(self, cluster, node_count, owner=None, node_name=None, node_role=None, resource_conf=None, **params):
5228-
""" Add one or more cluster nodes
5229-
"""
5230-
action = const.ACTION_ADD_CLUSTER_NODES
5231-
valid_keys = ['cluster', 'node_count', 'owner', 'node_name', 'node_role', 'resource_conf']
5232-
body = filter_out_none(locals(), valid_keys)
5233-
if not self.req_checker.check_params(body,
5234-
required_params=[
5235-
'cluster', 'node_count'],
5236-
integer_params=['node_count']
5237-
):
5238-
return None
5239-
5240-
return self.send_request(action, body)
5241-
5242-
def delete_cluster_nodes(self, cluster, nodes, owner=None):
5243-
""" Delete one or more cluster nodes
5244-
"""
5245-
action = const.ACTION_DELETE_CLUSTER_NODES
5246-
valid_keys = ['cluster', 'nodes', 'owner']
5247-
body = filter_out_none(locals(), valid_keys)
5248-
if not self.req_checker.check_params(body,
5249-
required_params=[
5250-
'cluster', 'nodes'],
5251-
list_params=['nodes']):
5252-
return None
5253-
5254-
return self.send_request(action, body)

qingcloud/iaas/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,4 @@
367367
ACTION_DESCRIBE_CLUSTERS = "DescribeClusters"
368368
ACTION_ADD_CLUSTER_NODES = "AddClusterNodes"
369369
ACTION_DELETE_CLUSTER_NODES = "DeleteClusterNodes"
370+
ACTION_DELETE_CLUSTERS = "DeleteClusters"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
setup(
1616
name='qingcloud-sdk',
17-
version='1.2',
17+
version='1.2.1',
1818
description='Software Development Kit for QingCloud.',
1919
long_description=open('README.rst', 'rb').read().decode('utf-8'),
2020
keywords='qingcloud iaas qingstor sdk',

0 commit comments

Comments
 (0)