|
| 1 | +import { HttpClient } from '@angular/common/http'; |
| 2 | +import { ChangeDetectionStrategy, Component, Signal, computed, inject, signal } from '@angular/core'; |
| 3 | +import { ActivatedRoute, Router } from '@angular/router'; |
| 4 | + |
| 5 | +import { |
| 6 | + IHeaderBreadcrumb, |
| 7 | + PageHeaderComponent, |
| 8 | + SignalStepHandle, |
| 9 | + StepComponent, |
| 10 | + SteppersComponent, |
| 11 | +} from '@stratosui/core'; |
| 12 | +import { writeWithJob } from '../../../services/async-jobs/write-with-job'; |
| 13 | +import { StratosJobError } from '../../../services/async-jobs/async-job.types'; |
| 14 | +import { |
| 15 | + RouteServiceBindingView, |
| 16 | + ServiceCatalogDataService, |
| 17 | + SignalSource, |
| 18 | +} from '../../../services/endpoint-data/service-catalog-data.service'; |
| 19 | +import { StServiceInstance } from '../../../services/endpoint-data/stratos-types'; |
| 20 | + |
| 21 | +// RouteServiceComponent — per-route "route service" management, reached via the |
| 22 | +// /services/route-service/:endpointId/:routeGuid route (Route Service row action |
| 23 | +// on the cf-routes lists). A route binds to 0-or-1 service instance (a route |
| 24 | +// service). When unbound (or rebinding) it shows a one-step stepper to pick a |
| 25 | +// managed service instance in the route's space and bind it; when bound it shows |
| 26 | +// the binding with Unbind / Rebind. Bind/unbind ride the writeWithJob contract. |
| 27 | +@Component({ |
| 28 | + selector: 'app-route-service', |
| 29 | + templateUrl: './route-service.component.html', |
| 30 | + standalone: true, |
| 31 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 32 | + imports: [PageHeaderComponent, SteppersComponent, StepComponent], |
| 33 | +}) |
| 34 | +export class RouteServiceComponent { |
| 35 | + private http = inject(HttpClient); |
| 36 | + private catalog = inject(ServiceCatalogDataService); |
| 37 | + private router = inject(Router); |
| 38 | + |
| 39 | + readonly cfGuid: string; |
| 40 | + readonly routeGuid: string; |
| 41 | + readonly spaceGuid: string; |
| 42 | + readonly routeUrl: string; |
| 43 | + |
| 44 | + readonly title: Signal<string> = computed(() => |
| 45 | + this.routeUrl ? `Route service for '${this.routeUrl}'` : 'Route service'); |
| 46 | + readonly breadcrumbs: IHeaderBreadcrumb[]; |
| 47 | + |
| 48 | + // Current binding (0-or-1), reloadable by swapping the source signal. |
| 49 | + private bindingSource = signal<SignalSource<RouteServiceBindingView | null>>( |
| 50 | + { value: signal<RouteServiceBindingView | null>(null).asReadonly(), isLoading: signal(false).asReadonly(), error: signal(null).asReadonly() }, |
| 51 | + ); |
| 52 | + readonly binding: Signal<RouteServiceBindingView | null> = computed(() => this.bindingSource().value()); |
| 53 | + readonly loading: Signal<boolean> = computed(() => this.bindingSource().isLoading()); |
| 54 | + |
| 55 | + // Managed service instances in the route's space — the bind picker, and the |
| 56 | + // source for resolving the bound instance's display name. |
| 57 | + private siSource: SignalSource<StServiceInstance[]>; |
| 58 | + readonly serviceInstances: Signal<StServiceInstance[]> = computed(() => |
| 59 | + this.siSource.value().filter(si => si.type !== 'user-provided')); |
| 60 | + boundInstanceName = computed(() => { |
| 61 | + const guid = this.binding()?.serviceInstanceGuid; |
| 62 | + if (!guid) return ''; |
| 63 | + return this.siSource.value().find(si => si.guid === guid)?.name ?? guid; |
| 64 | + }); |
| 65 | + |
| 66 | + readonly selectedSiGuid = signal<string | null>(null); |
| 67 | + readonly rebinding = signal(false); |
| 68 | + readonly busy = signal(false); |
| 69 | + readonly errorMessage = signal<string | null>(null); |
| 70 | + |
| 71 | + // Show the bind stepper when there is no binding, or the user chose Rebind. |
| 72 | + readonly showStepper = computed(() => !this.loading() && (!this.binding() || this.rebinding())); |
| 73 | + |
| 74 | + readonly bindStepHandle: SignalStepHandle = { |
| 75 | + valid: computed(() => !!this.selectedSiGuid() && !this.busy()), |
| 76 | + submit: () => this.bind(), |
| 77 | + }; |
| 78 | + |
| 79 | + constructor() { |
| 80 | + const route = inject(ActivatedRoute); |
| 81 | + this.cfGuid = route.snapshot.params.endpointId; |
| 82 | + this.routeGuid = route.snapshot.params.routeGuid; |
| 83 | + this.spaceGuid = route.snapshot.queryParamMap.get('space') ?? ''; |
| 84 | + this.routeUrl = route.snapshot.queryParamMap.get('url') ?? ''; |
| 85 | + this.breadcrumbs = [{ breadcrumbs: [{ value: 'Routes', routerLink: `/cloud-foundry/${this.cfGuid}/routes` }] }]; |
| 86 | + this.siSource = this.catalog.serviceInstancesInSpace(this.cfGuid, this.spaceGuid); |
| 87 | + this.reload(); |
| 88 | + } |
| 89 | + |
| 90 | + reload(): void { |
| 91 | + this.bindingSource.set(this.catalog.routeServiceBinding(this.cfGuid, this.routeGuid)); |
| 92 | + } |
| 93 | + |
| 94 | + startRebind(): void { |
| 95 | + this.selectedSiGuid.set(null); |
| 96 | + this.errorMessage.set(null); |
| 97 | + this.rebinding.set(true); |
| 98 | + } |
| 99 | + |
| 100 | + private async bind(): Promise<void> { |
| 101 | + const siGuid = this.selectedSiGuid(); |
| 102 | + if (!siGuid || this.busy()) return; |
| 103 | + this.busy.set(true); |
| 104 | + this.errorMessage.set(null); |
| 105 | + // Rebind: drop the existing binding first (route-service bindings are |
| 106 | + // immutable except metadata, so "edit" = unbind + rebind). |
| 107 | + const existing = this.binding(); |
| 108 | + try { |
| 109 | + if (existing) { |
| 110 | + await writeWithJob( |
| 111 | + this.http, |
| 112 | + this.http.delete(`/pp/v1/cf/service_route_bindings/${this.cfGuid}/${existing.guid}`, { observe: 'response' as const }), |
| 113 | + ); |
| 114 | + } |
| 115 | + const body = { |
| 116 | + relationships: { |
| 117 | + route: { data: { guid: this.routeGuid } }, |
| 118 | + service_instance: { data: { guid: siGuid } }, |
| 119 | + }, |
| 120 | + }; |
| 121 | + await writeWithJob( |
| 122 | + this.http, |
| 123 | + this.http.post(`/pp/v1/cf/service_route_bindings/${this.cfGuid}`, body, { observe: 'response' as const }), |
| 124 | + ); |
| 125 | + this.rebinding.set(false); |
| 126 | + this.selectedSiGuid.set(null); |
| 127 | + this.reload(); |
| 128 | + } catch (err: unknown) { |
| 129 | + this.errorMessage.set(`Failed to bind service: ${this.messageOf(err)}`); |
| 130 | + throw err instanceof Error ? err : new Error(this.messageOf(err)); |
| 131 | + } finally { |
| 132 | + this.busy.set(false); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + async unbind(): Promise<void> { |
| 137 | + const existing = this.binding(); |
| 138 | + if (!existing || this.busy()) return; |
| 139 | + this.busy.set(true); |
| 140 | + this.errorMessage.set(null); |
| 141 | + try { |
| 142 | + await writeWithJob( |
| 143 | + this.http, |
| 144 | + this.http.delete(`/pp/v1/cf/service_route_bindings/${this.cfGuid}/${existing.guid}`, { observe: 'response' as const }), |
| 145 | + ); |
| 146 | + this.reload(); |
| 147 | + } catch (err: unknown) { |
| 148 | + this.errorMessage.set(`Failed to unbind service: ${this.messageOf(err)}`); |
| 149 | + } finally { |
| 150 | + this.busy.set(false); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private messageOf(err: unknown): string { |
| 155 | + if (err instanceof StratosJobError) return err.message; |
| 156 | + if (err instanceof Error) return err.message; |
| 157 | + return 'unknown error'; |
| 158 | + } |
| 159 | +} |
0 commit comments