Skip to content

Commit 216dcbe

Browse files
authored
Merge pull request #52 from github/schema-version
feat: add `schema_version` key to groups for calculated yaml definitions
2 parents a5af7f9 + 58b661d commit 216dcbe

16 files changed

+121
-4
lines changed

Gemfile.lock

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
entitlements-app (1.1.0)
4+
entitlements-app (1.2.0)
55
concurrent-ruby (~> 1.3, >= 1.3.1)
66
faraday (~> 2.0)
77
logger (~> 1.6)
@@ -33,6 +33,7 @@ GEM
3333
crack (1.0.0)
3434
bigdecimal
3535
rexml
36+
date (3.4.1)
3637
debug (1.8.0)
3738
irb (>= 1.5.0)
3839
reline (>= 0.3.1)
@@ -67,7 +68,8 @@ GEM
6768
parser (3.3.1.0)
6869
ast (~> 2.4.1)
6970
racc
70-
psych (5.1.2)
71+
psych (5.2.3)
72+
date
7173
stringio
7274
public_suffix (5.0.5)
7375
racc (1.8.0)
@@ -131,7 +133,7 @@ GEM
131133
simplecov (< 1.0)
132134
simplecov-html (0.12.3)
133135
simplecov_json_formatter (0.1.4)
134-
stringio (3.1.0)
136+
stringio (3.1.4)
135137
tzinfo (2.0.6)
136138
concurrent-ruby (~> 1.0)
137139
unicode-display_width (2.5.0)

lib/entitlements/data/groups/calculated/yaml.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ def description
3434
parsed_data.fetch("description", "")
3535
end
3636

37+
# Standard interface: Get the schema version of this group.
38+
#
39+
# Takes no arguments.
40+
# Examples: "entitlements/v1", "entitlements/v1.2.3", "entitlements/1.2.3"
41+
# Format: namespace/major.minor.patch
42+
#
43+
# Returns a String with the schema version (k8s-style), or "entitlements/v1" if undefined.
44+
Contract C::None => String
45+
def schema_version
46+
schema_version = parsed_data.fetch("schema_version", "entitlements/v1").to_s
47+
48+
namespace, version = schema_version.split("/")
49+
50+
unless namespace.match?(/\A[a-zA-Z0-9]+\z/) && version.match?(/\A(v?\d+(\.\d+){0,2})\z/)
51+
raise "Invalid schema version format: #{schema_version} - Expected format is '<namespace>/<version>' " \
52+
"- Examples: entitlements/v1, entitlements/v1.2.3, entitlements/1.2.3"
53+
end
54+
55+
# rebuild the schema version string to ensure it's in the correct format and return it
56+
return "#{namespace}/#{version}"
57+
end
58+
3759
# Files can support modifiers that act independently of rules.
3860
# This returns the modifiers from the file as a hash.
3961
#

lib/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Entitlements
44
module Version
5-
VERSION = "1.1.0"
5+
VERSION = "1.2.0"
66
end
77
end

spec/unit/entitlements/data/groups/calculated/yaml_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,57 @@
3535
end
3636
end
3737

38+
describe "#schema_version" do
39+
it "returns the version string when one is set" do
40+
filename = fixture("ldap-config/filters/no-filters-with-schema-version.yaml")
41+
subject = described_class.new(filename: filename)
42+
expect(subject.schema_version).to eq("entitlements/1.2.3")
43+
end
44+
45+
it "returns the version string when one is set without the patch" do
46+
filename = fixture("ldap-config/filters/no-filters-with-schema-version-no-patch.yaml")
47+
subject = described_class.new(filename: filename)
48+
expect(subject.schema_version).to eq("entitlements/1.2")
49+
end
50+
51+
it "returns the version string when one is set with just the major version" do
52+
filename = fixture("ldap-config/filters/no-filters-with-schema-version-major.yaml")
53+
subject = described_class.new(filename: filename)
54+
expect(subject.schema_version).to eq("entitlements/1")
55+
end
56+
57+
it "returns the version string when one is set with just the major version (with v prefix)" do
58+
filename = fixture("ldap-config/filters/no-filters-with-schema-version-major-with-v.yaml")
59+
subject = described_class.new(filename: filename)
60+
expect(subject.schema_version).to eq("entitlements/v1")
61+
end
62+
63+
64+
it "returns the version string when one is set (with v prefix)" do
65+
filename = fixture("ldap-config/filters/no-filters-with-schema-version-with-v.yaml")
66+
subject = described_class.new(filename: filename)
67+
expect(subject.schema_version).to eq("entitlements/v1.2.3")
68+
end
69+
70+
it "returns the default version when schema_version is undefined" do
71+
filename = fixture("ldap-config/filters/no-filters-description.yaml")
72+
subject = described_class.new(filename: filename)
73+
expect(subject.schema_version).to eq("entitlements/v1")
74+
end
75+
76+
it "throws an error when an invalid schema_version string is provided" do
77+
filename = fixture("ldap-config/filters/no-filters-with-bad-schema-version.yaml")
78+
subject = described_class.new(filename: filename)
79+
expect { subject.schema_version }.to raise_error(RuntimeError, /Invalid schema version format/)
80+
end
81+
82+
it "throws an error when the version string is missing the namespace" do
83+
filename = fixture("ldap-config/filters/no-filters-with-missing-version-namespace.yaml")
84+
subject = described_class.new(filename: filename)
85+
expect { subject.schema_version }.to raise_error(RuntimeError, /Invalid schema version format/)
86+
end
87+
end
88+
3889
describe "#initialize_filters" do
3990
it "returns the default filter hash when no filters are defined" do
4091
filename = fixture("ldap-config/filters/no-filters.yaml")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
description: Yo kittens
2+
schema_version: entitlements/1.abc2.3
3+
rules:
4+
or:
5+
- username: russianblue
6+
- username: BlackManx
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
description: Yo kittens
2+
schema_version: 1.2.3
3+
rules:
4+
or:
5+
- username: russianblue
6+
- username: BlackManx
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
description: Yo kittens
2+
schema_version: entitlements/v1
3+
rules:
4+
or:
5+
- username: russianblue
6+
- username: BlackManx
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
description: Yo kittens
2+
schema_version: entitlements/1
3+
rules:
4+
or:
5+
- username: russianblue
6+
- username: BlackManx
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
description: Yo kittens
2+
schema_version: entitlements/1.2
3+
rules:
4+
or:
5+
- username: russianblue
6+
- username: BlackManx
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
description: Yo kittens
2+
schema_version: entitlements/v1.2.3
3+
rules:
4+
or:
5+
- username: russianblue
6+
- username: BlackManx

0 commit comments

Comments
 (0)