@@ -28,6 +28,12 @@ interface FDCResponse {
2828 results : CFApp [ ] ;
2929}
3030
31+ interface CreateServiceOptions {
32+ xsSecurityProjectName ?: string ;
33+ templatePathOverwrite ?: string ;
34+ logger ?: ToolsLogger ;
35+ }
36+
3137const PARAM_MAP : Map < string , string > = new Map ( [
3238 [ 'spaceGuids' , 'space_guids' ] ,
3339 [ 'planNames' , 'service_plan_names' ] ,
@@ -133,57 +139,38 @@ export async function getFDCApps(appHostIds: string[], cfConfig: CfConfig, logge
133139}
134140
135141/**
136- * Creates a service.
142+ * Creates a service instance .
137143 *
138- * @param {string } spaceGuid - The space GUID.
139- * @param {string } plan - The plan.
144+ * @param {string } plan - The service plan.
140145 * @param {string } serviceInstanceName - The service instance name.
141- * @param {string[] } tags - The tags.
142- * @param {string } [serviceName] - The service name.
143- * @param {object } [security] - Security configuration.
144- * @param {string | null } security.filePath - The security file path.
145- * @param {string } [security.xsappname] - The XS app name.
146- * @param {string } [templatePathOverwrite] - The template path overwrite.
147- * @param {ToolsLogger } [logger] - The logger.
146+ * @param {string } serviceName - The service name.
147+ * @param {CreateServiceOptions } [options] - Additional options.
148+ * @returns {Promise<void> } The promise.
148149 */
149- export async function createService (
150- spaceGuid : string ,
150+ export async function createServiceInstance (
151151 plan : string ,
152152 serviceInstanceName : string ,
153- tags : string [ ] ,
154- serviceName : string | undefined ,
155- security ?: {
156- filePath : string | null ;
157- xsappname ?: string ;
158- } ,
159- templatePathOverwrite ?: string ,
160- logger ?: ToolsLogger
153+ serviceName : string ,
154+ options ?: CreateServiceOptions
161155) : Promise < void > {
156+ const { xsSecurityProjectName, templatePathOverwrite, logger } = options ?? { } ;
157+
162158 try {
163- const { filePath, xsappname } = security ?? { } ;
164- if ( ! serviceName ) {
165- const json : CfAPIResponse < CfServiceOffering > = await requestCfApi < CfAPIResponse < CfServiceOffering > > (
166- `/v3/service_offerings?per_page=1000&space_guids=${ spaceGuid } `
167- ) ;
168- const serviceOffering = json ?. resources ?. find (
169- ( resource : CfServiceOffering ) => resource . tags && tags . every ( ( tag ) => resource . tags ?. includes ( tag ) )
170- ) ;
171- serviceName = serviceOffering ?. name ;
172- }
173159 logger ?. log (
174160 `Creating service instance '${ serviceInstanceName } ' of service '${ serviceName } ' with '${ plan } ' plan`
175161 ) ;
176162
177- const commandParameters : string [ ] = [ 'create-service' , serviceName ?? '' , plan , serviceInstanceName ] ;
178- if ( filePath ) {
163+ const commandParameters : string [ ] = [ 'create-service' , serviceName , plan , serviceInstanceName ] ;
164+
165+ if ( xsSecurityProjectName ) {
179166 let xsSecurity = null ;
180167 try {
181168 const baseTmplPath = path . join ( __dirname , '../../../templates' ) ;
182169 const templatePath = templatePathOverwrite ?? baseTmplPath ;
183170 const filePath = path . resolve ( templatePath , 'cf/xs-security.json' ) ;
184171 const xsContent = fs . readFileSync ( filePath , 'utf-8' ) ;
185172 xsSecurity = JSON . parse ( xsContent ) as unknown as { xsappname ?: string } ;
186- xsSecurity . xsappname = xsappname ;
173+ xsSecurity . xsappname = xsSecurityProjectName ;
187174 } catch ( err ) {
188175 logger ?. error ( `Failed to parse xs-security.json file: ${ err } ` ) ;
189176 throw new Error ( t ( 'error.xsSecurityJsonCouldNotBeParsed' ) ) ;
@@ -200,53 +187,64 @@ export async function createService(
200187 }
201188}
202189
190+ /**
191+ * Gets the service name by tags.
192+ *
193+ * @param {string } spaceGuid - The space GUID.
194+ * @param {string[] } tags - The service tags for discovery.
195+ * @returns {Promise<string> } The service name.
196+ */
197+ export async function getServiceNameByTags ( spaceGuid : string , tags : string [ ] ) : Promise < string > {
198+ const json : CfAPIResponse < CfServiceOffering > = await requestCfApi < CfAPIResponse < CfServiceOffering > > (
199+ `/v3/service_offerings?per_page=1000&space_guids=${ spaceGuid } `
200+ ) ;
201+ const serviceOffering = json ?. resources ?. find (
202+ ( resource : CfServiceOffering ) => resource . tags && tags . every ( ( tag ) => resource . tags ?. includes ( tag ) )
203+ ) ;
204+ return serviceOffering ?. name ?? '' ;
205+ }
206+
203207/**
204208 * Creates the services.
205209 *
206- * @param {string } projectPath - The project path.
207210 * @param {MtaYaml } yamlContent - The YAML content.
208211 * @param {string[] } initialServices - The initial services.
209212 * @param {string } timestamp - The timestamp.
210- * @param {string } spaceGuid - The space GUID.
211213 * @param {string } [templatePathOverwrite] - The template path overwrite.
212214 * @param {ToolsLogger } logger - The logger.
213215 * @returns {Promise<void> } The promise.
214216 */
215217export async function createServices (
216- projectPath : string ,
217218 yamlContent : MtaYaml ,
218219 initialServices : string [ ] ,
219220 timestamp : string ,
220- spaceGuid : string ,
221221 templatePathOverwrite ?: string ,
222222 logger ?: ToolsLogger
223223) : Promise < void > {
224224 const excludeServices = new Set ( [ ...initialServices , 'portal' , 'html5-apps-repo' ] ) ;
225- const xsSecurityPath = path . join ( projectPath , 'xs-security.json' ) ;
226225 const xsSecurityProjectName = getProjectNameForXsSecurity ( yamlContent , timestamp ) ;
227226 for ( const resource of yamlContent . resources ?? [ ] ) {
228227 if ( ! excludeServices . has ( resource ?. parameters ?. service ?? '' ) ) {
229228 if ( resource ?. parameters ?. service === 'xsuaa' ) {
230- await createService (
231- spaceGuid ,
229+ await createServiceInstance (
232230 resource . parameters [ 'service-plan' ] ?? '' ,
233231 resource . parameters [ 'service-name' ] ?? '' ,
234- [ ] ,
235232 resource . parameters . service ,
236- { filePath : xsSecurityPath , xsappname : xsSecurityProjectName } ,
237- templatePathOverwrite ,
238- logger
233+ {
234+ xsSecurityProjectName,
235+ templatePathOverwrite,
236+ logger
237+ }
239238 ) ;
240239 } else {
241- await createService (
242- spaceGuid ,
240+ await createServiceInstance (
243241 resource . parameters [ 'service-plan' ] ?? '' ,
244242 resource . parameters [ 'service-name' ] ?? '' ,
245- [ ] ,
246- resource . parameters . service ,
247- { filePath : null , xsappname : xsSecurityProjectName } ,
248- templatePathOverwrite ,
249- logger
243+ resource . parameters . service ?? '' ,
244+ {
245+ templatePathOverwrite ,
246+ logger
247+ }
250248 ) ;
251249 }
252250 }
0 commit comments