myscripts.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from dcim.models import Site
  2. from extras.scripts import Script, BooleanVar, IntegerVar, ObjectVar, StringVar
  3. class NoInputScript(Script):
  4. description = "This script does not require any input"
  5. def run(self, data):
  6. self.log_debug("This a debug message.")
  7. self.log_info("This an info message.")
  8. self.log_success("This a success message.")
  9. self.log_warning("This a warning message.")
  10. self.log_failure("This a failure message.")
  11. class DemoScript(Script):
  12. name = "Script Demo"
  13. description = "A quick demonstration of the available field types"
  14. my_string1 = StringVar(
  15. description="Input a string between 3 and 10 characters",
  16. min_length=3,
  17. max_length=10
  18. )
  19. my_string2 = StringVar(
  20. description="This field enforces a regex: three letters followed by three numbers",
  21. regex=r'[a-z]{3}\d{3}'
  22. )
  23. my_number = IntegerVar(
  24. description="Pick a number between 1 and 255 (inclusive)",
  25. min_value=1,
  26. max_value=255
  27. )
  28. my_boolean = BooleanVar(
  29. description="Use the checkbox to toggle true/false"
  30. )
  31. my_object = ObjectVar(
  32. description="Select a NetBox site",
  33. queryset=Site.objects.all()
  34. )
  35. def run(self, data):
  36. self.log_info("Your string was {}".format(data['my_string1']))
  37. self.log_info("Your second string was {}".format(data['my_string2']))
  38. self.log_info("Your number was {}".format(data['my_number']))
  39. if data['my_boolean']:
  40. self.log_info("You ticked the checkbox")
  41. else:
  42. self.log_info("You did not tick the checkbox")
  43. self.log_info("You chose the sites {}".format(data['my_object']))
  44. return "Here's some output"