forked from spinnaker/deck
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
1,393 additions
and
0 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
app/scripts/modules/core/src/application/service/ApplicationReader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { API } from 'core/api'; | ||
import { SchedulerFactory } from 'core/scheduler'; | ||
import { Application } from '../application.model'; | ||
import { ApplicationDataSource } from '../service/applicationDataSource'; | ||
import { ApplicationDataSourceRegistry } from './ApplicationDataSourceRegistry'; | ||
import { InferredApplicationWarningService } from './InferredApplicationWarningService'; | ||
|
||
export interface IApplicationDataSourceAttribute { | ||
enabled: string[]; | ||
disabled: string[]; | ||
} | ||
|
||
export interface IApplicationSummary { | ||
accounts?: string; | ||
aliases?: string; | ||
cloudProviders?: string; | ||
createTs?: string; | ||
description?: string; | ||
email?: string; | ||
name: string; | ||
pdApiKey?: string; | ||
updateTs?: string; | ||
} | ||
|
||
export class ApplicationReader { | ||
public static listApplications(): PromiseLike<IApplicationSummary[]> { | ||
return API.all('applications').useCache().getList(); | ||
} | ||
|
||
public static getApplicationAttributes(name: string): PromiseLike<any> { | ||
return API.one('applications', name) | ||
.withParams({ expand: false }) | ||
.get() | ||
.then((fromServer: Application) => { | ||
this.splitAttributes(fromServer.attributes, ['accounts', 'cloudProviders']); | ||
return fromServer.attributes; | ||
}); | ||
} | ||
|
||
public static getApplicationPermissions(applicationName: string): PromiseLike < any > { | ||
return API.one('applications', applicationName) | ||
.withParams({ | ||
expand: false, | ||
}) | ||
.get() | ||
.then((application: Application) => { | ||
return application.attributes.permissions; | ||
}); | ||
} | ||
|
||
public static getApplication(name: string, expand = true): PromiseLike<Application> { | ||
return API.one('applications', name) | ||
.withParams({ expand: expand }) | ||
.get() | ||
.then((fromServer: Application) => { | ||
const configs = ApplicationDataSourceRegistry.getDataSources(); | ||
const application: Application = new Application(fromServer.name, SchedulerFactory.createScheduler(), configs); | ||
application.attributes = fromServer.attributes; | ||
this.splitAttributes(application.attributes, ['accounts', 'cloudProviders']); | ||
this.setDisabledDataSources(application); | ||
application.refresh(); | ||
return application; | ||
}); | ||
} | ||
|
||
private static splitAttributes(attributes: any, fields: string[]) { | ||
fields.forEach((field) => { | ||
if (attributes[field]) { | ||
if (!Array.isArray(attributes[field])) { | ||
attributes[field] = attributes[field].split(','); | ||
} | ||
} else { | ||
attributes[field] = []; | ||
} | ||
}); | ||
} | ||
|
||
public static setDisabledDataSources(application: Application) { | ||
const allDataSources = application.dataSources; | ||
const appDataSources: IApplicationDataSourceAttribute = application.attributes.dataSources; | ||
|
||
if (!appDataSources) { | ||
allDataSources.filter((ds) => ds.optIn).forEach((ds) => this.setDataSourceDisabled(ds, application, true)); | ||
if (InferredApplicationWarningService.isInferredApplication(application)) { | ||
allDataSources | ||
.filter((ds) => ds.requireConfiguredApp) | ||
.forEach((ds) => this.setDataSourceDisabled(ds, application, true)); | ||
} | ||
} else { | ||
allDataSources.forEach((ds) => { | ||
if (ds.optional) { | ||
if (ds.optIn) { | ||
this.setDataSourceDisabled(ds, application, !appDataSources.enabled.includes(ds.key)); | ||
} else { | ||
this.setDataSourceDisabled(ds, application, appDataSources.disabled.includes(ds.key)); | ||
} | ||
} | ||
}); | ||
} | ||
allDataSources | ||
.filter((ds) => ds.requiresDataSource) | ||
.forEach((ds) => { | ||
const parent = allDataSources.find((p) => p.key === ds.requiresDataSource); | ||
if (parent) { | ||
this.setDataSourceDisabled(ds, application, parent.disabled); | ||
} | ||
}); | ||
} | ||
|
||
private static setDataSourceDisabled(dataSource: ApplicationDataSource, application: Application, disabled: boolean) { | ||
dataSource.disabled = disabled; | ||
if (dataSource.badge) { | ||
application.dataSources.find((ds) => ds.key === dataSource.badge).disabled = disabled; | ||
} | ||
} | ||
} |
Oops, something went wrong.