summaryrefslogtreecommitdiff
path: root/openstack_dashboard/static/dashboard/cloud-services/cloud-services.js
blob: 88ea08401947d6df8fe8c32d5faa6749b7dd8fb3 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 *    (c) Copyright 2015 Hewlett-Packard Development Company, L.P.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

(function () {
  'use strict';

  var fromJson = angular.fromJson,
      isArray = angular.isArray;

  angular.module('hz.dashboard')

  /**
   * @ngdoc factory
   * @name hz.dashboard:factory:cloudServices
   * @module hz.dashboard
   * @kind hash table
   * @description
   *
   * Provides a hash table contains all the cloud services so that:
   *
   * 1) Easy to inject all the services since they are injected with one dependency.
   * 2) Provides a way to look up a service by name programmatically.
   *
   * The use of this is currently limited to existing API services. Use at
   * your own risk for extensibility purposes at this time. The API will
   * be evolving in the coming release and backward compatibility is not
   * guaranteed. This also makes no guarantee that the back-end service
   * is actually enabled.
   */

  .factory('cloudServices', [
    'cinderAPI',
    'glanceAPI',
    'keystoneAPI',
    'neutronAPI',
    'novaAPI',
    'novaExtensions',
    'securityGroup',
    'serviceCatalog',
    'settingsService',

    function (cinderAPI,
              glanceAPI,
              keystoneAPI,
              neutronAPI,
              novaAPI,
              novaExtensions,
              securityGroup,
              serviceCatalog,
              settingsService) {

      return {
        cinder:           cinderAPI,
        glance:           glanceAPI,
        keystone:         keystoneAPI,
        neutron:          neutronAPI,
        nova:             novaAPI,
        novaExtensions:   novaExtensions,
        securityGroup:    securityGroup,
        serviceCatalog:   serviceCatalog,
        settingsService:  settingsService
      };
    }
  ])

  /**
   * @ngdoc factory
   * @name hz.dashboard:factory:ifFeaturesEnabled
   * @module hz.dashboard
   * @kind function
   * @description
   *
   * Check to see if all the listed features are enabled on a certain service,
   * which is described by the service name.
   *
   * This is an asynchronous operation.
   *
   * @param String serviceName The name of the service, e.g. `novaExtensions`.
   * @param Array<String> features A list of feature's names.
   * @return Promise the promise of the deferred task that gets resolved
   * when all the sub-tasks are resolved.
   */

  .factory('ifFeaturesEnabled', ['$q', 'cloudServices',
    function ($q, cloudServices) {
      return function ifFeaturesEnabled(serviceName, features) {
        // each cloudServices[serviceName].ifEnabled(feature) is an asynchronous
        // operation which returns a promise, thus requiring the use of $q.all
        // to defer.
        return $q.all(
          features.map(function (feature) {
            return cloudServices[serviceName].ifEnabled(feature);
          })
        );//return
      };//return
    }
  ])

  /**
   * @ngdoc factory
   * @name hz.dashboard:factory:createDirectiveSpec
   * @module hz.dashboard
   * @kind function
   * @description
   *
   * A normalized function that can create a directive specification object
   * based on `serviceName`.
   *
   * @param String serviceName The name of the service, e.g. `novaExtensions`.
   * @param String attrName The name of the attribute in the service.
   * @return Object a directive specification object that can be used to
   * create an angular directive.
   */

  .factory('createDirectiveSpec', ['ifFeaturesEnabled',
    function (ifFeaturesEnabled) {
      return function createDirectiveSpec(serviceName, attrName) {

        function link(scope, element, attrs, ctrl, transclude) {
          element.addClass('ng-hide');
          var features = fromJson(attrs[attrName]);
          if (isArray(features)) {
            ifFeaturesEnabled(serviceName, features).then(
              // if the feature is enabled:
              function () {
                element.removeClass('ng-hide');
              },
              // if the feature is not enabled:
              function () {
                element.remove();
              }
            );
          }
          transclude(scope, function (clone) {
            element.append(clone);
          });
        }

        return {
          link: link,
          restrict: 'E',
          transclude: true
        };
      };
    }
  ])

  /**
   * @ngdoc directive
   * @name hz.dashboard:directive:novaExtension
   * @module hz.dashboard
   * @description
   *
   * This is to enable specifying conditional UI in a declarative way.
   * Some UI components should be showing only when some certain extensions
   * are enabled on `novaExtensions` service.
   *
   * @example
   *
   ```html
    <nova-extension required-extensions='["config_drive"]'>
      <div class="checkbox customization-script-source">
        <label>
          <input type="checkbox"
                 ng-model="model.newInstanceSpec.config_drive">
          {$ ::label.configurationDrive $}
        </label>
      </div>
    </nova-extension>

    <nova-extension required-extensions='["disk_config"]'>
      <div class="form-group disk-partition">
        <label for="launch-instance-disk-partition">
          {$ ::label.diskPartition $}
        </label>
        <select class="form-control"
                id="launch-instance-disk-partition"
                ng-model="model.newInstanceSpec.disk_config"
                ng-options="option.value as option.text for option in diskConfigOptions">
        </select>
      </div>
    </nova-extension>
   ```
   */

  .directive('novaExtension', ['createDirectiveSpec',
    function (createDirectiveSpec) {
      return createDirectiveSpec('novaExtensions', 'requiredExtensions');
    }
  ])

  /**
   * @ngdoc directive
   * @name hz.dashboard:directive:settingsService
   * @module hz.dashboard
   * @description
   *
   * This is to enable specifying conditional UI in a declarative way.
   * Some UI components should be showing only when some certain settings
   * are enabled on `settingsService` service.
   *
   * @example
   *
   ```html
    <settings-service required-settings='["something"]'>
      <!-- ui code here -->
    </settings-service>
   ```
   */

  .directive('settingsService', ['createDirectiveSpec',
    function (createDirectiveSpec) {
      return createDirectiveSpec('settingsService', 'requiredSettings');
    }
  ])

;})();