-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathservice-registration.directive.js
218 lines (197 loc) · 7.16 KB
/
service-registration.directive.js
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
(function() {
'use strict';
angular
.module('app.view')
.directive('serviceRegistration', serviceRegistration);
serviceRegistration.$inject = ['app.basePath'];
/**
* @namespace app.view.serviceRegistration
* @memberof app.view
* @name serviceRegistration
* @description A service-registration directive
* @param {string} path - the application base path
* @returns {object} The service-registration directive definition object
*/
function serviceRegistration(path) {
return {
bindToController: {
showOverlayRegistration: '=?'
},
controller: ServiceRegistrationController,
controllerAs: 'serviceRegistrationCtrl',
scope: {},
templateUrl: path + 'view/service-registration/service-registration.html'
};
}
ServiceRegistrationController.$inject = [
'$scope',
'app.model.modelManager',
'app.api.apiManager',
'helion.framework.widgets.detailView'
];
/**
* @namespace app.view.ServiceRegistrationController
* @memberof app.view
* @name ServiceRegistrationController
* @constructor
* @param {object} $scope - the Angular $scope service
* @param {app.model.modelManager} modelManager - the application model manager
* @param {app.api.apiManager} apiManager - the application API manager
* @param {helion.framework.widgets.detailView} detailView - detail view service
* @property {boolean} overlay - flag to show or hide this component
* @property {app.model.serviceInstance} serviceInstanceModel - the service instance model
* @property {app.model.user} userModel - the user model
* @property {array} serviceInstances - the service instances available to user
* @property {string} warningMsg - the warning message to show if expired
*/
function ServiceRegistrationController($scope, modelManager, apiManager, detailView) {
var that = this;
this.overlay = angular.isDefined(this.showOverlayRegistration);
this.clusterAddFlyoutActive = false;
this.cnsiModel = modelManager.retrieve('app.model.serviceInstance');
this.userCnsiModel = modelManager.retrieve('app.model.serviceInstance.user');
this.userModel = modelManager.retrieve('app.model.user');
this.serviceInstances = {};
this.serviceInstanceApi = apiManager.retrieve('app.api.serviceInstance');
this.credentialsFormOpen = false;
this.warningMsg = gettext('Authentication failed, please try reconnect.');
this.detailView = detailView;
this.currentEndpoints = [];
// TODO woodnt: There must be a more reproducable/general way of doing this.
this.cfModel = modelManager.retrieve('cloud-foundry.model.application');
$scope.$watchCollection(function () {
return that.cnsiModel.serviceInstances;
}, function (newCnsis) {
_.forEach(newCnsis, function (cnsi) {
var guid = cnsi.guid;
if (angular.isUndefined(that.serviceInstances[guid])) {
that.serviceInstances[guid] = cnsi;
} else {
angular.extend(that.serviceInstances[guid], cnsi);
}
});
});
$scope.$watchCollection(function() {
return that.serviceInstances;
}, function(newCnsis) {
that.currentEndpoints = _.map(newCnsis,
function(c) {
var endpoint = c.api_endpoint;
return endpoint.Scheme + '://' + endpoint.Host;
});
});
this.userCnsiModel.list().then(function() {
angular.extend(that.serviceInstances, that.userCnsiModel.serviceInstances);
that.cnsiModel.list();
});
}
angular.extend(ServiceRegistrationController.prototype, {
/**
* @function completeRegistration
* @memberOf app.view.ServiceRegistrationController
* @description Set service instances as registered
*/
completeRegistration: function () {
var that = this;
if (this.userCnsiModel.numValid > 0) {
this.userModel.updateRegistered(true)
.then(function () {
that.showOverlayRegistration = false;
});
}
},
/**
* @function connect
* @memberOf app.view.ServiceRegistrationController
* @description Connect service instance for user
* @param {object} serviceInstance - the service instance to connect
*/
connect: function (serviceInstance) {
this.activeServiceInstance = serviceInstance;
this.credentialsFormOpen = true;
},
/**
* @function disconnect
* @memberOf app.view.ServiceRegistrationController
* @description Disconnect service instance for user
* @param {object} serviceInstance - the service instance to disconnect
*/
disconnect: function (serviceInstance) {
var that = this;
// Our mocking system uses "id" but the real systems use "guid".
// This bandaid will allow the use of either.
var id = angular.isUndefined(serviceInstance.guid) ? serviceInstance.id : serviceInstance.guid;
this.userCnsiModel.disconnect(id)
.then(function success() {
delete serviceInstance.account;
delete serviceInstance.expires_at;
delete serviceInstance.valid;
that.userCnsiModel.numValid -= 1;
that.cfModel.all();
});
},
onConnectCancel: function () {
this.credentialsFormOpen = false;
},
onConnectSuccess: function () {
this.userCnsiModel.numValid += 1;
this.credentialsFormOpen = false;
this.activeServiceInstance = null;
},
remove: function (serviceInstance) {
var that = this;
this.cnsiModel.remove(serviceInstance)
.then(function success() {
that.serviceInstances = {};
that.userCnsiModel.list().then(function () {
angular.extend(that.serviceInstances, that.userCnsiModel.serviceInstances);
that.cnsiModel.list();
});
});
},
/**
* @function showClusterAddForm
* @memberOf app.view.ServiceRegistrationController
* @description Show the cluster add form flyout
*/
showClusterAddForm: function () {
this.clusterAddFlyoutActive = true;
},
/**
* @function hideClusterAddForm
* @memberOf app.view.ServiceRegistrationController
* @description Hide the cluster add form flyout
*/
hideClusterAddForm: function () {
this.clusterAddFlyoutActive = false;
},
/**
* @function showHCEEndpointAddForm
* @memberOf app.view.ServiceRegistrationController
* @description Show the HCE Endpoint add form detail view
*/
showHCEEndpointAddForm: function () {
// This code is shamelessly copied from app/view/cluster-registration/cluster-registration.directive.js
// I take that back, it was EXTREMELY SHAMEFUL.
// -- woodnt
var that = this;
var data = { name: '', url: '' };
this.detailView(
{
templateUrl: 'app/view/hce-registration/hce-registration.html',
title: gettext('Register Code Engine Endpoint')
},
{
data: data,
options: {
instances: this.currentEndpoints
}
}
).result.then(function () {
return that.serviceInstanceApi.createHCE(data.url, data.name).then(function () {
that.cnsiModel.list();
});
});
}
});
})();