public async Task <HttpResponseMessage> CreateVm([FromBody] CreateVm vM) { try { if (!ModelState.IsValid) { var result = new HttpResponseMessage() { StatusCode = HttpStatusCode.BadRequest, Content = new StringContent( "Model State is not valid", Encoding.UTF8, "application/json"), ReasonPhrase = "ModelState is not valid" }; return(result); } LogThis(EventLogEntryType.Information, "VM Create Request Submitted", 2, 1); vM.CreatedBy = await GetWapAdmin(vM.SubscriptionId); var cwdb = new CmpWapDb(); //*** Map WAP sub to ResProvGroupId var resourceProviderGroupName = cwdb.FetchDefaultResourceProviderGroupName(vM.SubscriptionId); if (null == resourceProviderGroupName) { throw new Exception("Could not locate DefaultResourceProviderGroupName for WAP subscription"); } vM.EnvResourceGroupName = resourceProviderGroupName; var cmpi = new VMServiceRepository(_eventLog); lock (vM) { //Insert app data to the DB cmpi.PerformAppDataOps(new CreateVm { VmAppName = vM.VmAppName, VmAppId = vM.VmAppId, SubscriptionId = vM.SubscriptionId, AccountAdminLiveEmailId = vM.AccountAdminLiveEmailId, VmRegion = vM.VmRegion }); //Submit VM information to the WAP DB vM = cmpi.SubmitVmRequest(vM); AddVmToList(vM); } LogThis(EventLogEntryType.Information, "VM Create Request Submitted OK", 2, 2); //return Ok(vM); return(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK }); } catch (Exception ex) { LogThis(ex, EventLogEntryType.Error, "CmpWapExtension.VmsController.CreateVm()", 100, 1); //return InternalServerError(ex); //throw new Microsoft.WindowsAzurePack.CmpWapExtension.Common.PortalException(ex.Message); var reason = "Exception while submitting request to Create VM : " + Regex.Replace(CmpCommon.Utilities.UnwindExceptionMessages(ex), @"\t|\n|\r", ""); // Making this as a bad request instead of Internal Server error because the reason phrase for Internal Server error is not able to be customized. var result = new HttpResponseMessage() { StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = reason }; return(result); } }
private void FetchDefaultResourceGroupTest() { var ctr = new CmpWapDb(); var result = ctr.FetchDefaultResourceProviderGroupName(""); }