Skip to content

Commit

Permalink
Make server annotation convert safe (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-soltesz authored Jul 13, 2021
1 parent d9dc04e commit fa227b3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
13 changes: 8 additions & 5 deletions api/v2/api-v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func convert(s *uuid.ServerAnnotations) *api.Annotations {
// API and should be retired with the annotation-service once the annotation
// export processes are complete.
func ConvertAnnotationsToServerAnnotations(a *api.Annotations) *uuid.ServerAnnotations {
return &uuid.ServerAnnotations{
s := &uuid.ServerAnnotations{
Geo: &uuid.Geolocation{
ContinentCode: a.Geo.ContinentCode,
CountryCode: a.Geo.CountryCode,
Expand All @@ -221,12 +221,15 @@ func ConvertAnnotationsToServerAnnotations(a *api.Annotations) *uuid.ServerAnnot
ASNumber: a.Network.ASNumber,
ASName: a.Network.ASName,
Missing: a.Network.Missing,
// M-Lab Servers only define one System.
Systems: []uuid.System{
{ASNs: a.Network.Systems[0].ASNs},
},
},
}
if len(a.Network.Systems) > 0 {
// M-Lab Servers only define one System.
s.Network.Systems = []uuid.System{
{ASNs: a.Network.Systems[0].ASNs},
}
}
return s
}

// ConvertAnnotationsToClientAnnotations accepts an annotation from the v2 API
Expand Down
20 changes: 20 additions & 0 deletions api/v2/api-v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ func TestConvertAnnotationsToServerAnnotations(t *testing.T) {
},
},
}
empty := &types.Annotations{
Geo: &types.GeolocationIP{
Missing: true,
},
Network: &types.ASData{
Missing: true,
},
}
expectedServer := &uuid.ServerAnnotations{
// NOTE: the Site and Machine fields will not be specified.
Geo: &uuid.Geolocation{
Expand All @@ -241,6 +249,14 @@ func TestConvertAnnotationsToServerAnnotations(t *testing.T) {
},
},
}
expectedEmpty := &uuid.ServerAnnotations{
Geo: &uuid.Geolocation{
Missing: true,
},
Network: &uuid.Network{
Missing: true,
},
}
expectedClient := &uuid.ClientAnnotations{
Geo: &uuid.Geolocation{
ContinentCode: "NA",
Expand Down Expand Up @@ -269,6 +285,10 @@ func TestConvertAnnotationsToServerAnnotations(t *testing.T) {
if !reflect.DeepEqual(gs, expectedServer) {
t.Errorf("ConvertAnnotationsToServerAnnotations() = %v, want %v", gs, expectedServer)
}
gempty := api.ConvertAnnotationsToServerAnnotations(empty)
if !reflect.DeepEqual(gempty, expectedEmpty) {
t.Errorf("ConvertAnnotationsToServerAnnotations() = %v, want %v", gempty, expectedEmpty)
}

gc := api.ConvertAnnotationsToClientAnnotations(a)
if !reflect.DeepEqual(gc, expectedClient) {
Expand Down

0 comments on commit fa227b3

Please sign in to comment.