werecloud.api.controller.v1_0_0.VirtualMachineController.java Source code

Java tutorial

Introduction

Here is the source code for werecloud.api.controller.v1_0_0.VirtualMachineController.java

Source

/*
  werecloud provides a simple API to the VMWare vCenter data.
  Copyright (C) 2015  NigelB
    
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.
    
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
    
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package werecloud.api.controller.v1_0_0;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import werecloud.api.bean.VMWareMacCache;

import java.util.Hashtable;
import java.util.Map;

/**
 * <code>VirtualMachineController</code>
 * Date: 28/12/2014
 * Time: 10:26 AM
 */
@Controller
@RequestMapping("1.0.0/vm")
public class VirtualMachineController {

    private VMWareMacCache con;

    public VirtualMachineController(VMWareMacCache con) {
        this.con = con;
    }

    @RequestMapping(value = "ping", method = RequestMethod.GET)
    public ModelAndView ping() {
        Map<String, Boolean> toRet = new Hashtable<String, Boolean>();
        toRet.put("ping", true);
        return new ModelAndView("json", "model", toRet);
    }

    @RequestMapping(value = "name/{mac}", method = RequestMethod.GET)
    public ModelAndView getVMNameFromMac(@PathVariable("mac") String mac) {
        return new ModelAndView("string", "string", String.format("%s %s\n", mac, con.getVM(mac)));
    }

    @RequestMapping(value = "name/{mac}.json", method = RequestMethod.GET)
    public ModelAndView getVMNameFromMacJSON(@PathVariable("mac") String mac) {
        Map<String, String> toRet = new Hashtable<String, String>();
        toRet.put(mac, con.getVM(mac));
        return new ModelAndView("json", "model", toRet);
    }
}