-
Notifications
You must be signed in to change notification settings - Fork 2
Description
I created an API function for creating 'components' of the Configuration Database (Sites, Enclosures, Telescopes, ..., Generic Modes) in the following way:
List of all possible ConfigDB components
LIST_OF_CONFIGDB_COMPONENTS = {
'Sites': 'sites',
'Enclosures': 'enclosures',
'Telescopes': 'telescopes',
'Instruments': 'instruments',
'Cameras': 'cameras',
'Camera Types': 'cameratypes',
'Instrument Types': 'instrumenttypes',
'Optical Element Groups': 'opticalelementgroups',
'Optical Elements': 'opticalelements',
'Generic Mode Groups': 'genericmodegroups',
'Generic Modes': 'genericmodes'
}
Set the ConfigDB component
COMPONENT = LIST_OF_CONFIGDB_COMPONENTS['Optical Element Groups']
Base URL of the ConfigDB for the OCS Example Project
BASE_URL = 'http://127.0.0.1:7000'
Authorization tokens for 'test_user'
AUTHORIZATION_TOKEN = 'sutoken1234abcd'
Function
def create_component(BASE_URL, COMPONENT, AUTHORIZATION_TOKEN, component_new):
"""
Create a new component with the provided data.
Args:
BASE_URL: Base URL of your ConfigDB.
COMPONENT: Possible ConfigDB component.
AUTHORIZATION_TOKEN: Authorization token (super-user or local configDB token).
component_new (dict): The JSON payload containing the new component data.
Returns:
dict: Newly created component details or error message.
"""
endpoint = f'{BASE_URL}/{COMPONENT}/'
headers = {"Authorization": f"Token {AUTHORIZATION_TOKEN}"}
response = requests.post(endpoint, data=component_new, headers=headers)
print(f"Status Code --> [{response.status_code} : {response.reason}]")
if response.status_code == 201:
json_response = response.json()
print (f"New {COMPONENT[:-1]} with 'id = {json_response['id']}' was successfully created.")
return json_response
else:
print (f"Failed to create new {COMPONENT[:-1]}.")
return {}
#
When I ran this script for all 'components', everything worked well except for the 'Optical Element Groups' and 'Optical Elements'.
Issue 1
The issue/bug I encountered for the 'Optical Element Groups' is that I cannot add a new optical element group.
For example, if I try to add the following optical element group:
new_optical_element_group = {
'name': 'Test Filter 3',
'type': 'new filters',
'optical_elements': [
{
'name': 'x',
'code': 'x',
'schedulable': True
},
{
'name': 'y',
'code': 'y',
'schedulable': True
},
{
'name': 'z',
'code': 'z',
'schedulable': True
}
],
'element_change_overhead': 0
}
where 'Optical Elements' x, y, and z were already added in the DB, I am always getting the following error:
created_component = create_component(BASE_URL, COMPONENT, AUTHORIZATION_TOKEN, new_optical_element_group)
Status Code --> [400 : Bad Request]
Response: {"optical_elements":["This field is required."]}
When I use 'json=new_comp' instead of 'data=new_comp' in the 'requests.post' function, then I get the following error:
Status Code --> [400 : Bad Request]
Response: {"optical_elements":[{"code":["optical element with this code already exists."]},{"code":["optical element with this code already exists."]},{"code":["optical element with this code already exists."]}]}
I can add a new 'Optical Element Group' manually in the database using http://127.0.0.1:7000/admin/hardware/opticalelementgroup/add/
So basically I am not able to either add a new 'Optical Element Group' or update the existing one because of this.
Issue 2
Regarding the issue with the 'Optical Elements', I can add new 'Optical Elements' using a function from above (like 'x', 'y', 'z'), but the problem is when I add them, there are no IDs associated with them. When you look in the database (http://127.0.0.1:7000/admin/hardware/opticalelement/), really there is no ID field.
The problem is that without an ID field for the 'Optical Element,' you cannot retrieve, update or destroy 'Optical Element' through API functions.
Looking into the API documentation (https://observatorycontrolsystem.github.io/api/configdb/) it is expected to have an ID for 'Optical Element'.
I would appreciate very much if you could help me with this. I am trying to install and run OCS for the Observatory in Belgrade, Serbia.
Thank you,
Nikola