1*** Settings ***
2Documentation   BMC and host redfish utility keywords.
3
4Resource        resource.robot
5Resource        bmc_redfish_resource.robot
6
7
8*** Keywords ***
9
10Redfish Power Operation
11    [Documentation]  Do Redfish host power operation.
12    [Arguments]      ${reset_type}
13    # Description of arguments:
14    # reset_type     Type of power operation.
15    #                (e.g. On/ForceOff/GracefulRestart/GracefulShutdown)
16
17    # Example:
18    # "Actions": {
19    # "#ComputerSystem.Reset": {
20    #  "ResetType@Redfish.AllowableValues": [
21    #    "On",
22    #    "ForceOff",
23    #    "ForceOn",
24    #    "ForceRestart",
25    #    "GracefulRestart",
26    #    "GracefulShutdown"
27    #    "PowerCycle",
28    #    "Nmi"
29    #  ],
30    #  "target": "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset"
31    #  }
32    # }
33
34    ${target}=  redfish_utils.Get Target Actions  /redfish/v1/Systems/system/  ComputerSystem.Reset
35    ${payload}=  Create Dictionary  ResetType=${reset_type}
36    ${resp}=  Redfish.Post  ${target}  body=&{payload}
37
38
39Redfish BMC Reset Operation
40    [Documentation]  Do Redfish BMC reset operation.
41
42    # Example:
43    # "Actions": {
44    # "#Manager.Reset": {
45    #  "ResetType@Redfish.AllowableValues": [
46    #    "GracefulRestart"
47    #  ],
48    #  "target": "/redfish/v1/Managers/bmc/Actions/Manager.Reset"
49    # }
50
51    ${target}=  redfish_utils.Get Target Actions  /redfish/v1/Managers/bmc/  Manager.Reset
52    ${payload}=  Create Dictionary  ResetType=GracefulRestart
53    Redfish.Post  ${target}  body=&{payload}
54
55
56Reset BIOS Via Redfish
57    [Documentation]  Do BIOS reset through Redfish.
58
59    ${target}=  redfish_utils.Get Target Actions  /redfish/v1/Systems/system/Bios/  Bios.ResetBios
60    Redfish.Post  ${target}  valid_status_codes=[${HTTP_OK}]
61
62
63Delete All Redfish Sessions
64    [Documentation]  Delete all active redfish sessions.
65
66    ${saved_session_info}=  Get Redfish Session Info
67
68    ${resp_list}=  Redfish_Utils.Get Member List
69    ...  /redfish/v1/SessionService/Sessions
70
71    # Remove the current login session from the list.
72    Remove Values From List  ${resp_list}  ${saved_session_info["location"]}
73
74    FOR  ${session}  IN  @{resp_list}
75        Redfish.Delete  ${session}
76    END
77
78Get Valid FRUs
79    [Documentation]  Return a dictionary containing all of the valid FRU records for the given fru_type.
80    [Arguments]  ${fru_type}
81
82    # NOTE: A valid FRU record will have a "State" key of "Enabled" and a "Health" key of "OK".
83
84    # Description of argument(s):
85    # fru_type  The type of fru (e.g. "Processors", "Memory", etc.).
86
87    ${fru_records}=  Redfish_Utils.Enumerate Request
88    ...  /redfish/v1/Systems/system/${fru_type}  return_json=0
89    ${fru_records}=  Filter Struct  ${fru_records}  [('State', 'Enabled'), ('Health', 'OK')]
90
91    [Return]  ${fru_records}
92
93
94Get Num Valid FRUs
95    [Documentation]  Return the number of valid FRU records for the given fru_type.
96    [Arguments]  ${fru_type}
97
98    # Description of argument(s):
99    # fru_type  The type of fru (e.g. "Processors", "Memory", etc.).
100
101    ${fru_records}=  Get Valid FRUs  ${fru_type}
102    ${num_valid_frus}=  Get length  ${fru_records}
103
104    [Return]  ${num_valid_frus}
105
106
107Verify Valid Records
108    [Documentation]  Verify all records retrieved with the given arguments are valid.
109    [Arguments]  ${record_type}  ${redfish_uri}  ${reading_type}
110
111    # Description of Argument(s):
112    # record_type    The sensor record type (e.g. "PowerSupplies")
113    # redfish_uri    The power supply URI (e.g. /redfish/v1/Chassis/chassis/Power)
114    # reading_type   The power watt readings (e.g. "PowerInputWatts")
115
116    # A valid record will have "State" key "Enabled" and "Health" key "OK".
117    ${records}=  Redfish.Get Attribute  ${redfish_uri}  ${record_type}
118
119    Rprint Vars  records
120
121    # Example output:
122    # records:
123    #   [0]:
124    #     [@odata.id]:                 /redfish/v1/Chassis/chassis/Power#/PowerControl/0
125    #     [@odata.type]:               #Power.v1_0_0.PowerControl
126    #     [MemberId]:                  0
127    #     [Name]:                      Chassis Power Control
128    #     [PowerConsumedWatts]:        264.0
129    #     [PowerLimit]:
130    #       [LimitInWatts]:            None
131    #     [PowerMetrics]:
132    #       [AverageConsumedWatts]:    325
133    #       [IntervalInMin]:           3
134    #       [MaxConsumedWatts]:        538
135    #     [Status]:
136    #       [Health]:                  OK
137    #       [State]:                   Enabled
138
139    ${invalid_records}=  Filter Struct  ${records}
140    ...  [('Health', '^OK$'), ('State', '^Enabled$'), ('${reading_type}', '')]  regex=1  invert=1
141    Valid Length  invalid_records  max_length=0
142
143    [Return]  ${records}
144
145
146Redfish Create User
147    [Documentation]  Redfish create user.
148    [Arguments]   ${user_name}  ${password}  ${role_id}  ${enabled}  ${force}=${False}
149
150    # Description of argument(s):
151    # user_name           The user name to be created.
152    # password            The password to be assigned.
153    # role_id             The role ID of the user to be created.
154    #                     (e.g. "Administrator", "Operator", etc.).
155    # enabled             Indicates whether the username being created.
156    #                     should be enabled (${True}, ${False}).
157    # force               Delete user account and re-create if force is True.
158
159    ${curr_role}=  Run Keyword And Ignore Error  Get User Role  ${user_name}
160    # Ex: ${curr_role} = ('PASS', 'Administrator')
161
162    ${user_exists}=  Run Keyword And Return Status  Should Be Equal As Strings  ${curr_role}[0]  PASS
163
164    # Delete user account when force is True.
165    Run Keyword If  ${force} == ${True}  Redfish.Delete  ${REDFISH_ACCOUNTS_URI}${user_name}
166    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NOT_FOUND}]
167
168    # Create specified user when force is True or User does not exist.
169    ${payload}=  Create Dictionary
170    ...  UserName=${user_name}  Password=${password}  RoleId=${role_id}  Enabled=${enabled}
171
172    Run Keyword If  ${force} == ${True} or ${user_exists} == ${False}
173    ...  Redfish.Post  ${REDFISH_ACCOUNTS_URI}  body=&{payload}
174    ...  valid_status_codes=[${HTTP_CREATED}]
175
176
177Get User Role
178    [Documentation]  Get User Role.
179    [Arguments]  ${user_name}
180
181    # Description of argument(s):
182    # user_name    User name to get it's role.
183
184    ${role_config}=  Redfish_Utils.Get Attribute
185    ...  ${REDFISH_ACCOUNTS_URI}${user_name}  RoleId
186
187    [Return]  ${role_config}
188
189
190Create Users With Different Roles
191    [Documentation]  Create users with different roles.
192    [Arguments]  ${users}  ${force}=${False}
193
194    # Description of argument(s):
195    # users    Dictionary of roles and user credentails to be created.
196    #          Ex:  {'Administrator': '[admin_user, TestPwd123]', 'Operator': '[operator_user, TestPwd123]'}
197    # force    Delete given user account if already exists when force is True.
198
199    FOR  ${role}  IN  @{users}
200      Redfish Create User  ${users['${role}'][0]}  ${users['${role}']}[1]  ${role}  ${True}  ${force}
201    END
202
203
204Delete BMC Users Via Redfish
205    [Documentation]  Delete BMC users via redfish.
206    [Arguments]  ${users}
207
208    # Description of argument(s):
209    # users    Dictionary of roles and user credentials to be deleted.
210
211    FOR  ${role}  IN  @{users}
212        Redfish.Delete  /redfish/v1/AccountService/Accounts/${users['${role}'][0]}
213        ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NOT_FOUND}]
214    END
215
216