1
- import { Component , OnInit , TemplateRef , ViewChild } from "@angular/core" ;
1
+ import { Component , OnInit , TemplateRef } from "@angular/core" ;
2
2
import { NotificationsService } from "angular2-notifications" ;
3
3
import { BsModalRef , BsModalService } from "ngx-bootstrap" ;
4
4
import { Fabric , FabricList } from "../_model/fabric" ;
@@ -16,12 +16,8 @@ export class FabricComponent implements OnInit {
16
16
loadingMessage : string ;
17
17
selectedFabric : Fabric ;
18
18
fabric : Fabric ;
19
- editing = { } ;
20
19
fabrics : Fabric [ ] ;
21
- confirmPassword : string ;
22
- fromAddTemplate = false ;
23
20
fabricSorts : any ;
24
- @ViewChild ( 'editTemplate' ) editModal : TemplateRef < any > ;
25
21
26
22
constructor ( private backendService : BackendService , private notificationService : NotificationsService ,
27
23
private modalService : BsModalService ) {
@@ -30,21 +26,18 @@ export class FabricComponent implements OnInit {
30
26
}
31
27
32
28
ngOnInit ( ) : void {
33
- this . getFabrics ( false ) ;
29
+ this . getFabrics ( ) ;
34
30
}
35
31
36
32
onSort ( event ) {
37
33
this . backendService . prefs . fabric_sort = event . sorts ;
38
34
}
39
35
40
- getFabrics ( toBeVerified ) {
36
+ getFabrics ( ) {
41
37
this . loading = true ;
42
38
this . backendService . getFabrics ( ) . subscribe ( ( results : FabricList ) => {
43
39
this . fabrics = results . objects ;
44
40
this . rows = results . objects ;
45
- if ( toBeVerified ) {
46
- this . verifyFabric ( this . fabric ) ;
47
- }
48
41
this . loading = false ;
49
42
} , ( err ) => {
50
43
if ( err [ 'error' ] !== undefined && err [ 'error' ] [ 'error' ] !== undefined ) {
@@ -57,51 +50,44 @@ export class FabricComponent implements OnInit {
57
50
}
58
51
59
52
verifyFabric ( fabric : Fabric ) {
60
- const fromAdd = this . fromAddTemplate ;
61
- this . fromAddTemplate = false ;
53
+ const oldLoadingMessage = this . loadingMessage ;
54
+ this . loadingMessage = 'Validating fabric ' + fabric . fabric ;
55
+ this . loading = true ;
62
56
this . backendService . verifyFabric ( fabric ) . subscribe ( ( results ) => {
63
57
if ( results [ 'success' ] ) {
64
58
this . notificationService . success ( 'Success' , 'Credentials validated, fetching controllers' ) ;
65
59
this . backendService . updateFabricControllers ( fabric ) . subscribe ( ( results ) => {
66
60
if ( results [ 'success' ] ) {
67
61
this . notificationService . success ( 'Success' , 'Controllers fetched' ) ;
68
- this . getFabrics ( false ) ;
69
- this . modalRef . hide ( ) ;
62
+ this . loadingMessage = oldLoadingMessage ;
63
+ this . getFabrics ( ) ;
70
64
} else {
71
65
this . notificationService . error ( 'Error' , results [ 'error' ] ) ;
72
- if ( fromAdd ) {
73
- this . modalRef . hide ( ) ;
74
- this . openModal ( this . editModal , fabric ) ;
75
- }
66
+ this . loading = false ;
67
+ this . loadingMessage = oldLoadingMessage ;
76
68
}
77
69
} , ( err ) => {
78
70
this . notificationService . error ( 'Error' , 'Could not get controllers' ) ;
79
- if ( fromAdd ) {
80
- this . modalRef . hide ( ) ;
81
- this . openModal ( this . editModal , fabric ) ;
82
- }
71
+ this . loading = false ;
72
+ this . loadingMessage = oldLoadingMessage ;
83
73
} ) ;
84
74
} else {
85
75
this . notificationService . error ( 'Error' , results [ 'error' ] ) ;
86
- if ( fromAdd ) {
87
- this . modalRef . hide ( ) ;
88
- this . openModal ( this . editModal , fabric ) ;
89
- }
76
+ this . loading = false ;
77
+ this . loadingMessage = oldLoadingMessage ;
90
78
}
91
79
} , ( err ) => {
92
80
this . notificationService . error ( 'Error' , 'Could not verify APIC' ) ;
93
- if ( fromAdd ) {
94
- this . modalRef . hide ( ) ;
95
- this . openModal ( this . editModal , fabric ) ;
96
- }
81
+ this . loading = false ;
82
+ this . loadingMessage = oldLoadingMessage ;
97
83
} ) ;
98
84
}
99
85
100
86
deleteFabric ( ) {
101
87
this . loading = true ;
102
88
this . backendService . deleteFabric ( this . selectedFabric ) . subscribe ( ( results ) => {
103
- this . getFabrics ( false ) ;
104
- this . modalRef . hide ( ) ;
89
+ this . hideModal ( ) ;
90
+ this . getFabrics ( ) ;
105
91
} , ( err ) => {
106
92
if ( err [ 'error' ] !== undefined && err [ 'error' ] [ 'error' ] !== undefined ) {
107
93
this . notificationService . error ( err [ 'error' ] [ 'error' ] ) ;
@@ -113,32 +99,40 @@ export class FabricComponent implements OnInit {
113
99
}
114
100
115
101
public onSubmit ( ) {
116
- this . loading = true ;
117
- this . backendService . createFabric ( this . fabric ) . subscribe ( ( results ) => {
118
- this . getFabrics ( true ) ;
119
- } , ( err ) => {
120
- if ( err [ 'error' ] !== undefined && err [ 'error' ] [ 'error' ] !== undefined ) {
121
- this . notificationService . error ( err [ 'error' ] [ 'error' ] ) ;
102
+ if ( this . fabric . apic_password !== undefined && this . fabric . apic_password != '' && this . fabric . apic_password == this . fabric . password_confirm ) {
103
+ const validate = this . fabric . validate ;
104
+ if ( this . fabric . is_new ) {
105
+ this . backendService . createFabric ( this . fabric ) . subscribe ( ( results ) => {
106
+ if ( validate ) {
107
+ this . verifyFabric ( this . fabric ) ;
108
+ } else {
109
+ this . getFabrics ( ) ;
110
+ }
111
+ } , ( err ) => {
112
+ if ( err [ 'error' ] !== undefined && err [ 'error' ] [ 'error' ] !== undefined ) {
113
+ this . notificationService . error ( err [ 'error' ] [ 'error' ] ) ;
114
+ } else {
115
+ this . notificationService . error ( 'Error' , 'Could not add fabric' ) ;
116
+ }
117
+ this . loading = false ;
118
+ } ) ;
122
119
} else {
123
- this . notificationService . error ( 'Error' , 'Could not add fabric' ) ;
120
+ this . backendService . updateFabric ( this . fabric ) . subscribe ( ( results ) => {
121
+ if ( validate ) {
122
+ this . verifyFabric ( this . fabric ) ;
123
+ } else {
124
+ this . getFabrics ( ) ;
125
+ }
126
+ } , ( err ) => {
127
+ if ( err [ 'error' ] !== undefined && err [ 'error' ] [ 'error' ] !== undefined ) {
128
+ this . notificationService . error ( err [ 'error' ] [ 'error' ] ) ;
129
+ } else {
130
+ this . notificationService . error ( 'Error' , 'Could not update fabric' ) ;
131
+ }
132
+ this . loading = false ;
133
+ } ) ;
124
134
}
125
- this . loading = false ;
126
- } ) ;
127
- }
128
-
129
- public updateValue ( ) {
130
- if ( this . confirmPassword !== this . fabric . apic_password ) {
131
- this . notificationService . error ( 'Passwords do not match' ) ;
132
- return ;
133
135
}
134
- this . confirmPassword = '' ;
135
- this . backendService . updateFabric ( this . fabric ) . subscribe ( ( results ) => {
136
- this . notificationService . success ( 'Success' , 'Changes saved' ) ;
137
- this . getFabrics ( true ) ;
138
- } , ( err ) => {
139
- this . notificationService . error ( 'Error' , 'Could not update fabric' ) ;
140
- this . loading = false ;
141
- } ) ;
142
136
}
143
137
144
138
updateFilter ( event ) {
@@ -150,7 +144,6 @@ export class FabricComponent implements OnInit {
150
144
151
145
public openAddModal ( template : TemplateRef < any > ) {
152
146
this . fabric = new Fabric ( ) ;
153
- this . fromAddTemplate = true ;
154
147
this . modalRef = this . modalService . show ( template , {
155
148
animated : true ,
156
149
keyboard : true ,
@@ -162,11 +155,16 @@ export class FabricComponent implements OnInit {
162
155
163
156
public openModal ( template : TemplateRef < any > , fabric : Fabric ) {
164
157
this . selectedFabric = fabric ;
165
- if ( this . editModal === template ) {
166
- this . fabric = new Fabric (
167
- fabric . apic_cert , fabric . apic_hostname , fabric . apic_username , fabric . apic_password , fabric . controllers , fabric . fabric
168
- ) ;
169
- }
158
+ this . fabric = new Fabric (
159
+ fabric . apic_cert ,
160
+ fabric . apic_hostname ,
161
+ fabric . apic_username ,
162
+ fabric . apic_password ,
163
+ fabric . controllers ,
164
+ fabric . fabric ,
165
+ false ,
166
+ false
167
+ ) ;
170
168
this . modalRef = this . modalService . show ( template , {
171
169
animated : true ,
172
170
keyboard : true ,
0 commit comments