summaryrefslogtreecommitdiffstats
path: root/app/common/directives/certificate.js
blob: 63dc59463eb5560ec5abb741434dce911750fe7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
window.angular && (function(angular) {
  'use strict';

  angular.module('app.common.directives').directive('certificate', [
    'APIUtils',
    function(APIUtils) {
      return {
        'restrict': 'E',
        'template': require('./certificate.html'),
        'scope': {'cert': '=', 'reload': '&'},
        'controller': [
          '$scope', 'APIUtils', 'toastService',
          function($scope, APIUtils, toastService) {
            var certificateType = 'PEM';
            $scope.replaceCertificate = function(certificate) {
              $scope.loading = true;
              if (certificate.file.name.split('.').pop() !==
                  certificateType.toLowerCase()) {
                toastService.error(
                    'Certificate must be replaced with a .pem file.');
                return;
              }
              var file =
                  document
                      .getElementById(
                          'upload_' + certificate.Description + certificate.Id)
                      .files[0];
              var reader = new FileReader();
              reader.onloadend = function(e) {
                var data = {};
                data.CertificateString = e.target.result;
                data.CertificateUri = {'@odata.id': certificate['@odata.id']};
                data.CertificateType = certificateType;
                APIUtils.replaceCertificate(data).then(
                    function(data) {
                      $scope.loading = false;
                      toastService.success(
                          certificate.Description + ' was replaced.');
                      $scope.reload();
                    },
                    function(error) {
                      console.log(error);
                      $scope.loading = false;
                      toastService.error(
                          'Unable to replace ' + certificate.Description);
                    });
              };
              reader.readAsBinaryString(file);
            };
          }
        ]
      };
    }
  ]);
})(window.angular);
OpenPOWER on IntegriCloud