all-projects and public general options 49/8549/13
authorpinoa <alberto.pino@altran.com>
Mon, 3 Feb 2020 10:49:01 +0000 (11:49 +0100)
committerpinoa <alberto.pino@altran.com>
Tue, 18 Feb 2020 11:59:53 +0000 (12:59 +0100)
Change-Id: If67673d84a2efe7bb8896cdfd090aa7fe6a33a73
Signed-off-by: pinoa <alberto.pino@altran.com>
osmclient/scripts/osm.py
osmclient/sol005/client.py
osmclient/sol005/http.py
osmclient/sol005/project.py
osmclient/sol005/role.py
osmclient/sol005/user.py

index 5c6d3ba..b2caff0 100755 (executable)
@@ -97,6 +97,12 @@ def check_client_version(obj, what, version='sol005'):
                    'Also can set OSM_PROJECT in environment')
 @click.option('-v', '--verbose', count=True,
               help='increase verbosity (-v INFO, -vv VERBOSE, -vvv DEBUG)')
+@click.option('--all-projects',
+              default=False,
+              is_flag=True,
+              help='include all projects')
+@click.option('--public/--no-public', default=None,
+              help='flag for public items (packages, instances, VIM accounts, etc.)')
 #@click.option('--so-port',
 #              default=None,
 #              envvar='OSM_SO_PORT',
@@ -118,7 +124,7 @@ def check_client_version(obj, what, version='sol005'):
 #              help='hostname of RO server.  ' +
 #                   'Also can set OSM_RO_PORT in environment')
 @click.pass_context
-def cli_osm(ctx, hostname, user, password, project, verbose):
+def cli_osm(ctx, hostname, user, password, project, verbose, all_projects, public):
     global logger
     if hostname is None:
         print((
@@ -141,6 +147,10 @@ def cli_osm(ctx, hostname, user, password, project, verbose):
         kwargs['password']=password
     if project is not None:
         kwargs['project']=project
+    if all_projects:
+        kwargs['all_projects']=all_projects
+    if public is not None:
+        kwargs['public']=public
     ctx.obj = client.Client(host=hostname, sol005=sol005, **kwargs)
     logger = logging.getLogger('osmclient')
 
index 3ceb1b3..515de4e 100644 (file)
@@ -59,7 +59,6 @@ class Client(object):
         self._auth_endpoint = '/admin/v1/tokens'
         self._headers = {}
         self._token = None
-
         if len(host.split(':')) > 1:
             # backwards compatible, port provided as part of host
             self._host = host.split(':')[0]
@@ -69,7 +68,7 @@ class Client(object):
             self._so_port = so_port
 
         self._http_client = http.Http(
-            'https://{}:{}/osm'.format(self._host,self._so_port))
+            'https://{}:{}/osm'.format(self._host,self._so_port), **kwargs)
         self._headers['Accept'] = 'application/json'
         self._headers['Content-Type'] = 'application/yaml'
         http_header = ['{}: {}'.format(key, val)
@@ -105,7 +104,8 @@ class Client(object):
                                'password': self._password,
                                'project_id': self._project}
             http_code, resp = self._http_client.post_cmd(endpoint=self._auth_endpoint,
-                                                         postfields_dict=postfields_dict)
+                                                         postfields_dict=postfields_dict,
+                                                         skip_query_admin=True)
 #            if http_code not in (200, 201, 202, 204):
 #                message ='Authentication error: not possible to get auth token\nresp:\n{}'.format(resp)
 #                raise ClientException(message)
@@ -120,6 +120,15 @@ class Client(object):
                 self._http_client.set_http_header(http_header)
 
     def get_version(self):
-        _, resp = self._http_client.get2_cmd(endpoint="/version")
+        _, resp = self._http_client.get2_cmd(endpoint="/version", skip_query_admin=True)
         resp = json.loads(resp)
         return "{} {}".format(resp.get("version"), resp.get("date"))
+
+    def set_default_params(self, **kwargs):
+        host = kwargs.pop('host', None)
+        if host != None:
+            self._host=host
+        port  = kwargs.pop('port', None)
+        if port != None:
+            self._so_port=port
+        self._http_client.set_query_admin(**kwargs)
index 3d21465..d130879 100644 (file)
@@ -25,18 +25,44 @@ from osmclient.common.exceptions import OsmHttpException, NotFound
 
 class Http(http.Http):
 
-    def __init__(self, url, user='admin', password='admin'):
+    def __init__(self, url, user='admin', password='admin', **kwargs):
         self._url = url
         self._user = user
         self._password = password
         self._http_header = None
         self._logger = logging.getLogger('osmclient')
+        self._default_query_admin = None
+        self._all_projects = None;
+        self._public = None;
+        if 'all_projects' in kwargs:
+            self._all_projects=kwargs['all_projects']
+        if 'public' in kwargs:
+            self._public=kwargs['public']
+        self._default_query_admin = self._complete_default_query_admin()
 
-    def _get_curl_cmd(self, endpoint):
+    def _complete_default_query_admin(self):
+        query_string_list=[]
+        if self._all_projects:
+            query_string_list.append("ADMIN")
+        if self._public is not None:
+            query_string_list.append("PUBLIC={}".format(self._public))
+        return "&".join(query_string_list)
+
+    def _complete_endpoint(self, endpoint):
+        if self._default_query_admin:
+            if '?' in endpoint:
+                endpoint = '&'.join([endpoint, self._default_query_admin])
+            else:
+                endpoint = '?'.join([endpoint, self._default_query_admin])
+        return endpoint
+
+    def _get_curl_cmd(self, endpoint, skip_query_admin=False):
         self._logger.debug("")
         curl_cmd = pycurl.Curl()
         if self._logger.getEffectiveLevel() == logging.DEBUG:
             curl_cmd.setopt(pycurl.VERBOSE, True)
+        if not skip_query_admin:
+            endpoint = self._complete_endpoint(endpoint)
         curl_cmd.setopt(pycurl.URL, self._url + endpoint)
         curl_cmd.setopt(pycurl.SSL_VERIFYPEER, 0)
         curl_cmd.setopt(pycurl.SSL_VERIFYHOST, 0)
@@ -44,10 +70,10 @@ class Http(http.Http):
             curl_cmd.setopt(pycurl.HTTPHEADER, self._http_header)
         return curl_cmd
 
-    def delete_cmd(self, endpoint):
+    def delete_cmd(self, endpoint, skip_query_admin=False):
         self._logger.debug("")
         data = BytesIO()
-        curl_cmd = self._get_curl_cmd(endpoint)
+        curl_cmd = self._get_curl_cmd(endpoint, skip_query_admin)
         curl_cmd.setopt(pycurl.CUSTOMREQUEST, "DELETE")
         curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
         self._logger.info("Request METHOD: {} URL: {}".format("DELETE",self._url + endpoint))
@@ -66,10 +92,11 @@ class Http(http.Http):
 
     def send_cmd(self, endpoint='', postfields_dict=None,
                  formfile=None, filename=None,
-                 put_method=False, patch_method=False):
+                 put_method=False, patch_method=False,
+                 skip_query_admin=False):
         self._logger.debug("")
         data = BytesIO()
-        curl_cmd = self._get_curl_cmd(endpoint)
+        curl_cmd = self._get_curl_cmd(endpoint, skip_query_admin)
         if put_method:
             curl_cmd.setopt(pycurl.CUSTOMREQUEST, "PUT")
         elif patch_method:
@@ -118,36 +145,39 @@ class Http(http.Http):
             return http_code, None
 
     def post_cmd(self, endpoint='', postfields_dict=None,
-                 formfile=None, filename=None):
+                 formfile=None, filename=None,
+                 skip_query_admin=False):
         self._logger.debug("")
         return self.send_cmd(endpoint=endpoint,
                              postfields_dict=postfields_dict,
-                             formfile=formfile,
-                             filename=filename,
-                             put_method=False, patch_method=False)
+                             formfile=formfile, filename=filename,
+                             put_method=False, patch_method=False,
+                             skip_query_admin=skip_query_admin)
 
     def put_cmd(self, endpoint='', postfields_dict=None,
-                formfile=None, filename=None):
+                formfile=None, filename=None,
+                skip_query_admin=False):
         self._logger.debug("")
         return self.send_cmd(endpoint=endpoint,
                              postfields_dict=postfields_dict,
-                             formfile=formfile,
-                             filename=filename,
-                             put_method=True, patch_method=False)
+                             formfile=formfile, filename=filename,
+                             put_method=True, patch_method=False,
+                             skip_query_admin=skip_query_admin)
 
     def patch_cmd(self, endpoint='', postfields_dict=None,
-                formfile=None, filename=None):
+                formfile=None, filename=None,
+                skip_query_admin=False):
         self._logger.debug("")
         return self.send_cmd(endpoint=endpoint,
                              postfields_dict=postfields_dict,
-                             formfile=formfile,
-                             filename=filename,
-                             put_method=False, patch_method=True)
+                             formfile=formfile, filename=filename,
+                             put_method=False, patch_method=True,
+                             skip_query_admin=skip_query_admin)
 
-    def get2_cmd(self, endpoint):
+    def get2_cmd(self, endpoint, skip_query_admin=False):
         self._logger.debug("")
         data = BytesIO()
-        curl_cmd = self._get_curl_cmd(endpoint)
+        curl_cmd = self._get_curl_cmd(endpoint, skip_query_admin)
         curl_cmd.setopt(pycurl.HTTPGET, 1)
         curl_cmd.setopt(pycurl.WRITEFUNCTION, data.write)
         self._logger.info("Request METHOD: {} URL: {}".format("GET",self._url + endpoint))
@@ -174,3 +204,10 @@ class Http(http.Http):
             if http_code == 404:
                 raise NotFound("Error {}{}".format(http_code, resp))
             raise OsmHttpException("Error {}{}".format(http_code, resp))
+
+    def set_query_admin(self, **kwargs):
+        if 'all_projects' in kwargs:
+            self._all_projects=kwargs['all_projects']
+        if 'public' in kwargs:
+            self._public=kwargs['public']
+        self._default_query_admin = self._complete_default_query_admin()
index dc2e9a5..ed781fa 100644 (file)
@@ -43,7 +43,8 @@ class Project(object):
         self._logger.debug("")
         self._client.get_token()
         http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
-                                              postfields_dict=project)
+                                              postfields_dict=project,
+                                              skip_query_admin=True)
         #print('HTTP CODE: {}'.format(http_code))
         #print('RESP: {}'.format(resp))
         #if http_code in (200, 201, 202, 204):
@@ -69,7 +70,8 @@ class Project(object):
         self._client.get_token()
         proj = self.get(project)
         http_code, resp = self._http.patch_cmd(endpoint='{}/{}'.format(self._apiBase, proj['_id']),
-                                             postfields_dict=project_changes)
+                                             postfields_dict=project_changes,
+                                             skip_query_admin=True)
         # print('HTTP CODE: {}'.format(http_code))
         # print('RESP: {}'.format(resp))
         if http_code in (200, 201, 202):
@@ -100,7 +102,8 @@ class Project(object):
         if force:
             querystring = '?FORCE=True'
         http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
-                                                project['_id'], querystring))
+                                                project['_id'], querystring),
+                                                skip_query_admin=True)
         #print('HTTP CODE: {}'.format(http_code))
         #print('RESP: {}'.format(resp))
         if http_code == 202:
@@ -126,7 +129,8 @@ class Project(object):
         filter_string = ''
         if filter:
             filter_string = '?{}'.format(filter)
-        _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string))
+        _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string),
+                                                    skip_query_admin=True)
         #print('RESP: {}'.format(resp))
         if resp:
             return json.loads(resp)
index c80e50e..2544a7b 100644 (file)
@@ -65,7 +65,8 @@ class Role(object):
             role["permissions"] = role_permissions
 
         http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
-                                              postfields_dict=role)
+                                              postfields_dict=role,
+                                              skip_query_admin=True)
         # print('HTTP CODE: {}'.format(http_code))
         # print('RESP: {}'.format(resp))
         #if http_code in (200, 201, 202, 204):
@@ -148,7 +149,8 @@ class Role(object):
             del new_role_obj["permissions"]
 
         http_code, resp = self._http.patch_cmd(endpoint='{}/{}'.format(self._apiBase, role_obj['_id']),
-                                             postfields_dict=new_role_obj)
+                                               postfields_dict=new_role_obj,
+                                               skip_query_admin=True)
         # print('HTTP CODE: {}'.format(http_code))
         # print('RESP: {}'.format(resp))
         if http_code in (200, 201, 202):
@@ -184,7 +186,8 @@ class Role(object):
         if force:
             querystring = '?FORCE=True'
         http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
-                                                                 role['_id'], querystring))
+                                                                 role['_id'], querystring),
+                                                                 skip_query_admin=True)
         # print('HTTP CODE: {}'.format(http_code))
         # print('RESP: {}'.format(resp))
         if http_code == 202:
@@ -214,7 +217,7 @@ class Role(object):
         filter_string = ''
         if filter:
             filter_string = '?{}'.format(filter)
-        _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase, filter_string))
+        _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase, filter_string),skip_query_admin=True)
         # print('RESP: {}'.format(resp))
         if resp:
             return json.loads(resp)
index fac26cd..190fd95 100644 (file)
@@ -57,15 +57,15 @@ class User(object):
                 for role in roles:
                     mapping = {"project": project, "role": role}
 
-                    if mapping not in project_role_mappings: 
+                    if mapping not in project_role_mappings:
                         project_role_mappings.append(mapping)
-            
             user["project_role_mappings"] = project_role_mappings
         else:
             del user["project_role_mappings"]
 
         http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
-                                       postfields_dict=user)
+                                              postfields_dict=user,
+                                              skip_query_admin=True)
         #print('HTTP CODE: {}'.format(http_code))
         #print('RESP: {}'.format(resp))
         #if http_code in (200, 201, 202, 204):
@@ -145,7 +145,7 @@ class User(object):
             raise ClientException("At least something should be changed.")
 
         http_code, resp = self._http.patch_cmd(endpoint='{}/{}'.format(self._apiBase, myuser['_id']),
-                                             postfields_dict=update_user)
+                                             postfields_dict=update_user, skip_query_admin=True)
         # print('HTTP CODE: {}'.format(http_code))
         # print('RESP: {}'.format(resp))
         if http_code in (200, 201, 202):
@@ -176,7 +176,7 @@ class User(object):
         if force:
             querystring = '?FORCE=True'
         http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
-                                         user['_id'], querystring))
+                                         user['_id'], querystring), skip_query_admin=True)
         #print('HTTP CODE: {}'.format(http_code))
         #print('RESP: {}'.format(resp))
         if http_code == 202:
@@ -202,7 +202,7 @@ class User(object):
         filter_string = ''
         if filter:
             filter_string = '?{}'.format(filter)
-        _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string))
+        _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string,skip_query_admin=True))
         #print('RESP: {}'.format(resp))
         if resp:
             return json.loads(resp)