Migration to AGL gerrit (update go import)
[src/xds/xds-agent.git] / webapp / src / app / pages / config / config-xds / config-xds.component.ts
1 /**
2 * @license
3 * Copyright (C) 2017-2018 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 import { Component, OnInit } from '@angular/core';
20 import { Observable } from 'rxjs/Observable';
21
22 import { XDSConfigService } from '../../../@core-xds/services/xds-config.service';
23 import { IXDServerCfg } from '../../../@core-xds/services/xdsagent.service';
24 import { AlertService, IAlert } from '../../../@core-xds/services/alert.service';
25 import { NotificationsComponent } from '../../notifications/notifications.component';
26
27 // Import RxJs required methods
28 import 'rxjs/add/operator/map';
29 import 'rxjs/add/operator/catch';
30
31 @Component({
32   selector: 'xds-config-xds',
33   styleUrls: ['./config-xds.component.scss'],
34   templateUrl: './config-xds.component.html',
35 })
36 export class ConfigXdsComponent {
37
38   // TODO: cleanup agentStatus$: Observable<IAgentStatus>;
39   applying = false;
40   xdsServerUrl = '';
41   server: IXDServerCfg = { id: '', url: 'http://localhost:8000', connRetry: 10, connected: false };
42
43   configFormChanged = false;
44
45   constructor(
46     private XdsConfigSvr: XDSConfigService,
47     private alert: AlertService,
48   ) {
49     // FIXME support multiple servers
50     this._updateServerCfg(this.XdsConfigSvr.getCurServer());
51     this.XdsConfigSvr.onCurServer().subscribe(svr => this._updateServerCfg(svr));
52   }
53
54   private _updateServerCfg(svr: IXDServerCfg) {
55     if (!svr || svr.url === '') {
56       return;
57     }
58     this.xdsServerUrl = svr.url;
59     this.server = Object.assign({}, svr);
60   }
61
62   isApplyBtnEnable(): boolean {
63     return this.xdsServerUrl !== '' && (!this.server.connected || this.configFormChanged);
64   }
65
66   onSubmit() {
67     if (!this.configFormChanged && this.server.connected) {
68       return;
69     }
70     this.configFormChanged = false;
71     this.applying = true;
72     this.server.url = this.xdsServerUrl;
73     this.XdsConfigSvr.setCurServer(this.server)
74       .subscribe(cfg => {
75         this.alert.info('XDS Server successfully connected (' + cfg.url + ')');
76         this.server = Object.assign({}, cfg);
77         this.applying = false;
78       },
79       err => {
80         this.applying = false;
81         this.alert.error(err);
82       });
83   }
84
85 }
86