From d9dc04efa2ae38a00df26dd5102168f3e048c252 Mon Sep 17 00:00:00 2001 From: Stephen Soltesz Date: Wed, 7 Jul 2021 17:14:52 -0400 Subject: [PATCH] Add api to uuid-annotator conversion (#290) --- api/v2/api-v2.go | 81 +++++++++++++++++++++++++++++++++++++++++ api/v2/api-v2_test.go | 85 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) diff --git a/api/v2/api-v2.go b/api/v2/api-v2.go index 54ca54ce..f1429e34 100644 --- a/api/v2/api-v2.go +++ b/api/v2/api-v2.go @@ -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{} diff --git a/api/v2/api-v2_test.go b/api/v2/api-v2_test.go index 8ffbf38a..26b4a4c6 100644 --- a/api/v2/api-v2_test.go +++ b/api/v2/api-v2_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "reflect" "strings" "testing" "time" @@ -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() { @@ -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) + } +}