def listNodes(module, stub_config): try: fabricnodes_svc = Nodes(stub_config) except Error as ex: api_error = ex.data.convert_to(ApiError) module.fail_json(msg='API Error listing nodes: %s' % (api_error.error_message)) return fabricnodes_svc.list()
def getEdheNodeByName(module, stub_config): nodes_svc = Nodes(stub_config) nodes_list = nodes_svc.list(resource_type='EdgeNode') for node in nodes_list.results: en = node.convert_to(EdgeNode) if en.display_name == module.params['display_name']: return en return None
def main(): module = AnsibleModule( argument_spec=dict( display_name=dict(required=True, type='str'), description=dict(required=False, type='str', default=None), form_factor=dict(required=False, type='str', default='MEDIUM', choices=['SMALL', 'MEDIUM', 'LARGE']), vsphere_cluster=dict(required=True, type='str'), host_id=dict(required=False, type='str', default=None), data_network_ids=dict(required=True, type='list'), default_gateway_addresses=dict(required=True, type='list'), hostname=dict(required=True, type='str'), management_network_id=dict(required=True, type='str'), management_port_subnet=dict(required=True, type='str'), management_port_prefix=dict(required=True, type='int'), storage_id=dict(required=True, type='str'), vc_id=dict(required=True, type='str'), cli_password=dict(required=True, type='str', no_log=True), root_password=dict(required=True, type='str', no_log=True), state=dict(required=False, type='str', default="present", choices=['present', 'absent']), nsx_manager=dict(required=True, type='str'), nsx_username=dict(required=True, type='str'), nsx_passwd=dict(required=True, type='str', no_log=True) ), supports_check_mode=True ) if not HAS_PYNSXT: module.fail_json(msg='pynsxt is required for this module') session = requests.session() session.verify = False nsx_url = 'https://%s:%s' % (module.params['nsx_manager'], 443) connector = connect.get_requests_connector( session=session, msg_protocol='rest', url=nsx_url) stub_config = StubConfigurationFactory.new_std_configuration(connector) security_context = create_user_password_security_context(module.params["nsx_username"], module.params["nsx_passwd"]) connector.set_security_context(security_context) requests.packages.urllib3.disable_warnings() node = getEdheNodeByName(module, stub_config) if module.params['state'] == "present": if node: module.exit_json(changed=False, id=node.id, msg="Edge with name %s already exists!" % (module.params['display_name'])) elif not node: createEdge(module, stub_config) elif module.params['state'] == "absent": if node: nodes_svc = Nodes(stub_config) nodes_svc.delete(node.id) module.exit_json(changed=True, object_name=module.params['display_name'], message="Node with name %s deleted"%(module.params['display_name'])) elif not node: module.exit_json(changed=False, object_name=module.params['display_name'], message="Node with name %s does not exists"%(module.params['display_name']))
def main(): args = getargs.getargs() stub_config = auth.get_session_auth_stub_config(args.user, args.password, args.nsx_host, args.tcp_port) # Create the services we'll need. fabricnodes_svc = Nodes(stub_config) status_svc = Status(stub_config) # List all of the fabric nodes result = fabricnodes_svc.list() # Iterate over the results for vs in result.results: fn = vs.convert_to(Node) print("Type: %s, Name: %s, id: %s" % (fn.resource_type, fn.display_name, fn.id)) fn_status = status_svc.get(fn.id) print(" mp conn: %s, cp conn: %s" % (fn_status.mpa_connectivity_status, fn_status.lcp_connectivity_status))
def createNode(module, stub_config): if module.params['os_type'] == "ESXI": os_type = HostNode.OS_TYPE_ESXI elif module.params['os_type'] == "RHEL": os_type = HostNode.OS_TYPE_RHELKVM elif module.params['os_type'] == "UBUNTU": os_type = HostNode.OS_TYPE_UBUNTUKVM ip_addr = [] ip_addr.append(module.params['ip_address']) fnodes_svc = Nodes(stub_config) newNode = HostNode(display_name=module.params['display_name'], ip_addresses=ip_addr, os_type=os_type, os_version=module.params['os_version'], host_credential=HostNodeLoginCredential( username=module.params['node_username'], password=module.params['node_passwd'], thumbprint=module.params['thumbprint'])) try: fnodes_svc.create(newNode) except Error as ex: api_error = ex.data.convert_to(ApiError) module.fail_json(msg='API Error creating node: %s' % (api_error.error_message)) time.sleep(20) resultNode = getNodeByName(module, stub_config) status_svc = Status(stub_config) while True: fn_status = status_svc.get(resultNode.id) if fn_status.host_node_deployment_status == "INSTALL_IN_PROGRESS": time.sleep(10) elif fn_status.host_node_deployment_status == "INSTALL_SUCCESSFUL": time.sleep(5) return resultNode else: module.fail_json(msg='Error in Node status: %s' % (str(fn_status)))
def createEdge(module, stub_config): nodes_svc = Nodes(stub_config) edgeNode = EdgeNode( deployment_config = createDeploymentConfig(module, stub_config), description = module.params['description'], display_name = module.params['display_name'], tags = None ) try: tmp_node = nodes_svc.create(edgeNode) node = tmp_node.convert_to(EdgeNode) status_svc = Status(stub_config) while True: node_status = status_svc.get(node.id) time.sleep(5) if node_status.mpa_connectivity_status == 'UP' and node_status.host_node_deployment_status == 'NODE_READY': module.exit_json(changed=True, id=node.id, msg="Edge VM with name %s created!" % (module.params['display_name'])) except Error as ex: api_error = ex.data.convert_to(ApiError) module.fail_json('API Error creating node: %s' % (api_error.error_message))
def deleteNode(module, node, stub_config): fnodes_svc = Nodes(stub_config) node_id = node.id node_name = node.display_name try: fnodes_svc.delete(node_id) except Error as ex: api_error = ex.data.convert_to(ApiError) module.exit_json(changed=False, object_id=node_id, object_name=node_name, message=api_error) module.fail_json(msg='API Error Deleting node: %s' % (api_error.error_message)) status_svc = Status(stub_config) while True: try: fn_status = status_svc.get(node_id) time.sleep(10) except Error as ex: module.exit_json(changed=True, object_id=node_id, object_name=node_name)