leverage authenticate decorator to identity check 13/53213/3
authorSerenaFeng <feng.xiaowei@zte.com.cn>
Wed, 7 Mar 2018 07:55:00 +0000 (15:55 +0800)
committerSerenaFeng <feng.xiaowei@zte.com.cn>
Wed, 7 Mar 2018 07:56:36 +0000 (15:56 +0800)
Change-Id: If9248161ecb02153d0685c8f4325be96f2f1d5da
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
testapi/testapi-client/testapiclient/auth.py
testapi/testapi-client/testapiclient/command.py
testapi/testapi-client/testapiclient/identity.py
testapi/testapi-client/testapiclient/pods.py
testapi/testapi-client/testapiclient/projects.py

index 49da309..3728498 100644 (file)
@@ -1,28 +1,14 @@
-import logging
-from cliff.command import Command
+from testapiclient import command
 from testapiclient import identity
 
 
-class Auth(Command):
+class Auth(command.Command):
     "Handle Authentication for users"
 
-    log = logging.getLogger(__name__)
-
     def get_parser(self, prog_name):
         parser = super(Auth, self).get_parser(prog_name)
-        parser.add_argument('-u',
-                            type=str,
-                            required=True,
-                            help='Username for authentication')
-        parser.add_argument('-p',
-                            type=str,
-                            required=True,
-                            help='Password for authentication')
         return parser
 
+    @identity.authenticate
     def take_action(self, parsed_args):
-        response = identity.authenticate(parsed_args.u, parsed_args.p)
-        if "login" in response.text:
-            print "Authentication has failed."
-        else:
-            print "Authentication has been successful!"
+        print "Authentication has been successful!"
index 5c2ce5c..7dba312 100644 (file)
@@ -1,5 +1,4 @@
 from cliff import command
-# from testapiclient import identity
 
 
 class Command(command.Command):
index 5456270..d62ed7a 100644 (file)
@@ -2,9 +2,10 @@ import requests
 from user import User
 from config import Config
 import urllib
+import functools
 
 
-def authenticate(username, password):
+def _authenticate(username, password):
     session = requests.Session()
     hostname = '{}{}{}'.format(
         Config.config.get("cas", "auth_url"),
@@ -14,3 +15,17 @@ def authenticate(username, password):
     response = session.post(hostname, data)
     User.session = session
     return response
+
+
+def authenticate(xstep):
+    @functools.wraps(xstep)
+    def wrapper(self, parsed_args):
+        username = parsed_args.u
+        password = parsed_args.p
+        if(username and password):
+            response = _authenticate(username, password)
+            if "login" in response.text:
+                print "Authentication has failed."
+            else:
+                xstep(self, parsed_args)
+    return wrapper
index 81d4b9e..b73839e 100644 (file)
@@ -61,13 +61,9 @@ class PodCreate(command.Command):
                                  'mode should be either "metal" or "virtual.')
         return parser
 
+    @identity.authenticate
     def take_action(self, parsed_args):
         http_client = HTTPClient.get_Instance()
-        if(parsed_args.u and parsed_args.p):
-            response = identity.authenticate(parsed_args.u, parsed_args.p)
-            if "login" in response.text:
-                print "Authentication has failed."
-                return
         response = http_client.post(PODS_URL,
                                     User.session,
                                     parsed_args.pod)
@@ -88,13 +84,8 @@ class PodDelete(command.Command):
                             help='Delete pods using name')
         return parser
 
+    @identity.authenticate
     def take_action(self, parsed_args):
         http_client = HTTPClient.get_Instance()
-        if(parsed_args.u and parsed_args.p):
-            response = identity.authenticate(parsed_args.u, parsed_args.p)
-            if "login" in response.text:
-                print "Authentication has failed."
-                return
-        pods = http_client.delete(PODS_URL + "/" + parsed_args.name,
-                                  User.session)
-        print pods
+        print http_client.delete(PODS_URL + "/" + parsed_args.name,
+                                 User.session)
index 36d0960..2e61369 100644 (file)
@@ -1,10 +1,11 @@
 import json
-from user import User
+
 from testapiclient import command
-from httpClient import HTTPClient
 from testapiclient import identity
-from config import Config
 
+from config import Config
+from httpClient import HTTPClient
+from user import User
 
 PROJECTS_URL = Config.config.get("api", "url") + "/projects"
 
@@ -55,13 +56,9 @@ class ProjectCreate(command.Command):
                                  '"description": (optional)""}')
         return parser
 
+    @identity.authenticate
     def take_action(self, parsed_args):
         httpClient = HTTPClient.get_Instance()
-        if(parsed_args.u and parsed_args.p):
-            response = identity.authenticate(parsed_args.u, parsed_args.p)
-            if "login" in response.text:
-                print "Authentication has failed."
-                return
         response = httpClient.post(ProjectCreate.projects_url,
                                    User.session,
                                    parsed_args.project)
@@ -81,13 +78,9 @@ class ProjectDelete(command.Command):
                             help='Delete project by name')
         return parser
 
+    @identity.authenticate
     def take_action(self, parsed_args):
         httpClient = HTTPClient.get_Instance()
-        if(parsed_args.u and parsed_args.p):
-            response = identity.authenticate(parsed_args.u, parsed_args.p)
-            if "login" in response.text:
-                print "Authentication has failed."
-                return
         projects = httpClient.delete(
             PROJECTS_URL + "/" + parsed_args.name,
             User.session)
@@ -109,13 +102,9 @@ class ProjectPut(command.Command):
                                  '"description": (optional)""}')
         return parser
 
+    @identity.authenticate
     def take_action(self, parsed_args):
         httpClient = HTTPClient.get_Instance()
-        if(parsed_args.u and parsed_args.p):
-            response = identity.authenticate(parsed_args.u, parsed_args.p)
-            if "login" in response.text:
-                print "Authentication has failed."
-                return
         projects = httpClient.put(
             PROJECTS_URL + "/" + parsed_args.name,
             User.session,