Skip to content

Commit d321ca1

Browse files
committed
reset the kirk api to the previous version
1 parent a26adb8 commit d321ca1

File tree

7 files changed

+1011
-0
lines changed

7 files changed

+1011
-0
lines changed

examples/kirk/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Examples
2+
3+
```
4+
$ python list_apps.py <YOUR_AK> <YOUR_SK>
5+
```

examples/kirk/list_apps.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
# flake8: noqa
3+
4+
import sys
5+
from qiniu import QiniuMacAuth
6+
from qiniu import AccountClient
7+
8+
access_key = sys.argv[1]
9+
secret_key = sys.argv[2]
10+
11+
acc_client = AccountClient(QiniuMacAuth(access_key, secret_key))
12+
13+
ret, info = acc_client.list_apps()
14+
15+
print(ret)
16+
print(info)
17+
18+
assert len(ret) is not None

examples/kirk/list_services.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding: utf-8 -*-
2+
# flake8: noqa
3+
4+
import sys
5+
from qiniu import QiniuMacAuth
6+
from qiniu import AccountClient
7+
8+
access_key = sys.argv[1]
9+
secret_key = sys.argv[2]
10+
11+
acc_client = AccountClient(QiniuMacAuth(access_key, secret_key))
12+
apps, info = acc_client.list_apps()
13+
14+
for app in apps:
15+
if app.get('runMode') == 'Private':
16+
uri = app.get('uri')
17+
qcos = acc_client.get_qcos_client(uri)
18+
if qcos != None:
19+
stacks, info = qcos.list_stacks()
20+
for stack in stacks:
21+
stack_name = stack.get('name')
22+
services, info = qcos.list_services(stack_name)
23+
print("list_services of '%s : %s':"%(uri, stack_name))
24+
print(services)
25+
print(info)
26+
assert len(services) is not None

examples/kirk/list_stacks.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
# flake8: noqa
3+
4+
import sys
5+
from qiniu import QiniuMacAuth
6+
from qiniu import AccountClient
7+
8+
access_key = sys.argv[1]
9+
secret_key = sys.argv[2]
10+
11+
acc_client = AccountClient(QiniuMacAuth(access_key, secret_key))
12+
apps, info = acc_client.list_apps()
13+
14+
for app in apps:
15+
if app.get('runMode') == 'Private':
16+
uri = app.get('uri')
17+
qcos = acc_client.get_qcos_client(uri)
18+
if qcos != None:
19+
stacks, info = qcos.list_stacks()
20+
print("list_stacks of '%s':"%uri)
21+
print(stacks)
22+
print(info)
23+
assert len(stacks) is not None

qiniu/services/compute/app.py

+224
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
# -*- coding: utf-8 -*-
2+
from qiniu import http, QiniuMacAuth
3+
from .config import KIRK_HOST
4+
from .qcos_api import QcosClient
5+
6+
7+
class AccountClient(object):
8+
"""客户端入口
9+
10+
使用账号密钥生成账号客户端,可以进一步:
11+
1、获取和操作账号数据
12+
2、获得部署的应用的客户端
13+
14+
属性:
15+
auth: 账号管理密钥对,QiniuMacAuth对象
16+
host: API host,在『内网模式』下使用时,auth=None,会自动使用 apiproxy 服务
17+
18+
接口:
19+
get_qcos_client(app_uri)
20+
create_qcos_client(app_uri)
21+
get_app_keys(app_uri)
22+
get_valid_app_auth(app_uri)
23+
get_account_info()
24+
get_app_region_products(app_uri)
25+
get_region_products(region)
26+
list_regions()
27+
list_apps()
28+
create_app(args)
29+
delete_app(app_uri)
30+
31+
"""
32+
33+
def __init__(self, auth, host=None):
34+
self.auth = auth
35+
self.qcos_clients = {}
36+
if (auth is None):
37+
self.host = KIRK_HOST['APPPROXY']
38+
else:
39+
self.host = host or KIRK_HOST['APPGLOBAL']
40+
acc, info = self.get_account_info()
41+
self.uri = acc.get('name')
42+
43+
def get_qcos_client(self, app_uri):
44+
"""获得资源管理客户端
45+
缓存,但不是线程安全的
46+
"""
47+
48+
client = self.qcos_clients.get(app_uri)
49+
if (client is None):
50+
client = self.create_qcos_client(app_uri)
51+
self.qcos_clients[app_uri] = client
52+
53+
return client
54+
55+
def create_qcos_client(self, app_uri):
56+
"""创建资源管理客户端
57+
58+
"""
59+
60+
if (self.auth is None):
61+
return QcosClient(None)
62+
63+
products = self.get_app_region_products(app_uri)
64+
auth = self.get_valid_app_auth(app_uri)
65+
66+
if products is None or auth is None:
67+
return None
68+
69+
return QcosClient(auth, products.get('api'))
70+
71+
def get_app_keys(self, app_uri):
72+
"""获得账号下应用的密钥
73+
74+
列出指定应用的密钥,仅当访问者对指定应用有管理权限时有效:
75+
用户对创建的应用有管理权限。
76+
用户对使用的第三方应用没有管理权限,第三方应用的运维方有管理权限。
77+
78+
Args:
79+
- app_uri: 应用的完整标识
80+
81+
Returns:
82+
返回一个tuple对象,其格式为(<result>, <ResponseInfo>)
83+
- result 成功返回秘钥列表,失败返回None
84+
- ResponseInfo 请求的Response信息
85+
"""
86+
87+
url = '{0}/v3/apps/{1}/keys'.format(self.host, app_uri)
88+
return http._get_with_qiniu_mac(url, None, self.auth)
89+
90+
def get_valid_app_auth(self, app_uri):
91+
"""获得账号下可用的应用的密钥
92+
93+
列出指定应用的可用密钥
94+
95+
Args:
96+
- app_uri: 应用的完整标识
97+
98+
Returns:
99+
返回一个tuple对象,其格式为(<result>, <ResponseInfo>)
100+
- result 成功返回可用秘钥列表,失败返回None
101+
- ResponseInfo 请求的Response信息
102+
"""
103+
104+
ret, retInfo = self.get_app_keys(app_uri)
105+
106+
if ret is None:
107+
return None
108+
109+
for k in ret:
110+
if (k.get('state') == 'enabled'):
111+
return QiniuMacAuth(k.get('ak'), k.get('sk'))
112+
113+
return None
114+
115+
def get_account_info(self):
116+
"""获得当前账号的信息
117+
118+
查看当前请求方(请求鉴权使用的 AccessKey 的属主)的账号信息。
119+
120+
Returns:
121+
返回一个tuple对象,其格式为(<result>, <ResponseInfo>)
122+
- result 成功返回用户信息,失败返回None
123+
- ResponseInfo 请求的Response信息
124+
"""
125+
126+
url = '{0}/v3/info'.format(self.host)
127+
return http._get_with_qiniu_mac(url, None, self.auth)
128+
129+
def get_app_region_products(self, app_uri):
130+
"""获得指定应用所在区域的产品信息
131+
132+
Args:
133+
- app_uri: 应用的完整标识
134+
135+
Returns:
136+
返回产品信息列表,若失败则返回None
137+
"""
138+
apps, retInfo = self.list_apps()
139+
if apps is None:
140+
return None
141+
142+
for app in apps:
143+
if (app.get('uri') == app_uri):
144+
return self.get_region_products(app.get('region'))
145+
146+
return
147+
148+
def get_region_products(self, region):
149+
"""获得指定区域的产品信息
150+
151+
Args:
152+
- region: 区域,如:"nq"
153+
154+
Returns:
155+
返回该区域的产品信息,若失败则返回None
156+
"""
157+
158+
regions, retInfo = self.list_regions()
159+
if regions is None:
160+
return None
161+
162+
for r in regions:
163+
if r.get('name') == region:
164+
return r.get('products')
165+
166+
def list_regions(self):
167+
"""获得账号可见的区域的信息
168+
169+
列出当前用户所有可使用的区域。
170+
171+
Returns:
172+
返回一个tuple对象,其格式为(<result>, <ResponseInfo>)
173+
- result 成功返回区域列表,失败返回None
174+
- ResponseInfo 请求的Response信息
175+
"""
176+
177+
url = '{0}/v3/regions'.format(self.host)
178+
return http._get_with_qiniu_mac(url, None, self.auth)
179+
180+
def list_apps(self):
181+
"""获得当前账号的应用列表
182+
183+
列出所属应用为当前请求方的应用列表。
184+
185+
Returns:
186+
返回一个tuple对象,其格式为(<result>, <ResponseInfo>)
187+
- result 成功返回应用列表,失败返回None
188+
- ResponseInfo 请求的Response信息
189+
"""
190+
191+
url = '{0}/v3/apps'.format(self.host)
192+
return http._get_with_qiniu_mac(url, None, self.auth)
193+
194+
def create_app(self, args):
195+
"""创建应用
196+
197+
在指定区域创建一个新应用,所属应用为当前请求方。
198+
199+
Args:
200+
- args: 请求参数(json),参考 http://kirk-docs.qiniu.com/apidocs/
201+
202+
Returns:
203+
- result 成功返回所创建的应用信息,若失败则返回None
204+
- ResponseInfo 请求的Response信息
205+
"""
206+
207+
url = '{0}/v3/apps'.format(self.host)
208+
return http._post_with_qiniu_mac(url, args, self.auth)
209+
210+
def delete_app(self, app_uri):
211+
"""删除应用
212+
213+
删除指定标识的应用,当前请求方对该应用应有删除权限。
214+
215+
Args:
216+
- app_uri: 应用的完整标识
217+
218+
Returns:
219+
- result 成功返回空dict{},若失败则返回None
220+
- ResponseInfo 请求的Response信息
221+
"""
222+
223+
url = '{0}/v3/apps/{1}'.format(self.host, app_uri)
224+
return http._delete_with_qiniu_mac(url, None, self.auth)

qiniu/services/compute/config.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
3+
KIRK_HOST = {
4+
'APPGLOBAL': "https://app-api.qiniu.com", # 公有云 APP API
5+
'APPPROXY': "http://app.qcos.qiniu", # 内网 APP API
6+
'APIPROXY': "http://api.qcos.qiniu", # 内网 API
7+
}
8+
9+
CONTAINER_UINT_TYPE = {
10+
'1U1G': '单核(CPU),1GB(内存)',
11+
'1U2G': '单核(CPU),2GB(内存)',
12+
'1U4G': '单核(CPU),4GB(内存)',
13+
'1U8G': '单核(CPU),8GB(内存)',
14+
'2U2G': '双核(CPU),2GB(内存)',
15+
'2U4G': '双核(CPU),4GB(内存)',
16+
'2U8G': '双核(CPU),8GB(内存)',
17+
'2U16G': '双核(CPU),16GB(内存)',
18+
'4U8G': '四核(CPU),8GB(内存)',
19+
'4U16G': '四核(CPU),16GB(内存)',
20+
'8U16G': '八核(CPU),16GB(内存)',
21+
}

0 commit comments

Comments
 (0)