-
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
29 changed files
with
1,027 additions
and
55 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
File renamed without changes.
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,93 @@ | ||
# 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. | ||
|
||
## 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 all the 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 fake Instance object that contain only **name** and **quota_scope** | ||
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 | ||
- Numbers of stars on Squest is under 250 | ||
|
||
```python | ||
import requests | ||
from service_catalog.forms import SurveyValidator | ||
|
||
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") | ||
|
||
url = 'https://api.github.com/repos/HewlettPackard/squest' | ||
headers = {'Accept': 'application/vnd.github.preview'} | ||
response = requests.get(url, headers=headers).json() | ||
squest_stars_on_github = response.get('watchers_count', 0) | ||
|
||
if squest_stars_on_github <= 250: | ||
self.fail("Number of stars on Squest is below 250, come back when it's 251") | ||
``` |
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
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
Oops, something went wrong.