Skip to content

Commit

Permalink
Merge pull request #4 from segadson/network_pools
Browse files Browse the repository at this point in the history
Network pools
  • Loading branch information
segadson authored Jun 27, 2024
2 parents 29e2992 + b05ce8a commit 911d24b
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 268 deletions.
19 changes: 7 additions & 12 deletions plugins/modules/sddc_manager_avns.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,9 @@ def run(self):
self.avns_payload['edgeClusterId'] = edge_cluster_id

if self.operation == 'create':
try:
payload_data = self.create_avns()
self.module.exit_json(changed=True, meta=payload_data)
except Exception as e:
self.module.fail_json(msg=f"Error: {e}")
return self.create_avns()
elif self.operation == 'validate':
try:
payload_data = self.validate_avns()
self.module.exit_json(changed=True, meta=payload_data)
except Exception as e:
self.module.fail_json(msg=f"Error: {e}")

return self.validate_avns()
def main():
parameters = dict(
sddc_manager_ip=dict(type='str', required=True),
Expand All @@ -162,7 +153,11 @@ def main():

module = AnsibleModule(argument_spec=parameters, supports_check_mode=True)
sddc_manager_avns = SDDCManagerAVNS(module)
sddc_manager_avns.run()
try:
payload_data = sddc_manager_avns.run()
module.exit_json(changed=True, meta=payload_data)
except VcfAPIException as e:
module.fail_json(msg=f"Error: {e}")

if __name__ == '__main__':
main()
190 changes: 91 additions & 99 deletions plugins/modules/sddc_manager_host_commisson.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,118 +79,110 @@
2) Decommission Host:
- Check if host are in inventory with UNASSIGNED_USEABLE
'''
def get_hosts_by_name_valid_for_commisson(sddc_manager_ip, sddc_manager_user, sddc_manager_password, hosts_list_payload):
valid_hosts_lists = []
for host in hosts_list_payload:
hosts_name = host['fqdn']
api_client = SddcManagerApiClient(sddc_manager_ip, sddc_manager_user, sddc_manager_password)
api_response = api_client.get_all_hosts()
payload_data = api_response.data
for element in payload_data['elements']:
print("Element: ", element['fqdn'])
if element['fqdn'] == hosts_name and element['status'] != 'UNASSIGNED_USEABLE' and element['status'] != 'ASSIGNED':
print(f"Host {hosts_name} is not Valid")
else:
valid_hosts_lists.append(host)
return valid_hosts_lists

def get_hosts_by_name_valid_for_decommisson(sddc_manager_ip, sddc_manager_user, sddc_manager_password, hosts_list_payload):
valid_hosts_lists = []
for host in hosts_list_payload:
hosts_name = host['fqdn']
api_client = SddcManagerApiClient(sddc_manager_ip, sddc_manager_user, sddc_manager_password)
api_response = api_client.get_all_hosts()
class SddcManagerHostCommission:
def __init__(self, module):
self.module = module
self.sddc_manager_ip = module.params['sddc_manager_ip']
self.sddc_manager_user = module.params['sddc_manager_user']
self.sddc_manager_password = module.params['sddc_manager_password']
self.hosts_list_payload = module.params['hosts_list_payload']
self.api_client = SddcManagerApiClient(self.sddc_manager_ip, self.sddc_manager_user, self.sddc_manager_password)

def get_hosts_by_name_valid_for_commisson(self):
hosts_list_payload = self.hosts_list_payload
valid_hosts_lists = []
for host in hosts_list_payload:
hosts_name = host['fqdn']
api_response = self.api_client.get_all_hosts()
payload_data = api_response.data
for element in payload_data['elements']:
if element['fqdn'] == hosts_name and element['status'] != 'UNASSIGNED_USEABLE' and element['status'] != 'ASSIGNED':
print(f"Host {hosts_name} is not Valid")
else:
valid_hosts_lists.append(host)
return valid_hosts_lists

def get_hosts_by_name_valid_for_decommisson(self):
hosts_list_payload = self.hosts_list_payload
valid_hosts_lists = []
for host in hosts_list_payload:
hosts_name = host['fqdn']
api_response = self.api_client.get_all_hosts()
payload_data = api_response.data
for element in payload_data['elements']:
if element['fqdn'] == hosts_name and element['status'] == 'UNASSIGNED_USEABLE':
valid_hosts_lists.append(hosts_name)
return valid_hosts_lists

def get_network_pool_by_name(self, name):
api_response = self.api_client.get_network_pools()
payload_data = api_response.data
for element in payload_data['elements']:
if element['fqdn'] == hosts_name and element['status'] == 'UNASSIGNED_USEABLE':
valid_hosts_lists.append(hosts_name)
return valid_hosts_lists

def get_network_pool_by_name(sddc_manager_ip, sddc_manager_user, sddc_manager_password, name):
api_client = SddcManagerApiClient(sddc_manager_ip, sddc_manager_user, sddc_manager_password)
api_response = api_client.get_network_pools()
payload_data = api_response.data
for element in payload_data['elements']:
if element['name'] == name:
return element
return None

def main():
parameters = {
"sddc_manager_ip": {"required": True, "type": "str"},
"sddc_manager_user": {"required": True, "type": "str"},
"sddc_manager_password": {"required": True, "type": "str"},
"hosts_list_payload": {"required": True, "type": "dict"}, # List of Hosts
"validate": {"required": False, "type": "bool", "default": False},
"state": {"required": True, "type": "str", "choices": ['commission', 'decommission']}
}
if element['name'] == name:
return element
return None

module = AnsibleModule(supports_check_mode=True,
argument_spec=parameters)

sddc_manager_ip = module.params['sddc_manager_ip']
sddc_manager_user = module.params['sddc_manager_user']
sddc_manager_password = module.params['sddc_manager_password']
hosts_list_payload = module.params['hosts_list_payload']
validate = module.params['validate']
state = module.params['state']

updated_host_payload = hosts_list_payload['hosts']

#Check for vvolStorageProtocolType
# if state == 'commission':
# for item in updated_host_payload:
# if item['vvolStorageProtocolType'] == None and item['storageType'] == ['VVOL', 'NFS', 'VMFS_FC']:
# module.fail_json(msg="vvolStorageProtocolType is required for VVOL, NFS or VMFS_FC StorageType")
# elif item['vvolStorageProtocolType'] == None and item['storageType'] == ['VSAN', 'VSAN_ESA', 'VSAN_REMOTE']:
# module.fail_json(msg="vvolStorageProtocolType should be null")

print("Host List: ", updated_host_payload)
if state == 'commission' and validate == True:
#############
# Get Network Pool by Name and add to payload
###############
# updated_host_payload = get_hosts_by_name_valid_for_commisson(sddc_manager_ip, sddc_manager_user,
# sddc_manager_password, host_list)

def host_commission_validate_hosts(self):
hosts_list_payload = self.hosts_list_payload
updated_host_payload = hosts_list_payload['hosts']
for item in updated_host_payload:
item['networkPoolId'] = get_network_pool_by_name(sddc_manager_ip, sddc_manager_user,
sddc_manager_password, item['networkPoolName'])['id']
print("Updated Host Payload: ", json.dumps(updated_host_payload))
# stop
item['networkPoolId'] = self.get_network_pool_by_name(item['networkPoolName'])['id']
try:
api_client = SddcManagerApiClient(sddc_manager_ip, sddc_manager_user, sddc_manager_password)
api_response = api_client.validate_hosts(json.dumps(updated_host_payload))
api_response = self.api_client.validate_hosts(json.dumps(updated_host_payload))
payload_data = api_response.data
module.exit_json(changed=False, meta=payload_data)
return payload_data
except VcfAPIException as e:
module.fail_json(msg=f"Error: {e}")
elif state == 'commission' and validate == False:
# updated_host_payload = get_hosts_by_name_valid_for_commisson(sddc_manager_ip, sddc_manager_user,
# sddc_manager_password, host_list)
self.module.fail_json(msg=f"Error: {e}")

def host_commission_commission_hosts(self):
hosts_list_payload = self.hosts_list_payload
updated_host_payload = hosts_list_payload['hosts']
for item in updated_host_payload:
item['networkPoolId'] = get_network_pool_by_name(sddc_manager_ip, sddc_manager_user,
sddc_manager_password, item['networkPoolName'])['id']
item['networkPoolId'] = self.get_network_pool_by_name(item['networkPoolName'])['id']
try:
api_client = SddcManagerApiClient(sddc_manager_ip, sddc_manager_user, sddc_manager_password)
api_response = api_client.commission_hosts(json.dumps(updated_host_payload))
api_response = self.api_client.commission_hosts(json.dumps(updated_host_payload))
payload_data = api_response.data
module.exit_json(changed=False, meta=payload_data)
return payload_data
except VcfAPIException as e:
module.fail_json(msg=f"Error: {e}")
elif state == 'decommission' and validate == False:

updated_host_payload = get_hosts_by_name_valid_for_decommisson(sddc_manager_ip, sddc_manager_user,
sddc_manager_password, host_list)
self.module.fail_json(msg=f"Error: {e}")

def host_commission_decommission_hosts(self):
hosts_list_payload = self.hosts_list_payload
updated_host_payload = hosts_list_payload['hosts']
updated_host_payload = self.get_hosts_by_name_valid_for_decommisson()
try:
api_client = SddcManagerApiClient(sddc_manager_ip, sddc_manager_user, sddc_manager_password)
api_response = api_client.decommission_hosts(json.dumps(updated_host_payload))
api_response = self.api_client.decommission_hosts(json.dumps(updated_host_payload))
payload_data = api_response.data
module.exit_json(changed=False, meta=payload_data)
return payload_data
except VcfAPIException as e:
module.fail_json(msg=f"Error: {e}")
else:
module.exit_json(changed=False, meta="Not Valid Action")
self.module.fail_json(msg=f"Error: {e}")

def run(self):
if self.module.params['state'] == 'commission' and self.module.params['validate'] == True:
result = self.host_commission_validate_hosts()
self.module.exit_json(changed=False, meta=result)
elif self.module.params['state'] == 'commission' and self.module.params['validate'] == False:
result = self.host_commission_commission_hosts()
self.module.exit_json(changed=True, meta=result)
elif self.module.params['state'] == 'decommission' and self.module.params['validate'] == False:
result = self.host_commission_decommission_hosts()
self.module.exit_json(changed=True, meta=result)
else:
self.module.fail_json(msg="Not Valid Action")

def main():
parameters = dict(
sddc_manager_ip=dict(required=True, type='str'),
sddc_manager_user=dict(required=True, type='str'),
sddc_manager_password=dict(required=True, type='str'),
hosts_list_payload=dict(required=True, type='dict'),
validate=dict(required=False, type='bool', default=False),
state=dict(required=True, type='str', choices=['commission', 'decommission'])
)

module = AnsibleModule(argument_spec=parameters, supports_check_mode=True)
host_commission = SddcManagerHostCommission(module)
host_commission.run()

if __name__ == '__main__':
main()
main()
Loading

0 comments on commit 911d24b

Please sign in to comment.