Skip to content

Commit 1dfd53b

Browse files
authored
GitOps - added un/subscribe and update tokens events (#412)
1 parent c193ce4 commit 1dfd53b

File tree

4 files changed

+168
-75
lines changed

4 files changed

+168
-75
lines changed

src/app/dashboard-module/domain-module/ext-gitops/ext-gitops.component.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ <h5 class="card-title">Details</h5>
8282
<div *ngIf="reloading" class="options-spinner">
8383
<mat-spinner [diameter]="25"></mat-spinner>
8484
</div>
85-
<button *ngIf="!reloading" mat-button class="btn-element" (click)="onReload()" matTooltip="Reload current environment account">
85+
<button *ngIf="canReload()" mat-button class="btn-element" (click)="onReload()" matTooltip="Reload current environment account">
8686
<mat-icon style="font-size: large;">refresh</mat-icon>
8787
<div class="options-section-label">Reload</div>
8888
</button>
@@ -149,7 +149,11 @@ <h5 class="card-title">Settings</h5>
149149
</button>
150150
<button *ngIf="!gitOpsSelected.ID" mat-button class="btn-element" (click)="onCreate()">
151151
<div class="options-section-label">Create</div>
152-
<mat-icon style="font-size: large;">open_in_new</mat-icon>
152+
<mat-icon style="font-size: large;">save</mat-icon>
153+
</button>
154+
<button *ngIf="!gitOpsSelected.ID" mat-button class="btn-element" (click)="onDiscard()">
155+
<div class="options-section-label">Discard</div>
156+
<mat-icon style="font-size: large;">close</mat-icon>
153157
</button>
154158
<button *ngIf="gitOpsSelected.ID" mat-button class="btn-element btn-red" (click)="onUnsubscribe()">
155159
<mat-icon style="font-size: large;">cloud_off</mat-icon>

src/app/dashboard-module/domain-module/ext-gitops/ext-gitops.component.ts

Lines changed: 77 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { NgbdModalConfirmComponent } from 'src/app/_helpers/confirmation-dialog'
2121
selector: 'app-ext-gitops',
2222
templateUrl: './ext-gitops.component.html',
2323
styleUrls: [
24-
'../common/css/detail.component.css',
24+
'../common/css/detail.component.css',
2525
'./ext-gitops.component.css'
2626
]
2727
})
@@ -54,7 +54,7 @@ export class ExtGitOpsComponent implements OnInit, OnDestroy {
5454
private readonly toastService: ToastService,
5555
private readonly modalService: NgbModal,
5656
private readonly fb: FormBuilder
57-
) {
57+
) {
5858
this.activatedRoute.parent.params.subscribe(params => {
5959
this.domainId = params.domainid;
6060
this.domainName = decodeURIComponent(params.name);
@@ -83,7 +83,7 @@ export class ExtGitOpsComponent implements OnInit, OnDestroy {
8383
const dialogRef = this.dialog.open(GitOpsEnvSelectionComponent, {
8484
width: '400px',
8585
minWidth: window.innerWidth < 450 ? '95vw' : '',
86-
data: {
86+
data: {
8787
excludeEnvironments: this.gitOpsAccounts
8888
.filter(account => account.ID)
8989
.map(account => account.environment),
@@ -92,10 +92,7 @@ export class ExtGitOpsComponent implements OnInit, OnDestroy {
9292
});
9393

9494
dialogRef.afterClosed().subscribe(result => {
95-
this.gitOpsAccounts = this.gitOpsAccounts.filter(account => account.ID);
96-
if (this.gitOpsAccounts.length > 0) {
97-
this.selectAccount(this.gitOpsAccounts[0]);
98-
}
95+
this.purgePendingAccounts();
9996

10097
if (result) {
10198
const account = buildNewGitOpsAccount(result.environment, this.domainId, this.domainName);
@@ -128,16 +125,30 @@ export class ExtGitOpsComponent implements OnInit, OnDestroy {
128125
return;
129126
}
130127

131-
this.toastService.showSuccess('GitOps account created successfully');
132-
this.gitOpsSelected.ID = 'new';
133-
this.selectAccount(this.gitOpsSelected);
128+
this.gitOpsService.subscribeGitOpsAccount(this.gitOpsSelected)
129+
.pipe(takeUntil(this.unsubscribe))
130+
.subscribe({
131+
next: data => {
132+
this.selectAccount(data, false);
133+
this.gitOpsAccounts = this.gitOpsAccounts.map(account => account.environment === data.environment ? data : account);
134+
135+
this.toastService.showSuccess('GitOps account created successfully');
136+
},
137+
error: () => {
138+
this.toastService.showError('Failed to create GitOps account');
139+
}
140+
});
141+
}
142+
143+
onDiscard(): void {
144+
this.purgePendingAccounts();
134145
}
135146

136147
onUpdateTokens(): void {
137148
const dialogRef = this.dialog.open(GitOpsUpdateTokensComponent, {
138149
width: '400px',
139150
minWidth: window.innerWidth < 450 ? '95vw' : '',
140-
data: {
151+
data: {
141152
environments: this.gitOpsAccounts
142153
.filter(account => account.ID)
143154
.map(account => account.environment)
@@ -146,7 +157,20 @@ export class ExtGitOpsComponent implements OnInit, OnDestroy {
146157

147158
dialogRef.afterClosed().subscribe(result => {
148159
if (result) {
149-
this.toastService.showSuccess('GitOps account tokens updated successfully');
160+
this.gitOpsService.updateGitOpsAccountTokens(result.token, this.domainId, result.environments)
161+
.pipe(takeUntil(this.unsubscribe))
162+
.subscribe({
163+
next: data => {
164+
if (data?.result) {
165+
this.toastService.showSuccess('GitOps account tokens updated successfully');
166+
} else {
167+
this.toastService.showError('Failed to update GitOps account tokens: ' + data?.message);
168+
}
169+
},
170+
error: () => {
171+
this.toastService.showError('Failed to update GitOps account tokens');
172+
}
173+
});
150174
}
151175
});
152176
}
@@ -157,13 +181,22 @@ export class ExtGitOpsComponent implements OnInit, OnDestroy {
157181
modalConfirmation.componentInstance.question = 'Are you sure you want to delete this GitOps account?';
158182
modalConfirmation.result.then((result) => {
159183
if (result) {
160-
this.gitOpsAccounts.splice(this.gitOpsAccounts.indexOf(this.gitOpsSelected), 1);
161-
162-
if (this.gitOpsAccounts.length > 0) {
163-
this.selectAccount(this.gitOpsAccounts[0]);
164-
}
165-
166-
this.toastService.showSuccess('GitOps account unsubscribed successfully');
184+
this.gitOpsService.unsubscribeGitOpsAccount(this.gitOpsSelected)
185+
.pipe(takeUntil(this.unsubscribe))
186+
.subscribe({
187+
next: () => {
188+
this.gitOpsAccounts.splice(this.gitOpsAccounts.indexOf(this.gitOpsSelected), 1);
189+
190+
if (this.gitOpsAccounts.length > 0) {
191+
this.selectAccount(this.gitOpsAccounts[0]);
192+
}
193+
194+
this.toastService.showSuccess('GitOps account unsubscribed successfully');
195+
},
196+
error: () => {
197+
this.toastService.showError('Failed to unsubscribe GitOps account');
198+
}
199+
});
167200
}
168201
});
169202
}
@@ -174,6 +207,8 @@ export class ExtGitOpsComponent implements OnInit, OnDestroy {
174207
.subscribe({
175208
next: data => {
176209
this.selectAccount(data, false);
210+
this.gitOpsAccounts = this.gitOpsAccounts.map(account => account.environment === data.environment ? data : account);
211+
177212
this.toastService.showSuccess('GitOps account refresh command sent successfully');
178213
},
179214
error: () => {
@@ -184,16 +219,17 @@ export class ExtGitOpsComponent implements OnInit, OnDestroy {
184219

185220
onReload(): void {
186221
this.reloading = true;
187-
222+
188223
this.gitOpsService.findGitOpsAccount(this.gitOpsSelected)
189224
.pipe(takeUntil(this.unsubscribe))
190225
.subscribe({
191226
next: data => {
227+
if (data.length) {
228+
this.selectAccount(data[0], false);
229+
this.gitOpsAccounts = this.gitOpsAccounts.map(account => account.environment === data[0].environment ? data[0] : account);
230+
}
231+
192232
setTimeout(() => {
193-
if (data.length) {
194-
this.selectAccount(data[0], false);
195-
}
196-
197233
this.reloading = false;
198234
}, 5000);
199235
},
@@ -220,7 +256,11 @@ export class ExtGitOpsComponent implements OnInit, OnDestroy {
220256
}
221257

222258
canRefresh(): boolean {
223-
return this.gitOpsSelected.ID && this.gitOpsSelected.domain.lastcommit !== 'refresh';
259+
return this.gitOpsSelected.ID && this.gitOpsSelected.domain.lastcommit !== 'refresh';
260+
}
261+
262+
canReload(): boolean {
263+
return this.gitOpsSelected.ID?.length && !this.reloading;
224264
}
225265

226266
private formInit(): void {
@@ -242,8 +282,8 @@ export class ExtGitOpsComponent implements OnInit, OnDestroy {
242282
}
243283

244284
private readFeatureFlag(): void {
245-
this.featureService.isEnabled({
246-
feature: 'GITOPS_INTEGRATION',
285+
this.featureService.isEnabled({
286+
feature: 'GITOPS_INTEGRATION',
247287
parameters: {
248288
value: this.domainId
249289
}
@@ -286,12 +326,12 @@ export class ExtGitOpsComponent implements OnInit, OnDestroy {
286326
}
287327
});
288328
}
289-
);
329+
);
290330
}
291331

292332
private updateRoute(): void {
293333
if (this.fetch) {
294-
this.domainRouteService.updatePath(this.domainId, this.domainName, Types.DOMAIN_TYPE,
334+
this.domainRouteService.updatePath(this.domainId, this.domainName, Types.DOMAIN_TYPE,
295335
`/dashboard/domain/${this.domainName}/${this.domainId}`);
296336
}
297337
}
@@ -336,7 +376,7 @@ export class ExtGitOpsComponent implements OnInit, OnDestroy {
336376
}
337377

338378
const gitOpsSelectedForm = this.formGroup.value;
339-
379+
340380
this.gitOpsSelected.repository = gitOpsSelectedForm.repository;
341381
this.gitOpsSelected.branch = gitOpsSelectedForm.branch;
342382
this.gitOpsSelected.path = gitOpsSelectedForm.path;
@@ -347,4 +387,11 @@ export class ExtGitOpsComponent implements OnInit, OnDestroy {
347387

348388
return true;
349389
}
390+
391+
private purgePendingAccounts(): void {
392+
this.gitOpsAccounts = this.gitOpsAccounts.filter(account => account.ID);
393+
if (this.gitOpsAccounts.length > 0) {
394+
this.selectAccount(this.gitOpsAccounts[0]);
395+
}
396+
}
350397
}

src/app/model/gitops.ts

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,57 @@
11
export const TOKEN_VALUE = '********************************';
22

33
export class GitOpsAccount {
4-
ID?: string;
5-
repository: string;
6-
branch?: string;
7-
path?: string;
8-
token?: string;
9-
environment: string;
10-
domain: GitOpsDomain;
11-
settings: GitOpsSettings;
4+
ID?: string;
5+
repository: string;
6+
branch?: string;
7+
path?: string;
8+
token?: string;
9+
environment: string;
10+
domain: GitOpsDomain;
11+
settings: GitOpsSettings;
1212
}
1313

1414
export class GitOpsDomain {
15-
id: string;
16-
name: string;
17-
version: number;
18-
lastcommit: string;
19-
lastdate: string;
20-
status: string;
21-
message: string;
15+
id: string;
16+
name: string;
17+
version: number;
18+
lastcommit: string;
19+
lastdate: string;
20+
status: string;
21+
message: string;
2222
}
2323

2424
export class GitOpsSettings {
25-
active: boolean;
26-
window: string;
27-
forceprune: boolean;
25+
active: boolean;
26+
window: string;
27+
forceprune: boolean;
28+
}
29+
30+
export class GitOpsAccountTokenResponse {
31+
result: boolean;
32+
message: string;
2833
}
2934

3035
export function buildNewGitOpsAccount(environment: string, domainId: string, domainName: string): GitOpsAccount {
31-
return {
32-
environment,
33-
repository: '',
34-
branch: '',
35-
path: '',
36-
token: '',
37-
domain: {
38-
id: domainId,
39-
name: domainName,
40-
version: 0,
41-
lastcommit: 'refresh',
42-
lastdate: '',
43-
status: 'Pending',
44-
message: 'Creating GitOps Account'
45-
},
46-
settings: {
47-
active: true,
48-
window: '1m',
49-
forceprune: false
50-
}
51-
};
52-
}
36+
return {
37+
environment,
38+
repository: '',
39+
branch: '',
40+
path: '',
41+
token: '',
42+
domain: {
43+
id: domainId,
44+
name: domainName,
45+
version: 0,
46+
lastcommit: 'refresh',
47+
lastdate: '',
48+
status: 'Pending',
49+
message: 'Creating GitOps Account'
50+
},
51+
settings: {
52+
active: true,
53+
window: '1m',
54+
forceprune: false
55+
}
56+
};
57+
}

0 commit comments

Comments
 (0)