Skip to content

Commit 3742ded

Browse files
authored
api: Add endpoint returning all available attributes (#452)
Add a dataset/attributes API endpoint that returns all available attributes including the special ones (e.g. hostname, servertype). Clients can use this, for example, to get the available attributes for auto completion or selection.
1 parent 2f5bede commit 3742ded

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

serveradmin/api/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
dataset_query,
1111
dataset_commit,
1212
dataset_new_object,
13+
dataset_attributes,
1314
api_call,
1415
)
1516

@@ -18,5 +19,6 @@
1819
path('dataset/query', dataset_query),
1920
path('dataset/commit', dataset_commit),
2021
path('dataset/new_object', dataset_new_object),
22+
path('dataset/attributes', dataset_attributes),
2123
path('call', api_call),
2224
]

serveradmin/api/views.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
PermissionDenied,
99
ValidationError,
1010
)
11+
from django.http import JsonResponse
1112
from django.template.response import HttpResponse
1213

1314
from adminapi.filters import BaseFilter, FilterValueError
1415
from serveradmin.api import ApiError, AVAILABLE_API_FUNCTIONS
1516
from serveradmin.api.decorators import api_view
17+
from serveradmin.serverdb.models import Attribute
1618
from serveradmin.serverdb.query_committer import commit_query
1719
from serveradmin.serverdb.query_executer import execute_query
1820
from serveradmin.serverdb.query_materializer import (
@@ -66,6 +68,48 @@ def dataset_query(request, app, data):
6668
}
6769

6870

71+
@api_view
72+
def dataset_attributes(request, app, data):
73+
"""Return all available attributes
74+
75+
This includes the special attributes (e.g. hostname, servertype) that
76+
are not stored in the attribute table but are queryable like any other
77+
attribute.
78+
"""
79+
attributes = list(Attribute.objects.all())
80+
attributes.extend(Attribute.specials.values())
81+
82+
result = []
83+
for attribute in attributes:
84+
result.append({
85+
'attribute_id': attribute.attribute_id,
86+
'type': attribute.type,
87+
'multi': attribute.multi,
88+
'hovertext': attribute.hovertext,
89+
'group': attribute.group,
90+
'help_link': attribute.help_link,
91+
'inet_address_family': attribute.inet_address_family,
92+
'readonly': attribute.readonly,
93+
'clone': attribute.clone,
94+
'history': attribute.history,
95+
'regexp': attribute.regexp,
96+
'reversed_attribute': attribute.reversed_attribute_id,
97+
# Special attributes are not saved to the database, so accessing
98+
# their many-to-many target_servertype is not possible.
99+
'target_servertypes': (
100+
[] if attribute.special else
101+
list(attribute.target_servertype.values_list(
102+
'servertype_id', flat=True
103+
))
104+
),
105+
})
106+
107+
return {
108+
'status': 'success',
109+
'result': result,
110+
}
111+
112+
69113
@api_view
70114
def dataset_new_object(request, app, data):
71115
try:

0 commit comments

Comments
 (0)