Search in sources :

Example 1 with VmwareConfig

use of org.opennms.netmgt.config.vmware.VmwareConfig in project opennms by OpenNMS.

the class VmwareRequisitionTool method main.

public static void main(String[] args) throws Exception {
    final Options options = new Options();
    final CommandLineParser parser = new PosixParser();
    final CommandLine cmd = parser.parse(options, args);
    @SuppressWarnings("unchecked") List<String> arguments = (List<String>) cmd.getArgList();
    if (arguments.size() < 1) {
        usage(options, cmd);
        System.exit(1);
    }
    // Internal trick to avoid confusions.
    String urlString = arguments.remove(0).replaceFirst("vmware", "http");
    URL url = new URL(urlString);
    // Parse vmware-config.xml and retrieve the credentials to avoid initialize Spring
    if (!url.getQuery().contains("username") && url.getUserInfo() == null) {
        File cfg = new File(ConfigFileConstants.getFilePathString(), "vmware-config.xml");
        if (cfg.exists()) {
            String username = null;
            String password = null;
            VmwareConfig config = JaxbUtils.unmarshal(VmwareConfig.class, cfg);
            for (VmwareServer srv : config.getVmwareServerCollection()) {
                if (srv.getHostname().equals(url.getHost())) {
                    username = srv.getUsername();
                    password = srv.getPassword();
                }
            }
            if (username == null || password == null) {
                throw new IllegalArgumentException("Can't retrieve credentials for " + url.getHost() + " from " + cfg);
            }
            // Add credentials to URL
            urlString = urlString + ";username=" + username + ";password=" + password;
            url = new URL(urlString);
        }
    }
    VmwareRequisitionUrlConnection c = new VmwareRequisitionUrlConnection(url) {

        @Override
        protected Requisition getExistingRequisition(String foreignSource) {
            // This is not elegant but it is necessary to avoid booting Spring
            File req = new File(ConfigFileConstants.getFilePathString(), "imports" + File.separator + foreignSource + ".xml");
            if (req.exists()) {
                return JaxbUtils.unmarshal(Requisition.class, req);
            }
            return null;
        }
    };
    c.connect();
    InputStream is = c.getInputStream();
    if (is == null) {
        System.err.println("Couldn't generate requisition from " + urlString);
        System.exit(1);
    } else {
        System.out.println(IOUtils.toString(is, StandardCharsets.UTF_8));
    }
}
Also used : Options(org.apache.commons.cli.Options) InputStream(java.io.InputStream) PosixParser(org.apache.commons.cli.PosixParser) VmwareConfig(org.opennms.netmgt.config.vmware.VmwareConfig) URL(java.net.URL) CommandLine(org.apache.commons.cli.CommandLine) VmwareServer(org.opennms.netmgt.config.vmware.VmwareServer) List(java.util.List) CommandLineParser(org.apache.commons.cli.CommandLineParser) File(java.io.File)

Aggregations

File (java.io.File)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 List (java.util.List)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 Options (org.apache.commons.cli.Options)1 PosixParser (org.apache.commons.cli.PosixParser)1 VmwareConfig (org.opennms.netmgt.config.vmware.VmwareConfig)1 VmwareServer (org.opennms.netmgt.config.vmware.VmwareServer)1