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

Add/link api #13

Merged
merged 41 commits into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
08e1c60
add tdd test for link
Jan 11, 2017
8b4d408
Merge branch 'master' into Add/link-api
Jan 11, 2017
45949d0
link : get all link api
Jan 11, 2017
340f1f6
add createLinkForMysql test
Jan 14, 2017
ab09b7b
Merge branch 'master' into Add/link-api
Jan 14, 2017
b679941
add getLinkAllForEmpty and deleteLinkAll
Jan 14, 2017
dabf58a
add createLinkForMysql
Jan 14, 2017
588f608
add updateLinkForMysql and getLinkByLinkName
Jan 14, 2017
6317a9d
add getLinkByConnectorName
Jan 14, 2017
36dfcd0
add updateLinkDisable and updateLinkEnable
Jan 14, 2017
020d9a8
add getLinkAll
Jan 14, 2017
377fb5b
fix
Jan 14, 2017
93e09bf
fix
Jan 14, 2017
9d23272
fix
Jan 14, 2017
ac456d0
fix
Jan 14, 2017
9647134
fix for merge
Jan 14, 2017
ad3a08f
Merge branch 'master' into Add/link-api
Jan 14, 2017
a6f20f7
support mysql-for-test
Jan 14, 2017
656b441
Merge branch 'Add/mysql-in-docker-for-test' into Add/link-api
Jan 14, 2017
ef261ff
fix
Jan 14, 2017
8c863b7
Merge branch 'master' into Add/link-api
Jan 14, 2017
39b060d
remove mysql docker
Jan 14, 2017
fb2636e
test connect to localhost mysql from docker
Jan 14, 2017
f6ab838
auto code style
Jan 14, 2017
39eb252
fix and use mysql docker for test
Jan 14, 2017
4b616fe
use lodash、promise.all、foreach and fix test expect
Jan 15, 2017
efaa604
add test for create hdfs
Jan 15, 2017
73b3d98
fix code style and use map
Jan 15, 2017
e5088f2
remove tostring, not use lodash if simple , use version not v1
Jan 15, 2017
f2ca8f5
Merge branch 'master' into Add/link-api
Jan 15, 2017
074784b
Merge branch 'master' into Add/link-api
jimexist Jan 15, 2017
c4c0e22
use faker for test data and use await instead of promise in async fun…
Jan 15, 2017
c0c0955
Merge branch 'Add/link-api' of https://github.com/Madadata/hasoop int…
Jan 15, 2017
f86a7c2
use await Promise.all(a list of promise) and use linkType.mysql inste…
Jan 15, 2017
96cda19
tear up setCreateLinkRequestBody into some smaller function and use _…
Jan 15, 2017
2976264
fix : add merge
Jan 15, 2017
1919690
use two separate functions for mysql and hdfs
Jan 15, 2017
f5e2f4e
use Object.assign not merge
Jan 16, 2017
2d4b220
use transform-object-rest-spread not Object.assign
Jan 16, 2017
5d0b45a
fix : remove -p in mysql docker
Jan 16, 2017
93f360f
fix code style : import / lodash / Object
Jan 16, 2017
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
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"regenerator": true,
"moduleName": "babel-runtime"
}
]
],
["transform-export-extensions"]
]
}
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ sudo: required

services:
- docker
- mysql

language: node_js

Expand All @@ -13,8 +12,8 @@ node_js:
- "4"

before_install:
- docker run -d -p 12000:12000 jimexist/sqoop:1.99.7-2
- docker run --name test-mysql -e MYSQL_ROOT_PASSWORD=1234 -d -p 3307:3306 mysql
- docker run --name test-sqoop --link test-mysql:mysql -d -p 12000:12000 jimexist/sqoop:1.99.7-2

script:
- echo "USE mysql;\nUPDATE user SET password=PASSWORD('1234') WHERE user='root';\nFLUSH PRIVILEGES;\n" | mysql -u root
- npm run coverage
- npm run coverage
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,23 @@
"prepush": "npm run lint",
"prepublish": "npm run clean && npm run build && npm run lint",
"coverage": "nyc npm test && nyc report --reporter=text-lcov | coveralls",
"pre-test-mysql": "mysql -uroot -p1234 < test/init.sql"
"pre-test-mysql": "docker exec -i test-mysql mysql -uroot -p1234 < test/init.sql"
},
"dependencies": {
"isomorphic-fetch": "^2.2.1"
"isomorphic-fetch": "^2.2.1",
"merge": "^1.2.0"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-core": "^6.21.0",
"babel-plugin-transform-export-extensions": "^6.8.0",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-preset-latest": "^6.16.0",
"chai": "^3.5.0",
"coveralls": "^2.11.15",
"faker": "^3.1.0",
"husky": "^0.12.0",
"lodash": "^4.17.4",
"mocha": "^3.2.0",
"nyc": "^10.0.0",
"preset": "^1.0.0",
Expand Down
66 changes: 63 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@
* Created by Chyroc on 17/1/10.
*/

import { sendGetRequest } from './utils/sendRequest'
import { sendGetRequest, senPostRequest, senPutRequest, senDeleteRequest } from './sendRequest'
import { setCreateLinkRequestBody, setUpdateLinkRequestBody } from './setLinkOptions'

import url from 'url'
import path from 'path'
import querystring from 'querystring'

export const linkType = {
mysql: 'mysql',
hdfs: 'hdfs'
}

export const version = 'v1'
const versionUri = 'version'
const driverUri = 'v1/driver'
const connectorUri = 'v1/connector'
const driverUri = `${version}/driver`
const connectorUri = `${version}/connector`
const linkUri = `${version}/link`

export class Hasoop {
constructor (config) {
Expand Down Expand Up @@ -39,16 +47,19 @@ export class Hasoop {
return url.format(urlObj)
}

// version
getVersion () {
const url = this.formatUrl([versionUri])
return sendGetRequest(url)
}

// driver
getDriver () {
const url = this.formatUrl([driverUri], 'all')
return sendGetRequest(url)
}

// connector
getConnectorAll () {
const url = this.formatUrl([connectorUri], 'all')
return sendGetRequest(url)
Expand All @@ -58,4 +69,53 @@ export class Hasoop {
const url = this.formatUrl([connectorUri], connectorName)
return sendGetRequest(url)
}

// link
createLink (config) {
const body = setCreateLinkRequestBody(config)
const url = this.formatUrl([linkUri])
return senPostRequest(url, JSON.stringify(body))
}

updateLinkConfig (oldLinkName, config) {
const body = setUpdateLinkRequestBody(config)
const url = this.formatUrl([linkUri], oldLinkName)
return senPutRequest(url, JSON.stringify(body))
}

updateLinkEnable (linkName) {
const url = this.formatUrl([linkUri], linkName, 'enable')
return senPutRequest(url)
}

updateLinkDisable (linkName) {
const url = this.formatUrl([linkUri], linkName, 'disable')
return senPutRequest(url)
}

getLinkAll () {
const url = this.formatUrl([linkUri], 'all')
return sendGetRequest(url)
}

getLinkByConnectorName (connectorName) {
const url = this.formatUrl([linkUri, {'cname': connectorName}], 'all')
return sendGetRequest(url)
}

getLinkByLinkName (linkName) {
const url = this.formatUrl([linkUri], linkName)
return sendGetRequest(url)
}

deleteLink (linkName) {
const url = this.formatUrl([linkUri], linkName)
return senDeleteRequest(url)
}

async deleteLinkAll () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you need to look at Promise.all

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also if you explicitly return Promise then you do not need to use async

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async 是为了 getLinkAll ,不是后面那些delete

const data = await this.getLinkAll()
const deleteList = data['links'].map(link => this.deleteLink(link['name']))
return await Promise.all(deleteList)
}
}
File renamed without changes.
3 changes: 0 additions & 3 deletions src/setGetOptions.js

This file was deleted.

191 changes: 191 additions & 0 deletions src/setLinkOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/**
* Created by Chyroc on 17/1/11.
*/
import merge from 'merge'
import _ from 'lodash'
import { linkType } from './index'

function setCreateLinkRequestMainBody (linkName, connectorName) {
return {
'id': -1,
'name': linkName,
'connector-name': connectorName,
'enabled': true,
'creation-date': Date.now(),
'creation-user': null,
'update-user': null,
'update-date': Date.now()
}
}
function setCreateLinkRequestMysqlBody (jdbcDriver, connectionString, fetchSize, identifierEnclose, linkConfig) {
return {
'link-config-values': {
'configs': [
{
'validators': [],
'inputs': [
{
'size': 128,
'editable': 'ANY',
'validators': [],
'name': 'linkConfig.jdbcDriver',
'id': 67,
'sensitive': false,
'overrides': '',
'type': 'STRING',
'value': jdbcDriver
}, {
'size': 2000,
'editable': 'ANY',
'validators': [],
'name': 'linkConfig.connectionString',
'id': 68,
'sensitive': false,
'overrides': '',
'type': 'STRING',
'value': encodeURIComponent(connectionString)
}, {
'size': 40,
'editable': 'ANY',
'validators': [],
'name': 'linkConfig.username',
'id': 69,
'sensitive': false,
'overrides': '',
'type': 'STRING',
'value': linkConfig['username']
}, {
'size': 40,
'editable': 'ANY',
'validators': [],
'name': 'linkConfig.password',
'id': 70,
'sensitive': true,
'overrides': '',
'type': 'STRING',
'value': linkConfig['password']
}, {
'editable': 'ANY',
'validators': [],
'name': 'linkConfig.fetchSize',
'id': 71,
'sensitive': false,
'overrides': '',
'type': 'INTEGER',
'value': `${fetchSize}`
}, {
'editable': 'ANY',
'validators': [],
'name': 'linkConfig.jdbcProperties',
'id': 72,
'sensitive': false,
'overrides': '',
'type': 'MAP',
'sensitive-pattern': ''
}
],
'name': 'linkConfig',
'id': 17,
'type': 'LINK'
},
{
'validators': [],
'inputs': [
{
'size': 5,
'editable': 'ANY',
'validators': [],
'name': 'dialect.identifierEnclose',
'id': 73,
'sensitive': false,
'overrides': '',
'type': 'STRING',
'value': encodeURIComponent(identifierEnclose)
}
],
'name': 'dialect',
'id': 18,
'type': 'LINK'
}
],
'validators': []
}
}
}

function setCreateLinkRequestHdfsBody (linkConfig) {
return {
'link-config-values': {
'configs': [
{
'validators': [],
'inputs': [
{
'size': 255,
'editable': 'ANY',
'validators': [],
'name': 'linkConfig.uri',
'id': 52,
'sensitive': false,
'overrides': '',
'type': 'STRING',
'value': encodeURIComponent(linkConfig['uri'])
}, {
'size': 255,
'editable': 'ANY',
'validators': [],
'name': 'linkConfig.confDir',
'id': 53,
'sensitive': false,
'overrides': '',
'type': 'STRING'
}, {
'editable': 'ANY',
'validators': [],
'name': 'linkConfig.configOverrides',
'id': 54,
'sensitive': false,
'overrides': '',
'type': 'MAP',
// 'value': {'key': 'value'}, TODO
'sensitive-pattern': ''
}
],
'name': 'linkConfig',
'id': 13,
'type': 'LINK'
}
],
'validators': []
}
}
}

export function setCreateLinkRequestBody (linkConfig) {
if (linkConfig['linkType'] === linkType.mysql) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use two separate functions for mysql and hdfs

const fetchSize = linkConfig['fetchSize'] || 1000
const identifierEnclose = linkConfig['identifierEnclose'] || '`'
const port = linkConfig['port'] || 3306
const connectorName = 'generic-jdbc-connector'
const jdbcDriver = 'com.mysql.jdbc.Driver'
const connectionString = `jdbc:mysql://${linkConfig.host}:${port}/${linkConfig.databaseName}`
const mainBody = setCreateLinkRequestMainBody(linkConfig.linkName, connectorName)
const mysqlBody = setCreateLinkRequestMysqlBody(jdbcDriver, connectionString, fetchSize, identifierEnclose, linkConfig)
return {'links': [merge(mainBody, mysqlBody)]}
} else if (linkConfig['linkType'] === linkType.hdfs) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tear up this into two smaller functions

const connectorName = 'hdfs-connector'
const mainBody = setCreateLinkRequestMainBody(linkConfig.linkName, connectorName)
const hdfsBody = setCreateLinkRequestHdfsBody(linkConfig)
const createHdfsLinkBody = {'links': [merge(mainBody, hdfsBody)]}
if (linkConfig['hadoopConfDir']) {
_.set(createHdfsLinkBody, 'links[0].link-config-values.configs[0].inputs[1].value', encodeURIComponent(linkConfig['hadoopConfDir']))
}
return createHdfsLinkBody
} else {
throw new Error('linkType must be mysql or hdfs')
}
}

export function setUpdateLinkRequestBody (linkConfig) {
return setCreateLinkRequestBody(linkConfig)
}
3 changes: 1 addition & 2 deletions test/authorization.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-env mocha */
import { Hasoop } from '../src/index'

const sqoopClient = new Hasoop('')
import { sqoopClient } from './index'

suite('authorization', () => {
test.skip('createRole', () => {
Expand Down
6 changes: 4 additions & 2 deletions test/connector.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
/* eslint-env mocha */

import { expect } from 'chai'
import _ from 'lodash'
import { sqoopClient } from './index'

suite('connector', () => {
test('getConnectorAll', async () => {
const data = await sqoopClient.getConnectorAll()
expect(data['connectors'].length).to.equal(7)
expect(data.connectors.length).to.equal(7)
})

test('getConnectorByConnectorName', async () => {
const connectorName = 'generic-jdbc-connector'
const data = await sqoopClient.getConnectorByConnectorName(connectorName)
expect(data['connectors'][0]['name']).to.equal('generic-jdbc-connector')
expect(_.get(data, 'connectors[0].name')).to.equal('generic-jdbc-connector')
})
})
2 changes: 1 addition & 1 deletion test/driver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { sqoopClient } from './index'
suite('driver', () => {
test('getDriver', async () => {
const data = await sqoopClient.getDriver()
expect(data['version']).to.equal('1')
expect(data.version).to.equal('1')
expect(data['all-config-resources']['jarConfig.label']).to.equal('Classpath configuration')
})
})
Loading