Skip to content

Commit

Permalink
Allow managing vhosts from Hiera
Browse files Browse the repository at this point in the history
  • Loading branch information
jay7x committed Dec 16, 2024
1 parent cf6f869 commit 1ded83c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
29 changes: 29 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

* [`caddy::vhost`](#caddy--vhost): This defined type handles the Caddy virtual hosts.

### Data types

* [`Caddy::VirtualHost`](#Caddy--VirtualHost): Caddy virtual host type

## Classes

### <a name="caddy"></a>`caddy`
Expand Down Expand Up @@ -92,6 +96,7 @@ The following parameters are available in the `caddy` class:
* [`caddyfile_content`](#-caddy--caddyfile_content)
* [`config_dir`](#-caddy--config_dir)
* [`purge_config_dir`](#-caddy--purge_config_dir)
* [`vhosts`](#-caddy--vhosts)

##### <a name="-caddy--version"></a>`version`

Expand Down Expand Up @@ -384,6 +389,14 @@ Whether to purge Caddy config directory.

Default value: `true`

##### <a name="-caddy--vhosts"></a>`vhosts`

Data type: `Hash[String[1], Caddy::VirtualHost]`

List of virtual hosts to create.

Default value: `{}`

## Defined types

### <a name="caddy--vhost"></a>`caddy::vhost`
Expand Down Expand Up @@ -449,3 +462,19 @@ Where to store the vhost config file

Default value: `$caddy::config_dir`

## Data types

### <a name="Caddy--VirtualHost"></a>`Caddy::VirtualHost`

Caddy virtual host type

Alias of

```puppet
Struct[{
ensure => Optional[Enum['absent', 'present']],
source => Optional[Stdlib::Filesource],
content => Optional[String[1]],
}]
```

10 changes: 10 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@
# @param purge_config_dir
# Whether to purge Caddy config directory.
#
# @param vhosts
# List of virtual hosts to create.
#
class caddy (
String[1] $version = '2.0.0',
Optional[Enum['github','repo']] $install_method = undef,
Expand Down Expand Up @@ -163,6 +166,7 @@
Optional[Stdlib::Filesource] $caddyfile_source = undef,
Optional[String[1]] $caddyfile_content = undef,
Boolean $purge_config_dir = true,
Hash[String[1], Caddy::VirtualHost] $vhosts = {},
) {
case $caddy_architecture {
'x86_64', 'amd64': { $arch = 'amd64' }
Expand Down Expand Up @@ -199,6 +203,12 @@
contain caddy::config
contain caddy::service

$vhosts.each |String[1] $name, Caddy::VirtualHost $vhost| {
caddy::vhost { $name:
* => $vhost,
}
}

Class['caddy::install']
-> Class['caddy::config']
~> Class['caddy::service']
Expand Down
22 changes: 22 additions & 0 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,28 @@
is_expected.to contain_file('/etc/caddy/Caddyfile').with_source('http://example.com/Caddyfile').with_content(nil)
end
end

context 'with vhosts set' do
let(:params) do
{
vhosts: {
'h1.example.com': {
source: 'http://example.com/test-example-com.conf',
},
'h2.example.com': {
content: "localhost:1234{\n file_server\n}\n",
},
'h3.example.com': {
ensure: 'absent',
}
}
}
end

it { is_expected.to contain_file('/etc/caddy/config/h1.example.com.conf').with_source('http://example.com/test-example-com.conf') }
it { is_expected.to contain_file('/etc/caddy/config/h2.example.com.conf').with_content("localhost:1234{\n file_server\n}\n") }
it { is_expected.to contain_file('/etc/caddy/config/h3.example.com.conf').with_ensure('absent') }
end
end
end
end
6 changes: 6 additions & 0 deletions types/virtualhost.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# @summary Caddy virtual host type
type Caddy::VirtualHost = Struct[{
ensure => Optional[Enum['absent', 'present']],
source => Optional[Stdlib::Filesource],
content => Optional[String[1]],
}]

0 comments on commit 1ded83c

Please sign in to comment.