boring_services 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 156bb67ea987370602ed678f3d8a525f6eeae09dcbc2a3ae8ac23fa85b474ca4
4
- data.tar.gz: 8ce1325011cac932711b77afed6a5d0307bebedaa4e52fc2b65d9a153daf2e91
3
+ metadata.gz: 1cf51b8c8239ca7c000dc2fabe043321c3a490070e762741eaa44307fa521047
4
+ data.tar.gz: cfa55da92f539c5d60e444f1f7f0d0a95af80a96194112c6754303fda8b7cf43
5
5
  SHA512:
6
- metadata.gz: 72bf5746276b30243a9b75b8091491afd3e58e5e8feac6b3a8f62bc7cafa8f9088d6d1f279f075b6f3f77d0f350017a4bf277e18a3ef4844d273293fb2f3b363
7
- data.tar.gz: 07ac07fe8b309a06ff23ca77a7681aab03c77d8ecc0fac5e86e6723c999eb05976827e2bafdd072f7dfc0eef700c731329402a627b6ab64d2492983aaf6d125f
6
+ metadata.gz: 668cc2b9f6e65930f5d7e6916dbe33ca42522e0e4139aef025c4918f328c7d73345bd449cdea3bc5bf0368fdd512268a959f92d2a87f2012d5c46d4d7ceaf782
7
+ data.tar.gz: d5b0158106e76199da437fa08fb6375a103614e68b884c451730b417c0879109ad0ff278ba0b27d635ef45843e7ff9006f6092c1d03740fa949d056f115e0900
@@ -11,40 +11,31 @@ module BoringServices
11
11
  # Get all hosts for a service
12
12
  # Returns array of host hashes with :host, :private_ip, :label keys
13
13
  def hosts_for(service_name)
14
- service = config.service_config(service_name.to_s)
15
- return [] unless service
14
+ services = config.services.select { |s| s['name'] == service_name.to_s }
15
+ return [] if services.empty?
16
16
 
17
- normalize_hosts(service['hosts'] || [])
17
+ services.flat_map { |service| normalize_service_hosts(service) }
18
18
  end
19
19
 
20
- # Get the connection IP for a service by region/label
21
- # Prefers private_ip, falls back to host
22
- # Label matching: "redis-eu" matches region "eu", "redis-us" matches "us"
23
- def host_for_region(service_name, region)
24
- return nil if region.to_s.strip.empty?
25
-
20
+ # Get host by exact label match
21
+ def host_by_label(service_name, label)
26
22
  hosts = hosts_for(service_name)
27
- host_entry = hosts.find do |h|
28
- label = h[:label].to_s.downcase
29
- region_str = region.to_s.downcase
30
- label == region_str || label.end_with?("-#{region_str}") || label.start_with?("#{region_str}-")
31
- end
32
-
23
+ host_entry = hosts.find { |h| h[:label] == label.to_s }
33
24
  return nil unless host_entry
34
25
 
35
26
  connection_ip(host_entry)
36
27
  end
37
28
 
38
- # Get the first available host for a service (prefers private_ip)
39
- def primary_host(service_name)
40
- hosts = hosts_for(service_name)
41
- return nil if hosts.empty?
42
-
43
- connection_ip(hosts.first)
29
+ # Get all hosts as a hash keyed by label
30
+ # { "redis-eu-gcp" => "10.8.0.10", "redis-us-aws" => "10.8.0.60" }
31
+ def hosts_by_label(service_name)
32
+ hosts_for(service_name).each_with_object({}) do |h, hash|
33
+ hash[h[:label]] = connection_ip(h) if h[:label]
34
+ end
44
35
  end
45
36
 
46
37
  # Get all connection IPs for a service (prefers private_ip for each)
47
- def all_hosts(service_name)
38
+ def all_ips(service_name)
48
39
  hosts_for(service_name).map { |h| connection_ip(h) }
49
40
  end
50
41
 
@@ -54,9 +45,9 @@ module BoringServices
54
45
  service&.dig('port')
55
46
  end
56
47
 
57
- # Build a Redis URL for a region
58
- def redis_url(region: nil, password: nil, db: 0)
59
- host = region ? host_for_region('redis', region) : primary_host('redis')
48
+ # Build a Redis URL for a specific label
49
+ def redis_url(label: nil, password: nil, db: 0)
50
+ host = label ? host_by_label('redis', label) : all_ips('redis').first
60
51
  return nil unless host
61
52
 
62
53
  port = port_for('redis') || 6379
@@ -65,12 +56,12 @@ module BoringServices
65
56
  end
66
57
 
67
58
  # Build memcached connection string (host:port,host:port format)
68
- def memcached_servers(region: nil)
69
- hosts = if region
70
- host = host_for_region('memcached', region)
59
+ def memcached_servers(label: nil)
60
+ hosts = if label
61
+ host = host_by_label('memcached', label)
71
62
  host ? [host] : []
72
63
  else
73
- all_hosts('memcached')
64
+ all_ips('memcached')
74
65
  end
75
66
 
76
67
  return nil if hosts.empty?
@@ -81,8 +72,20 @@ module BoringServices
81
72
 
82
73
  private
83
74
 
84
- # Normalize hosts array - handles both simple strings and hashes
85
- def normalize_hosts(hosts)
75
+ # Normalize hosts from a service config
76
+ # Handles: single host:, array hosts:, or hosts: as array of hashes
77
+ def normalize_service_hosts(service)
78
+ # Single host entry (production style)
79
+ if service['host']
80
+ return [{
81
+ host: service['host'],
82
+ private_ip: service['private_ip'],
83
+ label: service['label']
84
+ }]
85
+ end
86
+
87
+ # Array of hosts
88
+ hosts = service['hosts'] || []
86
89
  hosts.map do |h|
87
90
  if h.is_a?(Hash)
88
91
  {
@@ -1,3 +1,3 @@
1
1
  module BoringServices
2
- VERSION = '0.4.0'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  end
@@ -39,9 +39,14 @@ module BoringServices
39
39
 
40
40
  # Convenience methods - delegate to locator
41
41
 
42
- # Get Redis host for a region (e.g., "eu", "us")
43
- def redis_host(region = nil)
44
- region ? locator.host_for_region('redis', region) : locator.primary_host('redis')
42
+ # Get all Redis hosts as hash { label => private_ip }
43
+ def redis_hosts
44
+ locator.hosts_by_label('redis')
45
+ end
46
+
47
+ # Get Redis host by exact label
48
+ def redis_host(label)
49
+ locator.host_by_label('redis', label)
45
50
  end
46
51
 
47
52
  # Get Redis port
@@ -49,14 +54,19 @@ module BoringServices
49
54
  locator.port_for('redis') || 6379
50
55
  end
51
56
 
52
- # Build Redis URL
53
- def redis_url(region: nil, password: nil, db: 0)
54
- locator.redis_url(region: region, password: password, db: db)
57
+ # Build Redis URL for a label
58
+ def redis_url(label: nil, password: nil, db: 0)
59
+ locator.redis_url(label: label, password: password, db: db)
60
+ end
61
+
62
+ # Get all Memcached hosts as hash { label => private_ip }
63
+ def memcached_hosts
64
+ locator.hosts_by_label('memcached')
55
65
  end
56
66
 
57
- # Get Memcached host for a region
58
- def memcached_host(region = nil)
59
- region ? locator.host_for_region('memcached', region) : locator.primary_host('memcached')
67
+ # Get Memcached host by exact label
68
+ def memcached_host(label)
69
+ locator.host_by_label('memcached', label)
60
70
  end
61
71
 
62
72
  # Get Memcached port
@@ -65,13 +75,18 @@ module BoringServices
65
75
  end
66
76
 
67
77
  # Get Memcached servers string (host:port,host:port)
68
- def memcached_servers(region: nil)
69
- locator.memcached_servers(region: region)
78
+ def memcached_servers(label: nil)
79
+ locator.memcached_servers(label: label)
80
+ end
81
+
82
+ # Generic: get all hosts for any service as hash { label => ip }
83
+ def hosts_for(service)
84
+ locator.hosts_by_label(service)
70
85
  end
71
86
 
72
- # Generic: get host for any service by region
73
- def host_for(service, region = nil)
74
- region ? locator.host_for_region(service, region) : locator.primary_host(service)
87
+ # Generic: get host by exact label for any service
88
+ def host_for(service, label)
89
+ locator.host_by_label(service, label)
75
90
  end
76
91
 
77
92
  # Generic: get port for any service
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boring_services
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - BoringCache