{"size": 1546, "ext": "js", "max_stars_repo_path": "examples/bulk_change.js", "max_stars_repo_name": "domeneshop/domeneshop.js", "max_stars_repo_stars_event_min_datetime": "2019-04-17T12:58:20.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-23T10:55:38.000Z", "max_issues_repo_path": "examples/bulk_change.js", "max_issues_repo_name": "domeneshop/domeneshop.js", "max_issues_repo_issues_event_min_datetime": "2020-07-27T13:02:34.000Z", "max_issues_repo_issues_event_max_datetime": "2021-01-06T11:40:31.000Z", "max_forks_repo_path": "examples/bulk_change.js", "max_forks_repo_name": "domeneshop/domeneshop.js", "max_forks_repo_forks_event_min_datetime": "2021-04-06T16:33:47.000Z", "max_forks_repo_forks_event_max_datetime": "2021-06-01T10:49:26.000Z"}
{"max_stars_count": 6, "max_issues_count": 2, "max_forks_count": 2, "avg_line_length": 31.5510204082, "max_line_length": 108, "alphanum_fraction": 0.6216041397}
"use strict";

/**
 * An example of updating all DNS records from one target to another.
 * 
 * Domeneshop AS (c) 2019
 */

const Domeneshop = require('domeneshop.js');

/**
 * Your information. 
 * See README.md for information about apiToken and apiSecret.
 * Record type is the type of record that should be changed. E.g. "A", "AAAA", "CNAME"
 * From data is what the current content of the record is. E.g. IP address.
 * To data is the new content of the record. E.g. IP address.
 */
const apiToken = "<API token>";
const apiSecret = "<API secret>";
const recordType = "<record type>";
const fromData = "<from data or IP-address>";
const toData = "<to data or IP-address>";

const api = new Domeneshop(apiToken, apiSecret);

async function update() {
    // Get all domains for your account.
    let domains = await api.getDomains();

    // Iterate over the domains.
    for (let domain of domains) {
        // Get all records
        let allRecords = await api.dns.getRecords(domain.id);
        // Get records that matches type and data.
        let records = allRecords.filter(e => {
            return e.data == fromData && e.type == recordType
        });
        // Modify records.
        for (let record of records) {
            console.log("Update " + recordType + "-record from data " + record.data + " to data " + toData);
            // Set new data.
            record.data = toData;
            // Will throw error on a fault.
            await api.dns.modifyRecord(domain.id, record.id, record);
        }
    }
}

update();