sqoop.links.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. });
  32. _attrs = transform_values(_attrs, {
  33. 'link_config_values': to_configs
  34. });
  35. return _attrs;
  36. }
  37. });
  38. var Link = koify.Node.extend({
  39. 'identifier': 'linkConfig',
  40. 'persists': true,
  41. 'model_class': LinkModel,
  42. 'base_url': '/sqoop/api/links/',
  43. 'initialize': function(options) {
  44. var self = this;
  45. self.parent.initialize.apply(self, arguments);
  46. self.selected = ko.observable();
  47. self.persisted = ko.computed(function() {
  48. return self.id() > -1;
  49. });
  50. self.connectionString = ko.computed(function() {
  51. var link_string = null;
  52. $.each(self.link_config_values(), function(index, config) {
  53. if (config.name() == 'linkConfig') {
  54. $.each(config.inputs(), function(index, input) {
  55. if (input.name() == 'linkConfig.connectionString') {
  56. link_string = input.value();
  57. }
  58. });
  59. }
  60. });
  61. return link_string;
  62. });
  63. self.jdbcDriver = ko.computed(function() {
  64. var jdbc_driver = null;
  65. $.each(self.link_config_values(), function(index, config) {
  66. if (config.name() == 'linkConfig') {
  67. $.each(config.inputs(), function(index, input) {
  68. if (input.name() == 'linkConfig.jdbcDriver') {
  69. jdbc_driver = input.value();
  70. }
  71. });
  72. }
  73. });
  74. return jdbc_driver;
  75. });
  76. self.host = ko.computed(function() {
  77. var pattern = null;
  78. switch (self.jdbcDriver()) {
  79. case 'com.mysql.jdbc.Driver':
  80. pattern = /jdbc:mysql:\/\/([^\:\/]+).*/;
  81. break;
  82. case 'org.postgresql.Driver':
  83. pattern = /jdbc:postgresql:\/\/([^\:\/]+).*/;
  84. break;
  85. case 'oracle.jdbc.OracleDriver':
  86. pattern = /jdbc:oracle:thin:@([^\:\/]+).*/;
  87. break;
  88. }
  89. if (pattern) {
  90. var res = self.connectionString().match(pattern);
  91. if (res) {
  92. return res[1];
  93. } else {
  94. return null;
  95. }
  96. }
  97. });
  98. self.port = ko.computed(function() {
  99. var pattern = null;
  100. switch (self.jdbcDriver()) {
  101. case 'com.mysql.jdbc.Driver':
  102. pattern = /jdbc:mysql:\/\/[^\:\/]+:(\d+)\/.*/;
  103. break;
  104. case 'org.postgresql.Driver':
  105. pattern = /jdbc:postgresql:\/\/[^\:\/]+:(\d+)\/.*/;
  106. break;
  107. case 'oracle.jdbc.OracleDriver':
  108. pattern = /jdbc:oracle:thin:@[^\:\/]+:(\d+):.*/;
  109. break;
  110. }
  111. if (pattern) {
  112. var res = self.connectionString().match(pattern);
  113. if (res) {
  114. return res[1];
  115. } else {
  116. return null;
  117. }
  118. }
  119. });
  120. self.hostAndPort = ko.computed(function() {
  121. if (self.host()) {
  122. if (self.port()) {
  123. return self.host() + ":" + self.port();
  124. } else {
  125. return self.host();
  126. }
  127. } else {
  128. return null;
  129. }
  130. });
  131. self.database = ko.computed(function() {
  132. var pattern = null;
  133. switch (self.jdbcDriver()) {
  134. case 'com.mysql.jdbc.Driver':
  135. pattern = /jdbc:mysql:\/\/.*?\/(.*)/;
  136. break;
  137. case 'org.postgresql.Driver':
  138. pattern = /jdbc:postgresql:\/\/.*?\/(.*)/;
  139. break;
  140. case 'oracle.jdbc.OracleDriver':
  141. pattern = /jdbc:oracle:thin:@.*?:.*?:(.*)/;
  142. break;
  143. }
  144. if (pattern) {
  145. var res = self.connectionString().match(pattern);
  146. if (res) {
  147. return res[1];
  148. } else {
  149. return null;
  150. }
  151. }
  152. });
  153. self.username = ko.computed(function() {
  154. var username = null;
  155. $.each(self.link_config_values(), function(index, config) {
  156. if (config.name() == 'linkConfig') {
  157. $.each(config.inputs(), function(index, input) {
  158. if (input.name() == 'linkConfig.username') {
  159. username = input.value();
  160. }
  161. });
  162. }
  163. });
  164. return username;
  165. });
  166. self.password = ko.computed(function() {
  167. var password = null;
  168. $.each(self.link_config_values(), function(index, config) {
  169. if (config.name() == 'linkConfig') {
  170. $.each(config.inputs(), function(index, input) {
  171. if (input.name() == 'linkConfig.password') {
  172. password = input.value();
  173. }
  174. });
  175. }
  176. });
  177. return password;
  178. });
  179. self.type = ko.computed(function() {
  180. var conn_string = self.connectionString();
  181. if (!conn_string) {
  182. return "unknown";
  183. }
  184. var parts = conn_string.split(':');
  185. if (parts.length < 2) {
  186. return "unknown";
  187. }
  188. return parts[1];
  189. });
  190. },
  191. 'map': function() {
  192. var self = this;
  193. var mapping_options = $.extend(true, {
  194. 'ignore': ['parent', 'initialize']
  195. }, configs.MapProperties);
  196. if ('__ko_mapping__' in self) {
  197. ko.mapping.fromJS(self.model, mapping_options, self);
  198. } else {
  199. var mapped = ko.mapping.fromJS(self.model, mapping_options);
  200. $.extend(self, mapped);
  201. }
  202. }
  203. });
  204. function fetch_links(options) {
  205. $(document).trigger('load.links', [options]);
  206. var request = $.extend({
  207. url: '/sqoop/api/links/',
  208. dataType: 'json',
  209. type: 'GET',
  210. success: fetcher_success('links', Link, options),
  211. error: fetcher_error('links', Link, options)
  212. }, options || {});
  213. $.ajax(request);
  214. }
  215. return {
  216. 'LinkModel': LinkModel,
  217. 'Link': Link,
  218. 'fetchLinks': fetch_links
  219. }
  220. })($);