hdfs.ko.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. return 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. }
  28. function printAcl(acl) {
  29. return acl.type() + ':' + acl.name() + ':' + (acl.r() ? 'r' : '-') + (acl.w() ? 'w' : '-') + (acl.x() ? 'x' : '-');
  30. }
  31. var Assist = function (vm, assist) {
  32. var self = this;
  33. self.path = ko.observable('');
  34. self.path.subscribe(function () {
  35. self.fetchPath()
  36. });
  37. self.files = ko.observableArray();
  38. self.acls = ko.observableArray();
  39. self.owner = ko.observable('');
  40. self.group = ko.observable('');
  41. self.changed = ko.computed(function() {
  42. return [1]; //$.grep(self.acls(), function(acl){ return acl.changed(); });
  43. });
  44. self.addAcl = function() {
  45. var newAcl = parseAcl('group::---');
  46. newAcl.status('new');
  47. self.acls.push(newAcl);
  48. };
  49. self.removeAcl = function(acl) {
  50. if (acl.status() == 'new') {
  51. self.acls.remove(acl);
  52. } else {
  53. acl.status('deleted');
  54. }
  55. };
  56. self.fetchPath = function () {
  57. $.getJSON('/filebrowser/view' + self.path() + "?pagesize=15&format=json", function (data) { // Might need to create a cleaner API by calling directly webhdfs#LISTDIR
  58. if (data['files'] && data['files'][0]['type'] == 'dir') { // Hack for now
  59. self.files.removeAll();
  60. $.each(data.files, function(index, item) {
  61. self.files.push(item.path);
  62. });
  63. }
  64. self.getAcls();
  65. }).fail(function (xhr, textStatus, errorThrown) {
  66. $(document).trigger("error", xhr.responseText);
  67. });
  68. };
  69. self.getAcls = function () {
  70. $.getJSON('/security/api/hdfs/get_acls', {
  71. 'path': self.path()
  72. }, function (data) {
  73. self.acls.removeAll();
  74. $.each(data.entries, function(index, item) {
  75. self.acls.push(parseAcl(item));
  76. });
  77. self.owner(data.owner);
  78. self.group(data.group);
  79. }).fail(function (xhr, textStatus, errorThrown) {
  80. if (xhr.responseText.search('FileNotFoundException') == -1) { // TODO only fetch on existing path
  81. $(document).trigger("error", xhr.responseText);
  82. }
  83. });
  84. };
  85. self.updateAcls = function () {
  86. var aclSpec = []
  87. $.each(self.acls(), function (index, acl) {
  88. aclSpec.push(printAcl(acl));
  89. });
  90. $.ajax({
  91. type: "POST",
  92. url: "/security/api/hdfs/remove_acl_entries",
  93. data: {
  94. 'path': self.path(),
  95. 'aclspec': $.grep(self.acls(), function(acl){ return acl.status() == 'deleted'; }).join()
  96. },
  97. async: false
  98. }).fail(function (xhr, textStatus, errorThrown) {
  99. $(document).trigger("error", xhr.responseText);
  100. });
  101. $.getJSON('/security/api/hdfs/modify_acl_entries', {
  102. 'path': self.path(),
  103. 'aclspec': aclSpec.join()
  104. }, function (data) {
  105. $(document).trigger("info", 'Done!');
  106. }).fail(function (xhr, textStatus, errorThrown) {
  107. $(document).trigger("error", xhr.responseText);
  108. });
  109. }
  110. }
  111. // Might rename Assist to Acls and create Assist for the tree widget?
  112. var HdfsViewModel = function (context_json) {
  113. var self = this;
  114. self.assist = new Assist(self, context_json.assist);
  115. self.assist.path('/tmp/acl');
  116. function logGA(page) {
  117. if (typeof trackOnGA == 'function') {
  118. trackOnGA('security/hdfs' + page);
  119. }
  120. }
  121. };