mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-28 14:50:08 +03:00
(cherry picked from commit ef648c95a3131ad5f5426d5262688bb80056487c)
This commit is contained in:
parent
637c1b5090
commit
c08c954269
@ -1,151 +0,0 @@
|
||||
/* ------------------------------------------------------------------------- *
|
||||
* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems *
|
||||
* *
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may *
|
||||
* not use this file except in compliance with the License. You may obtain *
|
||||
* a copy of the License at *
|
||||
* *
|
||||
* http://www.apache.org/licenses/LICENSE-2.0 *
|
||||
* *
|
||||
* Unless required by applicable law or agreed to in writing, software *
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, *
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
|
||||
* See the License for the specific language governing permissions and *
|
||||
* limitations under the License. *
|
||||
* ------------------------------------------------------------------------- */
|
||||
|
||||
const { sprintf } = require('sprintf-js')
|
||||
const { request: axios } = require('axios')
|
||||
const {
|
||||
defaultEmptyFunction,
|
||||
dockerUrl,
|
||||
} = require('server/utils/constants/defaults')
|
||||
|
||||
const {
|
||||
Actions: ActionsMarket,
|
||||
} = require('server/utils/constants/commands/market')
|
||||
|
||||
const {
|
||||
ok,
|
||||
internalServerError,
|
||||
notFound,
|
||||
} = require('server/utils/constants/http-codes')
|
||||
const { httpResponse } = require('server/utils/server')
|
||||
|
||||
const httpNotFoundRequest = httpResponse(notFound, '', '')
|
||||
const httpInternalError = httpResponse(internalServerError, '', '')
|
||||
|
||||
/**
|
||||
* Response http request.
|
||||
*
|
||||
* @param {object} res - http response
|
||||
* @param {Function} next - express stepper
|
||||
* @param {object} httpCode - object http code
|
||||
* @param {number} httpCode.id - http code number
|
||||
* @param {string} httpCode.message - http message
|
||||
* @param {object} [httpCode.data] - http data
|
||||
* @param {string} [httpCode.file] - file path
|
||||
*/
|
||||
const responseHttp = (res = {}, next = defaultEmptyFunction, httpCode) => {
|
||||
if (res && res.locals && res.locals.httpCode && httpCode) {
|
||||
res.locals.httpCode = httpCode
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Request tags docker.
|
||||
*
|
||||
* @param {string} url - url docker
|
||||
* @param {string[]} tags - tags
|
||||
* @param {Function} error - error callback
|
||||
*/
|
||||
const getTagsDocker = (url = '', tags = [], error = defaultEmptyFunction) => {
|
||||
axios({
|
||||
method: 'GET',
|
||||
url,
|
||||
headers: {
|
||||
'User-Agent': 'OpenNebula',
|
||||
},
|
||||
validateStatus: (status) => status,
|
||||
})
|
||||
.then(({ statusText, status, data }) => {
|
||||
if (status >= 200 && status < 400 && data) {
|
||||
return data
|
||||
} else if (data) {
|
||||
throw Error(data)
|
||||
}
|
||||
throw Error(statusText)
|
||||
})
|
||||
.then((data) => {
|
||||
const { next: dockerNext, results } = data
|
||||
if (results) {
|
||||
const result = Array.isArray(results) ? results : [results]
|
||||
result.forEach(({ name, last_updated: lastUpdated }) => {
|
||||
if (name && lastUpdated) {
|
||||
tags.push({ name, last_updated: lastUpdated })
|
||||
}
|
||||
})
|
||||
}
|
||||
if (dockerNext) {
|
||||
getTagsDocker(dockerNext, tags)
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
error(e)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Docker Hub Tags.
|
||||
*
|
||||
* @param {object} res - http response
|
||||
* @param {Function} next - express stepper
|
||||
* @param {object} params - params of http request
|
||||
* @param {number} params.id - market id
|
||||
* @param {object} userData - user data.
|
||||
* @param {string} userData.user - ONE username
|
||||
* @param {string} userData.password - ONE password
|
||||
* @param {Function} oneConnection - xmlrpc function
|
||||
*/
|
||||
const getDockerTags = (
|
||||
res = {},
|
||||
next = defaultEmptyFunction,
|
||||
params = {},
|
||||
userData = {},
|
||||
oneConnection = defaultEmptyFunction
|
||||
) => {
|
||||
const { id } = params
|
||||
const { user, password } = userData
|
||||
if (id && user && password) {
|
||||
const connect = oneConnection(user, password)
|
||||
connect(
|
||||
ActionsMarket.MARKET_INFO,
|
||||
[parseInt(id)],
|
||||
(err = {}, marketAppData = {}) => {
|
||||
if (
|
||||
marketAppData &&
|
||||
marketAppData.MARKETPLACE &&
|
||||
marketAppData.MARKETPLACE.NAME
|
||||
) {
|
||||
const tags = []
|
||||
getTagsDocker(
|
||||
sprintf(dockerUrl, marketAppData.MARKETPLACE.NAME),
|
||||
tags,
|
||||
() => responseHttp(res, next, httpInternalError)
|
||||
)
|
||||
responseHttp(res, next, httpResponse(ok, tags))
|
||||
} else {
|
||||
responseHttp(res, next, httpInternalError)
|
||||
}
|
||||
}
|
||||
)
|
||||
} else {
|
||||
responseHttp(res, next, httpNotFoundRequest)
|
||||
}
|
||||
}
|
||||
|
||||
const functionRoutes = {
|
||||
getDockerTags,
|
||||
}
|
||||
module.exports = functionRoutes
|
@ -1,26 +0,0 @@
|
||||
/* ------------------------------------------------------------------------- *
|
||||
* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems *
|
||||
* *
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may *
|
||||
* not use this file except in compliance with the License. You may obtain *
|
||||
* a copy of the License at *
|
||||
* *
|
||||
* http://www.apache.org/licenses/LICENSE-2.0 *
|
||||
* *
|
||||
* Unless required by applicable law or agreed to in writing, software *
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, *
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
|
||||
* See the License for the specific language governing permissions and *
|
||||
* limitations under the License. *
|
||||
* ------------------------------------------------------------------------- */
|
||||
|
||||
const { setApiRoutes } = require('server/utils/server')
|
||||
const { routes: marketRoutes } = require('./market')
|
||||
const { MARKET } = require('./string-routes')
|
||||
|
||||
const functionRoutes = {
|
||||
private: setApiRoutes(marketRoutes, MARKET),
|
||||
public: [],
|
||||
}
|
||||
|
||||
module.exports = functionRoutes
|
@ -1,41 +0,0 @@
|
||||
/* ------------------------------------------------------------------------- *
|
||||
* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems *
|
||||
* *
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may *
|
||||
* not use this file except in compliance with the License. You may obtain *
|
||||
* a copy of the License at *
|
||||
* *
|
||||
* http://www.apache.org/licenses/LICENSE-2.0 *
|
||||
* *
|
||||
* Unless required by applicable law or agreed to in writing, software *
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, *
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
|
||||
* See the License for the specific language governing permissions and *
|
||||
* limitations under the License. *
|
||||
* ------------------------------------------------------------------------- */
|
||||
|
||||
const {
|
||||
httpMethod,
|
||||
from: fromData,
|
||||
} = require('server/utils/constants/defaults')
|
||||
const { getDockerTags } = require('./functions')
|
||||
const { GET } = httpMethod
|
||||
|
||||
const routes = {
|
||||
[GET]: {
|
||||
dockertags: {
|
||||
action: getDockerTags,
|
||||
params: {
|
||||
id: {
|
||||
from: fromData.resource,
|
||||
name: 'id',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const authApi = {
|
||||
routes,
|
||||
}
|
||||
module.exports = authApi
|
@ -1,23 +0,0 @@
|
||||
/* ------------------------------------------------------------------------- *
|
||||
* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems *
|
||||
* *
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may *
|
||||
* not use this file except in compliance with the License. You may obtain *
|
||||
* a copy of the License at *
|
||||
* *
|
||||
* http://www.apache.org/licenses/LICENSE-2.0 *
|
||||
* *
|
||||
* Unless required by applicable law or agreed to in writing, software *
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, *
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
|
||||
* See the License for the specific language governing permissions and *
|
||||
* limitations under the License. *
|
||||
* ------------------------------------------------------------------------- */
|
||||
|
||||
const MARKET = 'market'
|
||||
|
||||
const Actions = {
|
||||
MARKET,
|
||||
}
|
||||
|
||||
module.exports = Actions
|
@ -14,24 +14,57 @@
|
||||
* limitations under the License. *
|
||||
* ------------------------------------------------------------------------- */
|
||||
|
||||
const { sprintf } = require('sprintf-js')
|
||||
const { request: axios } = require('axios')
|
||||
|
||||
const {
|
||||
defaultEmptyFunction,
|
||||
defaultCommandMarketApp,
|
||||
dockerUrl,
|
||||
} = require('server/utils/constants/defaults')
|
||||
|
||||
const {
|
||||
ok,
|
||||
internalServerError,
|
||||
badRequest,
|
||||
notFound,
|
||||
} = require('server/utils/constants/http-codes')
|
||||
const { httpResponse, executeCommand } = require('server/utils/server')
|
||||
|
||||
const {
|
||||
Actions: ActionsMarketApp,
|
||||
} = require('server/utils/constants/commands/marketapp')
|
||||
|
||||
const {
|
||||
Actions: ActionsMarket,
|
||||
} = require('server/utils/constants/commands/market')
|
||||
|
||||
const { httpResponse, executeCommand } = require('server/utils/server')
|
||||
const { getSunstoneConfig } = require('server/utils/yml')
|
||||
|
||||
const httpBadRequest = httpResponse(badRequest, '', '')
|
||||
const httpNotFoundRequest = httpResponse(notFound, '', '')
|
||||
|
||||
const appConfig = getSunstoneConfig()
|
||||
const prependCommand = appConfig.sunstone_prepend || ''
|
||||
|
||||
/**
|
||||
* Response http request.
|
||||
*
|
||||
* @param {object} res - http response
|
||||
* @param {Function} next - express stepper
|
||||
* @param {object} httpCode - object http code
|
||||
* @param {number} httpCode.id - http code number
|
||||
* @param {string} httpCode.message - http message
|
||||
* @param {object} [httpCode.data] - http data
|
||||
* @param {string} [httpCode.file] - file path
|
||||
*/
|
||||
const responseHttp = (res = {}, next = defaultEmptyFunction, httpCode) => {
|
||||
if (res && res.locals && res.locals.httpCode && httpCode) {
|
||||
res.locals.httpCode = httpCode
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports the marketplace app to the OpenNebula cloud.
|
||||
*
|
||||
@ -128,8 +161,153 @@ const importMarket = (res = {}, next = defaultEmptyFunction, params = {}) => {
|
||||
next()
|
||||
}
|
||||
|
||||
/**
|
||||
* Request tags docker.
|
||||
*
|
||||
* @param {string} url - url
|
||||
* @param {Function} success - success function
|
||||
* @param {Function} error - error function
|
||||
*/
|
||||
const getTagsDocker = (
|
||||
url = '',
|
||||
success = defaultEmptyFunction,
|
||||
error = defaultEmptyFunction
|
||||
) => {
|
||||
axios({
|
||||
method: 'GET',
|
||||
url,
|
||||
headers: {
|
||||
'User-Agent': 'OpenNebula',
|
||||
},
|
||||
validateStatus: (status) => status >= 200 && status <= 400,
|
||||
})
|
||||
.then(({ data }) => {
|
||||
success(data || '')
|
||||
})
|
||||
.catch(() => {
|
||||
error()
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Get market APP information.
|
||||
*
|
||||
* @param {Function} oneConnection - ONE connection
|
||||
* @param {number} id - ID market app
|
||||
* @param {Function} success - callback when have data
|
||||
* @param {Function} error - error callback
|
||||
*/
|
||||
const getMarketApp = (
|
||||
oneConnection = defaultEmptyFunction,
|
||||
id,
|
||||
success = defaultEmptyFunction,
|
||||
error = defaultEmptyFunction
|
||||
) => {
|
||||
oneConnection(
|
||||
ActionsMarketApp.MARKETAPP_INFO,
|
||||
[parseInt(id)],
|
||||
(err = undefined, marketApp = {}) => {
|
||||
if (err || !(marketApp && marketApp.MARKETPLACEAPP)) {
|
||||
error()
|
||||
|
||||
return
|
||||
}
|
||||
success(marketApp && marketApp.MARKETPLACEAPP)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get market information.
|
||||
*
|
||||
* @param {Function} oneConnection - ONE connection
|
||||
* @param {number} id - ID market
|
||||
* @param {Function} success - callback when have data
|
||||
* @param {Function} error - error callback
|
||||
*/
|
||||
const getMarket = (
|
||||
oneConnection = defaultEmptyFunction,
|
||||
id,
|
||||
success = defaultEmptyFunction,
|
||||
error = defaultEmptyFunction
|
||||
) => {
|
||||
oneConnection(
|
||||
ActionsMarket.MARKET_INFO,
|
||||
[parseInt(id)],
|
||||
(err = undefined, market = {}) => {
|
||||
if (err || !(market && market.MARKETPLACE)) {
|
||||
error()
|
||||
|
||||
return
|
||||
}
|
||||
success(market && market.MARKETPLACE)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Docker Hub Tags.
|
||||
*
|
||||
* @param {object} res - http response
|
||||
* @param {Function} next - express stepper
|
||||
* @param {object} params - params of http request
|
||||
* @param {number} params.id - market id
|
||||
* @param {number} [params.page] - page number
|
||||
* @param {number} [params.name] - filter name
|
||||
* @param {object} userData - user data.
|
||||
* @param {string} userData.user - ONE username
|
||||
* @param {string} userData.password - ONE password
|
||||
* @param {Function} oneConnection - xmlrpc function
|
||||
*/
|
||||
const getDockerTags = (
|
||||
res = {},
|
||||
next = defaultEmptyFunction,
|
||||
params = {},
|
||||
userData = {},
|
||||
oneConnection = defaultEmptyFunction
|
||||
) => {
|
||||
const { id, page, name } = params
|
||||
const { user, password } = userData
|
||||
if (id && user && password) {
|
||||
const connect = oneConnection(user, password)
|
||||
|
||||
const callbackNotfound = () => responseHttp(res, next, httpNotFoundRequest)
|
||||
const callbackBadRequest = () => responseHttp(res, next, httpBadRequest)
|
||||
const market = ({ MARKETPLACE_ID, NAME: MARKETAPP_NAME }) => {
|
||||
Number.isInteger(parseInt(MARKETPLACE_ID)) &&
|
||||
getMarket(
|
||||
connect,
|
||||
MARKETPLACE_ID,
|
||||
({ NAME: MARKET_NAME, MARKET_MAD }) => {
|
||||
if (MARKET_MAD !== 'dockerhub') {
|
||||
return callbackBadRequest()
|
||||
}
|
||||
|
||||
let url = sprintf(dockerUrl, MARKETAPP_NAME)
|
||||
if (page) {
|
||||
url += `&page=${page}`
|
||||
}
|
||||
if (name) {
|
||||
url += `&name=${name}`
|
||||
}
|
||||
getTagsDocker(
|
||||
url,
|
||||
(tags) => responseHttp(res, next, httpResponse(ok, tags)),
|
||||
callbackNotfound
|
||||
)
|
||||
},
|
||||
callbackNotfound
|
||||
)
|
||||
}
|
||||
getMarketApp(connect, id, market, callbackNotfound)
|
||||
} else {
|
||||
responseHttp(res, next, httpNotFoundRequest)
|
||||
}
|
||||
}
|
||||
|
||||
const functionRoutes = {
|
||||
exportApp,
|
||||
importMarket,
|
||||
getDockerTags,
|
||||
}
|
||||
module.exports = functionRoutes
|
||||
|
@ -18,8 +18,8 @@ const {
|
||||
httpMethod,
|
||||
from: fromData,
|
||||
} = require('server/utils/constants/defaults')
|
||||
const { exportApp, importMarket } = require('./functions')
|
||||
const { POST } = httpMethod
|
||||
const { exportApp, importMarket, getDockerTags } = require('./functions')
|
||||
const { POST, GET } = httpMethod
|
||||
|
||||
const routes = {
|
||||
[POST]: {
|
||||
@ -111,6 +111,25 @@ const routes = {
|
||||
},
|
||||
},
|
||||
},
|
||||
[GET]: {
|
||||
dockertags: {
|
||||
action: getDockerTags,
|
||||
params: {
|
||||
id: {
|
||||
from: fromData.resource,
|
||||
name: 'id',
|
||||
},
|
||||
page: {
|
||||
from: fromData.query,
|
||||
name: 'page',
|
||||
},
|
||||
name: {
|
||||
from: fromData.query,
|
||||
name: 'name',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const authApi = {
|
||||
|
@ -71,7 +71,6 @@ const defaults = {
|
||||
'2fa',
|
||||
'auth',
|
||||
'files',
|
||||
'market',
|
||||
'marketapp',
|
||||
'oneflow',
|
||||
'support',
|
||||
|
Loading…
x
Reference in New Issue
Block a user