hdfs.ko.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. m = acl.match(/(.*?):(.*?):(.)(.)(.)/);
  18. return ko.mapping.fromJS({
  19. 'type': m[1],
  20. 'name': m[2],
  21. 'r': m[3],
  22. 'w': m[4],
  23. 'x': m[5],
  24. 'changed': false,
  25. });
  26. }
  27. function printAcl(acl) {
  28. // ^(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})?)*$
  29. return acl.type() + ':' + acl.name() + ':' + acl.r() + acl.w() + acl.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.changed(true);
  47. self.acls.push(newAcl);
  48. };
  49. self.removeAcl = function(acl) {
  50. self.acls.remove(acl);
  51. };
  52. self.fetchPath = function () {
  53. $.getJSON('/filebrowser/view' + self.path() + "?pagesize=15&format=json", function (data) { // Might need to create a cleaner API by calling directly webhdfs#LISTDIR
  54. if (data['files'] && data['files'][0]['type'] == 'dir') { // Hack for now
  55. self.files.removeAll();
  56. $.each(data.files, function(index, item) {
  57. self.files.push(item.path);
  58. });
  59. }
  60. self.getAcls();
  61. }).fail(function (xhr, textStatus, errorThrown) {
  62. $(document).trigger("error", xhr.responseText);
  63. });
  64. };
  65. self.getAcls = function () {
  66. $.getJSON('/security/api/hdfs/get_acls', {
  67. 'path': self.path()
  68. }, function (data) {
  69. self.acls.removeAll();
  70. $.each(data.entries, function(index, item) {
  71. self.acls.push(parseAcl(item));
  72. });
  73. self.owner(data.owner);
  74. self.group(data.group);
  75. }).fail(function (xhr, textStatus, errorThrown) {
  76. if (xhr.responseText.search('FileNotFoundException') == -1) { // TODO only fetch on existing path
  77. $(document).trigger("error", xhr.responseText);
  78. }
  79. });
  80. };
  81. self.updateAcls = function () {
  82. var aclSpec = []
  83. $.each(self.acls(), function (index, acl) {
  84. aclSpec.push(printAcl(acl));
  85. });
  86. // grep modified + update them: printAcl
  87. $.getJSON('/security/api/hdfs/modify_acl_entries', {
  88. 'path': self.path(),
  89. 'aclspec': aclSpec.join()
  90. }, function (data) {
  91. $(document).trigger("info", 'Done!');
  92. }).fail(function (xhr, textStatus, errorThrown) {
  93. $(document).trigger("error", xhr.responseText);
  94. });
  95. }
  96. }
  97. // Might rename Assist to Acls and create Assist for the tree widget?
  98. var HdfsViewModel = function (context_json) {
  99. var self = this;
  100. // Models
  101. self.assist = new Assist(self, context_json.assist);
  102. self.assist.path('/tmp');
  103. function logGA(page) {
  104. if (typeof trackOnGA == 'function') {
  105. trackOnGA('security/hdfs' + page);
  106. }
  107. }
  108. };