在芯片设计行业,ETX是常见的远程访问环境。用户在通过ETX访问远程环境前必须首先加入ETX系统,然后通过profile分配相关的环境的访问权限。
通常这些操作在ETX WEB页面手工操作,如果我们期望实现用户帐号注册全自动化,就需要将以上操作通过脚本来实现。
经过调查发现ETX提供了REST访问接口,因此帐号注册全自动化就可以实现。
主要流程如下:
在调用ETX REST接口前还需要获取ETX API的URL以及访问密钥。登录ETX后,点击右上部用户头像旁边的齿轮图标,在用户设置中选择创建 API keys即可生成ETX API访问密钥。
ETX登录界面左上角选择 REST API即可显示所有ETX API,点击还可查看API的详细说明和示例。
通过 Python 自动创建ETX帐号的示例代码如下:
#!/opt/miniconda3/bin/python
################################################################################
#
# Author: Shuguangbo
#
################################################################################import traceback
import logging
import logging.config
import urllib3
import requests
import jsonurllib3.disable_warnings()class userHandler():def __init__(self):self._account = ''self._userID = ''self._etxUserID = ''self.getETXGroup()...def getETXUserID(self):session = NoneuserData = dict()userID = ''account = self._accounttry:apiurl = self._config['ETX']['ETX_URL'] + f'/etx/api/v2/users?where_login={account}'session = requests.Session()header = {'Content-Type': 'application/json', 'Authorization': f"ETX {self._config['ETX']['API_KEY']}"}result = session.get(apiurl, headers=header, verify=False)if result.status_code == 200:logging.info('Get ETX user ID succeeded.')userList = json.loads(result.text)userID = userList[0]['id']self._etxUserID = userIDelse:logging.error('Get ETX user ID failed.')except Exception as e:logging.error(f"Get ETX user ID failed. error: {str(e)}, stack: {traceback.format_exc()}")finally:if session:session.close()return userIDdef getETXGroup(self):groupData = dict()session = Nonetry:apiurl = self._config['ETX']['ETX_URL'] + '/etx/api/v2/usergroups'session = requests.Session()header = {'Content-Type': 'application/json', 'Authorization': f"ETX {self._config['ETX']['API_KEY']}"}result = session.get(apiurl, headers=header, verify=False)if result.status_code == 200:logging.info('Get ETX group info succeeded.')groupList = json.loads(result.text)for group in groupList:groupData[group['name']] = groupelse:logging.error(f'Get ETX group info failed. Error: {result.text}')exit(1)except Exception as e:logging.error(f"GET ETX user group info failed. error: {str(e)}, stack: {traceback.format_exc()}")exit(1)finally:if session:session.close()self._etxGroups = groupDatadef addETXUserIntoGroup(self, userID, groupID):result = Truesession = Nonetry:apiurl = self._config['ETX']['ETX_URL'] + f'/etx/api/v2/usergroups/{groupID}/members/addbyid/{userID}'session = requests.Session()header = {'Content-Type': 'application/json', 'Authorization': f"ETX {self._config['ETX']['API_KEY']}"}result = session.post(apiurl, headers=header, verify=False)if result and result.status_code == 200:logging.info(f'Add user {userID} into ETX group {groupID} succeeded.')else:logging.error(f'Add user {userID} into ETX group {groupID} failed.')result = Falseexcept Exception as e:logging.error(f"Add ETX user into user group failed. error: {str(e)}, stack: {traceback.format_exc()}")result = Falsefinally:if session:session.close()return resultdef registerETXUser(self):EMSG = ''session = Nonetry:account = self._accountname = self._nameemail = self._emailapiurl = self._config['ETX']['ETX_URL'] + '/etx/api/v2/users'session = requests.Session()header = {'Content-Type': 'application/json', 'Authorization': f"ETX {self._config['ETX']['API_KEY']}"}data = {'login': account, 'name': name, 'email': email, 'role':{'name':'User'}}result = session.post(apiurl, headers=header, data=json.dumps(data), verify=False)if result and result.status_code == 200:userData = json.loads(result.text)self._etxUserID = userData['id']logging.info(f'Register ETX user {account} succeeded. User id: {userData["id"]}')else:logging.error(f'Register ETX user {account} failed. Error: {result.text}')EMSG = f"Register ETX user {account} failed."except Exception as e:logging.error(f"Register ETX user {account} failed. Error: {str(e)}")EMSG = f"Register ETX user {account} failed."finally:if session:session.close()return EMSGdef addETXUser(self):EMSG = ''try:user = self._accountret = self._registerETXUser()if len(ret):EMSG += retreturn EMSGuserID = self._etxUserIDif len(userID) == 0:EMSG = "ETX user id is invalid.\n"return EMSGetxgroup = self._config['ETX']['DEFAULT_GROUP'] groupID = self._etxGroups[etxgroup]['id']ret = self.addETXUserIntoGroup(userID, groupID)if not ret:EMSG += f'Failed to add ETX profile for {user} .\n' except Exception as e:logging.error(f"Add ETX User profile failed. error: {str(e)}, stack: {traceback.format_exc()}"EMSG += "Add ETX User profile failed.\n"return EMSG