api.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const API = require('portainer_api')
  2. const { defer } = require('gefer')
  3. const originalCallApi = API.ApiClient.prototype.callApi
  4. API.ApiClient.prototype.callApi = function(path, httpMethod, pathParams,
  5. queryParams, collectionQueryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts,
  6. returnType, callback) {
  7. if (API.url) this.basePath = API.url + '/api'
  8. let deferred
  9. if (callback === undefined) {
  10. deferred = defer()
  11. callback = (err, res) => err ? deferred.reject(err) : deferred.resolve(res)
  12. }
  13. const superAgent = originalCallApi.call(this, path, httpMethod, pathParams,
  14. queryParams, collectionQueryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts,
  15. returnType, callback)
  16. return deferred && deferred.promise || superAgent
  17. }
  18. /**
  19. * @class
  20. * @param config Configuration for the API
  21. * @param config.username Username for Portainer
  22. * @param config.password Password for user
  23. * @param config.url Base url for portainer (http://myserver:9000)
  24. * @property {StacksApi} stacks
  25. * @property {EndpointsApi} endpoints
  26. */
  27. function Api({username, password, url}) {
  28. let login = null
  29. //if (!(this instanceof Api)) return new Api()
  30. const ensureLogin = () => {
  31. if (login === null) {
  32. const authApi = new API.AuthApi()
  33. API.ApiClient.instance.basePath = url + '/api'
  34. login = authApi.authenticateUser({username, password})
  35. }
  36. return login
  37. }
  38. Object.keys(providers).forEach(key => {
  39. const Class = providers[key]
  40. let instance = null
  41. const api = Object.assign({}, this[key])
  42. this[key] = api
  43. Object.keys(api).forEach(name => {
  44. const fn = api[name]
  45. api[name] = async function() {
  46. if (!instance) {
  47. instance = new Class()
  48. }
  49. const { jwt } = await ensureLogin()
  50. // The following steps must occur synchronously.
  51. API.ApiClient.instance.basePath = url + '/api'
  52. API.ApiClient.instance.authentications.jwt.apiKey = jwt
  53. const ret = await fn.apply(instance, arguments)
  54. if (ret && ret.hasOwnProperty('length')) {
  55. return Array.from(ret)
  56. }
  57. return ret
  58. }
  59. })
  60. })
  61. }
  62. const providers = {
  63. stacks: API.StacksApi,
  64. endpoints: API.EndpointsApi
  65. }
  66. /**
  67. * @typedef StacksApi
  68. * @class
  69. * @hideconstructor
  70. **/
  71. Api.prototype.stacks = {
  72. /**
  73. * @async
  74. * @memberof StacksApi#
  75. * @param {Number} type Stack deployment type. Possible values: 1 (Swarm stack) or 2 (Compose stack).
  76. * @param {String} method Stack deployment method. Possible values: file, string or repository.
  77. * @param {Number} endpointId Identifier of the endpoint that will be used to deploy the stack.
  78. * @param {module:model/StackCreateRequest} opts Stack details. Required when method equals string or repository.
  79. * @returns {module:model/Stack}
  80. */
  81. async create(type, method, endpointId, opts) { return this.stackCreate(type, method, endpointId, opts) },
  82. /**
  83. * @async
  84. * @returns {module:model/Stack[]}
  85. * @memberof StacksApi#
  86. * @param opts Filter options
  87. * @param {string} opts.filters Filters to process on the stack list. Encoded as JSON (a map[string]string). For example, {"SwarmID": "jpofkc0i9uo9wtx1zesuk649w"} will only return stacks that are part of the specified Swarm cluster. Available filters: EndpointID, SwarmID.
  88. */
  89. async list(opts) { return this.stackList(opts) }
  90. }
  91. /**
  92. * @typedef EndpointsApi
  93. * @class
  94. * @hideconstructor
  95. */
  96. Api.prototype.endpoints = {
  97. /**
  98. * @async
  99. * @returns {Promise<module:model/EndpointSubset[]>}
  100. * @memberof EndpointsApi#
  101. * */
  102. async list() { return this.endpointList() }
  103. }
  104. module.exports = Api