Skip to content

Commit

Permalink
[IMPROVEMENT] Add promgen register-job to register job from command…
Browse files Browse the repository at this point in the history
… line #192
  • Loading branch information
kfdm authored Nov 8, 2019
2 parents 042d9af + ce2c80f commit 7c6a4d3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
34 changes: 34 additions & 0 deletions promgen/management/commands/register-job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) 2019 LINE Corporation
# These sources are released under the terms of the MIT license: see LICENSE

from django.core.management.base import BaseCommand, CommandError

from promgen import models, util


class Command(BaseCommand):
def add_arguments(self, parser):
help_text = util.HelpFor(models.Exporter)

parser.add_argument("project", help="Existing Project")
parser.add_argument("job", help=help_text.job)
parser.add_argument("port", type=int, help=help_text.port)
parser.add_argument("path", default="", nargs="?", help=help_text.path)
parser.add_argument("--enabled", default=False, action="store_true", help=help_text.enabled)

def handle(self, **kwargs):
try:
project = models.Project.objects.get(name=kwargs["project"])
except models.Project.DoesNotExist:
raise CommandError("Unable to find Project :%s" % kwargs["project"])

job, created = models.Exporter.objects.get_or_create(
project=project,
job=kwargs["job"],
port=kwargs["port"],
defaults={"path": kwargs["path"], "enabled": kwargs["enabled"]},
)
if created:
print("Registered job", job)
else:
print("Found existing job", job)
8 changes: 4 additions & 4 deletions promgen/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@ def __str__(self):


class Exporter(models.Model):
job = models.CharField(max_length=128)
port = models.IntegerField()
path = models.CharField(max_length=128, blank=True)
project = models.ForeignKey('Project', on_delete=models.CASCADE)
job = models.CharField(max_length=128, help_text="Exporter name. Example node, jmx, app")
port = models.IntegerField(help_text="Port Exporter is running on")
path = models.CharField(max_length=128, blank=True, help_text="Exporter path. Defaults to /metrics")
project = models.ForeignKey("Project", on_delete=models.CASCADE)
enabled = models.BooleanField(default=True)

class Meta:
Expand Down
32 changes: 32 additions & 0 deletions promgen/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) 2019 LINE Corporation
# These sources are released under the terms of the MIT license: see LICENSE

from promgen import models
from promgen.tests import PromgenTest

from django.core import management
from django.core.management.base import CommandError


class CLITests(PromgenTest):
def test_register_job(self):
# Assert when project doesn't exist
with self.assertRaises(CommandError):
management.call_command("register-job", "missing-project", "example", 1234)

# Create a Service and Project and then try adding our job
shard = models.Shard.objects.create(name="TestShard")
service = models.Service.objects.create(name="TestService")
_ = models.Project.objects.create(name="TestProject", service=service, shard=shard)
management.call_command("register-job", "TestProject", "example", 1234)

# Ensure the jobs we expect exist
self.assertCount(models.Exporter, 1)

# Registering the same job again shouldn't change our count
management.call_command("register-job", "TestProject", "example", 1234)
self.assertCount(models.Exporter, 1)

# But registering a new one will
management.call_command("register-job", "TestProject", "example", 4321)
self.assertCount(models.Exporter, 2)
12 changes: 12 additions & 0 deletions promgen/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,15 @@ def setting(key, default=None, domain=None):
except KeyError:
return default
return rtn


class HelpFor:
# Wrap a model's lower level api so that we can easily
# grab help_text for a specific field
# help_text = HelpFor(DjangoModel)
# help_test.field_name
def __init__(self, model):
self.model = model

def __getattr__(self, name):
return self.model._meta.get_field(name).help_text

0 comments on commit 7c6a4d3

Please sign in to comment.