Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ns1_zones table #8

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ spec:
path: "localhost:7777"
tables: ['*']
destinations: ['file']
spec:
apiKey: '1234'
---
kind: destination
spec:
Expand Down
53 changes: 53 additions & 0 deletions src/nsone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { Logger } from 'winston';

const apiPrefix = 'https://api.nsone.net/v1';

async function nsoneRequest<T extends object>(
logger: Logger,
method: string,
url: string,
apiKey: string,
): Promise<T> {
const headers = {
'X-NSONE-Key': apiKey,
};
const fullUrl = `${apiPrefix}${url}`;
logger.info('Making request to NSone', method, fullUrl);

const response = await fetch(fullUrl, { method, headers });

if (response.ok) {
return (await response.json()) as T;
}

logger.info(`Error: ${await response.text()}`);
throw new Error('Non 200 status code returned from request');
}

export type ZoneSummary = {
id: string;
ttl: number;
nx_ttl: number;
retry: number;
zone: string;
refresh: number;
expiry: number;
dns_servers: string[];
networks: number[];
network_pools: string[];
meta: Record<string, unknown>;
hostmaster: string;
};

/**
* Returns a list of all active DNS zones along with basic zone configuration details for each.
*
* @see https://ns1.com/api?docId=2184
* @see https://jsapi.apiary.io/apis/ns1api/introduction/api-simulator-tutorial.html
*/
export function getZones(
logger: Logger,
apiKey: string,
): Promise<ZoneSummary[]> {
return nsoneRequest<ZoneSummary[]>(logger, 'GET', '/zones', apiKey);
}
11 changes: 6 additions & 5 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
newUnimplementedDestination,
} from '@cloudquery/plugin-sdk-javascript/plugin/plugin';
import type {
Client,
NewClientFunction,
Plugin,
SyncOptions,
Expand Down Expand Up @@ -80,21 +81,21 @@ export const plugin = () => {
},
};

const newClient: NewClientFunction = async (
const newClient: NewClientFunction = (
logger,
spec,
{ noConnection },
) => {
): Promise<Client> => {
pluginClient.spec = parseSpec(spec);
pluginClient.client = { id: () => 'ns1' };

if (noConnection) {
pluginClient.allTables = [];
return pluginClient;
return Promise.resolve(pluginClient);
}

pluginClient.allTables = await getTables(logger, pluginClient.spec);
return pluginClient;
pluginClient.allTables = getTables(logger, pluginClient.spec);
return Promise.resolve(pluginClient);
};

pluginClient.plugin = newPlugin('NS1', version, newClient);
Expand Down
7 changes: 6 additions & 1 deletion src/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export type Spec = {

export const parseSpec = (spec: string): Spec => {
const parsed = JSON.parse(spec) as Partial<Spec>;
const { concurrency = 10_000, apiKey = '' } = parsed;
const { concurrency = 10_000, apiKey } = parsed;

if (apiKey === undefined) {
throw new Error('API key not specified.');
}

return { concurrency, apiKey };
};
85 changes: 68 additions & 17 deletions src/tables.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,101 @@
import type { Writable } from 'stream';
import { Utf8 } from '@cloudquery/plugin-sdk-javascript/arrow';
import { Int64, Utf8 } from '@cloudquery/plugin-sdk-javascript/arrow';
import { createColumn } from '@cloudquery/plugin-sdk-javascript/schema/column';
import { pathResolver } from '@cloudquery/plugin-sdk-javascript/schema/resolvers';
import { createTable } from '@cloudquery/plugin-sdk-javascript/schema/table';
import type {
Table,
TableResolver,
} from '@cloudquery/plugin-sdk-javascript/schema/table';
import { JSONType } from '@cloudquery/plugin-sdk-javascript/types/json';
import type { Logger } from 'winston';
import { getZones } from './nsone.js';
import type { Spec } from './spec.js';

export const getTables = async (
logger: Logger,
spec: Spec,
): Promise<Table[]> => {
const zonesTable = (logger: Logger, spec: Spec): Table => {
const resolver: TableResolver = async (
clientMeta: unknown,
parent: unknown,
_clientMeta: unknown,
_parent: unknown,
stream: Writable,
) => {
await new Promise((_) => setTimeout(_, 500));
const data = await getZones(logger, spec.apiKey);

const data = new Array(10).fill(undefined);
data.forEach((_, index) => {
stream.write({ id: [index], name: [`hello ${index}`] });
data.forEach((zone) => {
stream.write(zone);
});
return;
};

const table = createTable({
name: 'test',
return createTable({
name: 'ns1_zones',
description:
'Active DNS zones along with basic zone configuration details for each.',
columns: [
createColumn({
name: 'id',
type: new Utf8(),
resolver: pathResolver('id'),
}),
createColumn({
name: 'name',
name: 'ttl',
type: new Int64(),
resolver: pathResolver('ttl'),
}),
createColumn({
name: 'nx_ttl',
type: new Int64(),
resolver: pathResolver('nx_ttl'),
}),
createColumn({
name: 'retry',
type: new Int64(),
resolver: pathResolver('retry'),
}),
createColumn({
name: 'zone',
type: new Utf8(),
resolver: pathResolver('zone'),
}),
createColumn({
name: 'refresh',
type: new Int64(),
resolver: pathResolver('refresh'),
}),
createColumn({
name: 'expiry',
type: new Int64(),
resolver: pathResolver('expiry'),
}),
createColumn({
name: 'dns_servers',
type: new JSONType(),
resolver: pathResolver('dns_servers'),
}),
createColumn({
name: 'networks',
type: new JSONType(),
resolver: pathResolver('networks'),
}),
createColumn({
name: 'network_pools',
type: new JSONType(),
resolver: pathResolver('network_pools'),
}),
createColumn({
name: 'meta',
type: new JSONType(),
resolver: pathResolver('meta'),
}),
createColumn({
name: 'hostmaster',
type: new Utf8(),
resolver: pathResolver('name'),
resolver: pathResolver('hostmaster'),
}),
],
description: 'testing',
resolver,
});
};

return Promise.resolve([table]);
export const getTables = (logger: Logger, spec: Spec): Table[] => {
return [zonesTable(logger, spec)];
};