Skip to content

Commit

Permalink
feat: allow attaching firewalls to the server
Browse files Browse the repository at this point in the history
Closes #157
  • Loading branch information
jooola committed Aug 13, 2024
1 parent 7bcfdcf commit bfde81c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .web-docs/components/builder/hcloud/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ builder.

- `public_ipv4_disabled` (bool) - Disable the public ipv6 for the created server.

- `firewalls` (array of strings) - List of Firewall by name or id to be attached
to the created server.

## Basic Example

Here is a basic example. It is completely valid as soon as you enter your own
Expand Down
11 changes: 6 additions & 5 deletions builder/hcloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ type Config struct {
SSHKeys []string `mapstructure:"ssh_keys"`
SSHKeysLabels map[string]string `mapstructure:"ssh_keys_labels"`

Networks []int64 `mapstructure:"networks"`
PublicIPv4 string `mapstructure:"public_ipv4"`
PublicIPv4Disabled bool `mapstructure:"public_ipv4_disabled"`
PublicIPv6 string `mapstructure:"public_ipv6"`
PublicIPv6Disabled bool `mapstructure:"public_ipv6_disabled"`
Networks []int64 `mapstructure:"networks"`
PublicIPv4 string `mapstructure:"public_ipv4"`
PublicIPv4Disabled bool `mapstructure:"public_ipv4_disabled"`
PublicIPv6 string `mapstructure:"public_ipv6"`
PublicIPv6Disabled bool `mapstructure:"public_ipv6_disabled"`
Firewalls []string `mapstructure:"firewalls"`

RescueMode string `mapstructure:"rescue"`

Expand Down
2 changes: 2 additions & 0 deletions builder/hcloud/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions builder/hcloud/step_create_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
sshKeys = append(sshKeys, sshKey)
}

firewalls := make([]*hcloud.ServerCreateFirewall, 0, len(c.Firewalls))
for _, value := range c.Firewalls {
firewall, _, err := client.Firewall.Get(ctx, value)
if err != nil {
return errorHandler(state, ui, fmt.Sprintf("Could not fetch firewall '%s'", k), err)

Check failure on line 59 in builder/hcloud/step_create_server.go

View workflow job for this annotation

GitHub Actions / acceptance (ubuntu-latest)

undefined: k

Check failure on line 59 in builder/hcloud/step_create_server.go

View workflow job for this annotation

GitHub Actions / unit (ubuntu-latest)

undefined: k

Check failure on line 59 in builder/hcloud/step_create_server.go

View workflow job for this annotation

GitHub Actions / acceptance (windows-latest)

undefined: k

Check failure on line 59 in builder/hcloud/step_create_server.go

View workflow job for this annotation

GitHub Actions / unit (windows-latest)

undefined: k

Check failure on line 59 in builder/hcloud/step_create_server.go

View workflow job for this annotation

GitHub Actions / acceptance (macos-latest)

undefined: k

Check failure on line 59 in builder/hcloud/step_create_server.go

View workflow job for this annotation

GitHub Actions / unit (macos-latest)

undefined: k
}
if firewall == nil {
return errorHandler(state, ui, "", fmt.Errorf("Could not find firewall '%s'", k))

Check failure on line 62 in builder/hcloud/step_create_server.go

View workflow job for this annotation

GitHub Actions / acceptance (ubuntu-latest)

undefined: k

Check failure on line 62 in builder/hcloud/step_create_server.go

View workflow job for this annotation

GitHub Actions / unit (ubuntu-latest)

undefined: k

Check failure on line 62 in builder/hcloud/step_create_server.go

View workflow job for this annotation

GitHub Actions / acceptance (windows-latest)

undefined: k

Check failure on line 62 in builder/hcloud/step_create_server.go

View workflow job for this annotation

GitHub Actions / unit (windows-latest)

undefined: k

Check failure on line 62 in builder/hcloud/step_create_server.go

View workflow job for this annotation

GitHub Actions / acceptance (macos-latest)

undefined: k

Check failure on line 62 in builder/hcloud/step_create_server.go

View workflow job for this annotation

GitHub Actions / unit (macos-latest)

undefined: k
}
firewalls = append(firewalls, &hcloud.ServerCreateFirewall{Firewall: *firewall})
}

var image *hcloud.Image
var err error
if c.Image != "" {
Expand All @@ -77,6 +89,7 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu
Name: c.ServerName,
ServerType: &hcloud.ServerType{Name: c.ServerType},
Image: image,
Firewalls: firewalls,
SSHKeys: sshKeys,
Location: &hcloud.Location{Name: c.Location},
UserData: userData,
Expand Down
3 changes: 3 additions & 0 deletions docs/builders/hcloud.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ builder.

- `public_ipv4_disabled` (bool) - Disable the public ipv6 for the created server.

- `firewalls` (array of strings) - List of Firewall by name or id to be attached
to the created server.

## Basic Example

Here is a basic example. It is completely valid as soon as you enter your own
Expand Down

0 comments on commit bfde81c

Please sign in to comment.