1 2 Previous Next 15 Replies Latest reply on Jun 18, 2018 4:10 AM by Andrew Waters

    TPL help

    Mahesh V

      Hello,

       

      I need a help in my TPL.

      Here, I need to read a file from specific location and split values like below format and display those attributes in SI.

       

      tag1 name := tag1 value;

      tag2 name := tag2 value;

       

      out put of file(mrttags.act) is as below:

      {

              { “<tag1 name>”: “<tag1 value”>, “<tag2 name>”: “<tag2 value”>,“<tag3 name>”: “<tag3 value”>,“<tag4 name>”: “<tag4 value”> }

      }

       

       

      Below is my TPL. Please help me where i need to correct.

       

      triggers

              on host := Host created, confirmed;

          end triggers;

       

          body

             tag1 name := "";

             tag2 name := "";

             tag3 name := "";    

       

             Tags := discovery.runCommand(host, "cat /esc/anbe/dacts.d/mrttags.act");

                   if Tags and Tags.result then

                           Attributes := text.strip(Tags.result," \r\n\t");

                           log.debug ("Attributes=%Attributes%");

                  

                           if Attributes then

       

                               si := model.SoftwareInstance(

                                     key := 'RTU_via_tagging + /%host.key%',

                                     type := 'RTU_via_tagging',

                                     name := 'RTU_via_tagging' + ' on %host.name%',

                                     short_name := 'RTUviatagging',

                                     tag1 name := tag1 name,

                                     tag2 name := tag2 name,

                                     tag3 name := tag3 name;

                                     _tw_meta_data_attrs := ['tag1 name','tag2 name','tag3 name']

                                     );

                 

                             end if;

                   end if;

         end body;

       

       

      Thanks in advance.

       

       

      Thank you,

      Mahesh V

        • 1. Re: TPL help
          Andrew Waters

          You can use discovery.fileGet which is specifically designed for getting files.

           

          This almost looks like JSON, does it really have just braces at the start and end? If it really looks like this then you are going to have to do some parsing because it is not valid JSON so you will not be able to use json.decode.

           

          It really does not look like a SoftwareInstance thing.

          1 of 1 people found this helpful
          • 2. Re: TPL help
            Mahesh V

            Thanks Andrew for your reply.

             

            Yes, it is JSON but format was incorrect. I have marked outermost level with brackets(format below).

            [{

            "SoftwareModule": "somo",

            "ComponentName": "cona",

            "ComponentRole": "coro",

            "Environment": "env",

            "Partition": "part"

            }]

             

            Basically i need to read above JSON file and display key and values in software instance in UI.

             

            Thank you,

            Mahesh V

            2 of 2 people found this helpful
            • 3. Re: TPL help
              Andrew Waters

              Then you can use the json.decode to parse the file.Something like this

              file := discovery.fileGet(host, "/esc/anbe/dacts.d/mrttags.act");

               

              if file and file.content then

                decoded := json.decode(file.content);

               

                if decoded then

                  si := model.SoftwareInstance(key              := 'RTU_via_tagging/%host.key%',

                                                                 type             := 'RTU_via_tagging',

                                                                 name           := 'RTU_via_tagging on %host.name%',

                                                                 short_name := 'RTUviatagging');

               

                  keys := [];

               

                  for key in decoded do

                    list.append(keys, key);

                    si[key] := decoded[key];

                  end for;

               

                  model.addDisplayAttribute(si, keys);

                end if;

              end if;

              will create an SoftwareInstance. Note that this is not handling removal of tags or if the file goes away (it will not age out). If also does no validation on tag names so it would be easy to create things which conflict with other attributes or are invalid.

              1 of 1 people found this helpful
              • 4. Re: TPL help
                Mahesh V

                It's throwing above error near line decoded := json.decode(file.content);.

                 

                 

                 

                 

                 

                 

                • 5. Re: TPL help
                  Mahesh V

                  is there any specific syntax to use json.decode function is TPL?

                  • 6. Re: TPL help
                    Andrew Waters

                    What tpl version do you have at the top of your file? You need at least 1.11 (it was added in Discovery 11.0).

                    1 of 1 people found this helpful
                    • 7. Re: TPL help
                      Mahesh V

                      OK, I have changed my TPL version from 1.6 to 1.14(which is the current TPL version)-above error gone.

                       

                      But getting one error while running TPL manually against the server and also getting ECA error when scanned the server.

                       

                      error while running TPL manually:

                      RuleError on rule tpl_build_SI_RTU_via_tagging_body_1 due to: Error in action inference.track_store -- TypeError: unhashable type: 'dict'

                       

                      ECA error after scanning the server:

                      Error MessageRuleError on rule tpl_build_SI_RTU_via_tagging_body_1 due to: Error in action inference.track_store -- TypeError: unhashable type: 'dict'
                      Internal Error DetailsFile "/usr/tideway/var/code/generated_code/software_instance_model/RTU_via_tagging/__init__.py", line 167, in execute engine.doAction(tracker, 'inference.track_store', state['__pattern_node'], state['__inferences'], state['__inferences_tracker'], 'si', [llr_tpl_si, llr_tpl_key], [['decoded', llr_tpl_decoded, llr_tpl_key]], []) File "./__init__.py", line 611, in doAction File "./inference_actions.py", line 1746, in perform

                       

                      Thank you,

                      Mahesh V

                      • 8. Re: TPL help
                        Andrew Waters

                        That would probably be because you have the square brackets, so that would mean it is a list so you would need to get the dictionary from the list using decoded[0] for example

                        for key in decoded[0] do

                        2 of 2 people found this helpful
                        • 9. Re: TPL help
                          Mahesh V

                          Thank you so much Andrew.

                           

                          Thank you,

                          Mahesh V

                          • 10. Re: TPL help
                            Mahesh V

                            Hello Andrew,

                             

                            I am getting one more error after using for key in decoded[0] do. Error message below.

                            Can I also use above condition to read array in JSON?

                            for eg:

                             

                            [

                            {"SoftwareModule": "somo",

                            "ComponentName": "cona",

                            "ComponentRole": "coro",

                            "Environment": "env",

                            "Partition": "part"

                            },

                            {"SoftwareModule": "somo2",

                            "ComponentName": "cona2",

                            "ComponentRole": "coro2",

                            "Environment": "env2",

                            "Partition": "part2"

                            }

                            ]

                             

                            Thanks for your help,

                            Mahesh V

                            • 11. Re: TPL help
                              Andrew Waters

                              Did you change decoded to decoded[0] in the for loop?

                               

                              Yes it would be possible to process a list of dictionaries You would need another for loop to iterate over each table in the list. I assume you would be building a different SoftwareInstance for each entry in which case the key would need to be changed to give you distinct SotwareInstances.

                              2 of 2 people found this helpful
                              • 12. Re: TPL help
                                Mahesh V

                                Yes, I did change decoded to decoded[0] in the for loop.

                                After changing itself i am getting "TypeError: list indices must be integets, not str"

                                 

                                Regarding array, I want to store values in one software instance as keys(Attributes) are common. like Environment = env, env1

                                 

                                Thank you,

                                Mahesh V

                                • 13. Re: TPL help
                                  Andrew Waters

                                  That is only the for loop, not all the decoded stuff, you need

                                  for key in decoded[0] do

                                    list.append(keys, key);

                                    si[key] := decoded[0][key];

                                  end for;

                                  3 of 3 people found this helpful
                                  • 14. Re: TPL help
                                    Mahesh V

                                    To read array, I have changed my TPL as below:

                                     

                                    Again getting the same error as "TypeError: list indices must be integets, not str"

                                    1 of 1 people found this helpful
                                    1 2 Previous Next