|
| 1 | +# |
| 2 | +# Copyright (c) 2024 NSONE, Inc. |
| 3 | +# |
| 4 | +# License under The MIT License (MIT). See LICENSE in project root. |
| 5 | +# |
| 6 | + |
| 7 | +from ns1 import NS1 |
| 8 | + |
| 9 | +# NS1 will use config in ~/.nsone by default |
| 10 | +api = NS1() |
| 11 | + |
| 12 | +# to specify an apikey here instead, use: |
| 13 | +# api = NS1(apiKey='<<CLEARTEXT API KEY>>') |
| 14 | + |
| 15 | +# to load an alternate configuration file: |
| 16 | +# api = NS1(configFile='/etc/ns1/api.json') |
| 17 | + |
| 18 | +# turn on "follow pagination". This will handle paginated responses for |
| 19 | +# redirect and certificate list. It's off by default. |
| 20 | +config = api.config |
| 21 | +config["follow_pagination"] = True |
| 22 | + |
| 23 | +redirects = api.redirects() |
| 24 | +certificates = api.redirect_certificates() |
| 25 | + |
| 26 | +########## |
| 27 | +# CREATE # |
| 28 | +########## |
| 29 | + |
| 30 | +# a redirect can only be created on an existing zone |
| 31 | +zone = api.createZone("example.com", nx_ttl=3600) |
| 32 | + |
| 33 | +# the simplest redirect, https://domain/path -> target, will have https_enabled=True |
| 34 | +# so it will also create a certificate for the domain |
| 35 | +redirect_https = redirects.create( |
| 36 | + domain="source.domain.example.com", |
| 37 | + path="/path", |
| 38 | + target="https://www.ibm.com/products/ns1-connect" |
| 39 | +) |
| 40 | + |
| 41 | +# an http redirect, http://domain/path -> target, will not hold any certificate for the domain |
| 42 | +redirect_http = redirects.create( |
| 43 | + domain="source.domain.example.com", |
| 44 | + path="/all/*", |
| 45 | + target="http://httpforever.com/", |
| 46 | + https_enabled=False, |
| 47 | +) |
| 48 | + |
| 49 | +# requesting the certificate manually so that we can use a wildcard; |
| 50 | +# note that this wildcard does not include *.domain.example.com, the previous domain |
| 51 | +certificate_wildcard = certificates.create("*.example.com") |
| 52 | +redirect_allsettings = redirects.create( |
| 53 | + certificate_id=certificate_wildcard["id"], |
| 54 | + domain="files.example.com", |
| 55 | + path="*.rpm", |
| 56 | + target="https://rpmfind.net/", |
| 57 | + https_enabled=True, |
| 58 | + https_forced=True, |
| 59 | + query_forwarding=False, |
| 60 | + forwarding_mode="all", |
| 61 | + forwarding_type="permanent", |
| 62 | + tags=["test","me"], |
| 63 | +) |
| 64 | + |
| 65 | +########## |
| 66 | +# SEARCH # |
| 67 | +########## |
| 68 | + |
| 69 | +# search; we can also use list() to get all redirects |
| 70 | +reds = redirects.searchSource('example.com') |
| 71 | +print (reds['total'], len(reds['results'])) |
| 72 | + |
| 73 | +certs = certificates.search('example.com') |
| 74 | +print (certs['total'], len(certs['results'])) |
| 75 | + |
| 76 | +################# |
| 77 | +# READ / UPDATE # |
| 78 | +################# |
| 79 | + |
| 80 | +# read |
| 81 | +redirect_tmp = redirects.retrieve(redirect_allsettings["id"]) |
| 82 | +print(redirect_tmp) |
| 83 | + |
| 84 | +# update |
| 85 | +redirect_tmp = redirects.update( |
| 86 | + redirect_tmp, |
| 87 | + forwarding_type="temporary", |
| 88 | +) |
| 89 | +print(redirect_tmp) |
| 90 | + |
| 91 | +########## |
| 92 | +# DELETE # |
| 93 | +########## |
| 94 | + |
| 95 | +# delete redirects |
| 96 | +redirects.delete(redirect_https['id']) |
| 97 | +redirects.delete(redirect_http['id']) |
| 98 | +redirects.delete(redirect_allsettings['id']) |
| 99 | + |
| 100 | +# also revoke certificate; |
| 101 | +# note that the domain in redirect_http is the same so the certificate is also the same |
| 102 | +certificates.delete(redirect_https["certificate_id"]) |
| 103 | +certificates.delete(redirect_allsettings["certificate_id"]) |
| 104 | + |
| 105 | +api.zones().delete('example.com') |
0 commit comments