-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
1,078 additions
and
60 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
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
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
4 changes: 4 additions & 0 deletions
4
docs/manual/advanced/validators.md → docs/manual/advanced/field_validators.md
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,89 @@ | ||
# Survey validators | ||
|
||
Survey validators are Python modules that can be added as plugin. It allows users to implement their own validation | ||
logic on a day1 or day2 operation against the full survey. | ||
|
||
## Creating survey validator | ||
|
||
Create a Python file in **SURVEY_VALIDATOR_PATH** (default is `plugins/survey_validators`). | ||
Create Python class that inherit from SurveyValidator with a method `validate_survey`. | ||
|
||
```python | ||
# plugins/survey_validators/MySurveyValidator.py | ||
from service_catalog.forms import SurveyValidator | ||
|
||
class MyCustomValidatorFoo(SurveyValidator): | ||
def validate_survey(self): | ||
# Implement your own logic here | ||
pass | ||
``` | ||
|
||
## SurveyValidator attributes | ||
|
||
### survey | ||
|
||
This is a dict containing survey + request_comment. Keys are variable name. | ||
type: dict | ||
|
||
```bash | ||
>>> print(self.survey) | ||
{ | ||
"request_comment": "commentary sent by user" | ||
"ram_gb": 8, | ||
"vcpu": 4 | ||
} | ||
``` | ||
|
||
### user | ||
|
||
User requesting operation. | ||
type: django.contrib.auth.models.User | ||
|
||
### operation | ||
|
||
Operation requested. | ||
type: service_catalog.models.Operation | ||
|
||
### instance | ||
|
||
Instance targeted. | ||
type: service_catalog.models.Instance | ||
|
||
!!!note | ||
For day 1 operation `self.instance` is a FakeInstance object that contains only **name** and **quota_scope** without `save` method. | ||
The real Instance object is created after validation. | ||
|
||
## SurveyValidator method | ||
|
||
### validate_survey(self) | ||
|
||
Redefine it to implement your own logic. | ||
|
||
### fail(self, message, field="\_\_all\_\_") | ||
|
||
Raise an exception and display message on UI/API. | ||
|
||
## Set validator to a form field | ||
|
||
In Squest, edit an Operation to set validators. Multiples validators can be added, validators are executed in alphabetical order by script name and class name. | ||
|
||
## Example | ||
|
||
This validator will always fail if: | ||
|
||
- ram and cpu are both equal 1 | ||
- It's not the weekend yet | ||
|
||
```python | ||
from service_catalog.forms import SurveyValidator | ||
import datetime | ||
|
||
class ValidatorForVM(SurveyValidator): | ||
def validate_survey(self): | ||
if self.survey.get("ram") == 1 and self.survey.get("vcpu") == 1: | ||
self.fail("Forbidden: you cannot use ram=1 and cpu=1") | ||
|
||
weekday = datetime.datetime.today().weekday() | ||
if weekday < 5: | ||
self.fail("Sorry it's not the weekend yet") | ||
``` |
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
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
Oops, something went wrong.