hdfs.ko.js 4.5 KB

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