Skip to content

Commit

Permalink
(PE-38814) add_compiler - Making primary_postgresql_host and avail_gr…
Browse files Browse the repository at this point in the history
…oup_letter optional (#468)

* (PE-38814) add_compiler - Making primary_postgresql_host and avail_group_letter optional

primary_postgresql_host, if not provided will be determined through get_peadm_config
avail_group_letter, is defaulting to A

* Updating spec tests for add compiler

Removing optional from avail_group_letter as not required with enum default value

* Updating reference.md

* Fixing linting issues

---------

Co-authored-by: Neil Anderson <neil.anderson@perforce.com>
  • Loading branch information
ragingra and ragingra authored Aug 12, 2024
1 parent cafbeb1 commit e610a2a
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 40 deletions.
6 changes: 5 additions & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,8 @@ Data type: `Enum['A', 'B']`

_ Either A or B; whichever of the two letter designations the compiler is being assigned to

Default value: `'A'`

##### <a name="-peadm--add_compiler--compiler_host"></a>`compiler_host`

Data type: `Peadm::SingleTargetSpec`
Expand All @@ -1597,10 +1599,12 @@ _ The hostname and certname of the primary Puppet server

##### <a name="-peadm--add_compiler--primary_postgresql_host"></a>`primary_postgresql_host`

Data type: `Peadm::SingleTargetSpec`
Data type: `Optional[Peadm::SingleTargetSpec]`

_ The hostname and certname of the PE-PostgreSQL server with availability group $avail_group_letter

Default value: `undef`

### <a name="peadm--add_database"></a>`peadm::add_database`

The peadm::add_database class.
Expand Down
27 changes: 24 additions & 3 deletions plans/add_compiler.pp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,41 @@
# @param primary_host _ The hostname and certname of the primary Puppet server
# @param primary_postgresql_host _ The hostname and certname of the PE-PostgreSQL server with availability group $avail_group_letter
plan peadm::add_compiler(
Enum['A', 'B'] $avail_group_letter,
Enum['A', 'B'] $avail_group_letter = 'A' ,
Optional[String[1]] $dns_alt_names = undef,
Peadm::SingleTargetSpec $compiler_host,
Peadm::SingleTargetSpec $primary_host,
Peadm::SingleTargetSpec $primary_postgresql_host,
Optional[Peadm::SingleTargetSpec] $primary_postgresql_host = undef,
) {
$compiler_target = peadm::get_targets($compiler_host, 1)
$primary_target = peadm::get_targets($primary_host, 1)
$primary_postgresql_target = peadm::get_targets($primary_postgresql_host, 1)

# Get current peadm config to determine where to setup additional rules for
# compiler's secondary PuppetDB instances
$peadm_config = run_task('peadm::get_peadm_config', $primary_target).first.value

if $primary_postgresql_host == undef {
# get the external PostgreSQL host for the specified availability group
$external_postgresql_host = $avail_group_letter ? {
'A' => $peadm_config['params']['primary_postgresql_host'],
default => $peadm_config['params']['replica_postgresql_host'],
}

# If the external_postgresql_host is undef, use the server for that availability group
$postgresql_host = $external_postgresql_host ? {
undef => $peadm_config['role-letter']['server'][$avail_group_letter],
default => $external_postgresql_host,
}

if $postgresql_host == undef {
fail_plan("No PostgreSQL host found for availability group ${avail_group_letter}")
}

$primary_postgresql_target = peadm::get_targets($postgresql_host, 1)
} else {
$primary_postgresql_target = peadm::get_targets($primary_postgresql_host, 1)
}

# Return the opposite server than the compiler to be added so it can be
# configured with the appropriate rules for Puppet Server access from
# compiler
Expand Down
129 changes: 93 additions & 36 deletions spec/plans/add_compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,133 @@

def allow_standard_non_returning_calls
allow_apply
allow_any_task
allow_any_command
execute_no_plan
allow_out_message
end

describe 'basic functionality' do
let(:params) do
{
'primary_host' => 'primary',
'compiler_host' => 'compiler',
'avail_group_letter' => 'A',
'primary_postgresql_host' => 'primary_postgresql',
}
end

let(:params_with_avail_group_b) do
params.merge({ 'avail_group_letter' => 'B' })
end

let(:params_with_primary_postgresql_host) do
params.merge({ 'primary_postgresql_host' => 'custom_postgresql' })
end

let(:cfg) do
{
'params' => {
'primary_host' => 'primary'
'primary_host' => 'primary',
'replica_host' => nil,
'primary_postgresql_host' => nil,
'replica_postgresql_host' => nil
},
'role-letter' => {
'server' => {
'A' => 'server_a',
'B' => 'server_b'
'B' => nil
},
'postgresql': {
'A' => nil,
'B' => nil
}
}
}
end
let(:certdata) { { 'certname' => 'primary', 'extensions' => { '1.3.6.1.4.1.34380.1.1.9813' => 'A' } } }

it 'runs successfully when no alt-names are specified' do
allow_standard_non_returning_calls

expect_task('peadm::get_peadm_config').always_return(cfg)
expect_task('peadm::get_psql_version').with_targets(['server_a'])

expect_plan('peadm::subplans::component_install')
expect_plan('peadm::util::copy_file').be_called_times(1)
expect_task('peadm::puppet_runonce').with_targets(['compiler'])
expect_task('peadm::puppet_runonce').with_targets(['server_a'])
expect(run_plan('peadm::add_compiler', params)).to be_ok
end

it 'handles different avail_group_letter values' do
allow_standard_non_returning_calls
cfg['role-letter']['server']['B'] = 'server_b'

expect_task('peadm::get_peadm_config').always_return(cfg)
expect_task('peadm::get_psql_version').with_targets(['server_b'])

# TODO: Due to difficulty mocking get_targets, with_params modifier has been commented out
expect_plan('peadm::subplans::component_install')
# .with_params({
# 'targets' => 'compiler',
# 'primary_host' => 'primary',
# 'avail_group_letter' => 'A',
# 'dns_alt_names' => nil,
# 'role' => 'pe_compiler'
# })
expect_plan('peadm::util::copy_file').be_called_times(1)
expect_task('peadm::puppet_runonce').with_targets(['compiler'])
expect_task('peadm::puppet_runonce').with_targets(['server_a'])
expect_task('peadm::puppet_runonce').with_targets(['server_b'])
expect(run_plan('peadm::add_compiler', params_with_avail_group_b)).to be_ok
end

it 'handles specified primary_postgresql_host' do
allow_standard_non_returning_calls

expect_task('peadm::get_peadm_config').always_return(cfg)
expect_task('peadm::get_psql_version').with_targets(['custom_postgresql'])

expect_plan('peadm::subplans::component_install')
expect_plan('peadm::util::copy_file').be_called_times(1)
expect_task('peadm::puppet_runonce').with_targets(['compiler'])
expect_task('peadm::puppet_runonce').with_targets(['custom_postgresql'])
expect(run_plan('peadm::add_compiler', params_with_primary_postgresql_host)).to be_ok
end

it 'handles external postgresql host group A' do
allow_standard_non_returning_calls
cfg['params']['primary_postgresql_host'] = 'external_postgresql'
cfg['params']['replica_postgresql_host'] = 'external_postgresql'

expect_task('peadm::get_peadm_config').always_return(cfg)
expect_task('peadm::get_psql_version').with_targets(['external_postgresql'])

expect_plan('peadm::subplans::component_install')
expect_plan('peadm::util::copy_file').be_called_times(1)
expect_task('peadm::puppet_runonce').with_targets(['compiler'])
expect_task('peadm::puppet_runonce').with_targets(['external_postgresql'])
expect(run_plan('peadm::add_compiler', params)).to be_ok
end

context 'with alt-names' do
let(:params2) do
params.merge({ 'dns_alt_names' => 'foo,bar' })
end

it 'runs successfully when alt-names are specified' do
allow_standard_non_returning_calls
expect_task('peadm::get_peadm_config').always_return(cfg)

# TODO: Due to difficulty mocking get_targets, with_params modifier has been commented out
expect_plan('peadm::subplans::component_install')
# .with_params({
# 'targets' => 'compiler',
# 'primary_host' => 'primary',
# 'avail_group_letter' => 'A',
# 'dns_alt_names' => 'foo,bar',
# 'role' => 'pe_compiler'
# })

expect_plan('peadm::util::copy_file').be_called_times(1)
expect(run_plan('peadm::add_compiler', params2)).to be_ok
end
it 'handles external postgresql host group A with replica' do
allow_standard_non_returning_calls
cfg['params']['primary_postgresql_host'] = 'external_postgresql'
cfg['role-letter']['server']['B'] = 'replica'

expect_task('peadm::get_peadm_config').always_return(cfg)
expect_task('peadm::get_psql_version').with_targets(['external_postgresql'])

expect_plan('peadm::subplans::component_install')
expect_plan('peadm::util::copy_file').be_called_times(1)
expect_task('peadm::puppet_runonce').with_targets(['compiler'])
expect_task('peadm::puppet_runonce').with_targets(['external_postgresql'])
expect_task('peadm::puppet_runonce').with_targets(['replica'])
expect(run_plan('peadm::add_compiler', params)).to be_ok
end

it 'handles external postgresql host group B' do
allow_standard_non_returning_calls
cfg['params']['replica_postgresql_host'] = 'replica_external_postgresql'

expect_task('peadm::get_peadm_config').always_return(cfg)
expect_task('peadm::get_psql_version').with_targets(['replica_external_postgresql'])

expect_plan('peadm::subplans::component_install')
expect_plan('peadm::util::copy_file').be_called_times(1)
expect_task('peadm::puppet_runonce').with_targets(['compiler'])
expect_task('peadm::puppet_runonce').with_targets(['replica_external_postgresql'])
expect_task('peadm::puppet_runonce').with_targets(['server_a'])
expect(run_plan('peadm::add_compiler', params_with_avail_group_b)).to be_ok
end
end
end

0 comments on commit e610a2a

Please sign in to comment.