Skip to content

Commit b8c6cd0

Browse files
committed
Add redirect docs and examples
1 parent 0e0ac36 commit b8c6cd0

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

doc/api/modules.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ ns1 package
99
zones
1010
records
1111
rest
12+
redirect

doc/api/redirect.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
ns1.redirect
2+
=========
3+
4+
Redirect is an object representing a single redirect; RedirectCertificate represents a redirect certificate
5+
and the Redirect.retrieveCertificate() method can be used to retrieve it.
6+
7+
.. note::
8+
9+
The mandatory fields are domain, path and target, which describe a redirect in the form ``domain/path -> target``.
10+
11+
By default, unless *https_enabled* is set to False, HTTPS will be enabled on the source domain: once there is a
12+
certificate for the source domain, all redirects using it are automatically HTTPS enabled, regardless of the value
13+
of *https_enabled*.
14+
15+
The possible values for *forwarding_mode* are (see https://www.ibm.com/docs/en/ns1-connect?topic=redirects-path-query-forwarding):
16+
17+
* ``all``: the entire URL path included in incoming requests to the source URL is appended to the target URL.
18+
* ``none``: no part of the requested URL path should be appended to the target URL.
19+
* ``capture``: only the segment of the requested URL path matching the wildcard segment defined in the source URL should be appended to the target URL.
20+
21+
The possible values for *forwarding_type* are (see https://www.ibm.com/docs/en/ns1-connect?topic=redirects-configuring-url-redirect):
22+
23+
* ``permanent``: answer clients with HTTP 301 Moved Permanently.
24+
* ``temporary``: answer clients with HTTP 302 Found.
25+
* ``masking``: answer clients with HTTP 200 OK and include the target in a frame.
26+
27+
28+
.. automodule:: ns1.redirect
29+
:members:
30+
:undoc-members:
31+
:show-inheritance:
32+
33+

doc/api/rest.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ A thin layer over the NS1 REST API
4747
:members:
4848
:undoc-members:
4949
:show-inheritance:
50+
51+
.. automodule:: ns1.rest.redirect
52+
:members:
53+
:undoc-members:
54+
:show-inheritance:

examples/redirect.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)