public async Task <IActionResult> Post( [FromServices] PnPServerContext dbContext, [FromBody] NetworkDeviceType networkDeviceType ) { if (networkDeviceType == null) { return(BadRequest()); } var existingRecord = await dbContext.NetworkDeviceTypes .Where(x => x.Name.Equals(networkDeviceType.Name, StringComparison.OrdinalIgnoreCase) ) .FirstOrDefaultAsync(); if (existingRecord != null) { // TODO : Make it so bed request explains duplicate record return(BadRequest()); } await dbContext.NetworkDeviceTypes.AddAsync(networkDeviceType); await dbContext.SaveChangesAsync(); return(new CreatedAtRouteResult("GetNetworkDeviceType", new { id = networkDeviceType.Id }, networkDeviceType)); }
public async Task <IActionResult> Put( [FromServices] PnPServerContext dbContext, Guid id, [FromBody] Template template ) { if (template == null || template.Id != id) { return(BadRequest()); } var existingRecord = await dbContext.Templates.FirstOrDefaultAsync(x => x.Id == id); if (existingRecord == null) { return(NotFound()); } existingRecord.Name = template.Name; existingRecord.Content = template.Content; dbContext.Templates.Update(existingRecord); await dbContext.SaveChangesAsync(); return(new NoContentResult()); }
public async Task <IActionResult> Post( [FromServices] PnPServerContext dbContext, [FromBody] NetworkDeviceLink networkDeviceLink ) { if ( networkDeviceLink == null || networkDeviceLink.Id != Guid.Empty || networkDeviceLink.NetworkDevice == null || networkDeviceLink.NetworkDevice.Id == Guid.Empty || networkDeviceLink.ConnectedToDevice == null || networkDeviceLink.ConnectedToDevice.Id == Guid.Empty ) { return(BadRequest()); } var networkDevice = await dbContext.NetworkDevices .Include("Uplinks") .Where(x => x.Id == networkDeviceLink.NetworkDevice.Id) .FirstOrDefaultAsync(); if (networkDevice == null) { System.Diagnostics.Debug.WriteLine("Invalid network device " + networkDeviceLink.NetworkDevice.Id.ToString()); return(NotFound()); } var connectedToDevice = await dbContext.NetworkDevices .Where(x => x.Id == networkDeviceLink.ConnectedToDevice.Id) .FirstOrDefaultAsync(); if (connectedToDevice == null) { System.Diagnostics.Debug.WriteLine("Invalid connected to network device " + networkDeviceLink.NetworkDevice.Id.ToString()); return(NotFound()); } // TODO : Check other links to see if the source or destination ports are in use elsewhere var newLink = new NetworkDeviceLink { InterfaceIndex = networkDeviceLink.InterfaceIndex, ConnectedToDevice = connectedToDevice, ConnectedToInterfaceIndex = networkDeviceLink.ConnectedToInterfaceIndex }; networkDevice.Uplinks.Add(newLink); dbContext.NetworkDevices.Update(networkDevice); await dbContext.SaveChangesAsync(); newLink.NetworkDevice.Uplinks = null; return(new CreatedAtRouteResult("GetNetworkDeviceLink", new { id = newLink.Id }, newLink)); }
public async Task <IActionResult> Post( [FromServices] PnPServerContext dbContext, Guid templateId, [FromBody] TemplateConfiguration templateConfiguration ) { if ( templateId == Guid.Empty || templateConfiguration == null || ( templateConfiguration.Template != null && templateConfiguration.Template.Id != Guid.Empty && templateConfiguration.Template.Id != templateId ) || templateConfiguration.NetworkDevice == null || templateConfiguration.NetworkDevice.Id == Guid.Empty ) { System.Diagnostics.Debug.WriteLine("Invalid parameters passed"); return(BadRequest()); } var template = await dbContext.Templates.Where(x => x.Id == templateId).FirstOrDefaultAsync(); if (template == null) { System.Diagnostics.Debug.WriteLine("Invalid template id specified " + templateId.ToString()); return(NotFound()); } var networkDevice = await dbContext.NetworkDevices.Where(x => x.Id == templateConfiguration.NetworkDevice.Id).FirstOrDefaultAsync(); if (networkDevice == null) { System.Diagnostics.Debug.WriteLine("Invalid network device specified " + templateConfiguration.NetworkDevice.Id.ToString()); return(NotFound()); } templateConfiguration.Template = template; templateConfiguration.NetworkDevice = networkDevice; await dbContext.TemplateConfigurations.AddAsync(templateConfiguration); await dbContext.SaveChangesAsync(); // TODO : Better solution to handling cyclic reference for (var i = 0; i < templateConfiguration.Properties.Count; i++) { templateConfiguration.Properties[i].TemplateConfiguration = null; } return(new CreatedAtRouteResult("GetTemplateConfiguration", new { id = templateConfiguration.Id }, templateConfiguration)); }
public async Task <IActionResult> Delete( [FromServices] PnPServerContext dbContext, Guid id ) { var item = await dbContext.Templates.FirstOrDefaultAsync(x => x.Id == id); if (item == null) { return(NotFound()); } dbContext.Templates.Remove(item); await dbContext.SaveChangesAsync(); return(new NoContentResult()); }
public async Task <IActionResult> Delete( [FromServices] PnPServerContext dbContext, Guid id ) { var item = await dbContext.NetworkDeviceTypes.FindAsync(id); if (item == null) { return(NotFound()); } dbContext.NetworkDeviceTypes.Remove(item); await dbContext.SaveChangesAsync(); return(new NoContentResult()); }
public async Task <IActionResult> Post( [FromServices] PnPServerContext dbContext, Guid networkDeviceTypeId, [FromBody] NetworkInterface networkInterface ) { if (networkDeviceTypeId == Guid.Empty || networkInterface == null || string.IsNullOrEmpty(networkInterface.Name) || (networkInterface.DeviceType != null && networkInterface.DeviceType.Id != networkDeviceTypeId) ) { return(BadRequest()); } var networkDeviceType = await dbContext.NetworkDeviceTypes.Include("Interfaces").FirstOrDefaultAsync(x => x.Id == networkDeviceTypeId); if (networkDeviceType == null) { System.Diagnostics.Debug.WriteLine("Network device type " + networkDeviceTypeId.ToString() + " does not exist"); return(BadRequest()); } var existingRecord = await dbContext.NetworkInterfaces .FirstOrDefaultAsync(x => x.Name == networkInterface.Name && x.DeviceType.Id == networkDeviceTypeId ); if (existingRecord != null) { System.Diagnostics.Debug.WriteLine("Network interface " + networkInterface.Name + " for device type " + networkDeviceType.Name + " already exists"); return(BadRequest()); } networkDeviceType.Interfaces.Add(networkInterface); dbContext.Update(networkDeviceType); await dbContext.SaveChangesAsync(); // TODO : Come up with a better solution for coping with cyclic references between interface and device type networkInterface.DeviceType.Interfaces = null; return(new CreatedAtRouteResult("GetNetworkInterface", new { networkDeviceTypeId = networkDeviceTypeId, id = networkInterface.Id }, networkInterface)); }
public async Task <IActionResult> Post( [FromServices] PnPServerContext dbContext, [FromBody] NetworkDevice networkDevice ) { if (networkDevice == null || networkDevice.DeviceType == null || networkDevice.DeviceType.Id == null) { return(BadRequest()); } var existingRecord = await dbContext.NetworkDevices .Where(x => x.Hostname.Equals(networkDevice.Hostname, StringComparison.OrdinalIgnoreCase) && x.DomainName.Equals(networkDevice.DomainName, StringComparison.OrdinalIgnoreCase) ) .FirstOrDefaultAsync(); if (existingRecord != null) { System.Diagnostics.Debug.WriteLine("Network device with name " + networkDevice.Hostname + "." + networkDevice.DomainName + " already exists"); return(BadRequest()); } var networkDeviceType = await dbContext.NetworkDeviceTypes.FindAsync(networkDevice.DeviceType.Id); if (networkDeviceType == null) { System.Diagnostics.Debug.WriteLine("Network device type " + networkDevice.DeviceType.Id.ToString() + " does not exist"); return(BadRequest()); } networkDevice.DeviceType = networkDeviceType; await dbContext.NetworkDevices.AddAsync(networkDevice); await dbContext.SaveChangesAsync(); return(new CreatedAtRouteResult("GetNetworkDevice", new { id = networkDevice.Id }, networkDevice)); }
public async Task <IActionResult> Post( [FromServices] PnPServerContext dbContext, [FromBody] Template template ) { if (template == null || string.IsNullOrEmpty(template.Name) || string.IsNullOrEmpty(template.Content) || template.Id != Guid.Empty) { return(BadRequest()); } var existingRecord = await dbContext.Templates.FirstOrDefaultAsync(x => x.Name == template.Name); if (existingRecord != null) { // TODO : Proper error for duplicate name record return(BadRequest()); } await dbContext.Templates.AddAsync(template); await dbContext.SaveChangesAsync(); return(new CreatedAtRouteResult("GetTemplate", new { id = template.Id }, template)); }
public async Task <IActionResult> PostRange( [FromServices] PnPServerContext dbContext, Guid networkDeviceTypeId, [FromBody] PostNetworkInterfaceRangeViewModel range ) { // TODO : Additional verification on range validity if (networkDeviceTypeId == Guid.Empty || range == null || string.IsNullOrEmpty(range.Name) ) { return(BadRequest()); } var interfaceName = InterfaceName.tryParse(range.Name); if (interfaceName == null) { System.Diagnostics.Debug.WriteLine("Invalid format for interface name : " + range.Name); return(BadRequest()); } var networkDeviceType = await dbContext.NetworkDeviceTypes.Include("Interfaces").FirstOrDefaultAsync(x => x.Id == networkDeviceTypeId); if (networkDeviceType == null) { System.Diagnostics.Debug.WriteLine("Network device type " + networkDeviceTypeId.ToString() + " does not exist"); return(BadRequest()); } var interfaceList = new List <string>(); var networkInterfaceRecordList = new List <NetworkInterface>(); for (var i = 0; i < range.Count; i++) { var newInterfaceName = interfaceName.subsequent(i).ToString(); interfaceList.Add(newInterfaceName); networkInterfaceRecordList.Add(new NetworkInterface { Name = newInterfaceName, InterfaceIndex = range.FirstIndex + i }); } var conflictingRecords = await dbContext.NetworkInterfaces .Where(x => interfaceList.Contains(x.Name, StringComparer.OrdinalIgnoreCase) && x.DeviceType.Id == networkDeviceTypeId ) .ToListAsync(); if (conflictingRecords.Count() != 0) { System.Diagnostics.Debug.WriteLine("Conflicting network interface names found"); return(BadRequest()); } networkDeviceType.Interfaces.AddRange(networkInterfaceRecordList); dbContext.Update(networkDeviceType); await dbContext.SaveChangesAsync(); foreach (var networkInterface in networkInterfaceRecordList) { networkInterface.DeviceType.Interfaces = null; } return(new CreatedAtRouteResult("GetNetworkInterfaces", networkInterfaceRecordList)); }