|
1 | | -using Microsoft.AspNetCore.Authorization; |
2 | | -using Microsoft.AspNetCore.Mvc; |
3 | | -using Microsoft.Extensions.Options; |
4 | | -using Cyclops.MultiCluster.Models.Api; |
5 | | -using Cyclops.MultiCluster.Services; |
6 | | - |
7 | | -namespace Cyclops.MultiCluster.Controllers |
8 | | -{ |
9 | | - /// <summary> |
10 | | - /// Host based operations happen here |
11 | | - /// </summary> |
12 | | - [ApiController] |
13 | | - [Authorize] |
14 | | - [Route("[controller]")] |
15 | | - public class HostController : ControllerBase |
16 | | - { |
17 | | - private readonly ILogger<HostController> _logger; |
18 | | - private readonly ICache _cache; |
19 | | - private readonly IOptions<MultiClusterOptions> _options; |
20 | | - |
21 | | - /// <summary> |
22 | | - /// Host based operations constructor |
23 | | - /// </summary> |
24 | | - /// <param name="logger"></param> |
25 | | - /// <param name="cache"></param> |
26 | | - /// <param name="options"></param> |
27 | | - public HostController(ILogger<HostController> logger, ICache cache, IOptions<MultiClusterOptions> options) |
28 | | - { |
29 | | - _logger = logger; |
30 | | - _cache = cache; |
31 | | - _options = options; |
32 | | - } |
33 | | - |
34 | | - /// <summary> |
35 | | - /// Updates the host with the new ip's |
36 | | - /// </summary> |
37 | | - /// <param name="model"></param> |
38 | | - /// <returns></returns> |
39 | | - [HttpPost] |
40 | | - [ProducesResponseType(204)] |
41 | | - [ProducesResponseType(400)] |
42 | | - [ProducesResponseType(401)] |
43 | | - [ProducesResponseType(500)] |
44 | | - public async Task<ActionResult> UpdateHost([FromBody] HostModel model) |
45 | | - { |
46 | | - using var scope = _logger.BeginScope(new { hostname = model.Hostname }); |
47 | | - |
48 | | - _logger.LogInformation("Received host update"); |
49 | | - _logger.LogDebug("Model {@model}", model); |
50 | | - |
51 | | - try |
52 | | - { |
53 | | - if (!ModelState.IsValid) |
54 | | - { |
55 | | - _logger.LogWarning("Invalid request state {@model}", model); |
56 | | - return BadRequest(ModelState); |
57 | | - } |
58 | | - |
59 | | - var clusterIdentifier = User.Identity!.Name!; |
60 | | - var hostIPs = Array.Empty<Models.Core.HostIP>(); |
61 | | - |
62 | | - if (model.HostIPs != null && model.HostIPs.Any()) |
63 | | - { |
64 | | - hostIPs = model.HostIPs.Select(ip => new Models.Core.HostIP |
65 | | - { |
66 | | - IPAddress = ip.IPAddress, |
67 | | - Priority = ip.Priority, |
68 | | - Weight = ip.Weight, |
69 | | - ClusterIdentifier = clusterIdentifier |
70 | | - }).ToArray(); |
71 | | - } |
72 | | - |
73 | | - var cluster = await _cache.GetHostsAsync(clusterIdentifier); |
74 | | - var hosts = cluster?.Where(x => x.Hostname != model.Hostname).ToList() ?? new List<Models.Core.Host>(); |
75 | | - hosts.Add(new Models.Core.Host |
76 | | - { |
77 | | - Hostname = model.Hostname, |
78 | | - HostIPs = hostIPs |
79 | | - }); |
80 | | - await _cache.SetClusterCacheAsync(clusterIdentifier, hosts.ToArray()); |
81 | | - |
82 | | - return NoContent(); |
83 | | - } |
84 | | - catch (Exception exception) |
85 | | - { |
86 | | - _logger.LogError(exception, "Error updating host with {@model}", model); |
87 | | - return base.Problem(exception.Message); |
88 | | - } |
89 | | - } |
90 | | - |
91 | | - /// <summary> |
92 | | - /// Get all hosts in this cluster |
93 | | - /// </summary> |
94 | | - /// <returns></returns> |
95 | | - [HttpGet] |
96 | | - [ProducesResponseType(typeof(HostModel[]), 200)] |
97 | | - [ProducesResponseType(404)] |
98 | | - public async Task<ActionResult<HostModel[]>> Get() |
99 | | - { |
100 | | - _logger.LogInformation("Getting a list of local hosts"); |
101 | | - try |
102 | | - { |
103 | | - var clusterIdentifier = _options.Value.ClusterIdentifier; |
104 | | - var hosts = await _cache.GetHostsAsync(clusterIdentifier); |
105 | | - |
106 | | - if (hosts == null) |
107 | | - { |
108 | | - _logger.LogWarning("Hosts for the local cluster is not found."); |
109 | | - return NotFound(); |
110 | | - } |
111 | | - |
112 | | - var result = hosts.Select(host => new HostModel |
113 | | - { |
114 | | - HostIPs = host.HostIPs.Select(ip => new HostIP |
115 | | - { |
116 | | - IPAddress = ip.IPAddress, |
117 | | - Priority = ip.Priority, |
118 | | - Weight = ip.Weight |
119 | | - }).ToArray(), |
120 | | - Hostname = host.Hostname |
121 | | - }); |
122 | | - |
123 | | - return Ok(result); |
124 | | - } |
125 | | - catch (Exception exception) |
126 | | - { |
127 | | - _logger.LogError(exception, "Unable to get local cluster hosts"); |
128 | | - throw; |
129 | | - } |
130 | | - } |
131 | | - } |
132 | | -} |
| 1 | +using Microsoft.AspNetCore.Authorization; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using Microsoft.Extensions.Options; |
| 4 | +using Cyclops.MultiCluster.Models.Api; |
| 5 | +using Cyclops.MultiCluster.Services; |
| 6 | + |
| 7 | +namespace Cyclops.MultiCluster.Controllers |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Host based operations happen here |
| 11 | + /// </summary> |
| 12 | + [ApiController] |
| 13 | + [Authorize] |
| 14 | + [Route("[controller]")] |
| 15 | + public class HostController : ControllerBase |
| 16 | + { |
| 17 | + private readonly ILogger<HostController> _logger; |
| 18 | + private readonly ICache _cache; |
| 19 | + private readonly IOptions<MultiClusterOptions> _options; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Host based operations constructor |
| 23 | + /// </summary> |
| 24 | + /// <param name="logger"></param> |
| 25 | + /// <param name="cache"></param> |
| 26 | + /// <param name="options"></param> |
| 27 | + public HostController(ILogger<HostController> logger, ICache cache, IOptions<MultiClusterOptions> options) |
| 28 | + { |
| 29 | + _logger = logger; |
| 30 | + _cache = cache; |
| 31 | + _options = options; |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Updates the host with the new ip's |
| 36 | + /// </summary> |
| 37 | + /// <param name="model"></param> |
| 38 | + /// <returns></returns> |
| 39 | + [HttpPost] |
| 40 | + [ProducesResponseType(204)] |
| 41 | + [ProducesResponseType(400)] |
| 42 | + [ProducesResponseType(401)] |
| 43 | + [ProducesResponseType(500)] |
| 44 | + public async Task<ActionResult> UpdateHost([FromBody] HostModel model) |
| 45 | + { |
| 46 | + using var scope = _logger.BeginScope(new { hostname = model.Hostname }); |
| 47 | + |
| 48 | + _logger.LogInformation("Received host update"); |
| 49 | + _logger.LogDebug("Model {@model}", model); |
| 50 | + |
| 51 | + try |
| 52 | + { |
| 53 | + if (!ModelState.IsValid) |
| 54 | + { |
| 55 | + _logger.LogWarning("Invalid request state {@model}", model); |
| 56 | + return BadRequest(ModelState); |
| 57 | + } |
| 58 | + |
| 59 | + var clusterIdentifier = User.Identity!.Name!; |
| 60 | + var hostIPs = Array.Empty<Models.Core.HostIP>(); |
| 61 | + |
| 62 | + if (model.HostIPs != null && model.HostIPs.Any()) |
| 63 | + { |
| 64 | + hostIPs = model.HostIPs.Select(ip => new Models.Core.HostIP |
| 65 | + { |
| 66 | + IPAddress = ip.IPAddress, |
| 67 | + Priority = ip.Priority, |
| 68 | + Weight = ip.Weight, |
| 69 | + ClusterIdentifier = clusterIdentifier |
| 70 | + }).ToArray(); |
| 71 | + } |
| 72 | + |
| 73 | + var cluster = await _cache.GetHostsAsync(clusterIdentifier); |
| 74 | + var hosts = cluster?.Where(x => x.Hostname != model.Hostname).ToList() ?? new List<Models.Core.Host>(); |
| 75 | + |
| 76 | + if (hostIPs.Length > 0) |
| 77 | + { |
| 78 | + hosts.Add(new Models.Core.Host |
| 79 | + { |
| 80 | + Hostname = model.Hostname, |
| 81 | + HostIPs = hostIPs |
| 82 | + }); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + _logger.LogInformation("Removing host {hostname} from cluster cache for {clusterIdentifier}", model.Hostname, clusterIdentifier); |
| 87 | + } |
| 88 | + |
| 89 | + await _cache.SetClusterCacheAsync(clusterIdentifier, hosts.ToArray()); |
| 90 | + |
| 91 | + return NoContent(); |
| 92 | + } |
| 93 | + catch (Exception exception) |
| 94 | + { |
| 95 | + _logger.LogError(exception, "Error updating host with {@model}", model); |
| 96 | + return base.Problem(exception.Message); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Get all hosts in this cluster |
| 102 | + /// </summary> |
| 103 | + /// <returns></returns> |
| 104 | + [HttpGet] |
| 105 | + [ProducesResponseType(typeof(HostModel[]), 200)] |
| 106 | + [ProducesResponseType(404)] |
| 107 | + public async Task<ActionResult<HostModel[]>> Get() |
| 108 | + { |
| 109 | + _logger.LogInformation("Getting a list of local hosts"); |
| 110 | + try |
| 111 | + { |
| 112 | + var clusterIdentifier = _options.Value.ClusterIdentifier; |
| 113 | + var hosts = await _cache.GetHostsAsync(clusterIdentifier); |
| 114 | + |
| 115 | + if (hosts == null) |
| 116 | + { |
| 117 | + _logger.LogWarning("Hosts for the local cluster is not found."); |
| 118 | + return NotFound(); |
| 119 | + } |
| 120 | + |
| 121 | + var result = hosts.Select(host => new HostModel |
| 122 | + { |
| 123 | + HostIPs = host.HostIPs.Select(ip => new HostIP |
| 124 | + { |
| 125 | + IPAddress = ip.IPAddress, |
| 126 | + Priority = ip.Priority, |
| 127 | + Weight = ip.Weight |
| 128 | + }).ToArray(), |
| 129 | + Hostname = host.Hostname |
| 130 | + }); |
| 131 | + |
| 132 | + return Ok(result); |
| 133 | + } |
| 134 | + catch (Exception exception) |
| 135 | + { |
| 136 | + _logger.LogError(exception, "Unable to get local cluster hosts"); |
| 137 | + throw; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments