sqoop.links.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 links = (function($) {
  17. var LinkModel = koify.Model.extend({
  18. 'id': -1,
  19. 'name': null,
  20. 'link_config_values': [],
  21. 'connector_id': 0,
  22. 'creation_date': null,
  23. 'creation_user': null,
  24. 'update_date': null,
  25. 'update_user': null,
  26. 'initialize': function(attrs) {
  27. var self = this;
  28. var _attrs = $.extend(true, {}, attrs);
  29. _attrs = transform_keys(_attrs, {
  30. 'connector-id': 'connector_id',
  31. 'link-config-values': 'link_config_values'
  32. });
  33. _attrs = transform_values(_attrs, {
  34. 'link_config_values': to_configs
  35. });
  36. return _attrs;
  37. }
  38. });
  39. var Link = koify.Node.extend({
  40. 'identifier': 'link',
  41. 'persists': true,
  42. 'model_class': LinkModel,
  43. 'base_url': '/sqoop/api/links/',
  44. 'initialize': function(options) {
  45. var self = this;
  46. self.parent.initialize.apply(self, arguments);
  47. self.selected = ko.observable();
  48. self.connectors = ko.observableArray();
  49. self.persisted = ko.computed(function() {
  50. return self.id() > -1;
  51. });
  52. self.connector = ko.computed(function() {
  53. var connector = null;
  54. $.each(self.connectors(), function(index, $connector) {
  55. if ($connector.id() == self.connector_id()) {
  56. connector = $connector;
  57. }
  58. });
  59. return connector;
  60. });
  61. self.isHdfs = ko.computed(function() {
  62. if (!self.connector()) {
  63. return false;
  64. }
  65. return self.connector().name() == connectors.CONNECTOR_NAMES[0];
  66. });
  67. self.isRdbms = ko.computed(function() {
  68. if (!self.connector()) {
  69. return false;
  70. }
  71. return self.connector().name() == connectors.CONNECTOR_NAMES[1];
  72. });
  73. self.hdfsUri = ko.computed(function() {
  74. var hdfs_uri = null;
  75. $.each(self.link_config_values(), function(index, config) {
  76. if (config.name() == 'linkConfig') {
  77. $.each(config.inputs(), function(index, input) {
  78. if (input.name() == 'linkConfig.uri') {
  79. hdfs_uri = input.value();
  80. }
  81. });
  82. }
  83. });
  84. return hdfs_uri;
  85. });
  86. self.connectionString = ko.computed(function() {
  87. var link_string = null;
  88. $.each(self.link_config_values(), function(index, config) {
  89. if (config.name() == 'linkConfig') {
  90. $.each(config.inputs(), function(index, input) {
  91. if (input.name() == 'linkConfig.connectionString') {
  92. link_string = input.value();
  93. }
  94. });
  95. }
  96. });
  97. return link_string;
  98. });
  99. self.jdbcDriver = ko.computed(function() {
  100. var jdbc_driver = null;
  101. $.each(self.link_config_values(), function(index, config) {
  102. if (config.name() == 'linkConfig') {
  103. $.each(config.inputs(), function(index, input) {
  104. if (input.name() == 'linkConfig.jdbcDriver') {
  105. jdbc_driver = input.value();
  106. }
  107. });
  108. }
  109. });
  110. return jdbc_driver;
  111. });
  112. self.host = ko.computed(function() {
  113. var pattern = null;
  114. switch (self.jdbcDriver()) {
  115. case 'com.mysql.jdbc.Driver':
  116. pattern = /jdbc:mysql:\/\/([^\:\/]+).*/;
  117. break;
  118. case 'org.postgresql.Driver':
  119. pattern = /jdbc:postgresql:\/\/([^\:\/]+).*/;
  120. break;
  121. case 'oracle.jdbc.OracleDriver':
  122. pattern = /jdbc:oracle:thin:@([^\:\/]+).*/;
  123. break;
  124. }
  125. if (pattern) {
  126. var res = self.connectionString().match(pattern);
  127. if (res) {
  128. return res[1];
  129. } else {
  130. return null;
  131. }
  132. }
  133. });
  134. self.port = ko.computed(function() {
  135. var pattern = null;
  136. switch (self.jdbcDriver()) {
  137. case 'com.mysql.jdbc.Driver':
  138. pattern = /jdbc:mysql:\/\/[^\:\/]+:(\d+)\/.*/;
  139. break;
  140. case 'org.postgresql.Driver':
  141. pattern = /jdbc:postgresql:\/\/[^\:\/]+:(\d+)\/.*/;
  142. break;
  143. case 'oracle.jdbc.OracleDriver':
  144. pattern = /jdbc:oracle:thin:@[^\:\/]+:(\d+):.*/;
  145. break;
  146. }
  147. if (pattern) {
  148. var res = self.connectionString().match(pattern);
  149. if (res) {
  150. return res[1];
  151. } else {
  152. return null;
  153. }
  154. }
  155. });
  156. self.hostAndPort = ko.computed(function() {
  157. if (self.isRdbms()) {
  158. if (self.host()) {
  159. if (self.port()) {
  160. return self.host() + ":" + self.port();
  161. } else {
  162. return self.host();
  163. }
  164. } else {
  165. return null;
  166. }
  167. } else if (self.isHdfs()) {
  168. return self.hdfsUri();
  169. } else {
  170. return null;
  171. }
  172. });
  173. self.database = ko.computed(function() {
  174. var pattern = null;
  175. switch (self.jdbcDriver()) {
  176. case 'com.mysql.jdbc.Driver':
  177. pattern = /jdbc:mysql:\/\/.*?\/(.*)/;
  178. break;
  179. case 'org.postgresql.Driver':
  180. pattern = /jdbc:postgresql:\/\/.*?\/(.*)/;
  181. break;
  182. case 'oracle.jdbc.OracleDriver':
  183. pattern = /jdbc:oracle:thin:@.*?:.*?:(.*)/;
  184. break;
  185. }
  186. if (pattern) {
  187. var res = self.connectionString().match(pattern);
  188. if (res) {
  189. return res[1];
  190. } else {
  191. return null;
  192. }
  193. }
  194. });
  195. self.username = ko.computed(function() {
  196. var username = null;
  197. $.each(self.link_config_values(), function(index, config) {
  198. if (config.name() == 'linkConfig') {
  199. $.each(config.inputs(), function(index, input) {
  200. if (input.name() == 'linkConfig.username') {
  201. username = input.value();
  202. }
  203. });
  204. }
  205. });
  206. return username;
  207. });
  208. self.password = ko.computed(function() {
  209. var password = null;
  210. $.each(self.link_config_values(), function(index, config) {
  211. if (config.name() == 'linkConfig') {
  212. $.each(config.inputs(), function(index, input) {
  213. if (input.name() == 'linkConfig.password') {
  214. password = input.value();
  215. }
  216. });
  217. }
  218. });
  219. return password;
  220. });
  221. self.type = ko.computed(function() {
  222. if (self.isRdbms()) {
  223. var conn_string = self.connectionString();
  224. if (!conn_string) {
  225. return "unknown";
  226. }
  227. var parts = conn_string.split(':');
  228. if (parts.length < 2) {
  229. return "unknown";
  230. }
  231. return parts[1];
  232. } else if (self.isHdfs()) {
  233. return "HDFS"
  234. } else {
  235. return "unknown";
  236. }
  237. });
  238. },
  239. 'map': function() {
  240. var self = this;
  241. var mapping_options = $.extend(true, {
  242. 'ignore': ['parent', 'initialize']
  243. }, configs.MapProperties);
  244. if ('__ko_mapping__' in self) {
  245. ko.mapping.fromJS(self.model, mapping_options, self);
  246. } else {
  247. var mapped = ko.mapping.fromJS(self.model, mapping_options);
  248. $.extend(self, mapped);
  249. }
  250. },
  251. 'getData': function() {
  252. var self = this;
  253. var model = ko.sqoop.fixModel(self);
  254. var data = {};
  255. model = transform_keys(model, {
  256. 'link_config_values': 'link-config-values'
  257. });
  258. data[self.identifier] = ko.utils.stringifyJson(model);
  259. return data;
  260. }
  261. });
  262. function fetch_links(options) {
  263. $(document).trigger('load.links', [options]);
  264. var request = $.extend({
  265. url: '/sqoop/api/links/',
  266. dataType: 'json',
  267. type: 'GET',
  268. success: fetcher_success('links', Link, options),
  269. error: fetcher_error('links', Link, options)
  270. }, options || {});
  271. $.ajax(request);
  272. }
  273. return {
  274. 'LinkModel': LinkModel,
  275. 'Link': Link,
  276. 'fetchLinks': fetch_links
  277. }
  278. })($);