def main():
    """
    Main function
    :returns: A message or server ID
    """
    module = AnsibleModule(argument_spec=dict(
        auth=dict(type='dict'),
        region=dict(default='na', type='str'),
        datacenter=dict(required=True, type='str'),
        network_domain=dict(required=False, default=None, type='str'),
        cluster=dict(required=False, default=None, type='str'),
        id=dict(required=True, type='str'),
        name=dict(required=True, type='str'),
        description=dict(required=False, default=None, type='str'),
        start=dict(required=False, default=False, type='bool'),
        connect_nics=dict(required=False, default=False, type='bool'),
        preserve_mac=dict(required=False, default=False, type='bool'),
        networks=dict(required=False,
                      default=None,
                      type='list',
                      elements='dict'),
        wait=dict(required=False, default=True, type='bool'),
        wait_time=dict(required=False, default=1800, type='int'),
        wait_poll_interval=dict(required=False, default=30, type='int'),
        wait_for_vmtools=dict(required=False, default=False, type='bool'),
        migrate=dict(required=False, default=False, type='bool')),
                           supports_check_mode=True)
    try:
        credentials = get_credentials(module)
    except ImportError as e:
        module.fail_json(msg='{0}'.format(e))

    # Check the region supplied is valid
    regions = get_regions()
    if module.params.get('region') not in regions:
        module.fail_json(
            msg='Invalid region. Regions must be one of {0}'.format(regions))

    if credentials is False:
        module.fail_json(msg='Could not load the user credentials')

    try:
        client = NTTMCPClient(credentials, module.params.get('region'))
    except NTTMCPAPIException as e:
        module.fail_json(msg=e.msg)

    try:
        snapshot = client.get_snapshot_by_id(module.params.get('id'))
        CORE['datacenter'] = snapshot.get('datacenterId')
        CORE['snapshot'] = snapshot
        CORE['start'] = module.params.get('start')
        if module.check_mode:
            module.exit_json(
                msg=
                'Snapshot with ID {0} will be used to create the Preview server {1}'
                .format(snapshot.get('id'), module.params.get('name')))
        if module.params.get('wait_for_vmtools'):
            CORE['start'] = True
        if snapshot.get('replica'):
            networks = check_replica_input(module, client)
            create_preview_server(module, client, True, networks)
        else:
            create_preview_server(module, client, False, None)
    except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e:
        module.fail_json(msg='Could not retrieve the Snapshot - {0}'.format(e))
def main():
    """
    Main function
    :returns: A message
    """
    module = AnsibleModule(argument_spec=dict(auth=dict(type='dict'),
                                              region=dict(default='na',
                                                          type='str'),
                                              id=dict(required=True,
                                                      type='str'),
                                              src_path=dict(required=True,
                                                            type='str'),
                                              dst_path=dict(required=True,
                                                            type='str'),
                                              username=dict(required=True,
                                                            type='str'),
                                              password=dict(required=True,
                                                            type='str'),
                                              wait=dict(required=False,
                                                        default=False,
                                                        type='bool'),
                                              wait_time=dict(required=False,
                                                             default=3600,
                                                             type='int'),
                                              wait_poll_interval=dict(
                                                  required=False,
                                                  default=30,
                                                  type='int')),
                           supports_check_mode=True)

    result = snapshot = None
    snapshot_id = module.params.get('id')

    try:
        credentials = get_credentials(module)
    except ImportError as e:
        module.fail_json(msg='{0}'.format(e))

    # Check the region supplied is valid
    regions = get_regions()
    if module.params.get('region') not in regions:
        module.fail_json(
            msg='Invalid region. Regions must be one of {0}'.format(regions))

    if credentials is False:
        module.fail_json(msg='Could not load the user credentials')

    try:
        client = NTTMCPClient(credentials, module.params.get('region'))
    except NTTMCPAPIException as e:
        module.fail_json(msg=e.msg)

    try:
        snapshot = client.get_snapshot_by_id(snapshot_id)
        if not snapshot:
            module.fail_json(
                msg='Snapshot with ID {0} does not exist'.format(snapshot_id))
        if module.check_mode:
            module.exit_json(msg='A snapshot restore will be performed')
        result = client.snapshot_restore(snapshot_id,
                                         module.params.get('src_path'),
                                         module.params.get('dst_path'),
                                         module.params.get('username'),
                                         module.params.get('password'))
        if module.params.get('wait'):
            server_id = next(
                (item
                 for item in result.get('info') if item["name"] == "serverId"),
                dict()).get('value')
            if not server_id:
                module.fail_json(
                    msg=
                    'Snapshot restore was successful but could not find the server to wait for status updates'
                )
            wait_for_snapshot(module, client, server_id)
            module.exit_json(
                changed=True,
                msg='The file/directory have been successfully restored')
        module.exit_json(changed=True,
                         msg='The restoration process is in progress. '
                         'Check the status manually or use server_info')

    except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e:
        module.fail_json(msg='Error restoring the file: {0}'.format(e))