Esempio n. 1
0
class NfsnTest(object):

    nfsn = Nfsn(login='******', api_key='1234567890123456')

    #def teardown(self):
    #    httpretty.disable()

    def test_httpretty_is_enabled(self):
        assert httpretty.is_enabled() is True
Esempio n. 2
0
Originally posted here: https://www.mitsake.net/2016/04/nfsn-and-ddns-take-2/
- Modified to include syslog messages to document checks/updates
'''

from nfsn import Nfsn
import requests
import sys
import syslog

user = "******"  # Your NFSN username
key = "<YOUR-API-KEY>"  # API key
domain = "<YOUR-NFSN-DOMAIN>"  # Your NFS-hosted domain
subdomain = "<YOUR-SUBDOMAIN>"  # The entry (host/subdomain) which will get your IP address

nfsn = Nfsn(user, key)  # Create the NFSN API object

syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_LOCAL1)
fqdn = ".".join((subdomain, domain))
syslog.syslog(syslog.LOG_INFO, "refreshing external IP for {}".format(fqdn))

try:
    currentip = requests.get('http://api.ipify.org').text
    listedip = nfsn.dns(domain).listRRs(subdomain)[0]['data']

    if currentip != listedip:
        # update required
        nfsn.dns(domain).removeRR(subdomain, 'A', listedip)
        nfsn.dns(domain).addRR(subdomain, 'A', currentip)

        syslog.syslog(
Esempio n. 3
0
    rrs = nfsn.dns(domain).listRRs(name=subdomain, type="A")
    assert len(rrs) <= 1, "Shouldn't have more than one A record"

    if len(rrs) == 0:
        log.info("Dynamic record does not exist, creating...")
        nfsn.dns(domain).addRR(name=subdomain, type="A", data=ip)
        return

    rr = rrs[0]
    if rr.data == ip:
        return

    log.info("Dynamic ip is {0}, actual ip is {1}, updating...".format(
        rr.data, ip))
    nfsn.dns(domain).removeRR(name=subdomain, type="A", data=rr.data)
    nfsn.dns(domain).addRR(name=subdomain, type="A", data=ip)


if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("nfsn_login")
    parser.add_argument("nfsn_api_key_file_path")
    parser.add_argument("domain")
    parser.add_argument("subdomain")
    args = parser.parse_args()

    with open(args.nfsn_api_key_file_path) as keyfile:
        nfsn = Nfsn(login=args.nfsn_login, api_key=keyfile.read())
    update_if_needed(nfsn, args.domain, args.subdomain, current_ip())
Esempio n. 4
0
 def test_constructor_implicit_loginfile(self, monkeypatch, tmpdir):
     login_file = os.path.join(fixtures_dir, 'nfsn-api-credential')
     copyfile(login_file, str(tmpdir.join('.nfsn-api')))
     monkeypatch.setenv('HOME', str(tmpdir))
     nfsn = Nfsn()
     assert type(nfsn) is Nfsn
Esempio n. 5
0
 def test_constructor_loginfile(self):
     login_file = os.path.join(fixtures_dir, 'nfsn-api-credential')
     nfsn = Nfsn(login_file=login_file)
     assert type(nfsn) is Nfsn
Esempio n. 6
0
 def test_constructor_login_and_key(self):
     nfsn = Nfsn(login='******', api_key='1234567890123456')
     assert type(nfsn) is Nfsn
Esempio n. 7
0
 def test_constructor_missing_login(self):
     with pytest.raises(ValueError):
         Nfsn(api_key='1234567890123456')
Esempio n. 8
0
 def test_constructor_missing_api_key(self):
     with pytest.raises(ValueError):
         Nfsn(login='******')
Esempio n. 9
0
 def test_constructor_no_args_file_missing(self, monkeypatch, tmpdir):
     monkeypatch.setenv('HOME', str(tmpdir))
     with pytest.raises(IOError):
         Nfsn()