hdfs.ko.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. function parseAcl(acl) {
  17. // (default:)?(user|group|mask|other):[[A-Za-z_][A-Za-z0-9._-]]*:([rwx-]{3})?
  18. m = acl.match(/(default:)?(user|group|mask|other):(.*?):(.)(.)(.)/);
  19. var acl = ko.mapping.fromJS({
  20. 'isDefault': m[1] != null,
  21. 'type': m[2],
  22. 'name': m[3],
  23. 'r': m[4] != '-',
  24. 'w': m[5] != '-',
  25. 'x': m[6] != '-',
  26. 'status': '',
  27. });
  28. acl.type.subscribe(function() {
  29. acl.status('modified');
  30. });
  31. acl.name.subscribe(function() {
  32. acl.status('modified');
  33. });
  34. acl.r.subscribe(function() {
  35. acl.status('modified');
  36. });
  37. acl.w.subscribe(function() {
  38. acl.status('modified');
  39. });
  40. acl.x.subscribe(function() {
  41. acl.status('modified');
  42. });
  43. return acl;
  44. }
  45. function printAcl(acl) {
  46. return acl.type() + ':' + acl.name() + ':' + (acl.r() ? 'r' : '-') + (acl.w() ? 'w' : '-') + (acl.x() ? 'x' : '-');
  47. }
  48. var Assist = function(vm, assist) {
  49. var self = this;
  50. self.path = ko.observable('');
  51. self.path.subscribe(function () {
  52. self.fetchPath()
  53. });
  54. self.files = ko.observableArray();
  55. self.acls = ko.observableArray();
  56. self.originalAcls = ko.observableArray();
  57. self.regularAcls = ko.computed(function() {
  58. return $.grep(self.acls(), function(acl){ return ! acl.isDefault(); });
  59. });
  60. self.defaultAcls = ko.computed(function() {
  61. return $.grep(self.acls(), function(acl){ return acl.isDefault(); });
  62. });
  63. self.changedAcls = ko.computed(function() {
  64. return $.grep(self.acls(), function(acl){ return ['new', 'deleted', 'modified'].indexOf(acl.status()) != -1 });
  65. });
  66. self.owner = ko.observable('');
  67. self.group = ko.observable('');
  68. self.addAcl = function() {
  69. var newAcl = parseAcl('group::---');
  70. newAcl.status('new');
  71. self.acls.push(newAcl);
  72. };
  73. self.addDefaultAcl = function() {
  74. var newAcl = parseAcl('default:group::---');
  75. newAcl.status('new');
  76. self.acls.push(newAcl);
  77. };
  78. self.removeAcl = function(acl) {
  79. if (acl.status() == 'new') {
  80. self.acls.remove(acl);
  81. } else {
  82. acl.status('deleted');
  83. }
  84. };
  85. self.fetchPath = function() {
  86. $.getJSON('/filebrowser/view' + self.path() + "?pagesize=15&format=json", function (data) {
  87. if (data['files'] && data['files'][0]['type'] == 'dir') { // Hack for now
  88. self.files.removeAll();
  89. $.each(data.files, function(index, item) {
  90. self.files.push(ko.mapping.fromJS({
  91. 'path': item.path,
  92. 'aclBit': item.rwx.indexOf('+') != -1
  93. })
  94. );
  95. });
  96. }
  97. self.getAcls();
  98. }).fail(function (xhr, textStatus, errorThrown) {
  99. $(document).trigger("error", xhr.responseText);
  100. });
  101. };
  102. self.getAcls = function() {
  103. $(".jHueNotify").hide();
  104. logGA('get_acls');
  105. $.getJSON('/security/api/hdfs/get_acls', {
  106. 'path': self.path()
  107. }, function (data) {
  108. self.acls.removeAll();
  109. self.originalAcls.removeAll();
  110. $.each(data.entries, function(index, item) {
  111. self.acls.push(parseAcl(item));
  112. self.originalAcls.push(parseAcl(item));
  113. });
  114. self.owner(data.owner);
  115. self.group(data.group);
  116. }).fail(function (xhr, textStatus, errorThrown) {
  117. if (xhr.responseText.search('FileNotFoundException') == -1) { // TODO only fetch on existing path
  118. $(document).trigger("error", xhr.responseText);
  119. }
  120. });
  121. };
  122. self.updateAcls = function() {
  123. $(".jHueNotify").hide();
  124. logGA('updateAcls');
  125. $.post("/security/api/hdfs/update_acls", {
  126. 'path': self.path(),
  127. 'acls': ko.mapping.toJSON(self.acls()),
  128. 'originalAcls': ko.mapping.toJSON(self.originalAcls()),
  129. }, function (data) {
  130. var toDelete = []
  131. $.each(self.acls(), function(index, item) {
  132. if (item.status() == 'deleted') {
  133. toDelete.push(item);
  134. } else {
  135. item.status('');
  136. }
  137. });
  138. $.each(toDelete, function(index, item) {
  139. self.acls.remove(item);
  140. });
  141. $(document).trigger("info", 'Done!');
  142. }
  143. ).fail(function (xhr, textStatus, errorThrown) {
  144. $(document).trigger("error", JSON.parse(xhr.responseText).message);
  145. });
  146. }
  147. }
  148. // Might rename Assist to Acls and create Assist for the tree widget?
  149. var HdfsViewModel = function(context_json) {
  150. var self = this;
  151. self.assist = new Assist(self, context_json.assist);
  152. self.assist.path('/tmp/dir');
  153. };
  154. function logGA(page) {
  155. if (typeof trackOnGA == 'function') {
  156. trackOnGA('security/hdfs' + page);
  157. }
  158. }