Skip to content

Commit

Permalink
Fix/get status by headers (#25)
Browse files Browse the repository at this point in the history
* hasoop return response not only body / expect with response hesders not body / add doMethod : get json if true so you can do what you want:1.if you want to get json and throw error when error occur, you can use doMethod / 2.if you want to get all data,you can use hasoop native method.

* ci sqoop host

* strict test

* fix ci test sqoop host

* create constant.js for some type /

* create constant.js for some type

* fix rename

* fix import keymirror
  • Loading branch information
Chyroc-MD authored and jimexist committed Feb 10, 2017
1 parent 7b60658 commit ea75294
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 159 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"postbuild": "npm run esdoc",
"esdoc": "esdoc",
"lint": "standard --verbose | snazzy",
"test": "npm run pre-test-mysql && SQOOPHOST=$(docker exec -i test-sqoop hostname) mocha",
"test": "npm run pre-test-mysql && SQOOP_HOST=$(docker exec -i test-sqoop hostname) mocha",
"precommit": "npm run lint",
"prepush": "npm run lint",
"prepublish": "npm run clean && npm run build && npm run lint",
Expand Down
73 changes: 73 additions & 0 deletions src/constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import keyMirror from 'keymirror'

/**
* type of all connectors, currently supporting generic (databases) and HDFS.
*/
export const connectorType = keyMirror({
generic: null,
hdfs: null
})

/**
* specific types within generic types.
*/
export const genericType = keyMirror({
mysql: null
})

/**
* type of all links, currently supporting mysql and hdfs.
*/
export const linkType = keyMirror({
mysql: null,
hdfs: null
})

/**
* action of sqoop, used in sqoop rest api querySting / params / body.
*/
export const sqoopAction = keyMirror({
start: null,
stop: null,
status: null,

all: null,
jname: null,
cname: null,

enable: null,
disable: null
})

/**
* type of all hasoop method
*/
export const hasoopMethod = keyMirror({
getVersion: null,
getDriver: null,
getConnectorAll: null,
getConnectorByConnectorName: null,
getLinkAll: null,
getLinkByConnectorName: null,
getLinkByLinkName: null,
createLink: null,
updateLinkConfig: null,
updateLinkEnable: null,
updateLinkDisable: null,
deleteLink: null,
deleteLinkAll: null,
getJobAll: null,
getJobByJobName: null,
getJobByConnectorName: null,
createJob: null,
updateJobConfig: null,
updateJobEnable: null,
updateJobDisable: null,
deleteJob: null,
deleteJobAll: null,
startJob: null,
stopJob: null,
jobStatus: null,
getSubmissionAll: null,
getSubmissionByJobName: null
})
77 changes: 25 additions & 52 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,15 @@
import url from 'url'
import _ from 'lodash'
import path from 'path'
import keyMirror from 'keymirror'
import querystring from 'querystring'

import { sendGetRequest, sendPostRequest, sendPutRequest, sendDeleteRequest } from './sendRequest'
import { setCreateLinkRequestBody, setUpdateLinkRequestBody } from './setLinkOptions'
import { setCreateJobRequestBody, setUpdateJobRequestBody } from './setJobOptions'
import { splitJobConfig } from './utils'

import { setCreateJobRequestBody, setUpdateJobRequestBody } from './setJobOptions'
import { setCreateLinkRequestBody, setUpdateLinkRequestBody } from './setLinkOptions'
import { sendGetRequest, sendPostRequest, sendPutRequest, sendDeleteRequest } from './sendRequest'
import { sqoopAction, hasoopMethod } from './constant'
export * from './constant'
export * from './utils'

/**
* type of all connectors, currently supporting generic (databases) and HDFS.
*/
export const connectorType = keyMirror({
generic: null,
hdfs: null
})

/**
* specific types within generic types.
*/
export const genericType = keyMirror({
mysql: null
})

/**
* type of all links, currently supporting mysql and hdfs.
*/
export const linkType = keyMirror({
mysql: null,
hdfs: null
})

/**
* * action of sqoop, used in sqoop rest api querySting / params / body.
*/
const sqoopAction = keyMirror({
start: null,
stop: null,
status: null,

all: null,
jname: null,
cname: null,

enable: null,
disable: null
})

export const version = 'v1'
const versionUri = 'version'
const driverUri = `${version}/driver`
Expand Down Expand Up @@ -96,6 +56,19 @@ export default class Hasoop {
return url.format(urlObj)
}

async launchRequest (methodName, ...params) {
if (!Object.keys(hasoopMethod).includes(methodName)) {
throw Error(`hasoop method ${methodName} is not supported`)
}
const res = await this[methodName](...params)
if (res.headers.get('sqoop-error-code') === '1000' && res.headers.get('sqoop-error-message') === 'OK') {
return await res.json()
} else {
const errorMessage = res.headers.get('sqoop-internal-error-message')
throw new Error(`Hasoop error of ${errorMessage}`)
}
}

/**
* get sqoop server version.
*
Expand Down Expand Up @@ -233,7 +206,7 @@ export default class Hasoop {
* @returns {*}
*/
async deleteLinkAll () {
const data = await this.getLinkAll()
const data = await this.launchRequest(hasoopMethod.getLinkAll)
const deleteList = data.links.map(link => this.deleteLink(link.name))
return await Promise.all(deleteList)
}
Expand Down Expand Up @@ -281,8 +254,8 @@ export default class Hasoop {
* @returns {*}
*/
async createJob (config) {
const fromLinkInfo = await this.getLinkByLinkName(config['fromLinkName'])
const toLinkInfo = await this.getLinkByLinkName(config['toLinkName'])
const fromLinkInfo = await this.launchRequest(hasoopMethod.getLinkByLinkName, config['fromLinkName'])
const toLinkInfo = await this.launchRequest(hasoopMethod.getLinkByLinkName, config['toLinkName'])
const body = setCreateJobRequestBody(config.jobName, config.jobConfig, fromLinkInfo, toLinkInfo)
const url = this.formatUrl([jobUri])
return sendPostRequest(url, JSON.stringify(body))
Expand All @@ -296,9 +269,9 @@ export default class Hasoop {
* @returns {*}
*/
async updateJobConfig (oldJobName, config) {
const oldJobConfig = splitJobConfig(await this.getJobByJobName(oldJobName))
const fromLinkInfo = await this.getLinkByLinkName(config['fromLinkName'])
const toLinkInfo = await this.getLinkByLinkName(config['toLinkName'])
const oldJobConfig = splitJobConfig(await this.launchRequest(hasoopMethod.getJobByJobName, oldJobName))
const fromLinkInfo = await this.launchRequest(hasoopMethod.getLinkByLinkName, config['fromLinkName'])
const toLinkInfo = await this.launchRequest(hasoopMethod.getLinkByLinkName, config['toLinkName'])
const body = setUpdateJobRequestBody(config.jobName, config.jobConfig, fromLinkInfo, toLinkInfo, oldJobConfig.topId)
const url = this.formatUrl([jobUri], oldJobName)
return sendPutRequest(url, JSON.stringify(body))
Expand Down Expand Up @@ -343,7 +316,7 @@ export default class Hasoop {
* @returns {*}
*/
async deleteJobAll () {
const data = await this.getJobAll()
const data = await this.launchRequest(hasoopMethod.getJobAll)
const deleteList = data.jobs.map(job => this.deleteJob(job.name))
return await Promise.all(deleteList)
}
Expand Down
7 changes: 1 addition & 6 deletions src/sendRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import fetch from 'isomorphic-fetch'
*/
function sendRequest (method, url, body = null) {
return fetch(url, {method: method, body: body})
.then(function (res) {
return res.json()
})
.catch(function (err) {
console.log(err)
})
.catch(err => console.log(err))
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
* Created by Chyroc on 17/1/15.
*/
import _ from 'lodash'
import { expect } from 'chai'

export function expectSqoopHeaders (res) {
expect(res.headers.get('sqoop-error-code')).to.equal('1000')
expect(res.headers.get('sqoop-error-message')).to.equal('OK')
}

/**
* @ignore
Expand Down
17 changes: 10 additions & 7 deletions test/connector.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
/* eslint-env mocha */

import { expect } from 'chai'
import _ from 'lodash'

import { sqoopClient } from './index'
import { expect } from 'chai'
import { sqoopClient, expectSqoopHeaders } from './index'

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

test('getConnectorByConnectorName', async () => {
const connectorName = 'generic-jdbc-connector'
const data = await sqoopClient.getConnectorByConnectorName(connectorName)
expect(_.get(data, 'connectors[0].name')).to.equal('generic-jdbc-connector')
const res = await sqoopClient.getConnectorByConnectorName(connectorName)
const json = await res.json()
expectSqoopHeaders(res)
expect(_.get(json, 'connectors[0].name')).to.equal('generic-jdbc-connector')
})
})
13 changes: 7 additions & 6 deletions test/driver.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint-env mocha */

import { expect } from 'chai'
import _ from 'lodash'

import { sqoopClient } from './index'
import { expect } from 'chai'
import { sqoopClient, expectSqoopHeaders } from './index'

suite('driver', () => {
test('getDriver', async () => {
const data = await sqoopClient.getDriver()
expect(data.version).to.equal('1')
expect(_.get(data, ['all-config-resources', 'jarConfig.label'])).to.equal('Classpath configuration')
const res = await sqoopClient.getDriver()
const json = await res.json()
expectSqoopHeaders(res)
expect(json.version).to.equal('1')
expect(_.get(json, ['all-config-resources', 'jarConfig.label'])).to.equal('Classpath configuration')
})
})
16 changes: 8 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
/* eslint-env mocha */

import Hasoop, { version, linkType } from '../src/index'
import Hasoop, { version, linkType, expectSqoopHeaders } from '../src/index'

const config = {
const sqoopHost = process.env.SQOOP_HOST

const sqoopClient = new Hasoop({
'userName': 'Developer',
'host': 'localhost',
'port': 12000,
'webapp': 'sqoop'
}
const sqoopClient = new Hasoop(config)

const SQOOPHOST = process.env.SQOOPHOST
})

function generateMysqlConfig (linkName) {
return {
Expand All @@ -27,7 +26,7 @@ function generateHdfsConfig (linkName) {
return {
'linkName': linkName,
'linkType': linkType.hdfs,
'uri': `hdfs://${SQOOPHOST}:9000`
'uri': `hdfs://${sqoopHost}:9000`
}
}

Expand All @@ -43,14 +42,15 @@ function generateFromMysqlToHdfsConfig (jobName, fromLinkName, toLinkName) {
// 'partitionColumn':'id' TODO

// for hdfs
'outputDirectory': `hdfs://${SQOOPHOST}:9000/data`
'outputDirectory': `hdfs://${sqoopHost}:9000/data`
}
}
}

export {
sqoopClient,
version,
expectSqoopHeaders,
generateMysqlConfig,
generateHdfsConfig,
generateFromMysqlToHdfsConfig
Expand Down
Loading

0 comments on commit ea75294

Please sign in to comment.