string q = req->getvars["q"];
string sMax = req->getvars["max"];
+ string sType = req->getvars["type"];
+
int maxEnts = 100;
int ents = 0;
+ // the following types of data can be searched for using the api
+ enum class Type
+ {
+ ALL,
+ ZONE,
+ RECORD,
+ COMMENT
+ } type;
+
if (q.empty())
throw ApiException("Query q can't be blank");
if (!sMax.empty())
if (maxEnts < 1)
throw ApiException("Maximum entries must be larger than 0");
+ if (sType.empty())
+ type = Type::ALL;
+ else if (sType == "all")
+ type = Type::ALL;
+ else if (sType == "zone")
+ type = Type::ZONE;
+ else if (sType == "record")
+ type = Type::RECORD;
+ else if (sType == "comment")
+ type = Type::COMMENT;
+ else
+ throw ApiException("Type must be one of the following options: all, zone, record, comment");
+
SimpleMatch sm(q,true);
UeberBackend B;
vector<DomainInfo> domains;
for(const DomainInfo di: domains)
{
- if (ents < maxEnts && sm.match(di.zone)) {
+ if ((type == Type::ALL || type == Type::ZONE) && ents < maxEnts && sm.match(di.zone)) {
doc.push_back(Json::object {
{ "object_type", "zone" },
{ "zone_id", apiZoneNameToId(di.zone) },
zoneIdZone[di.id] = di; // populate cache
}
- if (B.searchRecords(q, maxEnts, result_rr))
+ if ((type == Type::ALL || type == Type::RECORD) && B.searchRecords(q, maxEnts, result_rr))
{
for(const DNSResourceRecord& rr: result_rr)
{
}
}
- if (B.searchComments(q, maxEnts, result_c))
+ if ((type == Type::ALL || type == Type::COMMENT) && B.searchComments(q, maxEnts, result_c))
{
for(const Comment &c: result_c)
{
u'ttl': 3600, u'type': u'SOA', u'name': name},
])
+ def test_search_rr_exact_zone_filter_type_zone(self):
+ name = unique_zone_name()
+ data_type = "zone"
+ self.create_zone(name=name, serial=22, soa_edit_api='')
+ r = self.session.get(self.url("/api/v1/servers/localhost/search-data?q=" + name.rstrip('.') + "&type=" + data_type))
+ self.assert_success_json(r)
+ print(r.json())
+ self.assertEquals(r.json(), [
+ {u'object_type': u'zone', u'name': name, u'zone_id': name},
+ ])
+
+ def test_search_rr_exact_zone_filter_type_record(self):
+ name = unique_zone_name()
+ data_type = "record"
+ self.create_zone(name=name, serial=22, soa_edit_api='')
+ r = self.session.get(self.url("/api/v1/servers/localhost/search-data?q=" + name.rstrip('.') + "&type=" + data_type))
+ self.assert_success_json(r)
+ print(r.json())
+ self.assertEquals(r.json(), [
+ {u'content': u'ns1.example.com.',
+ u'zone_id': name, u'zone': name, u'object_type': u'record', u'disabled': False,
+ u'ttl': 3600, u'type': u'NS', u'name': name},
+ {u'content': u'ns2.example.com.',
+ u'zone_id': name, u'zone': name, u'object_type': u'record', u'disabled': False,
+ u'ttl': 3600, u'type': u'NS', u'name': name},
+ {u'content': u'a.misconfigured.powerdns.server. hostmaster.'+name+' 22 10800 3600 604800 3600',
+ u'zone_id': name, u'zone': name, u'object_type': u'record', u'disabled': False,
+ u'ttl': 3600, u'type': u'SOA', u'name': name},
+ ])
+
def test_search_rr_substring(self):
name = unique_zone_name()
search = name[5:-5]