@@ -138,12 +138,14 @@ func CmdCreateAppsec(c *cli.Context) error {
138138 "getWAFMode" : getWAFMode ,
139139 "isStructuredRule" : isStructuredRule ,
140140 "exportJSONWithoutKeys" : exportJSONWithoutKeys ,
141+ "exportJSONForCustomDefBotsWithoutKeys" : exportJSONForCustomDefBotsWithoutKeys ,
141142 "getCustomBotCategoryResourceNamesByIDs" : getCustomBotCategoryResourceNamesByIDs ,
142143 "getCustomBotCategoryNameByID" : getCustomBotCategoryNameByID ,
143144 "getCustomClientResourceNamesByIDs" : getCustomClientResourceNamesByIDs ,
144145 "getContentProtectionRuleResourceNamesByIDs" : getContentProtectionRuleResourceNamesByIDs ,
145146 "getProtectedHostsByID" : getProtectedHostsByID ,
146147 "getEvaluatedHostsByID" : getEvaluatedHostsByID ,
148+ "buildCategoryMap" : buildCategoryMap ,
147149 })
148150
149151 // The template processor
@@ -533,6 +535,55 @@ func exportJSONWithoutKeys(source map[string]interface{}, keys ...string) (strin
533535 return string (js ), nil
534536}
535537
538+ // exportJSONForCustomDefBotsWithoutKeys returns json string without specified keys and categoryId pointing to category resource category_id
539+ func exportJSONForCustomDefBotsWithoutKeys (source map [string ]interface {}, categoryData map [string ]string , keys ... string ) (string , error ) {
540+ // deep copy source by converting to json
541+ js , err := json .Marshal (source )
542+ if err != nil {
543+ return "" , err
544+ }
545+ dest := make (map [string ]interface {})
546+ err = json .Unmarshal (js , & dest )
547+ if err != nil {
548+ return "" , err
549+ }
550+ for _ , key := range keys {
551+ delete (dest , key )
552+ }
553+
554+ // Change the value of the categoryId key
555+ if _ , exists := dest ["categoryId" ]; exists {
556+ categoryName , _ := tools .EscapeName (categoryData [dest ["categoryId" ].(string )])
557+ dest ["categoryId" ] = "${akamai_botman_custom_bot_category" + "." + categoryName + "_" + dest ["categoryId" ].(string ) + "." + "category_id}"
558+ }
559+
560+ js , err = json .MarshalIndent (dest , "" , " " )
561+ if err != nil {
562+ return "" , err
563+ }
564+
565+ return string (js ), nil
566+ }
567+
568+ func buildCategoryMap (source []map [string ]interface {}) (map [string ]string , error ) {
569+ categoryMap := make (map [string ]string )
570+
571+ for _ , category := range source {
572+ categoryID , ok := category ["categoryId" ].(string )
573+ if ! ok {
574+ return nil , errors .New ("missing or invalid categoryId" )
575+ }
576+
577+ categoryName , ok := category ["categoryName" ].(string )
578+ if ! ok {
579+ return nil , errors .New ("missing or invalid categoryName" )
580+ }
581+
582+ categoryMap [categoryID ] = categoryName
583+ }
584+
585+ return categoryMap , nil
586+ }
536587func getCustomBotCategoryNameByID (customBotCategories []map [string ]interface {}, categoryID string ) (string , error ) {
537588 for _ , category := range customBotCategories {
538589 if category ["categoryId" ].(string ) == categoryID {
0 commit comments