@@ -734,6 +734,12 @@ <h6>Primitive Capability ${primitiveCounter + 1}</h6>
734734 <label>Description</label>
735735 <input type="text" class="form-control" name="capabilities.primitives[${ primitiveCounter } ].description">
736736 </div>
737+ <div class="form-group">
738+ <label>Endpoints</label>
739+ <div class="dynamic-list" id="primitive-endpoints-${ primitiveCounter } ">
740+ </div>
741+ <button type="button" class="btn-add" onclick="addListItem('primitive-endpoints-${ primitiveCounter } ', 'capabilities.primitives[${ primitiveCounter } ].endpoints')">Add Endpoint</button>
742+ </div>
737743 <div class="form-group">
738744 <label>Tags</label>
739745 <div class="dynamic-list" id="primitive-tags-${ primitiveCounter } ">
@@ -947,90 +953,62 @@ <h6>Endpoint Rule ${endpointCounter + 1}</h6>
947953 }
948954 }
949955
950- // Convert FormData to proper policy object
951956 function formDataToPolicyObject ( ) {
952957 const form = document . getElementById ( 'atpl-form' ) ;
953958 const formData = new FormData ( form ) ;
954959 const policy = { } ;
955960
956- // Helper function to set nested object properties
957- function setNestedProperty ( obj , path , value ) {
958- const keys = path . split ( '.' ) ;
959- let current = obj ;
960-
961- for ( let i = 0 ; i < keys . length - 1 ; i ++ ) {
962- const key = keys [ i ] ;
963- if ( ! current [ key ] ) current [ key ] = { } ;
964- current = current [ key ] ;
965- }
966-
967- const lastKey = keys [ keys . length - 1 ] ;
968-
969- // Handle array notation [index]
970- if ( lastKey . includes ( '[' ) && lastKey . includes ( ']' ) ) {
971- const match = lastKey . match ( / ( \w + ) \[ ( \d + ) \] / ) ;
961+ for ( let [ key , value ] of formData . entries ( ) ) {
962+ if ( value . trim ( ) === '' ) continue ;
963+
964+ // Fix: Properly merge primitives and their tags into the same object
965+ if ( key . includes ( '[' ) && key . includes ( ']' ) ) {
966+ const match = key . match ( / ( [ ^ . ] + ) \[ ( \d + ) \] \. ( .+ ) / ) ;
972967 if ( match ) {
973- const arrayKey = match [ 1 ] ;
968+ const arrayKey = match [ 1 ] ; // e.g., 'primitives'
974969 const index = parseInt ( match [ 2 ] ) ;
975-
976- if ( ! current [ arrayKey ] ) current [ arrayKey ] = [ ] ;
977- if ( ! current [ arrayKey ] [ index ] ) current [ arrayKey ] [ index ] = { } ;
978-
979- return current [ arrayKey ] [ index ] ;
970+ const propertyPath = match [ 3 ] ; // e.g., 'tags' or 'description'
971+
972+ if ( ! policy . capabilities ) policy . capabilities = { } ;
973+ if ( ! policy . capabilities [ arrayKey ] ) policy . capabilities [ arrayKey ] = [ ] ;
974+ if ( ! policy . capabilities [ arrayKey ] [ index ] ) policy . capabilities [ arrayKey ] [ index ] = { } ;
975+
976+ // Support array properties (e.g., tags[])
977+ if ( propertyPath . endsWith ( '[]' ) ) {
978+ const prop = propertyPath . replace ( '[]' , '' ) ;
979+ if ( ! policy . capabilities [ arrayKey ] [ index ] [ prop ] ) policy . capabilities [ arrayKey ] [ index ] [ prop ] = [ ] ;
980+ policy . capabilities [ arrayKey ] [ index ] [ prop ] . push ( value ) ;
981+ } else {
982+ policy . capabilities [ arrayKey ] [ index ] [ propertyPath ] = value ;
983+ }
984+ continue ;
980985 }
981986 }
982-
983- return current ;
984- }
985987
986- // Process form data
987- for ( let [ key , value ] of formData . entries ( ) ) {
988- if ( value . trim ( ) === '' ) continue ;
989-
988+ // Handle arrays for other fields
990989 if ( key . includes ( '[]' ) ) {
991- // Handle arrays
992990 const arrayKey = key . replace ( '[]' , '' ) ;
993991 const keys = arrayKey . split ( '.' ) ;
994992 let current = policy ;
995-
993+
996994 for ( let i = 0 ; i < keys . length - 1 ; i ++ ) {
997995 if ( ! current [ keys [ i ] ] ) current [ keys [ i ] ] = { } ;
998996 current = current [ keys [ i ] ] ;
999997 }
1000-
998+
1001999 const lastKey = keys [ keys . length - 1 ] ;
10021000 if ( ! current [ lastKey ] ) current [ lastKey ] = [ ] ;
10031001 current [ lastKey ] . push ( value ) ;
1004- } else if ( key . includes ( '[' ) && key . includes ( ']' ) ) {
1005- // Handle indexed arrays for capabilities
1006- const baseKey = key . substring ( 0 , key . indexOf ( '[' ) ) ;
1007- const match = key . match ( / \[ ( \d + ) \] \. ( \w + ) / ) ;
1008-
1009- if ( match ) {
1010- const index = parseInt ( match [ 1 ] ) ;
1011- const property = match [ 2 ] ;
1012-
1013- const keys = baseKey . split ( '.' ) ;
1014- let current = policy ;
1015-
1016- for ( let i = 0 ; i < keys . length ; i ++ ) {
1017- if ( ! current [ keys [ i ] ] ) current [ keys [ i ] ] = { } ;
1018- current = current [ keys [ i ] ] ;
1019- }
1020-
1021- if ( ! current [ index ] ) current [ index ] = { } ;
1022- current [ index ] [ property ] = value ;
1023- }
10241002 } else {
10251003 // Handle regular nested properties
10261004 const keys = key . split ( '.' ) ;
10271005 let current = policy ;
1028-
1006+
10291007 for ( let i = 0 ; i < keys . length - 1 ; i ++ ) {
10301008 if ( ! current [ keys [ i ] ] ) current [ keys [ i ] ] = { } ;
10311009 current = current [ keys [ i ] ] ;
10321010 }
1033-
1011+
10341012 current [ keys [ keys . length - 1 ] ] = value ;
10351013 }
10361014 }
@@ -1040,12 +1018,12 @@ <h6>Endpoint Rule ${endpointCounter + 1}</h6>
10401018 checkboxes . forEach ( checkbox => {
10411019 const keys = checkbox . name . split ( '.' ) ;
10421020 let current = policy ;
1043-
1021+
10441022 for ( let i = 0 ; i < keys . length - 1 ; i ++ ) {
10451023 if ( ! current [ keys [ i ] ] ) current [ keys [ i ] ] = { } ;
10461024 current = current [ keys [ i ] ] ;
10471025 }
1048-
1026+
10491027 current [ keys [ keys . length - 1 ] ] = checkbox . checked ;
10501028 } ) ;
10511029
@@ -1184,7 +1162,7 @@ <h6>Endpoint Rule ${endpointCounter + 1}</h6>
11841162 // Populate match criteria
11851163 if ( policy . match && policy . match . agent_tags ) {
11861164 policy . match . agent_tags . forEach ( tag => {
1187- addListItem ( 'agent-tags-list' , 'agent_tags' ) ;
1165+ addListItem ( 'agent-tags-list' , 'match. agent_tags' ) ;
11881166 const inputs = document . querySelectorAll ( '#agent-tags-list input' ) ;
11891167 inputs [ inputs . length - 1 ] . value = tag ;
11901168 } ) ;
@@ -1241,7 +1219,20 @@ <h6>Endpoint Rule ${endpointCounter + 1}</h6>
12411219
12421220 if ( primitive . id ) container . querySelector ( 'input[name*=".id"]' ) . value = primitive . id ;
12431221 if ( primitive . description ) container . querySelector ( 'input[name*=".description"]' ) . value = primitive . description ;
1244-
1222+
1223+ if ( primitive . endpoints ) {
1224+ const tagsList = container . querySelector ( '.dynamic-list' ) ;
1225+ primitive . endpoints . forEach ( endpoint => {
1226+ addListItem (
1227+ tagsList . id ,
1228+ 'capabilities.primitives[' + ( primitiveCounter - 1 ) + '].endpoint'
1229+ ) ;
1230+ const inputs = tagsList . querySelectorAll ( 'input' ) ;
1231+ inputs [ inputs . length - 1 ] . value = endpoint ;
1232+ } ) ;
1233+ }
1234+
1235+
12451236 if ( primitive . tags ) {
12461237 const tagsList = container . querySelector ( '.dynamic-list' ) ;
12471238 primitive . tags . forEach ( tag => {
0 commit comments