Skip to content

Commit

Permalink
NAS-133661 / 25.04 / simplify country_choices endpoints (#15433)
Browse files Browse the repository at this point in the history
* simplify country_choices endpoints

* more generic function name and add docstring
  • Loading branch information
yocalebo authored Jan 20, 2025
1 parent c3a4269 commit 0bbc5bb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 53 deletions.
9 changes: 4 additions & 5 deletions src/middlewared/middlewared/plugins/crypto_/cert_info.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from middlewared.schema import accepts, Dict, Ref, returns, Str
from middlewared.service import private, Service
from middlewared.utils.country_codes import get_country_codes

from .utils import EC_CURVES, EKU_OIDS

Expand All @@ -18,11 +19,9 @@ async def get_domain_names(self, cert_id):

@accepts()
@returns(Ref('country_choices'))
async def country_choices(self):
"""
Returns country choices for creating a certificate/csr.
"""
return await self.middleware.call('system.general.country_choices')
def country_choices(self):
"""Returns country choices for creating a certificate/csr."""
return get_country_codes()

@accepts()
@returns(Dict('acme_server_choices', additional_attrs=True))
Expand Down
55 changes: 7 additions & 48 deletions src/middlewared/middlewared/plugins/system_general/country.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,17 @@
import csv

from middlewared.schema import accepts, Dict, returns
from middlewared.service import private, Service

from middlewared.service import Service
from middlewared.utils.country_codes import get_country_codes

class SystemGeneralService(Service):

COUNTRY_CHOICES = None

class Config:
namespace = 'system.general'
cli_namespace = 'system.general'

@accepts()
@returns(Dict('country_choices', additional_attrs=True, register=True))
async def country_choices(self):
"""
Returns country choices.
"""
if not self.COUNTRY_CHOICES:
self.COUNTRY_CHOICES = await self.middleware.call('system.general.get_country_choices')

return self.COUNTRY_CHOICES

@private
def get_country_choices(self):
def _get_index(country_columns, column):
index = -1
i = 0
for c in country_columns:
if c.lower() == column.lower():
index = i
break

i += 1
return index

country_file = '/etc/iso_3166_2_countries.csv'
cni, two_li = None, None
country_choices = {}
with open(country_file, 'r', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)

for index, row in enumerate(reader):
if index != 0:
if row[cni] and row[two_li]:
if row[two_li] in country_choices:
# If two countries in the iso file have the same key, we concatenate their names
country_choices[row[two_li]] += f' + {row[cni]}'
else:
country_choices[row[two_li]] = row[cni]
else:
# ONLY CNI AND TWO_LI ARE BEING CONSIDERED FROM THE CSV
cni = _get_index(row, 'Common Name')
two_li = _get_index(row, 'ISO 3166-1 2 Letter Code')

return country_choices
def country_choices(self):
"""Return a dictionary whose keys represent the
ISO 3166-1 alpha 2 country code and values represent
the English short name (used in ISO 3166/MA)"""
return get_country_codes()
13 changes: 13 additions & 0 deletions src/middlewared/middlewared/utils/country_codes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from functools import cache
from json import load

__all__ = ("get_country_codes",)


@cache
def get_country_codes() -> dict[str, str]:
"""Return the ISO 3166-1 alpha 2 code as the key and the
English short name (used in ISO 3166/MA) of the country
as the value (i.e {"US": "United States of America", ...})"""
with open("/usr/share/iso-codes/json/iso_3166-1.json") as f:
return {i["alpha_2"]: i["name"] for i in load(f)["3166-1"]}

0 comments on commit 0bbc5bb

Please sign in to comment.