R10 tag/path update
[dcaegen2/platform/plugins.git] / k8s / k8splugin / discovery.py
index f3b87b6..99f5023 100644 (file)
@@ -1,7 +1,8 @@
 # ============LICENSE_START=======================================================
 # org.onap.dcae
 # ================================================================================
-# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2019 Pantheon.tech. All rights reserved.
 # ================================================================================
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # limitations under the License.
 # ============LICENSE_END=========================================================
 #
-# ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
-from functools import partial
 import json
 import logging
-import uuid
-import requests
-import consul
 import re
+import uuid
+from functools import partial
 
+import consul
+import requests
 
 logger = logging.getLogger("discovery")
 
@@ -54,16 +54,16 @@ def _wrap_consul_call(consul_func, *args, **kwargs):
 def generate_service_component_name(service_component_type):
     """Generate service component id used to pass into the service component
     instance and used as the key to the service component configuration.
-    
+
     Updated for use with Kubernetes.  Sometimes the service component name gets
     used in Kubernetes in contexts (such as naming a Kubernetes Service) that
-    requires the name to conform to the RFC1035 DNS "label" syntax: 
+    requires the name to conform to the RFC1035 DNS "label" syntax:
        -- starts with an alpha
        -- contains only of alphanumerics and "-"
        -- <= 63 characters long
 
     Format:
-    s<service component id>-<service component type>, 
+    s<service component id>-<service component type>,
         truncated to 63 characters, "_" replaced with "-" in service_component_type,
         other non-conforming characters removed from service_component_type
     """
@@ -197,7 +197,7 @@ def is_healthy(consul_host, instance):
 
 def add_to_entry(conn, key, add_name, add_value):
     """
-    Find 'key' in consul.  
+    Find 'key' in consul.
     Treat its value as a JSON string representing a dict.
     Extend the dict by adding an entry with key 'add_name' and value 'add_value'.
     Turn the resulting extended dict into a JSON string.
@@ -233,12 +233,10 @@ def add_to_entry(conn, key, add_name, add_value):
 
 def _find_matching_services(services, name_search, tags):
     """Find matching services given search criteria"""
-    def is_match(service):
-        srv_name, srv_tags = service
-        return name_search in srv_name and \
-                all(map(lambda tag: tag in srv_tags, tags))
+    tags = set(tags)
+    return [srv_name for srv_name in services
+            if name_search in srv_name and tags <= set(services[srv_name])]
 
-    return [ srv[0] for srv in services.items() if is_match(srv) ]
 
 def search_services(conn, name_search, tags):
     """Search for services that match criteria