Skip to content

Commit

Permalink
NatsOrganizationUser inherits from AbstractNatsApp (#14)
Browse files Browse the repository at this point in the history
* NatsOrganizationUser inherits from AbstractNatsApp

* delete unused code
  • Loading branch information
leigh-johnson authored Aug 14, 2022
1 parent 3d75fb2 commit 106f299
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ migrations:
docker-compose -f docker/local.yml run --rm django python manage.py makemigrations

migrate:
docker-compose -f docker/local.yml exec django python manage.py migrate
docker-compose -f docker/local.yml run --rm django python manage.py migrate

nsc-init:
docker-compose -f docker/local.yml exec django python manage.py nsc_init
Expand Down
43 changes: 43 additions & 0 deletions django_nats_nkeys/migrations/0005_auto_20220814_2329.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 3.2.7 on 2022-08-14 23:29

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('django_nats_nkeys', '0004_auto_20220813_2050'),
]

operations = [
migrations.AddField(
model_name='natsorganizationuser',
name='allow_pub',
field=models.CharField(help_text='add publish permissions, comma separated list. equivalent to `nsc add user ... --allow-pub=<permissions>`', max_length=255, null=True),
),
migrations.AddField(
model_name='natsorganizationuser',
name='allow_pubsub',
field=models.CharField(help_text='add publish/subscribe permissions, comma separated list. equivalent to `nsc add user ... --allow-pubsub=<permissions>`', max_length=255, null=True),
),
migrations.AddField(
model_name='natsorganizationuser',
name='allow_sub',
field=models.CharField(help_text='add subscribe permissions, comma separated list. equivalent to `nsc add user ... --allow-sub=<permissions>`', max_length=255, null=True),
),
migrations.AddField(
model_name='natsorganizationuser',
name='deny_pub',
field=models.CharField(help_text='deny publish permissions, comma separated list. equivalent to `nsc add user ... --deny-pub=<permissions>`', max_length=255, null=True),
),
migrations.AddField(
model_name='natsorganizationuser',
name='deny_pubsub',
field=models.CharField(help_text='deny publish/subscribe permissions, comma separated list. equivalent to `nsc add user ... --deny-pubsub=<permissions>`', max_length=255, null=True),
),
migrations.AddField(
model_name='natsorganizationuser',
name='deny_sub',
field=models.CharField(help_text='deny subscribe permissions, comma separated list. equivalent to `nsc add user ... --deny-sub=<permissions>`', max_length=255, null=True),
),
]
56 changes: 8 additions & 48 deletions django_nats_nkeys/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,54 +67,6 @@ class NatsOrganization(AbstractOrganization):
)


class NatsOrganizationUserManager(models.Manager):
def create_nsc(self, **kwargs):
from django_nats_nkeys.services import (
run_nsc_and_log_output,
save_describe_json,
)

org_user = self.create(**kwargs)
try:
# add organization user for account
run_nsc_and_log_output(
[
"nsc",
"add",
"user",
"--account",
org_user.organization.name,
"--name",
org_user.app_name,
"-K",
"service",
]
)
except subprocess.CalledProcessError as e:
# nsc add account command returned "Error: the account "<name>" already exists"
# we can proceed to saving output of `nsc describe account <name> --json``
if "already exists" in e.stderr:
pass
# re-raise other errors
raise e
save_describe_json(
org_user.organization.name, org_user, app_name=org_user.app_name
)
return org_user


class NatsOrganizationUser(AbstractOrganizationUser):
"""
Corresponds to a NATS user/client, intended for use for a human who owns one or more NatsApp instances and wants to publish/subscribe to all apps via signed credential.
"""

objects = NatsOrganizationUserManager()
app_name = models.CharField(max_length=255, default=_default_name)
json = models.JSONField(
max_length=255, help_text="Output of `nsc describe account`", default=dict
)


class AbstractNatsApp(models.Model):
"""
Corresponds to a NATS user/client within an Account group, intended for use by application
Expand Down Expand Up @@ -173,6 +125,14 @@ def create_nsc(self, **kwargs):
return nsc_add_app(obj.organization.name, obj.app_name, obj)


class NatsOrganizationUser(AbstractOrganizationUser, AbstractNatsApp):
"""
Corresponds to a NATS user/client, intended for use for a human who owns one or more NatsApp instances and wants to publish/subscribe to all apps via signed credential.
"""

objects = NatsOrganizationAppManager()


class NatsOrganizationApp(AbstractNatsApp):
"""
Corresponds to a NATS user/client within an Account group
Expand Down

0 comments on commit 106f299

Please sign in to comment.