Page 1 of 1

Read a directory

Posted: Thu Mar 05, 2020 10:53 am
by hudatolah

Code: Select all

tpl 1.14 module get_appfiledir_data;

     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"
    origin := "CUSTOM"';
    tkn_name := "Get List of APM Apps from Host";
end metadata;

    tags file;
end overview;

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

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

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

     if host.os_class = "Windows" then
               workingDirectoryListing := discovery.listDirectory(host, filepathWin);
               osSep := "\\"; 
               basePath := filepathWin + osSep; 
              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; 

Re: Read a directory

Posted: Thu Mar 05, 2020 11:20 am
by hudatolah
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
         tags CDMB, Extension;
   end overview;
   mapping from as host
   document -> BMC_Document; 
   end mapping; 
         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   := "",
                    FileContent          := file['content'], //Remember to create this custom field in BMC_Document class
                    DocumentType     := file['type'], 
                    Category              := "Software",
                    Type                    := "Software Distribution",
                    Item                     := "Package" 
                    Source                := computersystem, 
                    Destination         := document, 
                    Name                 := "HOSTEDAPPLICATIONDOCUMENT"
       end if;
      end for;
      end body;
      end syncmapping;