Skip to content

Commit 711d027

Browse files
bugfix: Make server location, SSH key optional (closes #110, thanks @avpnusr)
- reduce overly pedantic null checks - indicate nullability in getter name
1 parent 8f76f0d commit 711d027

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ You can find sources and pre-compiled binaries [here](https://github.com/JonasPr
1515

1616
```bash
1717
# Download the binary (this example downloads the binary for linux amd64)
18-
$ wget https://github.com/JonasProgrammer/docker-machine-driver-hetzner/releases/download/4.1.0/docker-machine-driver-hetzner_4.1.0_linux_amd64.tar.gz
19-
$ tar -xvf docker-machine-driver-hetzner_4.1.0_linux_amd64.tar.gz
18+
$ wget https://github.com/JonasProgrammer/docker-machine-driver-hetzner/releases/download/4.1.2/docker-machine-driver-hetzner_4.1.2_linux_amd64.tar.gz
19+
$ tar -xvf docker-machine-driver-hetzner_4.1.2_linux_amd64.tar.gz
2020

2121
# Make it executable and copy the binary in a directory accessible with your $PATH
2222
$ chmod +x docker-machine-driver-hetzner

driver/driver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ func (d *Driver) PreCreateCheck() error {
425425
return errors.Wrap(err, "could not get image")
426426
}
427427

428-
if _, err := d.getLocation(); err != nil {
428+
if _, err := d.getLocationNullable(); err != nil {
429429
return errors.Wrap(err, "could not get location")
430430
}
431431

@@ -563,7 +563,7 @@ func (d *Driver) Remove() error {
563563

564564
// failure to remove a server-specific key is a hard error
565565
if !d.IsExistingKey && d.KeyID != 0 {
566-
key, err := d.getKey()
566+
key, err := d.getKeyNullable()
567567
if err != nil {
568568
return errors.Wrap(err, "could not get ssh key")
569569
}

driver/hetzner_query.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ func (d *Driver) getClient() *hcloud.Client {
1414
return hcloud.NewClient(hcloud.WithToken(d.AccessToken), hcloud.WithApplication("docker-machine-driver", d.version))
1515
}
1616

17-
func (d *Driver) getLocation() (*hcloud.Location, error) {
17+
func (d *Driver) getLocationNullable() (*hcloud.Location, error) {
1818
if d.cachedLocation != nil {
1919
return d.cachedLocation, nil
2020
}
21+
if d.Location == "" {
22+
return nil, nil
23+
}
2124

2225
location, _, err := d.getClient().Location.GetByName(context.Background(), d.Location)
2326
if err != nil {
@@ -95,6 +98,17 @@ func (d *Driver) getImageArchitectureForLookup() (hcloud.Architecture, error) {
9598
}
9699

97100
func (d *Driver) getKey() (*hcloud.SSHKey, error) {
101+
key, err := d.getKeyNullable()
102+
if err != nil {
103+
return nil, err
104+
}
105+
if key == nil {
106+
return nil, fmt.Errorf("key not found: %v", d.KeyID)
107+
}
108+
return key, err
109+
}
110+
111+
func (d *Driver) getKeyNullable() (*hcloud.SSHKey, error) {
98112
if d.cachedKey != nil {
99113
return d.cachedKey, nil
100114
}
@@ -103,14 +117,11 @@ func (d *Driver) getKey() (*hcloud.SSHKey, error) {
103117
if err != nil {
104118
return nil, errors.Wrap(err, "could not get sshkey by ID")
105119
}
106-
if key == nil {
107-
return nil, fmt.Errorf("key not found: %v", d.KeyID)
108-
}
109120
d.cachedKey = key
110121
return instrumented(key), nil
111122
}
112123

113-
func (d *Driver) getRemoteKeyWithSameFingerprint(publicKeyBytes []byte) (*hcloud.SSHKey, error) {
124+
func (d *Driver) getRemoteKeyWithSameFingerprintNullable(publicKeyBytes []byte) (*hcloud.SSHKey, error) {
114125
publicKey, _, _, _, err := ssh.ParseAuthorizedKey(publicKeyBytes)
115126
if err != nil {
116127
return nil, errors.Wrap(err, "could not parse ssh public key")
@@ -122,9 +133,6 @@ func (d *Driver) getRemoteKeyWithSameFingerprint(publicKeyBytes []byte) (*hcloud
122133
if err != nil {
123134
return remoteKey, errors.Wrap(err, "could not get sshkey by fingerprint")
124135
}
125-
if remoteKey == nil {
126-
return nil, fmt.Errorf("key not found by fingerprint: %v", fp)
127-
}
128136
return instrumented(remoteKey), nil
129137
}
130138

driver/setup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (d *Driver) makeCreateServerOptions() (*hcloud.ServerCreateOpts, error) {
7272
}
7373
srvopts.Volumes = volumes
7474

75-
if srvopts.Location, err = d.getLocation(); err != nil {
75+
if srvopts.Location, err = d.getLocationNullable(); err != nil {
7676
return nil, errors.Wrap(err, "could not get location")
7777
}
7878
if srvopts.ServerType, err = d.getType(); err != nil {

driver/ssh_keys.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (d *Driver) createRemoteKeys() error {
7070
return errors.Wrap(err, "could not read ssh public key")
7171
}
7272

73-
key, err := d.getRemoteKeyWithSameFingerprint(buf)
73+
key, err := d.getRemoteKeyWithSameFingerprintNullable(buf)
7474
if err != nil {
7575
return errors.Wrap(err, "error retrieving potentially existing key")
7676
}
@@ -89,7 +89,7 @@ func (d *Driver) createRemoteKeys() error {
8989
d.KeyID = key.ID
9090
}
9191
for i, pubkey := range d.AdditionalKeys {
92-
key, err := d.getRemoteKeyWithSameFingerprint([]byte(pubkey))
92+
key, err := d.getRemoteKeyWithSameFingerprintNullable([]byte(pubkey))
9393
if err != nil {
9494
return errors.Wrapf(err, "error checking for existing key for %v", pubkey)
9595
}

0 commit comments

Comments
 (0)