-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API and management commands for enabling/disabling flags
This change adds two flag state functions, `enable_flag` and `disable_flag`, that will either add or modify an existing boolean condition to either enable or disable a flag. It also adds an `enable_flag` and `disable_flag` management commands which call those functions. This will enable easier enabling/disabling of a flag through automations, etc. If a flag has required conditions, those will take precedence over the boolean that's added/modified.
- Loading branch information
1 parent
88692db
commit fd4ddf1
Showing
13 changed files
with
236 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Management Commands | ||
|
||
## `enable_flag FLAG_NAME` | ||
|
||
Enable a flag by adding or setting an existing database boolean condition to `True`. If the flag has other required conditions, those will take precedence. | ||
|
||
This command calls the [`flags.state.enable_flag` function](../api/state#enable_flagflag_name-create_boolean_conditiontrue-requestnone) function. | ||
|
||
## `disable_flag FLAG_NAME` | ||
|
||
Disable a flag by adding or setting an existing database boolean condition to `False`. If the flag has other required conditions, those will take precedence. | ||
|
||
This command calls the [`flags.state.enable_flag` function](../api/state#disable_flagflag_name-create_boolean_conditiontrue-requestnone) function. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from django.core.management.base import BaseCommand, CommandError | ||
|
||
from flags.state import disable_flag | ||
|
||
|
||
class Command(BaseCommand): | ||
help = ( | ||
"Disables the given feature flag " | ||
"unless any required conditions (if defined) are met" | ||
) | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"flag_name", help="The name of the feature flag to disable" | ||
) | ||
|
||
def handle(self, *args, **options): | ||
try: | ||
disable_flag(options["flag_name"]) | ||
except KeyError as e: | ||
raise CommandError(e) | ||
|
||
self.stdout.write( | ||
self.style.SUCCESS(f"Successfully disabled {options['flag_name']}") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from django.core.management.base import BaseCommand, CommandError | ||
|
||
from flags.state import enable_flag | ||
|
||
|
||
class Command(BaseCommand): | ||
help = ( | ||
"Enables the given feature flag " | ||
"when any required conditions (if defined) are met" | ||
) | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"flag_name", help="The name of the feature flag to enable" | ||
) | ||
|
||
def handle(self, *args, **options): | ||
try: | ||
enable_flag(options["flag_name"]) | ||
except KeyError as e: | ||
raise CommandError(e) | ||
|
||
self.stdout.write( | ||
self.style.SUCCESS(f"Successfully enabled {options['flag_name']}") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from io import StringIO | ||
|
||
from django.core.management import call_command | ||
from django.core.management.base import CommandError | ||
from django.test import TestCase | ||
|
||
from flags.models import FlagState | ||
from flags.state import flag_disabled | ||
|
||
|
||
class disableFlagTestCase(TestCase): | ||
def test_disable_flag(self): | ||
FlagState.objects.create( | ||
name="DB_FLAG", condition="boolean", value="True" | ||
) | ||
out = StringIO() | ||
self.assertFalse(flag_disabled("DB_FLAG")) | ||
call_command("disable_flag", "DB_FLAG", stdout=out) | ||
self.assertTrue(flag_disabled("DB_FLAG")) | ||
self.assertIn("Successfully disabled", out.getvalue()) | ||
|
||
def test_disable_flag_non_existent_flag(self): | ||
with self.assertRaises(CommandError): | ||
call_command("disable_flag", "FLAG_DOES_NOT_EXIST") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from io import StringIO | ||
|
||
from django.core.management import call_command | ||
from django.core.management.base import CommandError | ||
from django.test import TestCase | ||
|
||
from flags.state import flag_enabled | ||
|
||
|
||
class EnableFlagTestCase(TestCase): | ||
def test_enable_flag(self): | ||
out = StringIO() | ||
self.assertFalse(flag_enabled("DB_FLAG")) | ||
call_command("enable_flag", "DB_FLAG", stdout=out) | ||
self.assertTrue(flag_enabled("DB_FLAG")) | ||
self.assertIn("Successfully enabled", out.getvalue()) | ||
|
||
def test_enable_flag_non_existent_flag(self): | ||
with self.assertRaises(CommandError): | ||
call_command("enable_flag", "FLAG_DOES_NOT_EXIST") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters