Skip to content

Commit 98f9071

Browse files
authored
Fix instance port conversion to be CPU arch independent (#35)
1 parent 289427c commit 98f9071

File tree

2 files changed

+72
-8
lines changed

2 files changed

+72
-8
lines changed

pkg/model/types.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ type Endpoint struct {
2323
Attributes map[string]string
2424
}
2525

26-
const ipv4Attr = "AWS_INSTANCE_IPV4"
27-
const portAttr = "AWS_INSTANCE_PORT"
26+
const (
27+
Ipv4Attr = "AWS_INSTANCE_IPV4"
28+
PortAttr = "AWS_INSTANCE_PORT"
29+
)
2830

2931
// NewEndpointFromInstance converts a Cloud Map InstanceSummary to an endpoint
3032
func NewEndpointFromInstance(inst *types.InstanceSummary) (*Endpoint, error) {
@@ -33,14 +35,14 @@ func NewEndpointFromInstance(inst *types.InstanceSummary) (*Endpoint, error) {
3335
Attributes: make(map[string]string, 0),
3436
}
3537

36-
if ipv4, hasIp := inst.Attributes[ipv4Attr]; hasIp {
38+
if ipv4, hasIp := inst.Attributes[Ipv4Attr]; hasIp {
3739
endpoint.IP = ipv4
3840
} else {
3941
return nil, errors.New(fmt.Sprintf("cannot convert service instance %s to endpoint without IP address", *inst.Id))
4042
}
4143

42-
if portStr, hasPort := inst.Attributes[portAttr]; hasPort {
43-
port, parseError := strconv.Atoi(portStr)
44+
if portStr, hasPort := inst.Attributes[PortAttr]; hasPort {
45+
port, parseError := strconv.ParseUint(portStr, 10, 16)
4446

4547
if parseError != nil {
4648
return nil, parseError
@@ -52,7 +54,7 @@ func NewEndpointFromInstance(inst *types.InstanceSummary) (*Endpoint, error) {
5254
}
5355

5456
for key, val := range inst.Attributes {
55-
if key != ipv4Attr && key != portAttr {
57+
if key != Ipv4Attr && key != PortAttr {
5658
endpoint.Attributes[key] = val
5759
}
5860
}
@@ -64,10 +66,10 @@ func NewEndpointFromInstance(inst *types.InstanceSummary) (*Endpoint, error) {
6466
func (e *Endpoint) GetAttributes() map[string]string {
6567
attrs := make(map[string]string, 0)
6668

67-
attrs[ipv4Attr] = e.IP
69+
attrs[Ipv4Attr] = e.IP
6870

6971
port := strconv.FormatInt(int64(e.Port), 10)
70-
attrs[portAttr] = port
72+
attrs[PortAttr] = port
7173

7274
for key, val := range e.Attributes {
7375
attrs[key] = val

pkg/model/types_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package model
2+
3+
import (
4+
"github.com/aws/aws-sdk-go-v2/service/servicediscovery/types"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
var instId = "my-instance"
10+
11+
func TestNewEndpointFromInstance(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
inst *types.InstanceSummary
15+
want *Endpoint
16+
wantErr bool
17+
}{
18+
{
19+
name: "happy case",
20+
inst: &types.InstanceSummary{
21+
Id: &instId,
22+
Attributes: map[string]string{
23+
Ipv4Attr: "192.168.0.1",
24+
PortAttr: "65535",
25+
"custom-attr": "custom-val",
26+
},
27+
},
28+
want: &Endpoint{
29+
Id: instId,
30+
IP: "192.168.0.1",
31+
Port: 65535,
32+
Attributes: map[string]string{
33+
"custom-attr": "custom-val",
34+
},
35+
},
36+
},
37+
{
38+
name: "invalid port",
39+
inst: &types.InstanceSummary{
40+
Id: &instId,
41+
Attributes: map[string]string{
42+
Ipv4Attr: "192.168.0.1",
43+
PortAttr: "99999",
44+
"custom-attr": "custom-val",
45+
},
46+
},
47+
wantErr: true,
48+
},
49+
}
50+
for _, tt := range tests {
51+
t.Run(tt.name, func(t *testing.T) {
52+
got, err := NewEndpointFromInstance(tt.inst)
53+
if (err != nil) != tt.wantErr {
54+
t.Errorf("NewEndpointFromInstance() error = %v, wantErr %v", err, tt.wantErr)
55+
return
56+
}
57+
if !reflect.DeepEqual(got, tt.want) {
58+
t.Errorf("NewEndpointFromInstance() got = %v, want %v", got, tt.want)
59+
}
60+
})
61+
}
62+
}

0 commit comments

Comments
 (0)