def refresh(self, args, props): namespace, _ = parseQName(args.name, props) url = '%(apibase)s/namespaces/%(namespace)s/packages/refresh' % { 'apibase': apiBase(props), 'namespace': urllib.quote(namespace) } res = request('POST', url, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: result = json.loads(res.read()) print '%(namespace)s refreshed successfully!' % {'namespace': args.name} print hilite('created bindings:', True) print '\n'.join(result['added']) print hilite('updated bindings:', True) print '\n'.join(result['updated']) print hilite('deleted bindings:', True) print '\n'.join(result['deleted']) return 0 elif res.status == httplib.NOT_IMPLEMENTED: print 'error: This feature is not implemented in the targeted deployment' return responseError(res) else: result = json.loads(res.read()) print 'error: %(error)s' % {'error': result['error']} return responseError(res)
def refresh(self, args, props): namespace, _ = parseQName(args.name, props) url = 'https://%(apibase)s/namespaces/%(namespace)s/packages/refresh' % { 'apibase': apiBase(props), 'namespace': urllib.quote(namespace) } res = request('POST', url, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: result = json.loads(res.read()) print '%(namespace)s refreshed successfully!' % {'namespace': args.name} print hilite('created bindings:', True) print '\n'.join(result['added']) print hilite('updated bindings:', True) print '\n'.join(result['updated']) print hilite('deleted bindings:', True) print '\n'.join(result['deleted']) return 0 elif res.status == httplib.NOT_IMPLEMENTED: print 'error: This feature is not implemented in the targeted deployment' return responseError(res) else: result = json.loads(res.read()) print 'error: %(error)s' % {'error': result['error']} return responseError(res)
def fire(self, args, props): namespace, pname = parseQName(args.name, props) url = '%(apibase)s/namespaces/%(namespace)s/triggers/%(name)s' % { 'apibase': apiBase(props), 'namespace': urllib.quote(namespace), 'name': self.getSafeName(pname) } payload = json.dumps(getActivationArgument(args)) headers = {'Content-Type': 'application/json'} res = request('POST', url, payload, headers, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: result = json.loads(res.read()) print('ok: triggered %(name)s with id %(id)s' % { 'name': args.name, 'id': result['activationId'] }) return 0 else: return responseError(res)
def get(self, args, props): res = self.httpGet(args, props) if res.status == httplib.OK: result = self.postProcessGet(json.loads(res.read())) if args.summary: summary = self.getEntitySummary(result) print summary elif args.project: if args.project in result: print 'ok: got %(item)s %(name)s, projecting %(p)s' % { 'item': self.name, 'name': args.name, 'p': args.project } print getPrettyJson(result[args.project]) return 0 else: print 'ok: got %(item)s %(name)s, but it does not contain property %(p)s' % { 'item': self.name, 'name': args.name, 'p': args.project } return 148 else: print 'ok: got %(item)s %(name)s' % { 'item': self.name, 'name': args.name } print getPrettyJson(result) return 0 else: return responseError(res)
def deleteFeed(self, args, props, res): trigger = json.loads(res.read()) hasFeed = [a['value'] for a in trigger['annotations'] if a['key'] == 'feed'] feedName = hasFeed[0] if hasFeed else None if feedName: ns = props['namespace'] triggerName = getQName(args.name, ns) parameters = [] parameters.append([ 'lifecycleEvent', 'DELETE' ]) parameters.append([ 'triggerName', triggerName ]) parameters.append([ 'authKey', args.auth ]) feedArgs = { 'verbose': args.verbose, 'name' : feedName, 'param': parameters, 'blocking': True, 'auth': args.auth } feedResponse = Action().doInvoke(dict2obj(feedArgs), props) if feedResponse.status == httplib.OK: return self.deleteResponse(args, res) else: print 'error: failed to delete %s feed %s but did delete the trigger' % (self.name, args.name) return responseError(feedResponse, None)
def bind(self, args, props): namespace, pname = parseQName(args.name, props) url = '%(apibase)s/namespaces/%(namespace)s/packages/%(name)s' % { 'apibase': apiBase(props), 'namespace': urllib.quote(namespace), 'name': self.getSafeName(pname) } pkgNamespace, pkgName = parseQName(args.package, props) if pkgName is None or len(pkgName) <= 0: print 'package name malformed. name or /namespace/name allowed' sys.exit(1) binding = { 'namespace': pkgNamespace, 'name': pkgName } payload = { 'binding': binding, 'annotations': getAnnotations(args), 'parameters': getParams(args) } args.shared = False self.addPublish(payload, args) headers= { 'Content-Type': 'application/json' } res = request('PUT', url, json.dumps(payload), headers, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: print 'ok: created binding %(name)s ' % {'name': args.name } return 0 else: return responseError(res)
def createFeed(self, args, props, putResponse): ns = props['namespace'] triggerName = getQName(args.name, ns) parameters = args.param if parameters is None: parameters = [] parameters.append([ 'lifecycleEvent', 'CREATE' ]) parameters.append([ 'triggerName', triggerName ]) parameters.append([ 'authKey', args.auth ]) feedArgs = { 'verbose': args.verbose, 'name' : args.feed, 'param': parameters, 'blocking': True, 'auth': args.auth } feedResponse = Action().doInvoke(dict2obj(feedArgs), props) if feedResponse.status == httplib.OK: print 'ok: created %s feed %s' % (self.name, args.name) else: print 'error: failed to create %s feed %s' % (self.name, args.name) # clean up by deleting trigger self.httpDelete(args, props) return responseError(feedResponse, None)
def list(self, args, props): namespace, pname = parseQName(args.name, props) if pname: pname = ('/%s' % pname) if pname.endswith('/') else '/%s/' % pname url = 'https://%(apibase)s/namespaces/%(namespace)s/%(collection)s%(package)s?skip=%(skip)s&limit=%(limit)s%(public)s' % { 'apibase': apiBase(props), 'namespace': urllib.quote(namespace), 'collection': self.collection, 'package': pname if pname else '', 'skip': args.skip, 'limit': args.limit, 'public': '&public=true' if 'shared' in args and args.shared else '' } res = request('GET', url, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: result = json.loads(res.read()) print bold(self.collection) for e in result: print self.formatListEntity(e) return 0 else: return responseError(res)
def bind(self, args, props): namespace, pname = parseQName(args.name, props) url = 'https://%(apibase)s/namespaces/%(namespace)s/packages/%(name)s' % { 'apibase': apiBase(props), 'namespace': urllib.quote(namespace), 'name': self.getSafeName(pname) } pkgNamespace, pkgName = parseQName(args.package, props) if pkgName is None or len(pkgName) <= 0: print 'package name malformed. name or /namespace/name allowed' sys.exit(1) binding = { 'namespace': pkgNamespace, 'name': pkgName } payload = { 'binding': binding, 'annotations': getAnnotations(args), 'parameters': getParams(args) } args.shared = False self.addPublish(payload, args) headers= { 'Content-Type': 'application/json' } res = request('PUT', url, json.dumps(payload), headers, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: print 'ok: created binding %(name)s ' % {'name': args.name } return 0 else: return responseError(res)
def setState(self, args, props, enable): namespace, pname = parseQName(args.name, props) desc = 'active' if enable else 'inactive' status = json.dumps({'status': desc}) url = '%(apibase)s/namespaces/%(namespace)s/rules/%(name)s' % { 'apibase': apiBase(props), 'namespace': urllib.quote(namespace), 'name': self.getSafeName(pname) } headers = {'Content-Type': 'application/json'} res = request('POST', url, status, headers, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: print 'ok: rule %(name)s is %(desc)s' % { 'desc': desc, 'name': args.name } return 0 elif res.status == httplib.ACCEPTED: desc = 'activating' if enable else 'deactivating' print 'ok: rule %(name)s is %(desc)s' % { 'desc': desc, 'name': args.name } return 0 else: return responseError(res)
def fetchMostRecent(self, args, props): a = copy.deepcopy(args) a.action = None a.name = args.name a.full = True a.skip = 0 a.limit = 1 a.upto = 0 a.since = 0 pr = copy.deepcopy(props) res = self.listCmd(a, pr) if res.status == httplib.OK: result = json.loads(res.read()) return None if len(result) == 0 else result[0] else: responseError(res) return None
def invoke(self, args, props): res = self.doInvoke(args, props) try: result = json.loads(res.read()) if 'activationId' in result: # if args.result is true, there is no activation id print 'ok: invoked %(name)s with id %(id)s' % {'name': args.name, 'id': result['activationId'] } if res.status == httplib.OK: # true iff args.blocking is true print getPrettyJson(result) # prints the activation or just the result if args.result return 0 elif res.status == httplib.ACCEPTED: return 0 if not args.blocking else res.status elif res.status == httplib.BAD_GATEWAY: return responseError(res, prefix = '', flatten = False) elif res.status == httplib.INTERNAL_SERVER_ERROR and 'code' not in result: return responseError(res, prefix = '', flatten = False) else: return responseError(res) except: return responseError(res)
def fetchActivations(self, beginMillis, args, props): # fetch all activations starting from SLACK_SECONDS seconds in the past if beginMillis > SLACK_SECONDS * 1000: beginMillis = beginMillis - (SLACK_SECONDS * 1000); a = copy.deepcopy(args) a.name = args.name a.full = True a.skip = 0 a.limit = 0 a.upto = 0 a.since = beginMillis pr = copy.deepcopy(props) res = self.listCmd(a, pr) if res.status == httplib.OK: result = json.loads(res.read()) return result else: responseError(res) return None
def putResponse(self, res, update): if res.status == httplib.OK: result = json.loads(res.read()) print 'ok: %(mode)s %(item)s %(name)s' % { 'mode': 'updated' if update else 'created', 'item': self.name, 'name': result['name'] } return 0 else: return responseError(res)
def fetchActivations(self, beginMillis, args, props): # fetch all activations starting from SLACK_SECONDS seconds in the past if beginMillis > SLACK_SECONDS * 1000: beginMillis = beginMillis - (SLACK_SECONDS * 1000) a = copy.deepcopy(args) a.name = args.name a.full = True a.skip = 0 a.limit = 0 a.upto = 0 a.since = beginMillis pr = copy.deepcopy(props) res = self.listCmd(a, pr) if res.status == httplib.OK: result = json.loads(res.read()) return result else: responseError(res) return None
def listNamespaces(self, args, props): url = 'https://%(apibase)s/namespaces' % { 'apibase': apiBase(props) } res = request('GET', url, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: result = json.loads(res.read()) print bold('namespaces') for n in result: print '{:<25}'.format(n) return 0 else: return responseError(res)
def listNamespaces(self, args, props): url = 'https://%(apibase)s/namespaces' % {'apibase': apiBase(props)} res = request('GET', url, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: result = json.loads(res.read()) print bold("namespaces") for n in result: print "{:<25}".format(n) return 0 else: return responseError(res)
def list(self, args, props): name = args.name if args.name else '/_' args.name = getQName(name, '_') # kludge: use default namespace unless explicitly specified res = self.listCmd(args, props) if res.status == httplib.OK: result = json.loads(res.read()) print bold('activations') for a in result: if args.full: print getPrettyJson(a) else: print '{:<45}{:<40}'.format(a['activationId'], a['name']) return 0 else: return responseError(res)
def listEntitiesInNamespace(self, args, props): namespace, _ = parseQName(args.name, props) url = 'https://%(apibase)s/namespaces/%(namespace)s' % { 'apibase': apiBase(props), 'namespace': urllib.quote(namespace) } res = request('GET', url, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: result = json.loads(res.read()) print 'entities in namespace: %s' % bold(namespace if namespace != '_' else 'default') self.printCollection(result, 'packages') self.printCollection(result, 'actions') self.printCollection(result, 'triggers') self.printCollection(result, 'rules') return 0 else: return responseError(res)
def invoke(self, args, props): res = self.doInvoke(args, props) try: result = json.loads(res.read()) if 'activationId' in result: # if args.result is true, there is no activation id print 'ok: invoked %(name)s with id %(id)s' % { 'name': args.name, 'id': result['activationId'] } if res.status == httplib.OK: # true iff args.blocking is true print getPrettyJson( result ) # prints the activation or just the result if args.result return 0 elif res.status == httplib.ACCEPTED: return 0 if not args.blocking else res.status elif res.status == httplib.BAD_GATEWAY: return responseError(res, prefix='', flatten=False) elif res.status == httplib.INTERNAL_SERVER_ERROR and 'code' not in result: return responseError(res, prefix='', flatten=False) else: return responseError(res) except: return responseError(res)
def getState(self, args, props): namespace, pname = parseQName(args.name, props) url = '%(apibase)s/namespaces/%(namespace)s/rules/%(name)s' % { 'apibase': apiBase(props), 'namespace': urllib.quote(namespace), 'name': self.getSafeName(pname) } res = request('GET', url, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: result = json.loads(res.read()) print 'ok: rule %(name)s is %(status)s' % { 'name': args.name, 'status': result['status'] } return 0 else: return responseError(res)
def invoke(self, args, props): res = self.doInvoke(args, props) # OK implies successful blocking invoke # ACCEPTED implies non-blocking # All else are failures if res.status == httplib.OK or res.status == httplib.ACCEPTED: result = json.loads(res.read()) if not (args.result and args.blocking and res.status == httplib.OK): print 'ok: invoked %(name)s with id %(id)s' % {'name': args.name, 'id': result['activationId'] } if res.status == httplib.OK and args.result: print getPrettyJson(result['response']['result']) elif res.status == httplib.OK : print bold('response:') print getPrettyJson(result['response']) return 0 else: return responseError(res)
def listEntitiesInNamespace(self, args, props): namespace, _ = parseQName(args.name, props) url = 'https://%(apibase)s/namespaces/%(namespace)s' % { 'apibase': apiBase(props), 'namespace': urllib.quote(namespace) } res = request('GET', url, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: result = json.loads(res.read()) print 'entities in namespace: %s' % bold( namespace if namespace != '_' else 'default') self.printCollection(result, 'packages') self.printCollection(result, 'actions') self.printCollection(result, 'triggers') self.printCollection(result, 'rules') return 0 else: return responseError(res)
def result(self, args, props): fqid = getQName(args.id, '_') # kludge: use default namespace unless explicitly specified namespace, aid = parseQName(fqid, props) url = '%(apibase)s/namespaces/%(namespace)s/activations/%(id)s/result' % { 'apibase': apiBase(props), 'namespace': urllib.quote(namespace), 'id': aid } res = request('GET', url, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: response = json.loads(res.read()) if 'result' in response: result = response['result'] print getPrettyJson(result) return 0 else: return responseError(res)
def fire(self, args, props): namespace, pname = parseQName(args.name, props) url = 'https://%(apibase)s/namespaces/%(namespace)s/triggers/%(name)s' % { 'apibase': apiBase(props), 'namespace': urllib.quote(namespace), 'name': self.getSafeName(pname) } payload = json.dumps(getActivationArgument(args)) headers= { 'Content-Type': 'application/json' } res = request('POST', url, payload, headers, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: result = json.loads(res.read()) print 'ok: triggered %(name)s with id %(id)s' % {'name': args.name, 'id': result['activationId'] } return 0 else: return responseError(res)
def logs(self, args, props): fqid = getQName(args.id, '_') # kludge: use default namespace unless explicitly specified namespace, aid = parseQName(fqid, props) url = '%(apibase)s/namespaces/%(namespace)s/activations/%(id)s/logs' % { 'apibase': apiBase(props), 'namespace': urllib.quote(namespace), 'id': aid } res = request('GET', url, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: result = json.loads(res.read()) logs = result['logs'] if args.strip: logs = map(stripTimeStampAndString, logs) print '\n'.join(logs) return 0 else: return responseError(res)
def get(self, args, props): res = self.httpGet(args, props) if res.status == httplib.OK: result = self.postProcessGet(json.loads(res.read())) if args.summary: summary = self.getEntitySummary(result) print summary elif args.project: if args.project in result: print 'ok: got %(item)s %(name)s, projecting %(p)s' % {'item': self.name, 'name': args.name, 'p': args.project } print getPrettyJson(result[args.project]) return 0 else: print 'ok: got %(item)s %(name)s, but it does not contain property %(p)s' % {'item': self.name, 'name': args.name, 'p': args.project } return 148 else: print 'ok: got %(item)s %(name)s' % {'item': self.name, 'name': args.name } print getPrettyJson(result) return 0 else: return responseError(res)
def invoke(self, args, props): res = self.doInvoke(args, props) # OK implies successful blocking invoke # ACCEPTED implies non-blocking # All else are failures if res.status == httplib.OK or res.status == httplib.ACCEPTED: result = json.loads(res.read()) if not (args.result and args.blocking and res.status == httplib.OK): print 'ok: invoked %(name)s with id %(id)s' % { 'name': args.name, 'id': result['activationId'] } if res.status == httplib.OK and args.result: print getPrettyJson(result['response']['result']) elif res.status == httplib.OK: print bold('response:') print getPrettyJson(result['response']) return 0 else: return responseError(res)
def setState(self, args, props, enable): namespace, pname = parseQName(args.name, props) desc = 'active' if enable else 'inactive' status = json.dumps({ 'status': desc }) url = '%(apibase)s/namespaces/%(namespace)s/rules/%(name)s' % { 'apibase': apiBase(props), 'namespace': urllib.quote(namespace), 'name': self.getSafeName(pname) } headers = { 'Content-Type': 'application/json' } res = request('POST', url, status, headers, auth=args.auth, verbose=args.verbose) if res.status == httplib.OK: print 'ok: rule %(name)s is %(desc)s' % {'desc': desc, 'name': args.name} return 0 elif res.status == httplib.ACCEPTED: desc = 'activating' if enable else 'deactivating' print 'ok: rule %(name)s is %(desc)s' % {'desc': desc, 'name': args.name} return 0 else: return responseError(res)
def delete(self, args, props): res = self.httpDelete(args, props) if res.status == httplib.OK: return self.deleteFeed(args, props, res) else: return responseError(res)
def deleteResponse(self, args, res): if res.status == httplib.OK: print 'ok: deleted %(name)s' % {'name': args.name } return 0 else: return responseError(res)
def deleteResponse(self, args, res): if res.status == httplib.OK: print 'ok: deleted %(name)s' % {'name': args.name} return 0 else: return responseError(res)