/DnsEntriesAPI

An API for storing DNS records (IP addresses) belonging to hostnames.

Primary LanguageRubyApache License 2.0Apache-2.0

DNS Entries API

A simple API for storing DNS records (IP addresses) belonging to hostnames, developed with Ruby (v2.6.5) on Rails (v5.2.3).

This is not meant to run as a production application.

Requirements

Getting Started

Note: These commands were tested in a Linux terminal.

  1. Clone the repo.

    git clone https://github.com/ivamluz/DnsEntriesAPI.git
    cd DnsEntriesAPI
  2. Create the .env file.

    cp .env.template .env
  3. Update the .env file with the required values.

  4. Start the services. The first run may take a few minutes as Docker images are pulled/built for the first time.

    make debug
  5. Open http://localhost:3000 in a web browser. You should see the Rails welcome page

Database setup

  1. Check what container the application is running at. Run docker ps and take note of the ruby:2.6.5 container id. It should look similar to this:
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                              NAMES
734bbd53f0ca        ruby:2.6.5          "entrypoint.sh rails…"   23 seconds ago      Up 22 seconds       0.0.0.0:3000->3000/tcp                       
  1. Apply migrations:

    docker exec 734bbd53f0ca rails db:migrate
  2. Setup the test database:

    docker exec 734bbd53f0ca rails db:setup RAILS_ENV=test
    

Run the test suite

docker exec 734bbd53f0ca rails test

Testing the API

Create some records

# Note: These commands were tested in a Linux terminal.

curl -XPOST \
  --header "Content-Type: application/json" \
  --header "Accept: application/json" \
  'http://localhost:3000/dns_records/' \
  -d @- << EOF
{ 
  "dns_records": { 
    "ip": "1.1.1.1", 
    "hostnames_attributes": [
      { "hostname": "lorem.com" },
      { "hostname": "ipsum.com" },
      { "hostname": "dolor.com" },
      { "hostname": "amet.com" }
    ] 
  }
}
EOF

curl -XPOST \
  --header "Content-Type: application/json" \
  --header "Accept: application/json" \
  'http://localhost:3000/dns_records/' \
  -d @- << EOF
{ 
  "dns_records": { 
    "ip": "2.2.2.2", 
    "hostnames_attributes": [
      { "hostname": "ipsum.com" }
    ] 
  }
}
EOF

curl -XPOST \
  --header "Content-Type: application/json" \
  --header "Accept: application/json" \
  'http://localhost:3000/dns_records/' \
  -d @- << EOF
{ 
  "dns_records": { 
    "ip": "3.3.3.3", 
    "hostnames_attributes": [
      { "hostname": "ipsum.com" },
      { "hostname": "dolor.com" },
      { "hostname": "amet.com" }
    ] 
  }
}
EOF

curl -XPOST \
  --header "Content-Type: application/json" \
  --header "Accept: application/json" \
  'http://localhost:3000/dns_records/' \
  -d @- << EOF
{ 
  "dns_records": { 
    "ip": "4.4.4.4", 
    "hostnames_attributes": [
      { "hostname": "ipsum.com" },
      { "hostname": "dolor.com" },
      { "hostname": "sit.com" },
      { "hostname": "amet.com" }
    ] 
  }
}
EOF

curl -XPOST \
  --header "Content-Type: application/json" \
  --header "Accept: application/json" \
  'http://localhost:3000/dns_records/' \
  -d @- << EOF
{ 
  "dns_records": { 
    "ip": "5.5.5.5", 
    "hostnames_attributes": [
      { "hostname": "sit.com" }
    ] 
  }
}
EOF

Query the API

Request

curl 'http://localhost:3000/dns_records?included[]=ipsum.com&included[]=dolor.com&excluded[]=sit.com&page=1'

Response

{
  "total_records": 2,
  "records": [
    {
      "id": 1,
      "ip_address": "1.1.1.1"
    },
    {
      "id": 3,
      "ip_address": "3.3.3.3"
    }
  ],
  "related_hostnames": [
    {
      "hostname": "amet.com",
      "count": 2
    },
    {
      "hostname": "lorem.com",
      "count": 1
    }
  ]
}

TODOs

  • Use page argument to paginate DB results.
  • Check query performance with a bigger dataset.
  • Check if there is a better place transform the DB result into the API format. This is currently done inside the DnsRecord model.

Wrapping-up

When you're finished, stop the services.

make stop

Additional Commands

All available commands can be seen by calling make help:

$ make help

Usage:
  make <target>

Targets:
  help                 Show help
  start                Start the services
  debug                Start the services in debug mode
  sql                  Start an interactive psql session (services must be running)
  logs                 Show the service logs (services must be running)
  stop                 Stop the services
  clear-db             Clear the sandbox and development databases

Credits

The Docker Compose setup and the Makefile where inspired on Plaid Pattern app.