Skip to content

Commit 04f0be4

Browse files
committed
(MODULES-11567): Enhance validation methods to resolve deferred values for validation in sqlserver_instance type
1 parent 4c3e5fb commit 04f0be4

File tree

5 files changed

+71
-7
lines changed

5 files changed

+71
-7
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ jobs:
6969
with:
7070
ref: ${{ github.event.pull_request.head.sha }}
7171

72-
- name: Activate Ruby 2.7
72+
- name: Activate Ruby 3.1
7373
uses: ruby/setup-ruby@v1
7474
with:
75-
ruby-version: "2.7"
75+
ruby-version: "3.1"
7676
bundler-cache: true
7777

7878
- name: Print bundle environment

.github/workflows/nightly.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ jobs:
6767
with:
6868
ref: ${{ github.event.pull_request.head.sha }}
6969

70-
- name: Activate Ruby 2.7
70+
- name: Activate Ruby 3.1
7171
uses: ruby/setup-ruby@v1
7272
with:
73-
ruby-version: "2.7"
73+
ruby-version: "3.1"
7474
bundler-cache: true
7575

7676
- name: Print bundle environment

lib/puppet/type/sqlserver_instance.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ def validate
131131
end
132132

133133
def set?(key)
134-
!self[key].nil? && !self[key].empty?
134+
self_key = resolve_deferred_value(self[key])
135+
!self_key.nil? && !self_key.empty?
135136
end
136137

137138
def validate_user_password_required(account, pass)
@@ -144,11 +145,11 @@ def validate_user_password_required(account, pass)
144145
end
145146

146147
def domain_or_local_user?(user)
147-
PuppetX::Sqlserver::ServerHelper.is_domain_or_local_user?(user, Facter.value(:hostname))
148+
PuppetX::Sqlserver::ServerHelper.is_domain_or_local_user?(resolve_deferred_value(user), Facter.value(:hostname))
148149
end
149150

150151
def strong_password?(key)
151-
password = self[key]
152+
password = resolve_deferred_value(self[key])
152153
return unless password
153154

154155
message_start = "Password for #{key} is not strong"
@@ -162,4 +163,12 @@ def strong_password?(key)
162163

163164
true
164165
end
166+
167+
# When preprocess_deferred is false, deferred values remain unresolved at the time of validation, causing it to fail.
168+
# To address this, following logic is added to explicitly resolve deferred values during validation
169+
def resolve_deferred_value(value)
170+
return value unless value.is_a?(Puppet::Pops::Evaluator::DeferredValue)
171+
172+
value.resolve
173+
end
165174
end

spec/acceptance/sqlserver_instance_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,35 @@ def sql_query_is_user_sysadmin(username)
134134
end
135135
end
136136
end
137+
138+
# Ensure that the instance can be created with deferred values
139+
# for service account and password, which are resolved at the time of
140+
# the instance creation.
141+
# This is useful for scenarios where the values are not known at the time
142+
# of the Puppet run, such as when using Hiera to fetch values from a
143+
# secure vault or when the values are dynamically generated.
144+
def ensure_sqlserver_instance_with_deferred_values
145+
inst_name = new_random_instance_name
146+
147+
pp = <<-MANIFEST
148+
sqlserver_instance{'#{inst_name}':
149+
agt_svc_account => Deferred('pick', ['nexus\\travis']),
150+
agt_svc_password => Deferred('pick', ['Hunter-2']),
151+
}
152+
MANIFEST
153+
154+
idempotent_apply(pp)
155+
end
156+
157+
context 'Deferred values' do
158+
it 'validate deferred values' do
159+
expect { ensure_sqlserver_instance_with_deferred_values }.not_to raise_error
160+
end
161+
162+
it 'apply deferred values' do
163+
ensure_sqlserver_instance_with_deferred_values
164+
165+
run_sql_query(run_sql_query_opts('MSSQLSERVER', sql_query_is_user_sysadmin('nexus\\travis'), 1))
166+
end
167+
end
137168
end

spec/unit/puppet/type/sqlserver_instance_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,28 @@
8484
end
8585
end
8686
end
87+
88+
describe 'agt_svc_account' do
89+
context 'when value is a deferred value' do
90+
let(:args) do
91+
basic_args.merge({ agt_svc_account: Puppet::Pops::Evaluator::DeferredValue.new(proc { 'nexus\\travis' }) })
92+
end
93+
94+
it 'validate' do
95+
subject = Puppet::Type.type(:sqlserver_instance).new(args)
96+
expect(subject.resolve_deferred_value(subject[:agt_svc_account])).to eq('nexus\\travis')
97+
end
98+
end
99+
100+
context 'when value is not a deferred value' do
101+
let(:args) do
102+
basic_args.merge({ agt_svc_account: 'nexus\\travis' })
103+
end
104+
105+
it 'validate' do
106+
subject = Puppet::Type.type(:sqlserver_instance).new(args)
107+
expect(subject.resolve_deferred_value(subject[:agt_svc_account])).to eq('nexus\\travis')
108+
end
109+
end
110+
end
87111
end

0 commit comments

Comments
 (0)