1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-02 09:51:09 +03:00

Merge pull request #6408 from ryanpetrello/fix-6406

clarify valid CredentialType field types
This commit is contained in:
Ryan Petrello 2017-06-02 10:04:04 -04:00 committed by GitHub
commit 2cb296d28b
3 changed files with 34 additions and 19 deletions

View File

@ -529,7 +529,7 @@ class CredentialTypeInputField(JSONSchemaField):
'items': { 'items': {
'type': 'object', 'type': 'object',
'properties': { 'properties': {
'type': {'enum': ['string', 'number']}, 'type': {'enum': ['string', 'boolean']},
'format': {'enum': ['ssh_private_key']}, 'format': {'enum': ['ssh_private_key']},
'choices': { 'choices': {
'type': 'array', 'type': 'array',
@ -578,6 +578,18 @@ class CredentialTypeInputField(JSONSchemaField):
) )
ids[id_] = True ids[id_] = True
if 'type' not in field:
# If no type is specified, default to string
field['type'] = 'string'
for key in ('choices', 'multiline', 'format'):
if key in field and field['type'] != 'string':
raise django_exceptions.ValidationError(
_('%s not allowed for %s type (%s)' % (key, field['type'], field['id'])),
code='invalid',
params={'value': value},
)
class CredentialTypeInjectorField(JSONSchemaField): class CredentialTypeInjectorField(JSONSchemaField):

View File

@ -60,15 +60,18 @@ def test_cloud_kind_uniqueness():
({'fields': {}}, False), ({'fields': {}}, False),
({'fields': 123}, False), ({'fields': 123}, False),
({'fields': [{'id': 'username', 'label': 'Username', 'foo': 'bar'}]}, False), ({'fields': [{'id': 'username', 'label': 'Username', 'foo': 'bar'}]}, False),
({'fields': [{'id': 'username', 'label': 'Username'}]}, True),
({'fields': [{'id': 'username', 'label': 'Username', 'type': 'string'}]}, True), ({'fields': [{'id': 'username', 'label': 'Username', 'type': 'string'}]}, True),
({'fields': [{'id': 'username', 'label': 'Username', 'help_text': 1}]}, False), ({'fields': [{'id': 'username', 'label': 'Username', 'help_text': 1}]}, False),
({'fields': [{'id': 'username', 'label': 'Username', 'help_text': 'Help Text'}]}, True), # noqa ({'fields': [{'id': 'username', 'label': 'Username', 'help_text': 'Help Text'}]}, True), # noqa
({'fields': [{'id': 'username', 'label': 'Username'}, {'id': 'username', 'label': 'Username 2'}]}, False), # noqa ({'fields': [{'id': 'username', 'label': 'Username'}, {'id': 'username', 'label': 'Username 2'}]}, False), # noqa
({'fields': [{'id': '$invalid$', 'label': 'Invalid'}]}, False), # noqa ({'fields': [{'id': '$invalid$', 'label': 'Invalid', 'type': 'string'}]}, False), # noqa
({'fields': [{'id': 'password', 'label': 'Password', 'type': 'number'}]}, True), ({'fields': [{'id': 'password', 'label': 'Password', 'type': 'invalid-type'}]}, False),
({'fields': [{'id': 'ssh_key', 'label': 'SSH Key', 'type': 'string', 'format': 'ssh_private_key'}]}, True), # noqa ({'fields': [{'id': 'ssh_key', 'label': 'SSH Key', 'type': 'string', 'format': 'ssh_private_key'}]}, True), # noqa
({'fields': [{'id': 'other', 'label': 'Other', 'type': 'boolean'}]}, False), ({'fields': [{'id': 'flag', 'label': 'Some Flag', 'type': 'boolean'}]}, True),
({'fields': [{'id': 'flag', 'label': 'Some Flag', 'type': 'boolean', 'choices': ['a', 'b']}]}, False),
({'fields': [{'id': 'certificate', 'label': 'Cert', 'multiline': True}]}, True), ({'fields': [{'id': 'certificate', 'label': 'Cert', 'multiline': True}]}, True),
({'fields': [{'id': 'certificate', 'label': 'Cert', 'multiline': True, 'type': 'boolean'}]}, False), # noqa
({'fields': [{'id': 'certificate', 'label': 'Cert', 'multiline': 'bad'}]}, False), # noqa ({'fields': [{'id': 'certificate', 'label': 'Cert', 'multiline': 'bad'}]}, False), # noqa
({'fields': [{'id': 'token', 'label': 'Token', 'secret': True}]}, True), ({'fields': [{'id': 'token', 'label': 'Token', 'secret': True}]}, True),
({'fields': [{'id': 'token', 'label': 'Token', 'secret': 'bad'}]}, False), ({'fields': [{'id': 'token', 'label': 'Token', 'secret': 'bad'}]}, False),

View File

@ -118,40 +118,40 @@ ordered fields for that type:
"inputs": { "inputs": {
"fields": [{ "fields": [{
"id": "api_token", # required - a unique name used to "id": "api_token", # required - a unique name used to
# reference the field value # reference the field value
"label": "API Token", # required - a unique label for the "label": "API Token", # required - a unique label for the
# field # field
"help_text": "User-facing short text describing the field.", "help_text": "User-facing short text describing the field.",
"type": ("string" | "number") # required, "type": ("string" | "boolean") # defaults to 'string'
"format": "ssh_private_key" # optional, can be used to enforce data "format": "ssh_private_key" # optional, can be used to enforce data
# format validity for SSH private keys # format validity for SSH private key
# data # data (only applicable to `type=string`)
"secret": true, # if true, the field will be treated "secret": true, # if true, the field value will be encrypted
# as sensitive and stored encrypted
"multiline": false # if true, the field should be rendered "multiline": false # if true, the field should be rendered
# as multi-line for input entry # as multi-line for input entry
# (only applicable to `type=string`)
},{ },{
# field 2... # field 2...
},{ },{
# field 3... # field 3...
}] }]
"required": ["api_token"] # optional; one or more fields can be marked as required "required": ["api_token"] # optional; one or more fields can be marked as required
}, },
As an alternative to static types, fields can also specify multiple choice When `type=string`, fields can optionally specify multiple choice options:
strings:
"inputs": { "inputs": {
"fields": [{ "fields": [{
"id": "api_token", # required - a unique name used to reference the field value "id": "api_token", # required - a unique name used to reference the field value
"label": "API Token", # required - a unique label for the field "label": "API Token", # required - a unique label for the field
"type": "string",
"choices": ["A", "B", "C"] "choices": ["A", "B", "C"]
}] }]
}, },