Example #1
0
    def showConfig(self):
        _credentialsPath = os.path.join(self.extensionCliHandler.aliyunConfigurePath,self.extensionCliHandler.credentials)
        _configurePath = os.path.join(self.extensionCliHandler.aliyunConfigurePath,self.extensionCliHandler.configure)
        config = dict()
        configContent = dict() 
	credentialsContent = dict ()
	if os.path.exists(_configurePath):
            for line in open(_configurePath):
                line = line.strip('\n')
                if line.find('=') > 0:
                    list = line.split("=",1)
		    configContent[list[0]] = list[1]
		else: 
		    pass
	config['configure'] = configContent
	if os.path.exists(_credentialsPath):
	    for line in open(_credentialsPath):
                line = line.strip('\n')
                if line.find('=') > 0:
                    list = line.split("=",1)
		    credentialsContent[list[0]] = list[1]
		else: 
		    pass 
	config ['credentials'] = credentialsContent
	response.display_response("showConfigure",config,'table')
Example #2
0
 def exportInstance(self,cmd,operation,version):
     if cmd.lower() == 'ecs' and operation.lower() =='exportinstance':
         operations = ['DescribeInstanceAttribute']
     else :
         return None
     _keyValues = self.parser.getCliKeyValues()
     # filename = self.getFileName(_keyValues)
     filename, errorMsg = self.getFileName(_keyValues)
     if filename is None:
         import cliError
         errorClass = cliError.error()
         errorClass.printInFormat(errorClass.CliException, errorMsg)
         return
     for item in operations:
         if self.apiHandler.isAvailableOperation(cmd, item, version):
             cmdInstance, mclassname = self.apiHandler.getInstanceByCmdOperation(cmd, item, version)
             if not cmdInstance is None:
                 newkeyValues = self.parser.getOpenApiKeyValues(_keyValues)
                 if self.apiHandler.needSetDefaultRegion(cmdInstance, newkeyValues):
                     newkeyValues["RegionId"] = [self.extensionHandler.getUserRegion()]
                 self.apiHandler.setAttr(cmdInstance, newkeyValues) # set all key values in instance
                 self.apiHandler.changeEndPoint(cmdInstance, newkeyValues)
                 try:
                     #result = cmdInstance.getResponse()
                     result = self.apiHandler.getResponse(cmd,operation, mclassname, cmdInstance, newkeyValues)
                     result = self._optimizeResult(result)
                     if("Code" in result):
                         response.display_response("error", result, "json")
                     else:
                         if not filename == None:
                             self.exportInstanceToFile(result,filename)
                         else:
                             print 'Filename is needed'
                 except Exception,e:
                     print(e)
Example #3
0
 def showConfig(self):
     _credentialsPath = os.path.join(
         self.extensionCliHandler.aliyunConfigurePath,
         self.extensionCliHandler.credentials)
     _configurePath = os.path.join(
         self.extensionCliHandler.aliyunConfigurePath,
         self.extensionCliHandler.configure)
     config = dict()
     configContent = dict()
     credentialsContent = dict()
     if os.path.exists(_configurePath):
         for line in open(_configurePath):
             line = line.strip('\n')
             if line.find('=') > 0:
                 list = line.split("=", 1)
                 configContent[list[0]] = list[1]
             else:
                 pass
     config['configure'] = configContent
     if os.path.exists(_credentialsPath):
         for line in open(_credentialsPath):
             line = line.strip('\n')
             if line.find('=') > 0:
                 list = line.split("=", 1)
                 credentialsContent[list[0]] = list[1]
             else:
                 pass
     config['credentials'] = credentialsContent
     response.display_response("showConfigure", config, 'table')
 def _handSubOperation(self, cmd, operations, keyValues, version):
     for item in operations:
         if self.apiHandler.isAvailableOperation(cmd, item, version):
             cmdInstance, mclassname = self.apiHandler.getInstanceByCmdOperation(
                 cmd, item, version)
             if not cmdInstance is None:
                 newkeyValues = self.parser.getOpenApiKeyValues(keyValues)
                 if self.apiHandler.needSetDefaultRegion(
                         cmdInstance, newkeyValues):
                     newkeyValues["RegionId"] = [
                         self.extensionHandler.getUserRegion()
                     ]
                 newkeyValues["ClientToken"] = [self.random_str()]
                 # print newkeyValues.keys()
                 # return
                 # self._setAttr(cmdInstance, newkeyValues) # set all key values in instance
                 # self.apiHandler.changeEndPoint(cmdInstance, newkeyValues)
                 try:
                     result = self.apiHandler.getResponse(
                         cmd, item, mclassname, cmdInstance, newkeyValues)
                     self.apiHandler.responseOptimize(result, cmd, item)
                     # result = cmdInstance.getResponse()
                     if ("Code" in result):
                         response.display_response("error", result, "json")
                     else:
                         response.display_response(item, result, "json")
                 except Exception, e:
                     print(e)
Example #5
0
 def _handSubOperation(self,cmd,operations,keyValues,version,_isAllocatePublicIp=False):
     if keyValues is None:
         return
     for item in operations:
         if self.apiHandler.isAvailableOperation(cmd, item, version):
             cmdInstance,mclassname = self.apiHandler.getInstanceByCmdOperation(cmd, item, version)
             if not cmdInstance is None:
                 newkeyValues = self.parser.getOpenApiKeyValues(keyValues)
                 if self.apiHandler.needSetDefaultRegion(cmdInstance, newkeyValues):
                     newkeyValues["RegionId"] = [self.extensionHandler.getUserRegion()]
                 # self._setAttr(cmdInstance, newkeyValues) # set all key values in instance
                 # self.apiHandler.changeEndPoint(cmdInstance, newkeyValues)
                 try:
                     # result = cmdInstance.getResponse()
                     result = self.apiHandler.getResponse(cmd, item, mclassname, cmdInstance, newkeyValues)
                     self.apiHandler.responseOptimize(result,cmd,item)
                     if("Code" in result):
                         response.display_response("error", result, "json")
                     else:
                         response.display_response(item, result, "json")
                         if _isAllocatePublicIp is True:
                             extraOperation = "AllocatePublicIpAddress"
                             instanceId = self.getInstanceIdFromJsonBody(result)
                             if instanceId is not None:
                                 key=list()
                                 _newkeyValues=dict()
                                 key.append(instanceId)
                                 _newkeyValues["InstanceId"] = key
                                 _newkeyValues["RegionId"] = newkeyValues["RegionId"]
                                 self._handExtraOperation(cmd,extraOperation,_newkeyValues,version)
                             else:
                                 print "InstanceId  is need!"
                 except Exception,e:
                     print(e)
Example #6
0
 def _handSubOperation(self,cmd,operations,keyValues,version,_isAllocatePublicIp=False,secureRequest=False):
     if keyValues is None:
         return
     for item in operations:
         if self.apiHandler.isAvailableOperation(cmd, item, version):
             cmdInstance,mclassname = self.apiHandler.getInstanceByCmdOperation(cmd, item, version)
             if not cmdInstance is None:
                 newkeyValues = self.parser.getOpenApiKeyValues(keyValues)
                 if self.apiHandler.needSetDefaultRegion(cmdInstance, newkeyValues):
                     newkeyValues["RegionId"] = [self.extensionHandler.getUserRegion()]
                 # self._setAttr(cmdInstance, newkeyValues) # set all key values in instance
                 # self.apiHandler.changeEndPoint(cmdInstance, newkeyValues)
                 try:
                     # result = cmdInstance.getResponse()
                     result = self.apiHandler.getResponse(cmd, item, mclassname, cmdInstance, newkeyValues,secureRequest)
                     self.apiHandler.responseOptimize(result,cmd,item)
                     if("Code" in result):
                         response.display_response("error", result, "json")
                     else:
                         response.display_response(item, result, "json")
                         if _isAllocatePublicIp is True:
                             extraOperation = "AllocatePublicIpAddress"
                             instanceId = self.getInstanceIdFromJsonBody(result)
                             if instanceId is not None:
                                 key=list()
                                 _newkeyValues=dict()
                                 key.append(instanceId)
                                 _newkeyValues["InstanceId"] = key
                                 _newkeyValues["RegionId"] = newkeyValues["RegionId"]
                                 self._handExtraOperation(cmd,extraOperation,_newkeyValues,version,secureRequest)
                             else:
                                 print "InstanceId  is need!"
                 except Exception,e:
                     print(e)
Example #7
0
 def _handExtraOperation(self,cmd,extraOperation,keyValues,version):
     if self.apiHandler.isAvailableOperation(cmd, extraOperation, version):
         cmdInstance,mclassname = self.apiHandler.getInstanceByCmdOperation(cmd, extraOperation, version)
         if  cmdInstance is not None:
             try:
                 # result = cmdInstance.getResponse()
                 result = self.apiHandler.getResponse(cmd, extraOperation, mclassname, cmdInstance, keyValues)
                 if("Code" in result):
                     response.display_response("error", result, "json")
                 else:
                     response.display_response(extraOperation, result, "json")
             except Exception,e:
                 print(e)
Example #8
0
 def _handExtraOperation(self,cmd,extraOperation,keyValues,version , secureRequest = False):
     if self.apiHandler.isAvailableOperation(cmd, extraOperation, version):
         cmdInstance,mclassname = self.apiHandler.getInstanceByCmdOperation(cmd, extraOperation, version)
         if  cmdInstance is not None:
             try:
                 # result = cmdInstance.getResponse()
                 result = self.apiHandler.getResponse(cmd, extraOperation, mclassname, cmdInstance, keyValues,secureRequest)
                 self.apiHandler.responseOptimize(result,cmd,extraOperation)
                 if("Code" in result):
                     response.display_response("error", result, "json")
                 else:
                     response.display_response(extraOperation, result, "json")
             except Exception,e:
                 print(e)
 def exportDBInstance(self, cmd, operation, version):
     rdsConfigure = commandConfigure.rds()
     if cmd.lower() == rdsConfigure.cmdName.lower() and operation.lower(
     ) == rdsConfigure.exportDBInstance.lower():
         pass
     else:
         return None
     _keyValues = self.parser.getCliKeyValues()
     filename, errorMsg = self.getFileName(_keyValues)
     if filename is None:
         import cliError
         errorClass = cliError.error()
         errorClass.printInFormat(errorClass.CliException, errorMsg)
         return
     operation = self.operations[0]
     if self.apiHandler.isAvailableOperation(cmd, operation, version):
         cmdInstance, mclassname = self.apiHandler.getInstanceByCmdOperation(
             cmd, operation, version)
         if not cmdInstance is None:
             newkeyValues = self.parser.getOpenApiKeyValues(_keyValues)
             if self.apiHandler.needSetDefaultRegion(
                     cmdInstance, newkeyValues):
                 newkeyValues["RegionId"] = [
                     self.extensionHandler.getUserRegion()
                 ]
             self.apiHandler.setAttr(
                 cmdInstance,
                 newkeyValues)  # set all key values in instance
             self.apiHandler.changeEndPoint(cmdInstance, newkeyValues)
             try:
                 result = self.apiHandler.getResponse(
                     cmd, operation, mclassname, cmdInstance, newkeyValues)
                 self.apiHandler.responseOptimize(result, cmd, operation)
                 # result = cmdInstance.getResponse()
                 # result = self._optimizeResult(result)
                 if ("Code" in result):
                     response.display_response("error", result, "json")
                 else:
                     if not filename == None:
                         self.exportInstanceToFile(result, filename)
                     else:
                         print 'Filename is needed'
             except Exception, e:
                 print(e)
Example #10
0
 def exportInstance(self, cmd, operation, version):
     if cmd.lower() == 'ecs' and operation.lower() == 'exportinstance':
         operations = ['DescribeInstanceAttribute']
     else:
         return None
     _keyValues = self.parser.getCliKeyValues()
     # filename = self.getFileName(_keyValues)
     filename, errorMsg = self.getFileName(_keyValues)
     if filename is None:
         import cliError
         errorClass = cliError.error()
         errorClass.printInFormat(errorClass.CliException, errorMsg)
         return
     for item in operations:
         if self.apiHandler.isAvailableOperation(cmd, item, version):
             cmdInstance, mclassname = self.apiHandler.getInstanceByCmdOperation(
                 cmd, item, version)
             if not cmdInstance is None:
                 newkeyValues = self.parser.getOpenApiKeyValues(_keyValues)
                 if self.apiHandler.needSetDefaultRegion(
                         cmdInstance, newkeyValues):
                     newkeyValues["RegionId"] = [
                         self.extensionHandler.getUserRegion()
                     ]
                 self.apiHandler.setAttr(
                     cmdInstance,
                     newkeyValues)  # set all key values in instance
                 self.apiHandler.changeEndPoint(cmdInstance, newkeyValues)
                 try:
                     #result = cmdInstance.getResponse()
                     result = self.apiHandler.getResponse(
                         cmd, operation, mclassname, cmdInstance,
                         newkeyValues)
                     result = self._optimizeResult(result)
                     if ("Code" in result):
                         response.display_response("error", result, "json")
                     else:
                         if not filename == None:
                             self.exportInstanceToFile(result, filename)
                         else:
                             print 'Filename is needed'
                 except Exception, e:
                     print(e)
Example #11
0
 def _handSubOperation(self,cmd,operations,keyValues,version):
     for item in operations:
         if self.apiHandler.isAvailableOperation(cmd, item, version):
             cmdInstance,mclassname = self.apiHandler.getInstanceByCmdOperation(cmd, item, version)
             if not cmdInstance is None:
                 newkeyValues = self.parser.getOpenApiKeyValues(keyValues)
                 if self.apiHandler.needSetDefaultRegion(cmdInstance, newkeyValues):
                     newkeyValues["RegionId"] = [self.extensionHandler.getUserRegion()]
                 # self._setAttr(cmdInstance, newkeyValues) # set all key values in instance
                 # self.apiHandler.changeEndPoint(cmdInstance, newkeyValues)
                 try:
                     # result = cmdInstance.getResponse()
                     result = self.apiHandler.getResponse(cmd, item, mclassname, cmdInstance, newkeyValues)
                     if("Code" in result):
                         response.display_response("error", result, "json")
                     else:
                         response.display_response(item, result, "json")
                 except Exception,e:
                     print(e)
Example #12
0
 def exportDBInstance(self, cmd, operation, version):
     rdsConfigure = commandConfigure.rds()
     if cmd.lower() == rdsConfigure.cmdName.lower() and operation.lower() ==rdsConfigure.exportDBInstance.lower():
         pass
     else :
         return None
     _keyValues = self.parser.getCliKeyValues()
     filename, errorMsg = self.getFileName(_keyValues)
     if filename is None:
         import cliError
         errorClass = cliError.error()
         errorClass.printInFormat(errorClass.CliException, errorMsg)
         return
     operation = self.operations[0]
     if self.apiHandler.isAvailableOperation(cmd, operation, version):
         cmdInstance, mclassname = self.apiHandler.getInstanceByCmdOperation(cmd, operation, version)
         if not cmdInstance is None:
             newkeyValues = self.parser.getOpenApiKeyValues(_keyValues)
             if self.apiHandler.needSetDefaultRegion(cmdInstance, newkeyValues):
                 newkeyValues["RegionId"] = [self.extensionHandler.getUserRegion()]
             self.apiHandler.setAttr(cmdInstance, newkeyValues) # set all key values in instance
             self.apiHandler.changeEndPoint(cmdInstance, newkeyValues)
             try:
                 result = self.apiHandler.getResponse(cmd,operation, mclassname, cmdInstance, newkeyValues)
                 self.apiHandler.responseOptimize(result,cmd,operation)
                 # result = cmdInstance.getResponse()
                 # result = self._optimizeResult(result)
                 if("Code" in result):
                     response.display_response("error", result, "json")
                 else:
                     if not filename == None:
                         self.exportInstanceToFile(result,filename)
                     else:
                         print 'Filename is needed'
             except Exception,e:
                 print(e)
Example #13
0
 def printInFormat(self, errorCode, errorMsg):
     content = {'Code': errorCode, 'Message': errorMsg}
     response.display_response("error", content, "json")
     pass
Example #14
0
    def main(self):

        # fetch command
        cmd = self.parser.getCliCmd()
        extensionCmdList = self.extensionHandler.getAllExtensionCommands()

        if cmd in extensionCmdList:
            self.handlerExtensionCmd(cmd)
            return

        # fetch operation
        operation = self.parser.getCliOperation()

        # fetch paramlist
        keyValues = self.parser._getKeyValues()
        outPutFormat = self.parser.getOutPutFormat(keyValues)
        if outPutFormat is None or len(outPutFormat) == 0:
            outPutFormat = self.extensionHandler.getUserFormat()
            if outPutFormat is None or outPutFormat == "":
                outPutFormat = 'json'
        else:
            outPutFormat = outPutFormat[0]

        if self.handler.isEndPointOperation(operation):
            keyValues = self.parser.getOpenApiKeyValues(keyValues)
            self.handler.handleEndPointOperation(cmd, operation, keyValues)
            return

        if self.handler.isAvailableCmd(cmd):
            # fetch getversion
            if self.handler.isNonStandardSdkCmd(cmd):
                self.handler.nonStandardSdkCmdHandle(cmd)
                return
            version = self.handler.getSdkVersion(cmd, keyValues)
            if version is None:
                return
            # here handler the openapi cmd
            if self.handler.isAvailableOperation(
                    cmd, operation,
                    version):  # cmd and operation both are right
                instanceAndClassName = self.handler.getInstanceByCmdOperation(
                    cmd, operation, version)
                if instanceAndClassName is not None and len(
                        instanceAndClassName) == 2:
                    cmdInstance = instanceAndClassName[0]
                    className = instanceAndClassName[1]
                    if cmdInstance is not None and className is not None:
                        if self.showInstanceAttribute(cmd, operation,
                                                      className):
                            return
                        # here should handle the keyValues first
                        keyValues = self.parser.getOpenApiKeyValues(keyValues)
                        if self.handler.needSetDefaultRegion(
                                cmdInstance, keyValues):
                            keyValues["RegionId"] = [
                                self.extensionHandler.getUserRegion()
                            ]
                        #check necessaryArgs as:accesskeyid accesskeysecret regionId
                        if not self.handler.hasNecessaryArgs(keyValues):
                            print 'accesskeyid/accesskeysecret/regionId is absence'
                            return
                        try:
                            result = self.handler.getResponse(
                                cmd, operation, className, cmdInstance,
                                keyValues)
                            if result is None:
                                return
                            self.handler.responseOptimize(
                                result, cmd, operation)
                            if ("Code" in result):
                                response.display_response(
                                    "error", result, "json")
                                # print("failed")
                                # print(result["Code"])
                                # print(result["Message"])
                                # print("Please check your parameters first.")
                            else:
                                #print(result)
                                response.display_response(
                                    operation, result, outPutFormat, keyValues)
                        except Exception as e:
                            print(e)
                    else:
                        print 'aliyuncli internal error, please contact: xixi.xxx'
            elif self.handler.isAvailableExtensionOperation(cmd, operation):
                if self.args.__len__() >= 3 and self.args[2] == 'help':
                    import commandConfigure
                    configure = commandConfigure.commandConfigure()
                    configure.showExtensionOperationHelp(cmd, operation)
                else:
                    self.extensionHandler.handlerExtensionOperation(
                        cmd, operation, version)
                # self.extensionHandler.handlerExtensionOperation(cmd,operation,version)
            else:
                # cmd is right but operation is not right
                self.helper.showOperationError(cmd, operation)
        else:
            self.helper.showCmdError(cmd)
Example #15
0
    def main(self):

        # fetch command
        cmd = self.parser.getCliCmd()
        extensionCmdList = self.extensionHandler.getAllExtensionCommands()

        if cmd in extensionCmdList:
            self.handlerExtensionCmd(cmd)
            return

        # fetch operation
        operation = self.parser.getCliOperation()

        # fetch paramlist
        keyValues = self.parser._getKeyValues()
        outPutFormat = self.parser.getOutPutFormat(keyValues)
        if outPutFormat is None or len(outPutFormat) == 0:
            outPutFormat = self.extensionHandler.getUserFormat()
            if outPutFormat is None or outPutFormat == "":
                outPutFormat = 'json'
        else:
            outPutFormat = outPutFormat[0]
        if self.handler.isAvailableCmd(cmd):
            # fetch getversion
            version=self.handler.getSdkVersion(cmd,keyValues)
            if version is None:
                return
            # here handler the openapi cmd
            if self.handler.isAvailableOperation(cmd, operation,version): # cmd and operation both are right
                instanceAndClassName=self.handler.getInstanceByCmdOperation(cmd, operation,version)
                if instanceAndClassName is not None and len(instanceAndClassName)==2:
                    cmdInstance = instanceAndClassName[0]
                    className = instanceAndClassName[1]
                    if cmdInstance is not None and className is not None:
                        if self.showInstanceAttribute(cmd, operation, className):
                            return
                        # here should handle the keyValues first
                        keyValues = self.parser.getOpenApiKeyValues(keyValues)
                        if self.handler.needSetDefaultRegion(cmdInstance, keyValues):
                            keyValues["RegionId"] = [self.extensionHandler.getUserRegion()]
                        #check necessaryArgs as:accesskeyid accesskeysecret regionId
                        if not self.handler.hasNecessaryArgs(keyValues):
                            print 'accesskeyid/accesskeysecret/regionId is absence'
                            return
                        try:
                            result = self.handler.getResponse(cmd,operation,className,cmdInstance,keyValues)
                            if result is None:
                                return
                            if("Code" in result):
                                response.display_response("error", result, "json")
                                # print("failed")
                                # print(result["Code"])
                                # print(result["Message"])
                                # print("Please check your parameters first.")
                            else:
                                #print(result)
                                response.display_response(operation, result, outPutFormat,keyValues)
                        except Exception as e:
                            print(e)
                    else:
                        print 'aliyuncli internal error, please contact: xixi.xxx'
            elif self.handler.isAvailableExtensionOperation(cmd, operation):
                if self.args.__len__() >= 3 and self.args[2] == 'help':
                    import commandConfigure
                    configure = commandConfigure.commandConfigure()
                    configure.showExtensionOperationHelp(cmd, operation)
                else:
                    self.extensionHandler.handlerExtensionOperation(cmd,operation,version)
                # self.extensionHandler.handlerExtensionOperation(cmd,operation,version)
            else:
                # cmd is right but operation is not right
                self.helper.showOperationError(cmd, operation)
        else:
            self.helper.showCmdError(cmd)
Example #16
0
 def printInFormat(self, errorCode, errorMsg):
     content = {'Code':errorCode, 'Message':errorMsg}
     response.display_response("error", content, "json")
     pass