Skip to content

Commit d88c82a

Browse files
Merge pull request #60 from fnkr/firewalls
Add support for firewalls
2 parents 8565344 + 786c54d commit d88c82a

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

LICENSE

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

3-
Copyright (c) 2017-2020 The docker-machine-driver-hetzner team
3+
Copyright (c) 2017-2021 The docker-machine-driver-hetzner team
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ $ docker-machine create \
103103
- `--hetzner-volumes`: Volume IDs or names which should be attached to the server
104104
- `--hetzner-networks`: Network IDs or names which should be attached to the server private network interface
105105
- `--hetzner-use-private-network`: Use private network
106+
- `--hetzner-firewalls`: Firewall IDs or names which should be applied on the server
106107
- `--hetzner-server-label`: `key=value` pairs of additional metadata to assign to the server.
107108

108109
#### Existing SSH keys
@@ -134,6 +135,7 @@ was used during creation.
134135
| `--hetzner-additional-key` | `HETZNER_ADDITIONAL_KEYS` | - |
135136
| `--hetzner-user-data` | `HETZNER_USER_DATA` | - |
136137
| `--hetzner-networks` | `HETZNER_NETWORKS` | - |
138+
| `--hetzner-firewalls` | `HETZNER_FIREWALLS` | - |
137139
| `--hetzner-volumes` | `HETZNER_VOLUMES` | - |
138140
| `--hetzner-use-private-network` | `HETZNER_USE_PRIVATE_NETWORK` | false |
139141
| `--hetzner-server-label` | `HETZNER_SERVER_LABELS` | `[]` |

driver.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Driver struct {
4141
volumes []string
4242
networks []string
4343
UsePrivateNetwork bool
44+
firewalls []string
4445
cachedServer *hcloud.Server
4546
serverLabels map[string]string
4647

@@ -64,6 +65,7 @@ const (
6465
flagVolumes = "hetzner-volumes"
6566
flagNetworks = "hetzner-networks"
6667
flagUsePrivateNetwork = "hetzner-use-private-network"
68+
flagFirewalls = "hetzner-firewalls"
6769
flagAdditionalKeys = "hetzner-additional-key"
6870
flagServerLabel = "hetzner-server-label"
6971
)
@@ -151,6 +153,12 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
151153
Name: flagUsePrivateNetwork,
152154
Usage: "Use private network",
153155
},
156+
mcnflag.StringSliceFlag{
157+
EnvVar: "HETZNER_FIREWALLS",
158+
Name: flagFirewalls,
159+
Usage: "Firewall IDs or names which should be applied on the server",
160+
Value: []string{},
161+
},
154162
mcnflag.StringSliceFlag{
155163
EnvVar: "HETZNER_ADDITIONAL_KEYS",
156164
Name: flagAdditionalKeys,
@@ -179,6 +187,7 @@ func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
179187
d.volumes = opts.StringSlice(flagVolumes)
180188
d.networks = opts.StringSlice(flagNetworks)
181189
d.UsePrivateNetwork = opts.Bool(flagUsePrivateNetwork)
190+
d.firewalls = opts.StringSlice(flagFirewalls)
182191
d.additionalKeys = opts.StringSlice(flagAdditionalKeys)
183192

184193
err := d.setLabelsFromFlags(opts)
@@ -327,6 +336,7 @@ func (d *Driver) Create() error {
327336
UserData: d.userData,
328337
Labels: d.serverLabels,
329338
}
339+
330340
networks := []*hcloud.Network{}
331341
for _, networkIDorName := range d.networks {
332342
network, _, err := d.getClient().Network.Get(context.Background(), networkIDorName)
@@ -340,6 +350,19 @@ func (d *Driver) Create() error {
340350
}
341351
srvopts.Networks = networks
342352

353+
firewalls := []*hcloud.ServerCreateFirewall{}
354+
for _, firewallIDorName := range d.firewalls {
355+
firewall, _, err := d.getClient().Firewall.Get(context.Background(), firewallIDorName)
356+
if err != nil {
357+
return errors.Wrap(err, "could not get firewall by ID or name")
358+
}
359+
if firewall == nil {
360+
return errors.Errorf("firewall '%s' not found", firewallIDorName)
361+
}
362+
firewalls = append(firewalls, &hcloud.ServerCreateFirewall{Firewall: *firewall})
363+
}
364+
srvopts.Firewalls = firewalls
365+
343366
volumes := []*hcloud.Volume{}
344367
for _, volumeIDorName := range d.volumes {
345368
volume, _, err := d.getClient().Volume.Get(context.Background(), volumeIDorName)

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ require (
66
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
77
github.com/docker/docker v0.0.0-20181018193557-f7e5154f37a4 // indirect
88
github.com/docker/machine v0.16.2
9-
github.com/google/go-cmp v0.3.0 // indirect
10-
github.com/hetznercloud/hcloud-go v1.17.0
9+
github.com/hetznercloud/hcloud-go v1.24.0
1110
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
1211
github.com/pkg/errors v0.8.1
1312
github.com/sirupsen/logrus v1.4.2 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ github.com/docker/machine v0.16.2 h1:jyF9k3Zg+oIGxxSdYKPScyj3HqFZ6FjgA/3sblcASiU
1111
github.com/docker/machine v0.16.2/go.mod h1:I8mPNDeK1uH+JTcUU7X0ZW8KiYz0jyAgNaeSJ1rCfDI=
1212
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
1313
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
14+
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1415
github.com/hetznercloud/hcloud-go v1.14.0 h1:6IdF0Vox/6j1pyEdUCbFPIzEH/K9xZZzVuSFro8Y2vw=
1516
github.com/hetznercloud/hcloud-go v1.14.0/go.mod h1:8lR3yHBHZWy2uGcUi9Ibt4UOoop2wrVdERJgCtxsF3Q=
1617
github.com/hetznercloud/hcloud-go v1.17.0 h1:IKH0GLLoTEfgMuBY+GaaVTwjYChecrHFVo4/t0sIkGU=
1718
github.com/hetznercloud/hcloud-go v1.17.0/go.mod h1:8lR3yHBHZWy2uGcUi9Ibt4UOoop2wrVdERJgCtxsF3Q=
19+
github.com/hetznercloud/hcloud-go v1.24.0 h1:/CeHDzhH3Fhm83pjxvE3xNNLbvACl0Lu1/auJ83gG5U=
20+
github.com/hetznercloud/hcloud-go v1.24.0/go.mod h1:3YmyK8yaZZ48syie6xpm3dt26rtB6s65AisBHylXYFA=
1821
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
1922
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
2023
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
@@ -41,5 +44,6 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w
4144
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb h1:fgwFCsaw9buMuxNd6+DQfAuSFqbNiQZpcgJQAgJsK6k=
4245
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4346
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
47+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
4448
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
4549
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=

0 commit comments

Comments
 (0)