Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

25167 add feature flags service enable won emails #1659

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,6 @@ MRAS_SVC_API_KEY=

# Local development only
DISABLE_NAMEREQUEST_SOLR_UPDATES=1

# launchdarkly
NAMEX_LD_SDK_ID=sdk-075200eb-4ff7-4e0e-872a-848585d3d460
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kzdev420
I bet this key has to be imported in api/config.py.

8 changes: 8 additions & 0 deletions api/flags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"flagValues": {
"string-flag": "a string value",
"bool-flag": true,
"integer-flag": 10,
"enable-won-emails": false
severinbeauvais marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion api/namex/VERSION.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.2.21'
__version__ = '1.2.22'
2 changes: 2 additions & 0 deletions api/namex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from namex.models import db, ma
from namex.resources import api
from namex.utils.run_version import get_run_version
from namex.services import flags

# noqa: I003; dont know what flake8 wants here

Expand All @@ -52,6 +53,7 @@ def create_app(run_mode=os.getenv('FLASK_ENV', 'production')):
integrations=[FlaskIntegration()]
)

flags.init_app(app)
queue.init_app(app)

db.init_app(app)
Expand Down
8 changes: 1 addition & 7 deletions api/namex/resources/name_requests/report_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,6 @@ def _get_template_data(nr_model):
nr_report_json['applicants']['countryName'] = \
pycountry.countries.search_fuzzy(nr_report_json['applicants']['countryTypeCd'])[0].name
actions_obj = ReportResource._get_next_action_text(nr_model['entity_type_cd'])
structured_log(request, "DEBUG", f"NR_notification - NameX API: {actions_obj}")
current_app.logger.debug(f"NR_notification - NameX API: {actions_obj}")
structured_log(request, "DEBUG", f"NR_notification - NameX API: {nr_report_json['request_action_cd']}")
current_app.logger.debug(f"NR_notification - NameX API: {nr_report_json['request_action_cd']}")
structured_log(request, "DEBUG", f"NR_notification - NameX API: {nr_model['entity_type_cd']}")
current_app.logger.debug(f"NR_notification - NameX API: {nr_model['entity_type_cd']}")
if actions_obj:
action_text = actions_obj.get(nr_report_json['request_action_cd'])
if not action_text:
Expand Down Expand Up @@ -298,7 +292,7 @@ def _is_potential_colin(legal_type):

@staticmethod
def _get_instruction_group(legal_type, request_action, corpNum):
if request_action == RequestAction.CHG.value or RequestAction.CNV.value:
if request_action in {RequestAction.CHG.value, RequestAction.CNV.value}:
# For the 'Name Change' or 'Alteration', return 'modernized' if the company is in LEAR, and 'colin' if not
return 'modernized' if ReportResource._is_lear_entity(corpNum) else 'colin'
if ReportResource._is_modernized(legal_type):
Expand Down
3 changes: 3 additions & 0 deletions api/namex/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
from .messages import MessageServices
from .audit_trail import EventRecorder
from .name_request.name_request_state import is_reapplication_eligible
from .flags import Flags

flags = Flags()
145 changes: 145 additions & 0 deletions api/namex/services/flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Copyright © 2025 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Manage the Feature Flags initialization, setup and service."""
from flask import current_app
from ldclient import get as ldclient_get, set_config as ldclient_set_config # noqa: I001
from ldclient.config import Config # noqa: I005
from ldclient.impl.integrations.files.file_data_source import _FileDataSource
from ldclient.interfaces import UpdateProcessor

from namex.models import User


class FileDataSource(UpdateProcessor):
"""FileDataStore has been removed, so this provides similar functionality."""

@classmethod
def factory(cls, **kwargs):
"""Provide a way to use local files as a source of feature flag state.

.. deprecated:: 6.8.0
This module and this implementation class are deprecated and may be changed or removed in the future.
Please use :func:`ldclient.integrations.Files.new_data_source()`.

The keyword arguments are the same as the arguments to :func:`ldclient.integrations.Files.new_data_source()`.
"""
return lambda config, store, ready: _FileDataSource(store, ready,
paths=kwargs.get('paths'),
auto_update=kwargs.get('auto_update', False),
poll_interval=kwargs.get('poll_interval', 1),
force_polling=kwargs.get('force_polling', False))


class Flags():
"""Wrapper around the feature flag system.

calls FAIL to FALSE

If the feature flag service is unavailable
AND
there is no local config file
Calls -> False

"""

def __init__(self, app=None):
"""Initialize this object."""
self.sdk_key = None
self.app = None

if app:
self.init_app(app)

def init_app(self, app):
"""Initialize the Feature Flag environment."""
self.app = app
self.sdk_key = app.config.get('NAMEX_LD_SDK_ID')

if self.sdk_key or app.env != 'production':

if app.env == 'production':
severinbeauvais marked this conversation as resolved.
Show resolved Hide resolved
config = Config(sdk_key=self.sdk_key)
else:
factory = FileDataSource.factory(paths=['flags.json'],
auto_update=True)
config = Config(sdk_key=self.sdk_key,
update_processor_class=factory,
send_events=False)

ldclient_set_config(config)
client = ldclient_get()

app.extensions['featureflags'] = client

def teardown(self, exception): # pylint: disable=unused-argument; flask method signature
"""Destroy all objects created by this extension."""
client = current_app.extensions['featureflags']
client.close()

def _get_client(self):
try:
client = current_app.extensions['featureflags']
except KeyError:
try:
self.init_app(current_app)
client = current_app.extensions['featureflags']
except KeyError:
client = None

return client

@staticmethod
def _get_anonymous_user():
return {
'key': 'anonymous'
}

@staticmethod
def _user_as_key(user: User):
user_json = {
'key': user.sub,
'firstName': user.firstname,
'lastName': user.lastname
}
return user_json

def is_on(self, flag: str, user: User = None) -> bool:
"""Assert that the flag is set for this user."""
client = self._get_client()

if user:
flag_user = self._user_as_key(user)
else:
flag_user = self._get_anonymous_user()

try:
return bool(client.variation(flag, flag_user, None))
except Exception as err:
current_app.logger.error('Unable to read flags: %s' % repr(err), exc_info=True)
return False

def value(self, flag: str, user: User = None) -> bool:
"""Retrieve the value of the (flag, user) tuple."""
client = self._get_client()

if user:
flag_user = self._user_as_key(user)
else:
flag_user = self._get_anonymous_user()

try:
return client.variation(flag, flag_user, None)
except Exception as err:
current_app.logger.error('Unable to read flags: %s' % repr(err), exc_info=True)
return False
183 changes: 183 additions & 0 deletions api/tests/python/services/test_flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# Copyright © 2024 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests to assure the Flag Services.

Test-Suite to ensure that the Flag Service is working as expected.
"""
import pytest
from flask import Flask

from namex.models import User
from namex.services.flags import Flags


def test_flags_init():
"""Ensure that extension can be initialized."""
app = Flask(__name__)
app.env = 'development'

with app.app_context():
flags = Flags(app)

assert flags
assert app.extensions['featureflags']


def test_flags_init_app():
"""Ensure that extension can be initialized."""
app = Flask(__name__)
app.env = 'development'
app.config['NAMEX_LD_SDK_ID'] = 'https://no.flag/avail'

with app.app_context():
flags = Flags()
flags.init_app(app)
assert app.extensions['featureflags']


def test_flags_init_app_production():
"""Ensure that extension can be initialized."""
app = Flask(__name__)
app.env = 'production'
app.config['NAMEX_LD_SDK_ID'] = 'https://no.flag/avail'

with app.app_context():
flags = Flags()
flags.init_app(app)
assert app.extensions['featureflags']


def test_flags_init_app_no_key_dev():
"""Assert that the extension is setup with a KEY, but in non-production mode."""
app = Flask(__name__)
app.config['NAMEX_LD_SDK_ID'] = None
app.env = 'development'

with app.app_context():
flags = Flags()
flags.init_app(app)
assert app.extensions['featureflags']


def test_flags_init_app_no_key_prod():
"""Assert that prod with no key initializes, but does not setup the extension."""
app = Flask(__name__)
app.config['NAMEX_LD_SDK_ID'] = None
app.env = 'production'

with app.app_context():
flags = Flags()
flags.init_app(app)
with pytest.raises(KeyError):
client = app.extensions['featureflags']
assert not client


def test_flags_bool_no_key_prod():
"""Assert that prod with no key initializes, but does not setup the extension."""
app = Flask(__name__)
app.config['NAMEX_LD_SDK_ID'] = None
app.env = 'production'

with app.app_context():
flags = Flags()
flags.init_app(app)
on = flags.is_on('bool-flag')

assert not on


def test_flags_bool():
"""Assert that a boolean (True) is returned, when using the local Flag.json file."""
app = Flask(__name__)
app.env = 'development'
app.config['NAMEX_LD_SDK_ID'] = 'https://no.flag/avail'

with app.app_context():
flags = Flags()
flags.init_app(app)
flag_on = flags.is_on('bool-flag')

assert flag_on


def test_flags_bool_missing_flag(app):
"""Assert that a boolean (False) is returned when flag doesn't exist, when using the local Flag.json file."""
from namex import flags
app_env = app.env
try:
with app.app_context():
flag_on = flags.is_on('no flag here')

assert not flag_on
except: # pylint: disable=bare-except; # noqa: B901, E722
# for tests we don't care
assert False
finally:
app.env = app_env


def test_flags_bool_using_current_app():
"""Assert that a boolean (True) is returned, when using the local Flag.json file."""
from namex import flags
app = Flask(__name__)
app.env = 'development'

with app.app_context():
flag_on = flags.is_on('bool-flag')

assert flag_on


@pytest.mark.parametrize('test_name,flag_name,expected', [
('boolean flag', 'bool-flag', True),
('string flag', 'string-flag', 'a string value'),
('integer flag', 'integer-flag', 10),
])
def test_flags_bool_value(test_name, flag_name, expected):
"""Assert that a boolean (True) is returned, when using the local Flag.json file."""
from namex import flags
app = Flask(__name__)
app.env = 'development'

with app.app_context():
val = flags.value(flag_name)

assert val == expected


def test_flag_bool_unique_user():
"""Assert that a unique user can retrieve a flag, when using the local Flag.json file."""
app = Flask(__name__)
app.env = 'development'
app.config['NAMEX_LD_SDK_ID'] = 'https://no.flag/avail'

user = User(username='username', firstname='firstname', lastname='lastname', sub='sub', iss='iss', idp_userid='123', login_source='IDIR')

app_env = app.env
try:
with app.app_context():
flags = Flags()
flags.init_app(app)
app.env = 'development'
val = flags.value('bool-flag', user)
flag_on = flags.is_on('bool-flag', user)

assert val
assert flag_on
except: # pylint: disable=bare-except; # noqa: B901, E722
# for tests we don't care
assert False
finally:
app.env = app_env
Loading
Loading