Skip to content

Commit

Permalink
adding correct extract of name for services (#494)
Browse files Browse the repository at this point in the history
adding correct extract of name for services

Reviewed-by: Anton Sidelnikov
  • Loading branch information
vladimirhasko authored Nov 25, 2024
1 parent 33f0185 commit bfd2621
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 82 deletions.
36 changes: 36 additions & 0 deletions otcextensions/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# import sys
import re
import threading
from urllib import parse

# import uuid
#
Expand Down Expand Up @@ -133,3 +134,38 @@ def extract_region_from_url(url):
if match:
return match.group()
return


def extract_url_parts(url: str, project_id: str) -> list:
"""
Extracts meaningful parts of a URL, excluding the project_id,
version identifiers, and empty segments.
Args:
url (str): The URL to process.
project_id (str): The project ID to exclude from the URL parts.
Returns:
list: A list of meaningful URL segments.
"""
# Parse and strip the URL path
path = parse.urlparse(url).path.strip()

# Remove leading '/' to keep list indexes consistent
if path.startswith('/'):
path = path[1:]

# Split the path into parts, excluding the project_id
url_parts = [x for x in path.split('/') if x != project_id]

# Exclude parts that are version identifiers
url_parts = list(
filter(
lambda x: not any(
c.isdigit() for c in x[1:]) and (x[0].lower() != 'v'),
url_parts
)
)

# Strip out empty or None segments and return
return [part for part in url_parts if part]
6 changes: 5 additions & 1 deletion otcextensions/sdk/anti_ddos/v1/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from openstack import proxy

from otcextensions.common.utils import extract_url_parts
from otcextensions.sdk.anti_ddos.v1 import alert_config as _alert
from otcextensions.sdk.anti_ddos.v1 import config as _config
from otcextensions.sdk.anti_ddos.v1 import floating_ip as _floating_ip
Expand All @@ -21,6 +22,9 @@ class Proxy(proxy.Proxy):

skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
return extract_url_parts(url, project_id)

# ======== Alert Config ========
def get_alert_config(self, **kwargs):
"""Get Alarm configuration
Expand Down
22 changes: 3 additions & 19 deletions otcextensions/sdk/bms/v1/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,14 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from urllib import parse

from openstack import proxy

from otcextensions.common.utils import extract_url_parts


class Proxy(proxy.Proxy):

skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
path = parse.urlparse(url).path.strip()
# Remove / from the beginning to keep the list indexes of interesting
# things consistent
if path.startswith('/'):
path = path[1:]

# Split url into parts and exclude potential project_id in some urls
url_parts = [
x for x in path.split('/') if x != project_id
]
# exclude version
url_parts = list(filter(lambda x: not any(
c.isdigit() for c in x[1:]) and (
x[0].lower() != 'v'), url_parts))

# Strip out anything that's empty or None
return [part for part in url_parts if part]
return extract_url_parts(url, project_id)
5 changes: 4 additions & 1 deletion otcextensions/sdk/dcaas/v2/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from openstack import proxy

from otcextensions.common.utils import extract_url_parts
from otcextensions.sdk.dcaas.v2 import virtual_gateway as _virtual_gateway
from otcextensions.sdk.dcaas.v2 import connection as _connection
from otcextensions.sdk.dcaas.v2 import virtual_interface as _virtual_interface
Expand All @@ -22,6 +22,9 @@ class Proxy(proxy.Proxy):

skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
return extract_url_parts(url, project_id)

# ======== Virtual gateways ========
def virtual_gateways(self, **query):
"""Retrieve a generator of virtual gateways
Expand Down
5 changes: 5 additions & 0 deletions otcextensions/sdk/ecs/v1/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
# under the License.
from openstack import proxy

from otcextensions.common.utils import extract_url_parts


class Proxy(proxy.Proxy):

skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
return extract_url_parts(url, project_id)
5 changes: 4 additions & 1 deletion otcextensions/sdk/elb/v2/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from openstack.load_balancer.v2 import _proxy

from otcextensions.common.utils import extract_url_parts
from otcextensions.sdk.elb.v2 import elb_certificate as _certificate
from otcextensions.sdk.elb.v2 import load_balancer as _load_balancer
from openstack.load_balancer.v2 import listener as _listener
Expand All @@ -22,6 +22,9 @@
class Proxy(_proxy.Proxy):
skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
return extract_url_parts(url, project_id)

# ======== Certificate ========
def create_certificate(self, **attrs):
"""Create a new certificate from attributes
Expand Down
22 changes: 3 additions & 19 deletions otcextensions/sdk/enterprise_dashboard/v1/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,14 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from urllib import parse

from openstack import proxy

from otcextensions.common.utils import extract_url_parts


class Proxy(proxy.Proxy):

skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
path = parse.urlparse(url).path.strip()
# Remove / from the beginning to keep the list indexes of interesting
# things consistent
if path.startswith('/'):
path = path[1:]

# Split url into parts and exclude potential project_id in some urls
url_parts = [
x for x in path.split('/') if x != project_id
]
# exclude version
url_parts = list(filter(lambda x: not any(
c.isdigit() for c in x[1:]) and (
x[0].lower() != 'v'), url_parts))

# Strip out anything that's empty or None
return [part for part in url_parts if part]
return extract_url_parts(url, project_id)
22 changes: 3 additions & 19 deletions otcextensions/sdk/enterprise_dashboard_v1/v1/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,14 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from urllib import parse

from openstack import proxy

from otcextensions.common.utils import extract_url_parts


class Proxy(proxy.Proxy):

skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
path = parse.urlparse(url).path.strip()
# Remove / from the beginning to keep the list indexes of interesting
# things consistent
if path.startswith('/'):
path = path[1:]

# Split url into parts and exclude potential project_id in some urls
url_parts = [
x for x in path.split('/') if x != project_id
]
# exclude version
url_parts = list(filter(lambda x: not any(
c.isdigit() for c in x[1:]) and (
x[0].lower() != 'v'), url_parts))

# Strip out anything that's empty or None
return [part for part in url_parts if part]
return extract_url_parts(url, project_id)
5 changes: 5 additions & 0 deletions otcextensions/sdk/er/v3/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
# under the License.
from openstack import proxy

from otcextensions.common.utils import extract_url_parts


class Proxy(proxy.Proxy):

skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
return extract_url_parts(url, project_id)
5 changes: 5 additions & 0 deletions otcextensions/sdk/function_graph/v2/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
# under the License.
from openstack import proxy

from otcextensions.common.utils import extract_url_parts


class Proxy(proxy.Proxy):

skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
return extract_url_parts(url, project_id)
5 changes: 5 additions & 0 deletions otcextensions/sdk/kms/v1/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.
from openstack import proxy

from otcextensions.common.utils import extract_url_parts
from otcextensions.sdk.kms.v1 import data_key as _data_key
from otcextensions.sdk.kms.v1 import key as _key
from otcextensions.sdk.kms.v1 import misc as _misc
Expand All @@ -19,6 +21,9 @@ class Proxy(proxy.Proxy):

skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
return extract_url_parts(url, project_id)

def __init__(self, session, *args, **kwargs):
super(Proxy, self).__init__(session=session, *args, **kwargs)
self.additional_headers = {
Expand Down
Empty file removed otcextensions/sdk/smn/__init_.py
Empty file.
5 changes: 4 additions & 1 deletion otcextensions/sdk/smn/v2/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from otcextensions.common.utils import extract_url_parts
from otcextensions.sdk.smn.v2 import topic as _topic
from otcextensions.sdk.smn.v2 import subscription as _subscription
from otcextensions.sdk.smn.v2 import template as _template
Expand All @@ -19,9 +20,11 @@


class Proxy(proxy.Proxy):

skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
return extract_url_parts(url, project_id)

# ======== Topic ========
def create_topic(self, **attrs):
"""Create a new topic from attributes
Expand Down
22 changes: 3 additions & 19 deletions otcextensions/sdk/swr/v2/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from urllib import parse

from openstack import proxy

from otcextensions.common.utils import extract_url_parts
from otcextensions.sdk.swr.v2 import _base
from otcextensions.sdk.swr.v2 import organization as _organization
from otcextensions.sdk.swr.v2 import repository as _repository
Expand All @@ -28,23 +28,7 @@ class Proxy(proxy.Proxy):
skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
path = parse.urlparse(url).path.strip()
# Remove / from the beginning to keep the list indexes of interesting
# things consistent
if path.startswith('/'):
path = path[1:]

# Split url into parts and exclude potential project_id in some urls
url_parts = [
x for x in path.split('/') if x != project_id
]
# exclude version
url_parts = list(filter(lambda x: not any(
c.isdigit() for c in x[1:]) and (
x[0].lower() != 'v'), url_parts))

# Strip out anything that's empty or None
return [part for part in url_parts if part]
return extract_url_parts(url, project_id)

def create_organization(self, **attrs):
"""Create a new organization from attributes
Expand Down
5 changes: 4 additions & 1 deletion otcextensions/sdk/vlb/v3/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from openstack import proxy
from openstack import resource

from otcextensions.common.utils import extract_url_parts
from otcextensions.sdk.vlb.v3 import availability_zone as _availability_zone
from otcextensions.sdk.vlb.v3 import certificate as _certificate
from otcextensions.sdk.vlb.v3 import flavor as _flavor
Expand All @@ -32,6 +32,9 @@
class Proxy(proxy.Proxy):
skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
return extract_url_parts(url, project_id)

# ======== Load balancer ========
def create_load_balancer(self, **attrs):
"""Create a new load balancer from attributes
Expand Down
4 changes: 4 additions & 0 deletions otcextensions/sdk/vpc/v1/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from openstack import exceptions
from openstack import proxy

from otcextensions.common.utils import extract_url_parts
from otcextensions.sdk.vpc.v1 import bandwidth as _bandwidth
from otcextensions.sdk.vpc.v1 import peering as _peering
from otcextensions.sdk.vpc.v1 import route as _route
Expand All @@ -31,6 +32,9 @@ class PublicInfo:
class Proxy(proxy.Proxy):
skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
return extract_url_parts(url, project_id)

# ======== Bandwidth ========
def assign_bandwidth(self, **attrs):
"""Assign bandwidth
Expand Down
5 changes: 4 additions & 1 deletion otcextensions/sdk/waf/v1/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
# under the License.
from openstack import proxy

from otcextensions.common.utils import extract_url_parts
from otcextensions.sdk.waf.v1 import certificate as _cert
from otcextensions.sdk.waf.v1 import domain as _domain


class Proxy(proxy.Proxy):

skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
return extract_url_parts(url, project_id)

def __init__(self, session, *args, **kwargs):
super(Proxy, self).__init__(session=session, *args, **kwargs)
self.endpoint_override = \
Expand Down
5 changes: 5 additions & 0 deletions otcextensions/sdk/wafd/v1/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
# under the License.
from openstack import proxy

from otcextensions.common.utils import extract_url_parts


class Proxy(proxy.Proxy):

skip_discovery = True

def _extract_name(self, url, service_type=None, project_id=None):
return extract_url_parts(url, project_id)

0 comments on commit bfd2621

Please sign in to comment.