Skip to content

Commit

Permalink
Add api to uuid-annotator conversion (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-soltesz authored Jul 7, 2021
1 parent 865549c commit d9dc04e
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
81 changes: 81 additions & 0 deletions api/v2/api-v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,87 @@ func convert(s *uuid.ServerAnnotations) *api.Annotations {
}
}

// ConvertAnnotationsToServerAnnotations accepts an annotation from the v2 API
// and returns the equivalent annotator.ServerAnnotations structure from the
// uuid-annotator. This function is only useful for migrating away from the v2
// 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{
Geo: &uuid.Geolocation{
ContinentCode: a.Geo.ContinentCode,
CountryCode: a.Geo.CountryCode,
CountryCode3: a.Geo.CountryCode3,
CountryName: a.Geo.CountryName,
Region: a.Geo.Region,
Subdivision1ISOCode: a.Geo.Subdivision1ISOCode,
Subdivision1Name: a.Geo.Subdivision1Name,
Subdivision2ISOCode: a.Geo.Subdivision2ISOCode,
Subdivision2Name: a.Geo.Subdivision2Name,
MetroCode: a.Geo.MetroCode,
City: a.Geo.City,
AreaCode: a.Geo.AreaCode,
PostalCode: a.Geo.PostalCode,
Latitude: a.Geo.Latitude,
Longitude: a.Geo.Longitude,
AccuracyRadiusKm: a.Geo.AccuracyRadiusKm,
Missing: a.Geo.Missing,
},
Network: &uuid.Network{
CIDR: a.Network.CIDR,
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},
},
},
}
}

// ConvertAnnotationsToClientAnnotations accepts an annotation from the v2 API
// and returns the equivalent annotator.ClientAnnotations structure from the
// uuid-annotator. This function is only useful for migrating away from the v2
// API and should be retired with the annotation-service once the annotation
// export processes are complete.
func ConvertAnnotationsToClientAnnotations(a *api.Annotations) *uuid.ClientAnnotations {
c := &uuid.ClientAnnotations{
Geo: &uuid.Geolocation{
ContinentCode: a.Geo.ContinentCode,
CountryCode: a.Geo.CountryCode,
CountryCode3: a.Geo.CountryCode3,
CountryName: a.Geo.CountryName,
Region: a.Geo.Region,
Subdivision1ISOCode: a.Geo.Subdivision1ISOCode,
Subdivision1Name: a.Geo.Subdivision1Name,
Subdivision2ISOCode: a.Geo.Subdivision2ISOCode,
Subdivision2Name: a.Geo.Subdivision2Name,
MetroCode: a.Geo.MetroCode,
City: a.Geo.City,
AreaCode: a.Geo.AreaCode,
PostalCode: a.Geo.PostalCode,
Latitude: a.Geo.Latitude,
Longitude: a.Geo.Longitude,
AccuracyRadiusKm: a.Geo.AccuracyRadiusKm,
Missing: a.Geo.Missing,
},
Network: &uuid.Network{
CIDR: a.Network.CIDR,
ASNumber: a.Network.ASNumber,
ASName: a.Network.ASName,
Missing: a.Network.Missing,
},
}
if len(a.Network.Systems) > 0 {
c.Network.Systems = []uuid.System{}
for i := range a.Network.Systems {
c.Network.Systems = append(c.Network.Systems, uuid.System{ASNs: a.Network.Systems[i].ASNs})
}
}
return c
}

func annotateServerIPs(ips []string) ([]string, map[string]*api.Annotations) {
clients := []string{}
results := map[string]*api.Annotations{}
Expand Down
85 changes: 85 additions & 0 deletions api/v2/api-v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"
"time"
Expand All @@ -17,6 +18,7 @@ import (
"github.com/m-lab/annotation-service/site"
"github.com/m-lab/go/content"
"github.com/m-lab/go/rtx"
uuid "github.com/m-lab/uuid-annotator/annotator"
)

func init() {
Expand Down Expand Up @@ -190,3 +192,86 @@ func TestSomeErrors(t *testing.T) {
t.Error("Expected err containing Internal Server Error", err)
}
}

func TestConvertAnnotationsToServerAnnotations(t *testing.T) {
a := &types.Annotations{
Geo: &types.GeolocationIP{
ContinentCode: "NA",
CountryCode: "US",
Subdivision1ISOCode: "NY",
Subdivision1Name: "New York",
City: "New York",
PostalCode: "10011",
Latitude: 1.2,
Longitude: 2.3,
AccuracyRadiusKm: 1,
Missing: false,
},
Network: &types.ASData{
CIDR: "192.168.0.0/26",
ASNumber: 10,
ASName: "fake AS name",
Missing: false,
Systems: []types.System{
{ASNs: []uint32{10}},
},
},
}
expectedServer := &uuid.ServerAnnotations{
// NOTE: the Site and Machine fields will not be specified.
Geo: &uuid.Geolocation{
ContinentCode: "NA",
CountryCode: "US",
Subdivision1ISOCode: "NY",
Subdivision1Name: "New York",
City: "New York",
PostalCode: "10011",
Latitude: 1.2,
Longitude: 2.3,
AccuracyRadiusKm: 1,
Missing: false,
},
Network: &uuid.Network{
CIDR: "192.168.0.0/26",
ASNumber: 10,
ASName: "fake AS name",
Missing: false,
Systems: []uuid.System{
{ASNs: []uint32{10}},
},
},
}
expectedClient := &uuid.ClientAnnotations{
Geo: &uuid.Geolocation{
ContinentCode: "NA",
CountryCode: "US",
Subdivision1ISOCode: "NY",
Subdivision1Name: "New York",
City: "New York",
PostalCode: "10011",
Latitude: 1.2,
Longitude: 2.3,
AccuracyRadiusKm: 1,
Missing: false,
},
Network: &uuid.Network{
CIDR: "192.168.0.0/26",
ASNumber: 10,
ASName: "fake AS name",
Missing: false,
Systems: []uuid.System{
{ASNs: []uint32{10}},
},
},
}

gs := api.ConvertAnnotationsToServerAnnotations(a)
if !reflect.DeepEqual(gs, expectedServer) {
t.Errorf("ConvertAnnotationsToServerAnnotations() = %v, want %v", gs, expectedServer)
}

gc := api.ConvertAnnotationsToClientAnnotations(a)
if !reflect.DeepEqual(gc, expectedClient) {
t.Errorf("ConvertAnnotationsToServerAnnotations() = %v, want %v", gc, expectedClient)
}
}

0 comments on commit d9dc04e

Please sign in to comment.