1# Redfish Coding Guidelines 2 3- For robot programs wishing to run Redfish commands, include the following in 4 your robot file: 5 6 ``` 7 *** Settings *** 8 9 Resource bmc_redfish_resource.robot 10 ``` 11 12- This git repository has some redfish wrapper modules: 13 14 - [redfish_plus.py](../lib/redfish_plus.py) 15 - [bmc_redfish.py](../lib/bmc_redfish.py) 16 - [bmc_redfish_utils.py](../lib/bmc_redfish_utils.py) 17 - Redfish wrapper module features: 18 19 For all Redfish REST requests (get, head, post, put, patch, delete): 20 21 - Support for python-like strings for all arguments which allows callers to 22 easily specify complex arguments such as lists or dictionaries. 23 24 So instead of coding this: 25 26 ``` 27 ${ldap_type_dict}= Create Dictionary ServiceEnabled=${False} 28 ${body}= Create Dictionary ${LDAP_TYPE}=${ldap_type_dict} 29 Redfish.Patch ${REDFISH_BASE_URI}AccountService body=${body} 30 ``` 31 32 You can do it in one fell swoop like this: 33 34 ``` 35 Redfish.Patch ${REDFISH_BASE_URI}AccountService body={'${LDAP_TYPE}': {'ServiceEnabled': ${False}}} 36 ``` 37 38 - Support for **valid_status_codes** argument and auto-failure: 39 40 As mentioned above, this argument may be either an actual robot/python 41 list or it may be a string value which python can translate into a list. 42 43 The default value is [${HTTP_OK}]. 44 45 This means that the Redfish REST request will fail **automatically** if 46 the resulting status code is not found in the valid_status_codes list. 47 48 So instead of having to do this: 49 50 ``` 51 ${resp}= Redfish.Get ${EVENT_LOG_URI}Entries 52 Should Be Equal As Strings ${resp.status_code} ${HTTP_OK} 53 ``` 54 55 You can simply do this: 56 57 ``` 58 ${resp}= Redfish.Get ${EVENT_LOG_URI}Entries 59 ``` 60 61 If, for some reason, you **expect** your command to fail, you can specify 62 the expected status code or codes: 63 64 ``` 65 Redfish.Patch ${REDFISH_BASE_URI}UpdateService body={'ApplyTime' : 'Invalid'} valid_status_codes=[${HTTP_BAD_REQUEST}] 66 ``` 67 68 - Login defaults for path, username and password are https://${OPENBMC_HOST}, 69 ${OPENBMC_USERNAME}, ${OPENBMC_PASSWORD}. 70 - Many utility functions are available. Examples:; 71 72 - get_properties 73 - get_attributes 74 - get_session_info 75 - list_request 76 - enumerate_request 77 78# Rules for use of Redfish.Login and Redfish.Logout 79 80It is desirable to avoid excessive redfish logins/logouts for the following 81reasons: 82 83- It simplifies the code base. 84- It allows calling keywords and testcases to keep control over login parameters 85 like USERNAME, PASSWORD, etc. Consider the following example: 86 87 ``` 88 # Login to redfish with non-standard username/password. 89 Redfish.Login ${LDAP_USER} ${LDAP_USER_PASSWORD} 90 # Run 'Some Keyword' while logged in as ${LDAP_USER}/${LDAP_USER_PASSWORD}. 91 Some Keyword 92 ``` 93 94 If 'Some Keyword' in the example above does its own Redfish.Login, it will 95 thwart the stated purpose of the caller. 96 97**Rules:** 98 99- Login should be done once in Suite Setup: 100 101 ``` 102 *** Keywords *** 103 Suite Setup Execution 104 Redfish.Login 105 ``` 106 107- Logout should be done once in Suite Teardown: 108 ``` 109 *** Keywords *** 110 Suite Teardown Execution 111 Redfish.Logout 112 ``` 113- As a result of the first two rules, all keywords and testcases that call upon 114 redfish functions (e.g. Redfish.Get, Redfish.Patch, etc.) have a right to 115 expect that login/logout have already been handled. Therefore, such keywords 116 and testcases should NOT do logins and logouts themselves. 117- There may be exceptions to the above but they require justification (e.g. a 118 test whose purpose is to verify that it can login with an **alternate** 119 username, etc.). 120- Any keyword or test case which breaks the above rules is responsible for 121 setting things right (i.e. back to a logged in state). 122 123# Rules for use of data/variables.py 124 125Avoid defining variables in data/variables.py for Redfish URIs. 126 127There's no obvious benefit to using such variables. Conversely, with literal 128values, it is much easier for the programmer to interpret the code. 129 130Consider the following example. 131 132Here's an excerpt from data/variables.py: 133 134``` 135# Redfish variables. 136REDFISH_BASE_URI = '/redfish/v1/' 137... 138REDFISH_ACCOUNTS = 'AccountService/Accounts/' 139REDFISH_ACCOUNTS_URI = REDFISH_BASE_URI + REDFISH_ACCOUNTS 140``` 141 142And here is a corresponding Robot code example: 143 144``` 145 # Rather than coding this: 146 Redfish.Delete ${REDFISH_ACCOUNTS_URI}user_user 147 148 # Code this: 149 Redfish.Delete /redfish/v1/AccountService/Accounts/user_user 150``` 151