Read a directory

Post Reply
hudatolah
Site Admin
Posts: 143
Joined: Thu Apr 04, 2013 8:10 pm
Are You a Headhunter?: Affirmative
Surfer?: Yes

Read a directory

Post by hudatolah » Thu Mar 05, 2020 10:53 am

Code: Select all

tpl 1.14 module get_appfiledir_data;

metadata
     tree_path := 'Custom','ComputerSystem hosted documents';
end metadata; 

pattern getJsonFileDir 1.5
'''
Pattern for reading Json formatted data files on hosts. 
This pattern reads all files in a directory where the extension is "json"
'''
metadata
    origin := "CUSTOM"';
    tkn_name := "Get List of APM Apps from Host";
end metadata;

overview
    tags file;
end overview;

constants 
        filepathUnix := raw "/opt/schwabAppFiles";
        filepathWin := raw "C:\myfolder\schwabAppFiles";
end constants;

triggers
     on host := Host created, confirmed where os_class = "UNIX" or os_class = "Windows";
end triggers;

body 
    documenttype := none;
    workingDirectory := none; 
    osSep := none;
    basePath := none; 
    newAppFiles := [];

     if host.os_class = "Windows" then
               workingDirectoryListing := discovery.listDirectory(host, filepathWin);
               osSep := "\\"; 
               basePath := filepathWin + osSep; 
     else
              workingDirectoryListing := discovery.listDirectory(host, filepathWin);
              osSep := "/";
              basePath := filepathUnix + osSep; 
    end if; 
    
    if workingDirectoryListing then
       for f in workingDirectoryListing do
                 if f['file_type'] = "File" and f['size'] < 5120 and text.lower(f['name'])  has substring ".json" then
                 appfile := discovery.fileGet(host, basePath + f['name']); 
                  
                        if appfile and appfile.content then
                            documenttype := "APMAppFileDirJSON";
                            
                            file_table := table();
                            file_table["name"] := f['name'];
                            file_table["content"] := appfile.content;
                            file_table["type"] := documenttype; 
                            
                            list.append(newAppfiles, file_table);
                         end if; 
                     end if; 
                     end for;
                     
                     host.appfiles := newAppFiles; 
                     model.addDisplayAttribute(host, "appfiles");
                    
     end if
    
    end body; 
   end pattern; 
    
The Blackholesurfer. My surfboard has teeth.

hudatolah
Site Admin
Posts: 143
Joined: Thu Apr 04, 2013 8:10 pm
Are You a Headhunter?: Affirmative
Surfer?: Yes

Re: Read a directory

Post by hudatolah » Thu Mar 05, 2020 11:20 am

This also needs an active pattern for BMC.CORE:BMC_Document.
As follows:

Code: Select all

tpl.1.12 module CDMB.Host_Document; 

from CMDB.Host_ComputerSystem import Host_ComputerSystem 2.2; 
// make sure to check the parent pattern for version
//This version number must the same as the active Host_ComputerSystem pattern

syncmapping Host_Document 1.0
"""
Maps Host to BMC_Document
"""
   overview
         tags CDMB, Extension;
   end overview;
   
   mapping from Host_ComputerSystem.host as host
   document -> BMC_Document; 
   end mapping; 
   
   body
         for file in host.appfiles do
              if file['type'] = "APMAppFileDirJSON" then
              computersystem := Host_ComputerSystem.computersystem; 
              
              //the key needs to be unique for each file, so we need to add filename to the key
              filename := file['name'];
              document_key := "%host.key%//DOC//%filename%";
              
              document := sync.BMC_Document
                    key                      := document_key,
                    Name                   := "%filename%",
                    NameFormat        := "HostName:DocumentName",
                    ShortDescription   := "%host.name%:%filename%",
                    FileContent          := file['content'], //Remember to create this custom field in BMC_Document class
                    DocumentType     := file['type'], 
                    Category              := "Software",
                    Type                    := "Software Distribution",
                    Item                     := "Package" 
                );
                
                sync.rel.BMC_Dependency(
                    Source                := computersystem, 
                    Destination         := document, 
                    Name                 := "HOSTEDAPPLICATIONDOCUMENT"
                    );
       end if;
      end for;
      end body;
      end syncmapping;

The Blackholesurfer. My surfboard has teeth.

Post Reply