Skip to content

Commit 5cea366

Browse files
address comments
Co-authored-by: Seth Hoenig <[email protected]>
1 parent 7b6f954 commit 5cea366

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed

client/fingerprint/env_digitalocean.go

+12-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fingerprint
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"net/http"
67
"net/url"
@@ -17,6 +18,7 @@ import (
1718

1819
const (
1920
// DigitalOceanMetadataURL is where the DigitalOcean metadata api normally resides.
21+
// https://docs.digitalocean.com/products/droplets/how-to/retrieve-droplet-metadata/#how-to-retrieve-droplet-metadata
2022
DigitalOceanMetadataURL = "http://169.254.169.254/metadata/v1/"
2123

2224
// DigitalOceanMetadataTimeout is the timeout used when contacting the DigitalOcean metadata
@@ -67,7 +69,7 @@ func (f *EnvDigitalOceanFingerprint) Get(attribute string, format string) (strin
6769
}
6870

6971
req := &http.Request{
70-
Method: "GET",
72+
Method: http.MethodGet,
7173
URL: parsedURL,
7274
Header: http.Header{
7375
"User-Agent": []string{useragent.String()},
@@ -76,36 +78,23 @@ func (f *EnvDigitalOceanFingerprint) Get(attribute string, format string) (strin
7678

7779
res, err := f.client.Do(req)
7880
if err != nil {
79-
f.logger.Debug("could not read value for attribute", "attribute", attribute, "error", err)
80-
return "", err
81-
} else if res.StatusCode != http.StatusOK {
82-
f.logger.Debug("could not read value for attribute", "attribute", attribute, "resp_code", res.StatusCode)
81+
f.logger.Debug("failed to request metadata", "attribute", attribute, "error", err)
8382
return "", err
8483
}
8584

86-
resp, err := ioutil.ReadAll(res.Body)
85+
body, err := ioutil.ReadAll(res.Body)
8786
res.Body.Close()
8887
if err != nil {
89-
f.logger.Error("error reading response body for DigitalOcean attribute", "attribute", attribute, "error", err)
88+
f.logger.Error("failed to read metadata", "attribute", attribute, "error", err, "resp_code", res.StatusCode)
9089
return "", err
9190
}
9291

93-
if res.StatusCode >= 400 {
94-
return "", ReqError{res.StatusCode}
92+
if res.StatusCode != http.StatusOK {
93+
f.logger.Debug("could not read value for attribute", "attribute", attribute, "resp_code", res.StatusCode)
94+
return "", fmt.Errorf("error reading attribute %s. digitalocean metadata api returned an error: resp_code: %d, resp_body: %s", attribute, res.StatusCode, body)
9595
}
9696

97-
return string(resp), nil
98-
}
99-
100-
func checkDigitalOceanError(err error, logger log.Logger, desc string) error {
101-
// If it's a URL error, assume we're not actually in an DigitalOcean environment.
102-
// To the outer layers, this isn't an error so return nil.
103-
if _, ok := err.(*url.Error); ok {
104-
logger.Debug("error querying DigitalOcean attribute; skipping", "attribute", desc)
105-
return nil
106-
}
107-
// Otherwise pass the error through.
108-
return err
97+
return string(body), nil
10998
}
11099

111100
func (f *EnvDigitalOceanFingerprint) Fingerprint(request *FingerprintRequest, response *FingerprintResponse) error {
@@ -138,7 +127,8 @@ func (f *EnvDigitalOceanFingerprint) Fingerprint(request *FingerprintRequest, re
138127
resp, err := f.Get(attr.path, "text")
139128
v := strings.TrimSpace(resp)
140129
if err != nil {
141-
return checkDigitalOceanError(err, f.logger, k)
130+
f.logger.Warn("failed to read attribute", "attribute", k, "err", err)
131+
continue
142132
} else if v == "" {
143133
f.logger.Debug("read an empty value", "attribute", k)
144134
continue

client/fingerprint/env_digitalocean_test.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/hashicorp/nomad/client/config"
1313
"github.com/hashicorp/nomad/helper/testlog"
1414
"github.com/hashicorp/nomad/nomad/structs"
15+
"github.com/stretchr/testify/assert"
1516
)
1617

1718
func TestDigitalOceanFingerprint_nonDigitalOcean(t *testing.T) {
@@ -90,13 +91,8 @@ func TestFingerprint_DigitalOcean(t *testing.T) {
9091
request := &FingerprintRequest{Config: &config.Config{}, Node: node}
9192
var response FingerprintResponse
9293
err := f.Fingerprint(request, &response)
93-
if err != nil {
94-
t.Fatalf("err: %v", err)
95-
}
96-
97-
if !response.Detected {
98-
t.Fatalf("expected response to be applicable")
99-
}
94+
assert.NoError(t, err)
95+
assert.True(t, response.Detected, "expected response to be applicable")
10096

10197
keys := []string{
10298
"unique.platform.digitalocean.id",
@@ -112,9 +108,7 @@ func TestFingerprint_DigitalOcean(t *testing.T) {
112108
assertNodeAttributeContains(t, response.Attributes, k)
113109
}
114110

115-
if len(response.Links) == 0 {
116-
t.Fatalf("Empty links for Node in DO Fingerprint test")
117-
}
111+
assert.NotEmpty(t, response.Links, "Empty links for Node in DO Fingerprint test")
118112

119113
// Make sure Links contains the DO ID.
120114
for _, k := range []string{"digitalocean"} {

0 commit comments

Comments
 (0)