sqoop.connections.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. var connections = (function($) {
  17. var ConnectionModel = koify.Model.extend({
  18. 'id': -1,
  19. 'name': null,
  20. 'connector': [],
  21. 'connector_id': 0,
  22. 'creation_date': null,
  23. 'creation_user': null,
  24. 'update_date': null,
  25. 'update_user': null,
  26. 'framework': [],
  27. 'initialize': function(attrs) {
  28. var self = this;
  29. var _attrs = $.extend(true, {}, attrs);
  30. _attrs = transform_keys(_attrs, {
  31. 'connector-id': 'connector_id'
  32. });
  33. _attrs = transform_values(_attrs, {
  34. 'connector': to_forms,
  35. 'framework': to_forms
  36. });
  37. return _attrs;
  38. }
  39. });
  40. var Connection = koify.Node.extend({
  41. 'identifier': 'connection',
  42. 'persists': true,
  43. 'model_class': ConnectionModel,
  44. 'base_url': '/sqoop/api/connections/',
  45. 'initialize': function(options) {
  46. var self = this;
  47. self.parent.initialize.apply(self, arguments);
  48. self.selected = ko.observable();
  49. self.persisted = ko.computed(function() {
  50. return self.id() > -1;
  51. });
  52. self.connectionString = ko.computed(function() {
  53. var connection_string = null;
  54. $.each(self.connector(), function(index, form) {
  55. if (form.name() == 'connection') {
  56. $.each(form.inputs(), function(index, input) {
  57. if (input.name() == 'connection.connectionString') {
  58. connection_string = input.value();
  59. }
  60. });
  61. }
  62. });
  63. return connection_string;
  64. });
  65. self.jdbcDriver = ko.computed(function() {
  66. var jdbc_driver = null;
  67. $.each(self.connector(), function(index, form) {
  68. if (form.name() == 'connection') {
  69. $.each(form.inputs(), function(index, input) {
  70. if (input.name() == 'connection.jdbcDriver') {
  71. jdbc_driver = input.value();
  72. }
  73. });
  74. }
  75. });
  76. return jdbc_driver;
  77. });
  78. self.host = ko.computed(function() {
  79. var pattern = null;
  80. switch (self.jdbcDriver()) {
  81. case 'com.mysql.jdbc.Driver':
  82. pattern = /jdbc:mysql:\/\/([^\:\/]+).*/;
  83. break;
  84. case 'org.postgresql.Driver':
  85. pattern = /jdbc:postgresql:\/\/([^\:\/]+).*/;
  86. break;
  87. case 'oracle.jdbc.OracleDriver':
  88. pattern = /jdbc:oracle:thin:@([^\:\/]+).*/;
  89. break;
  90. }
  91. if (pattern) {
  92. var res = self.connectionString().match(pattern);
  93. if (res) {
  94. return res[1];
  95. } else {
  96. return null;
  97. }
  98. }
  99. });
  100. self.port = ko.computed(function() {
  101. var pattern = null;
  102. switch (self.jdbcDriver()) {
  103. case 'com.mysql.jdbc.Driver':
  104. pattern = /jdbc:mysql:\/\/[^\:\/]+:(\d+)\/.*/;
  105. break;
  106. case 'org.postgresql.Driver':
  107. pattern = /jdbc:postgresql:\/\/[^\:\/]+:(\d+)\/.*/;
  108. break;
  109. case 'oracle.jdbc.OracleDriver':
  110. pattern = /jdbc:oracle:thin:@[^\:\/]+:(\d+):.*/;
  111. break;
  112. }
  113. if (pattern) {
  114. var res = self.connectionString().match(pattern);
  115. if (res) {
  116. return res[1];
  117. } else {
  118. return null;
  119. }
  120. }
  121. });
  122. self.hostAndPort = ko.computed(function() {
  123. if (self.host()) {
  124. if (self.port()) {
  125. return self.host() + ":" + self.port();
  126. } else {
  127. return self.host();
  128. }
  129. } else {
  130. return null;
  131. }
  132. });
  133. self.database = ko.computed(function() {
  134. var pattern = null;
  135. switch (self.jdbcDriver()) {
  136. case 'com.mysql.jdbc.Driver':
  137. pattern = /jdbc:mysql:\/\/.*?\/(.*)/;
  138. break;
  139. case 'org.postgresql.Driver':
  140. pattern = /jdbc:postgresql:\/\/.*?\/(.*)/;
  141. break;
  142. case 'oracle.jdbc.OracleDriver':
  143. pattern = /jdbc:oracle:thin:@.*?:.*?:(.*)/;
  144. break;
  145. }
  146. if (pattern) {
  147. var res = self.connectionString().match(pattern);
  148. if (res) {
  149. return res[1];
  150. } else {
  151. return null;
  152. }
  153. }
  154. });
  155. self.username = ko.computed(function() {
  156. var username = null;
  157. $.each(self.connector(), function(index, form) {
  158. if (form.name() == 'connection') {
  159. $.each(form.inputs(), function(index, input) {
  160. if (input.name() == 'connection.username') {
  161. username = input.value();
  162. }
  163. });
  164. }
  165. });
  166. return username;
  167. });
  168. self.password = ko.computed(function() {
  169. var password = null;
  170. $.each(self.connector(), function(index, form) {
  171. if (form.name() == 'connection') {
  172. $.each(form.inputs(), function(index, input) {
  173. if (input.name() == 'connection.password') {
  174. password = input.value();
  175. }
  176. });
  177. }
  178. });
  179. return password;
  180. });
  181. self.type = ko.computed(function() {
  182. var conn_string = self.connectionString();
  183. if (!conn_string) {
  184. return "unknown";
  185. }
  186. var parts = conn_string.split(':');
  187. if (parts.length < 2) {
  188. return "unknown";
  189. }
  190. return parts[1];
  191. });
  192. },
  193. 'map': function() {
  194. var self = this;
  195. var mapping_options = $.extend(true, {
  196. 'ignore': ['parent', 'initialize']
  197. }, forms.MapProperties);
  198. if ('__ko_mapping__' in self) {
  199. ko.mapping.fromJS(self.model, mapping_options, self);
  200. } else {
  201. var mapped = ko.mapping.fromJS(self.model, mapping_options);
  202. $.extend(self, mapped);
  203. }
  204. },
  205. });
  206. function fetch_connections(options) {
  207. $(document).trigger('load.connections', [options]);
  208. var request = $.extend({
  209. url: '/sqoop/api/connections/',
  210. dataType: 'json',
  211. type: 'GET',
  212. success: fetcher_success('connections', Connection, options),
  213. error: fetcher_error('connections', Connection, options)
  214. }, options || {});
  215. $.ajax(request);
  216. }
  217. return {
  218. 'ConnectionModel': ConnectionModel,
  219. 'Connection': Connection,
  220. 'fetchConnections': fetch_connections
  221. }
  222. })($);