@@ -10,8 +10,27 @@ export function findNonIdentifierArnProperty(resource: Resource) {
1010 return findArnProperty ( resource , ( name ) => ! resource . primaryIdentifier ?. includes ( name ) ) ;
1111}
1212
13- export function findArnProperty ( resource : Resource , filter : ( name : string ) => boolean = ( ) => true ) : any {
14- const possibleArnNames = [ 'Arn' , `${ resource . name } Arn` ] ;
13+ export function findArnProperty ( resource : Resource , filter : ( name : string ) => boolean = ( ) => true ) : string | undefined {
14+ const prefixes = [ '' , resource . name ] ;
15+ const suffixes = [ 'Arn' , 'ARN' ] ;
16+ const primaryIdentifierSuffixes = [ 'Id' , 'ID' ] ;
17+
18+ // if the primary identifier uses a prefix that is different than the resource name, we add that to the list
19+ if ( resource . primaryIdentifier ?. length === 1 ) {
20+ for ( const suffix of primaryIdentifierSuffixes ) {
21+ if ( resource . primaryIdentifier [ 0 ] . endsWith ( suffix ) ) {
22+ const prefix = resource . primaryIdentifier [ 0 ] . slice ( 0 , - suffix . length ) ;
23+ if ( prefix && ! prefixes . includes ( prefix ) ) {
24+ prefixes . push ( prefix ) ;
25+ }
26+ break ;
27+ }
28+ }
29+ }
30+
31+ // this is a combination of all prefixes with all suffixes, order by prefixes as they appear in the list
32+ const possibleArnNames = prefixes . flatMap ( prefix => suffixes . map ( suffix => prefix + suffix ) ) ;
33+
1534 for ( const name of possibleArnNames ) {
1635 const prop = resource . attributes [ name ] ;
1736 if ( prop && filter ( name ) ) {
@@ -21,3 +40,27 @@ export function findArnProperty(resource: Resource, filter: (name: string) => bo
2140 return undefined ;
2241}
2342
43+ /**
44+ * Extracts all variables from an ARN Format template
45+ *
46+ * @example
47+ * extractVariablesFromArnFormat("arn:${Partition}:sagemaker:${Region}:${Account}:workteam/${WorkteamName}")
48+ * // returns ['Partition', 'Region', 'Account', 'WorkteamName']
49+ */
50+ export function extractVariablesFromArnFormat ( format : string ) : string [ ] {
51+ return ( format . match ( / \$ { ( [ ^ { } ] + ) } / g) || [ ] ) . map ( match => match . slice ( 2 , - 1 ) ) ;
52+ }
53+
54+ /**
55+ * Extracts all resource specific variables from an ARN Format template.
56+ *
57+ * To get all variables, use `extractVariablesFromArnFormat(format)`
58+ *
59+ * @example
60+ * extractVariablesFromArnFormat("arn:${Partition}:sagemaker:${Region}:${Account}:workteam/${WorkteamName}")
61+ * // returns ['WorkteamName']
62+ */
63+ export function extractResourceVariablesFromArnFormat ( format : string ) {
64+ const [ _arn , _partition , _service , _region , _account , ...rest ] = format . split ( ':' ) ;
65+ return extractVariablesFromArnFormat ( rest . join ( ':' ) ) ;
66+ }
0 commit comments