Skip to content

Commit

Permalink
add cli plugin (#18)
Browse files Browse the repository at this point in the history
* update cli plugin

* update dockerfile

* add run docker run command

* update ndc-test command

* wip: add start command

* wip: add start command

* Add command to run the connector from the CLI

* Fix the CLI

* update package-lock file

* wip: fix ci tests

* wip: fix ci tests try 2

* add comment

* try to fic ci error

* run the connector without any explicit command

* update connector dockerfile

* update sequence of enpoint details in the config file

* minor update to dockerfile

* add start command

* Exit with status 0 after update

* add start command to connector-metadata.yaml file + remove jaeger

* wait for config generation to complete

---------

Co-authored-by: Karthikeyan C <[email protected]>
  • Loading branch information
pranshi06 and codingkarthik authored May 31, 2024
1 parent fa0871d commit a39533c
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 93 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ jobs:

- name: Run ndc-test
working-directory: ndc-spec
run: cargo run --bin ndc-test -- replay --endpoint http://0.0.0.0:8080 --snapshots-dir ../cosmos/ndc-test-snapshots
# temporary-solution: the --no-validate-responses flag is used to avoid the errors from the changes in ndc-spec in [PR:141](https://github.com/hasura/ndc-spec/pull/141)
run: cargo run --bin ndc-test -- replay --endpoint http://0.0.0.0:8080 --snapshots-dir ../cosmos/ndc-test-snapshots --no-validate-responses
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist
.env
config.json
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node:20

COPY ./ /app
WORKDIR /app

RUN npm install
RUN npm run install-bin

RUN mkdir /etc/connector/
WORKDIR /etc/connector/

ENTRYPOINT [ "ndc-azure-cosmos" ]
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
The Azuer Cosmos DB connector allows you to query data present in your Azure Cosmos DB containers as NDC models
for use in your Hasura DDN subgraphs.

## Steps for setting up the local development
## Steps for building and running the connector

### Using docker



### Using CLI

1. Make sure NodeJS v18+ is installed.

2. Run the following command to install all the required dependencies:

```sh
npm install -g
npm i
```

3. To build the connector,
Expand All @@ -30,7 +36,8 @@ export AZURE_COSMOS_DB_NAME=<YOUR_AZURE_DB_NAME>
5. Generate the configuration required to run the connector:

```sh
ndc-azure-cosmos-config update
npm install -g
ndc-azure-cosmos update
```

This will generate a `config.json` file in the root directory, by default. The
Expand All @@ -39,15 +46,15 @@ location of this file can be configured by providing the `--output-directory` co
4. To start the connector,

```sh
npm run start serve --configuration .
npm run start serve -- --configuration .
```

The `--configuration` directory should contain the configuration file generated in the previous step.

Alternatively, to use a local Azure Cosmos emulator, start the connector with the following command,

```sh
npm run start serve --configuration connector_config_emulator.json
npm run start serve -- --configuration connector_config_emulator.json
```

The emulator can be setup by following [this](https://learn.microsoft.com/en-us/azure/cosmos-db/how-to-develop-emulator?pivots=api-nosql&tabs=windows%2Ccsharp) link.
Expand All @@ -68,13 +75,13 @@ node app.js

```sh
npm run build
npm run start serve --configuration connector_config_emulator.json
npm run start serve -- --configuration connector_config_emulator.json
```

4. Checkout to the [ndc-spec repository](https://github.com/hasura/ndc-spec) and run

```sh
cargo run --bin ndc-test -- replay --endpoint http://localhost:8080 --snapshots-dir ../ndc-azure-cosmos-connector/ndc-test-snapshots
cargo run --bin ndc-test -- replay --endpoint http://localhost:8080 --snapshots-dir ../ndc-azure-cosmos-connector/ndc-test-snapshots --no-validate-responses
```

Note:
Expand Down
3 changes: 3 additions & 0 deletions connector-definition/.hasura-connector/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.hasura-connector/
*.hml
node_modules/
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
# Use Node.js 20 as the base image
FROM node:20 as build-stage
FROM ghcr.io/hasura/ndc-azure-cosmos:v0.1.0

WORKDIR /usr/src/app

COPY package*.json ./

# Install dependencies only first
RUN npm install

RUN npm install -g typescript

COPY . .

RUN tsc

FROM node:20 as production

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY --from=build-stage /usr/src/app/dist ./dist

RUN mkdir /etc/connector
WORKDIR /app

# Define the environment variable for configuration directory with a default value, which can be overridden
ENV HASURA_CONFIGURATION_DIRECTORY=/etc/connector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ supportedEnvironmentVariables:
default: "100"
commands:
update: docker run --rm -e AZURE_COSMOS_KEY="$AZURE_COSMOS_KEY" -e AZURE_COSMOS_DB_NAME="$AZURE_COSMOS_DB_NAME" -e AZURE_COSMOS_ENDPOINT="$AZURE_COSMOS_ENDPOINT" -v "$HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH":/etc/connector ghcr.io/hasura/ndc-azure-cosmos:v0.1.0 update
start: docker run --rm -e AZURE_COSMOS_KEY="$AZURE_COSMOS_KEY" -e AZURE_COSMOS_DB_NAME="$AZURE_COSMOS_DB_NAME" -e AZURE_COSMOS_ENDPOINT="$AZURE_COSMOS_ENDPOINT" -v "$HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH":/etc/connector ghcr.io/hasura/ndc-azure-cosmos:v0.1.0 start
65 changes: 24 additions & 41 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"version": "1.0.0",
"description": "NDC connector for the Azure Cosmos database",
"bin": {
"ndc-azure-cosmos-config": "./dist/cli/index.js"
"ndc-azure-cosmos": "./dist/cli/index.js"
},
"scripts": {
"test": "mocha",
"build": "rimraf ./build && tsc",
"start": "npm run build && ts-node ./src/index.ts"
"start": "npm run build && ts-node ./src/index.ts",
"install-bin": "tsc && npm install -g",
"cli": "ts-node ./src/cli/index.ts"
},
"author": "Karthikeyan Chinnakonda",
"license": "ISC",
Expand All @@ -22,18 +24,18 @@
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.17",
"chai-as-promised": "^7.1.1",
"commander": "^12.0.0",
"commander": "^11.0.0",
"mocha": "^10.3.0",
"nodemon": "^3.1.0",
"nyc": "^15.1.0",
"rimraf": "^5.0.5"
"rimraf": "^5.0.7"
},
"dependencies": {
"@azure/cosmos": "^4.0.0",
"@azure/identity": "^4.0.1",
"dotenv": "^16.4.5",
"quicktype-core": "^23.0.104",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
"typescript": "^5.4.5"
}
}
12 changes: 10 additions & 2 deletions src/cli/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,18 @@ export async function generateConnectorConfig(outputConfigDir: string) {
const rowsToFetch = process.env["AZURE_COSMOS_NO_OF_ROWS_TO_FETCH"] ?? "100";

try {
const dbClient = constructCosmosDbClient();
const schema = await getCollectionsSchema(dbClient, parseInt(rowsToFetch));
const client = constructCosmosDbClient();
const schema = await getCollectionsSchema(client.dbClient, parseInt(rowsToFetch));
const cosmosKey = client.connectionDetails.key;
const cosmosEndpoint = client.connectionDetails.endpoint;
const cosmosDbName = client.connectionDetails.databaseName;

const response: any = {
connection: {
endpoint: cosmosEndpoint,
key: cosmosKey,
databaseName: cosmosDbName
},
schema
};

Expand Down
22 changes: 15 additions & 7 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#! /usr/bin/env node

import { Command } from "commander";
import { Command, Option } from "commander";
import * as updateCmd from "./update";
import * as startCmd from "./start";
import { createConnector } from "../connector";
import { version } from "../../package.json"
import * as sdk from "@hasura/ndc-sdk-typescript";

export const program = new Command()
.version("0.0.1")
.description("Azure Cosmos Connector CLI")
// .addCommand(initCmd.cmd) TODO: Enable when required by the CLI spec
.addCommand(updateCmd.cmd);
function main() {
const program = new Command().name("ndc-azure-cosmos").version(version);

program.parse(process.argv);
program.addCommand(updateCmd.cmd);

program.addCommand(startCmd.cmd);

program.parse(process.argv);
}

main()
Loading

0 comments on commit a39533c

Please sign in to comment.