def getEcsListDetailFromAllRegin(self): userInfo = UserInfo.UserInfo() sourthToken = userInfo.getUserTokenByProjectId( regionProjectId=userInfo.sourthProjectId) sourthPubHeader = self.getPubHeaderForToken( projectId=userInfo.sourthProjectId, token=sourthToken) sourthServers = self.getEcsListDetailFromRegin( ecsPubHeaderForToken=sourthPubHeader, regionEndPoint=self.cnSouth1EndPoint) northToken = userInfo.getUserTokenByProjectId( regionProjectId=userInfo.northProjectId) northPubHeader = self.getPubHeaderForToken( projectId=userInfo.northProjectId, token=northToken) northServers = self.getEcsListDetailFromRegin( ecsPubHeaderForToken=northPubHeader, regionEndPoint=self.cnNorth1EndPoint) eastToken = userInfo.getUserTokenByProjectId( regionProjectId=userInfo.eastProjectId) eastPubHeader = self.getPubHeaderForToken( projectId=userInfo.eastProjectId, token=eastToken) eastServers = self.getEcsListDetailFromRegin( ecsPubHeaderForToken=eastPubHeader, regionEndPoint=self.cnEast2EndPoint) northEastToken = userInfo.getUserTokenByProjectId( regionProjectId=userInfo.northeastProjectId) northEastPubHeader = self.getPubHeaderForToken( projectId=userInfo.northeastProjectId, token=northEastToken) northEastServers = self.getEcsListDetailFromRegin( ecsPubHeaderForToken=northEastPubHeader, regionEndPoint=self.cnNortheast1EndPoint) hongKangToken = userInfo.getUserTokenByProjectId( regionProjectId=userInfo.hangKongProjectId) hongKangPubHeader = self.getPubHeaderForToken( projectId=userInfo.hangKongProjectId, token=hongKangToken) hongkangServers = self.getEcsListDetailFromRegin( ecsPubHeaderForToken=hongKangPubHeader, regionEndPoint=self.apSouthEast1EndPoint) servers = [] servers.append(sourthServers) servers.append(northServers) servers.append(eastServers) servers.append(northEastServers) servers.append(hongkangServers) HwcloudLog().debug( "get ECS List Detial Data From all Region Message is") HwcloudLog().debug(servers) HwcloudLog().info("获取ECS List 详情页面全region信息。") return servers
def getEcsListDetailFromRegin(self, ecsPubHeaderForToken, regionEndPoint): projectId_str = ecsPubHeaderForToken["X-Project-Id"] getUrl = regionEndPoint + "/v2/" + projectId_str + "/servers/detail" getEcsReq = requests.get(url=getUrl, headers=ecsPubHeaderForToken) if getEcsReq.status_code == 200: servers = getEcsReq.json() HwcloudLog().info("get Ecs List Detail from region %s success. " % regionEndPoint) return servers else: HwcloudLog().error("get Ecs List Detail from region %s failure. " % regionEndPoint) HwcloudLog().error("statue.code is %s" % getEcsReq.status_code) HwcloudLog().error("Error Message is : %s" % getEcsReq.text) return []
def getPubHeaderForToken(self, projectId, token): header = { "Content-Type": "application/json", "X-Project-Id": projectId, "X-Auth-Token": token, } HwcloudLog().debug("Return ECS API Request Header.") return header
def sendMessage(projectId, phones, token, message): message = message url = "https://smn.cn-north-1.myhwclouds.com/v2/" + projectId + "/notifications/sms" SMN_headers = { "Content-type": "application/json", "X-Auth-Token": token } # header for phone in phones: # 接口不允许群发短信,弄个循环搞定,phone取值后type为<str> body = {"endpoint": phone, "message": message} # 消息体json格式,从帮助中心获得! smsReq = requests.post(url=url, headers=SMN_headers, json=body) if smsReq.status_code == 200: HwcloudLog().info("sms send success to %s" % phone), time.sleep(0.1) else: HwcloudLog().error("sms send failure to %s" % phone)
def getUserTokenByDomainName(self): reqUrl = "https://iam.cn-north-1.myhuaweicloud.com/v3/auth/tokens" header = self.getRequestHeader() body = self.getRequestBodyByDomainName() iamReq = requests.post( url=reqUrl, headers=header, json=body, ) if iamReq.status_code == 201: HwcloudLog().info('get iam domain token success') token = iamReq.headers["X-Subject-Token"] HwcloudLog().debug("token values is %s" % token) return token else: HwcloudLog().error("get iam domain token failed") HwcloudLog().error(iamReq.status_code) HwcloudLog().error(iamReq.text) return ""
def getUserTokenByProjectId(self, regionProjectId): reqUrl = "https://iam.cn-north-1.myhuaweicloud.com/v3/auth/tokens" header = self.getRequestHeader() body = self.getRequestBodyByProjectId(regionProjectId=regionProjectId) iamReq = requests.post( url=reqUrl, headers=header, json=body, ) if iamReq.status_code == 201: HwcloudLog().info("get iam token success by project id.") token = iamReq.headers["X-Subject-Token"] HwcloudLog().debug("token values is %s" % token) return token else: HwcloudLog().error( "get iam token failed by project id, return none.") HwcloudLog().error(iamReq.status_code) HwcloudLog().error(iamReq.text) return ""
def getMessage(ecsLists): groupAList = [] groupBList = [] groupCList = [] groupDList = [] groupEList = [] groupOthers = [] messageEcs = "华为云账号hwx535937,全region Ecs资源为:" for ecsinfo in ecsLists: if ecsinfo.iamUserName == "groupA": groupAList.append(ecsinfo) elif ecsinfo.iamUserName == "groupB": groupBList.append(ecsinfo) elif ecsinfo.iamUserName == "groupC": groupCList.append(ecsinfo) elif ecsinfo.iamUserName == "groupD": groupDList.append(ecsinfo) elif ecsinfo.iamUserName == "groupE": groupEList.append(ecsinfo) else: groupOthers.append(ecsinfo) if len(groupAList) > 0: message = "\n用户groupA,有ECS资源" + str(len(groupAList)) + "台,分别是:" for ecsA in groupAList: # message += ecsA.ecsName + "(" + ecsA.ecsCreated + ")" message += ecsA.ecsName message += "," # print(message) messageEcs += message if len(groupBList) > 0: message = "\n用户groupB,有ECS资源" + str(len(groupBList)) + "台,分别是:" for ecsB in groupBList: # message += ecsB.ecsName + "(" + ecsB.ecsCreated + ")" message += ecsB.ecsName message += " , " # print(message) messageEcs += message if len(groupCList) > 0: message = "\n用户groupC,有ECS资源" + str(len(groupCList)) + "台,分别是:" for ecsC in groupCList: # message += ecsC.ecsName + "(" + ecsC.ecsCreated + ")" message += ecsC.ecsName message += " , " # print(message) messageEcs += message if len(groupDList) > 0: message = "\n用户groupD,有ECS资源" + str(len(groupDList)) + "台,分别是:" for ecsD in groupDList: # message += ecsD.ecsName + "(" + ecsD.ecsCreated + ")" message += ecsD.ecsName message += " , " # print(message) messageEcs += message if len(groupEList) > 0: message = "\n用户groupE,有ECS资源" + str(len(groupEList)) + "台,分别是:" for ecsC in groupEList: # message += ecsC.ecsName + "(" + ecsC.ecsCreated + ")" message += ecsC.ecsName message += " , " # print(message) messageEcs += message if len(groupOthers) > 0: message = "\n其他用户,有ECS资源" + str(len(groupOthers)) + "台,分别是:" for ecsC in groupOthers: # message += ecsC.ecsName + "(" + ecsC.ecsCreated + ")" message += ecsC.ecsName message += " , " # print(message) messageEcs += message messageEcs += "\n请各组长及时清理,避免浪费。" HwcloudLog().info(messageEcs) return messageEcs