public object PostManage(string api_version, [FromBody]JObject data) { HttpRequestAuth reqauth = new HttpRequestAuth(Request); if (!reqauth.IsHeaderVerfied() ) { return new { stat = Constants.ApiStatFailure, code = Constants.ApiErrorCodeInvalidParameter, message = "Invalid Parameters. Wrong headers" }; } if (!reqauth.IsTokenAuthenticated()) { return new { stat = Constants.ApiStatFailure, code = Constants.ApiErrorCodeUnauthorized, message = "No Token" }; } var entities = data["entities"]; VirtualMachineManager vmm; try { var credentials = new TokenCredentials(reqauth.GetTokenCache()); vmm = new VirtualMachineManager(credentials, reqauth.GetSubID()); } catch (CloudException ce) { return new { stat = Constants.ApiStatFailure, code =Constants.ApiErrorCodeUnauthorized, message = ce.Message }; } catch (Exception e) { return new { stat = Constants.ApiStatFailure, code = Constants.ApiErrorCodeUnknownExecutionError, message = e.Message }; } List<Dictionary<string,string>> result_entities = new List<Dictionary<string,string>>(); foreach (var entity in entities) { string action = entity["action"].ToString(); string name = entity["name"].ToString(); string resourcegroup = entity["resourcegroup"].ToString(); string status = string.Empty; string message = string.Empty; try { switch(action){ case "start": vmm.StartVirtualMachine(resourcegroup,name); break; case "stop": vmm.StopVirtualMachine(resourcegroup,name); break; case "restart": vmm.RestartVirtualMachine(resourcegroup,name); break; default: status = Constants.ApiStatFailure; message = "Invalid action param! Need to be either start, stop, or restart"; break; } status = Constants.ApiStatOK; message = "OK"; } catch (Exception e) { status = Constants.ApiStatFailure; message = e.Message; } Dictionary<string, string> result_entity = new Dictionary<string, string>(); result_entity.Add("name", name); result_entity.Add("resourcegroup", resourcegroup); result_entity.Add("action", action); result_entity.Add("status", status); result_entity.Add("message", message); result_entities.Add(result_entity); } return new { stat = Constants.ApiStatOK, result = result_entities }; }
public object GetListAll(string api_version) { HttpRequestAuth reqauth = new HttpRequestAuth(Request); if (!reqauth.IsHeaderVerfied() ) { return new { stat = Constants.ApiStatFailure, code = Constants.ApiErrorCodeInvalidParameter, message = "Invalid Parameters. Wrong headers" }; } if (!reqauth.IsTokenAuthenticated()) { return new { stat = Constants.ApiStatFailure, code = Constants.ApiErrorCodeUnauthorized, message = "No Token" }; } try { var credentials = new TokenCredentials(reqauth.GetTokenCache()); var vmm = new VirtualMachineManager(credentials, reqauth.GetSubID()); List<VirtualMachineObject> vmos = vmm.GetAllVirtualMachines(); return new { stat = Constants.ApiStatOK, result = vmos }; } catch (CloudException ce) { return new { stat = Constants.ApiStatFailure, code =Constants.ApiErrorCodeUnauthorized, message = ce.Message }; } catch (Exception e) { return new { stat = Constants.ApiStatFailure, code = Constants.ApiErrorCodeUnknownExecutionError, message = e.Message }; } }