Skip to content

Commit

Permalink
Merge pull request #69 from SreenidhiS1/Issue-136
Browse files Browse the repository at this point in the history
Issue 136 - Added new action to list the PCM metrics
  • Loading branch information
naveenkandakur authored Dec 3, 2024
2 parents ecce7bc + 033068f commit 2a1f8f7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
17 changes: 15 additions & 2 deletions plugins/module_utils/hmc_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,9 @@ def getSystemPCMpreferences(self, system_uuid):
response = resp.read()
return response

def getPCM(self, system_uuid):
def getPCM(self, system_uuid, action):
preference_map = {'LTM': 'LongTermMonitorEnabled', 'STM': 'ShortTermMonitorEnabled',
'AM': 'AggregationEnabled', 'CLTM': 'ComputeLTMEnabled', 'EM': 'EnergyMonitorEnabled'}
url = "https://{0}/rest/api/pcm/ManagedSystem/{1}/preferences".format(self.hmc_ip, system_uuid)
header = {'X-API-Session': self.session,
'Content-Type': 'application/xml'}
Expand All @@ -524,14 +526,25 @@ def getPCM(self, system_uuid):
logger.debug("Get of preferences failed. Respsonse code: %d", resp.code)
return None
response = resp.read()
if action is not None:
doc = xml_strip_namespace(response)
path = doc.xpath("//ManagedSystemPcmPreference")[0]
output = dict()
for item in preference_map:
if (path.xpath(preference_map[item])[0].text == "true"):
value = "Enabled"
else:
value = "Disabled"
output[preference_map[item]] = value
return output
return response

def updatePCM(self, system_uuid, metrics, disable):
logon_res = self.logon()
url = "https://{0}/rest/api/pcm/ManagedSystem/{1}/preferences".format(self.hmc_ip, system_uuid)
header = {'Content-Type': 'application/xml',
'X-API-Session': logon_res}
sys_details = self.getPCM(system_uuid)
sys_details = self.getPCM(system_uuid, None)
doc = xml_strip_namespace(sys_details)
preference_map = {'LTM': 'LongTermMonitorEnabled', 'STM': 'ShortTermMonitorEnabled',
'AM': 'AggregationEnabled', 'CLTM': 'ComputeLTMEnabled', 'EM': 'EnergyMonitorEnabled'}
Expand Down
64 changes: 61 additions & 3 deletions plugins/modules/power_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@
state:
description:
- C(facts) fetch details of specified I(system_name)
- C(pcm_facts) fetch Performance and Capacity Monitoring details of specified I(system_name)
type: str
choices: ['facts']
choices: ['facts', 'pcm_facts']
'''

EXAMPLES = '''
Expand Down Expand Up @@ -171,6 +172,15 @@
system_name: <managed_system_name/mtms>
state: facts
- name: Fetch facts about monitoring metrics
power_system:
hmc_host: "{{ inventory_hostname }}"
hmc_auth:
username: '{{ ansible_user }}'
password: '{{ hmc_password }}'
system_name: <managed_system_name/mtms>
state: pcm_facts
- name: enable the long-term monitoring
power_system:
hmc_host: "{{ inventory_hostname }}"
Expand Down Expand Up @@ -257,6 +267,10 @@ def validate_parameters(params):
mandatoryList = ['hmc_host', 'hmc_auth', 'system_name', 'metrics']
unsupportedList = ['new_name', 'power_off_policy', 'power_on_lpar_start_policy', 'requested_num_sys_huge_pages',
'mem_mirroring_mode', 'pend_mem_region_size']
elif opr == 'list_pcm':
mandatoryList = ['hmc_host', 'hmc_auth', 'system_name']
unsupportedList = ['new_name', 'power_off_policy', 'power_on_lpar_start_policy', 'requested_num_sys_huge_pages',
'mem_mirroring_mode', 'pend_mem_region_size', 'metrics']
else:
mandatoryList = ['hmc_host', 'hmc_auth', 'system_name']
unsupportedList = ['new_name', 'power_off_policy', 'power_on_lpar_start_policy', 'requested_num_sys_huge_pages',
Expand Down Expand Up @@ -439,6 +453,48 @@ def fetchManagedSysDetails(module, params):
return changed, system_prop, None


def listPCM(module, params):
hmc_host = params['hmc_host']
hmc_user = params['hmc_auth']['username']
password = params['hmc_auth']['password']
system_name = params['system_name']
system_prop = None
system_uuid = None
changed = False
validate_parameters(params)
hmc_conn = HmcCliConnection(module, hmc_host, hmc_user, password)
hmc = Hmc(hmc_conn)
if re.match(HmcConstants.MTMS_pattern, system_name):
try:
system_name = hmc.getSystemNameFromMTMS(system_name)
except HmcError as on_system_error:
return changed, repr(on_system_error), None
try:
rest_conn = HmcRestClient(hmc_host, hmc_user, password)
except Exception as error:
error_msg = parse_error_response(error)
module.fail_json(msg=error_msg)

try:
system_uuid, server_dom = rest_conn.getManagedSystem(system_name)
if not system_uuid:
module.fail_json(msg="Given system is not present")
else:
system_prop = rest_conn.getPCM(system_uuid, params['state'])
except (Exception, HmcError) as error:
error_msg = parse_error_response(error)
logger.debug("Line number: %d exception: %s", sys.exc_info()[2].tb_lineno, repr(error))
module.fail_json(msg=error_msg)
finally:
try:
rest_conn.logoff()
except Exception as logoff_error:
error_msg = parse_error_response(logoff_error)
module.warn(error_msg)

return changed, system_prop, None


def updatePCM(module, params):
hmc_host = params['hmc_host']
hmc_user = params['hmc_auth']['username']
Expand Down Expand Up @@ -507,7 +563,8 @@ def perform_task(module):
"modify_syscfg": modifySystemConfiguration,
"modify_hwres": modifySystemHardwareResources,
"enable_pcm": updatePCM,
"disable_pcm": updatePCM
"disable_pcm": updatePCM,
"pcm_facts": listPCM,
}
oper = 'action'
if params['action'] is None:
Expand Down Expand Up @@ -540,7 +597,7 @@ def run_module():
metrics=dict(type='list', elements='str', choices=['LTM', 'STM', 'AM', 'CLTM', 'EM']),
pend_mem_region_size=dict(type='str', choices=['auto', '16', '32', '64', '128', '256']),
action=dict(type='str', choices=['poweron', 'poweroff', 'modify_syscfg', 'modify_hwres', 'enable_pcm', 'disable_pcm']),
state=dict(type='str', choices=['facts']),
state=dict(type='str', choices=['facts', 'pcm_facts']),
)

module = AnsibleModule(
Expand All @@ -554,6 +611,7 @@ def run_module():
['action', 'modify_hwres', ['hmc_host', 'hmc_auth', 'system_name']],
['action', 'enable_pcm', ['hmc_host', 'hmc_auth', 'system_name', 'metrics']],
['action', 'disable_pcm', ['hmc_host', 'hmc_auth', 'system_name', 'metrics']],
['state', 'pcm_facts', ['hmc_host', 'hmc_auth', 'system_name']],
],
)

Expand Down
8 changes: 7 additions & 1 deletion tests/unit/modules/test_hmc_power_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@
"ParameterError: mandatory parameter 'system_name' is missing"),
# when mertrics is mssing
({'hmc_host': "0.0.0.0", 'hmc_auth': hmc_auth, 'action': 'enable_pcm', 'state': None, 'system_name': "system_name", 'metrics': None},
"ParameterError: mandatory parameter 'metrics' is missing")]
"ParameterError: mandatory parameter 'metrics' is missing"),
# when hostname is misssing for pcm facts
({'hmc_host': None, 'hmc_auth': hmc_auth, 'action': None, 'state': 'pcm_facts', 'system_name': "system_name", 'metrics': None},
"ParameterError: mandatory parameter 'hmc_host' is missing"),
# when system_name is misssing for pcm facts
({'hmc_host': '0.0.0.0', 'hmc_auth': hmc_auth, 'action': None, 'state': 'pcm_facts', 'system_name': None, 'metrics': None},
"ParameterError: mandatory parameter 'system_name' is missing")]


def common_mock_setup(mocker):
Expand Down

0 comments on commit 2a1f8f7

Please sign in to comment.