From fc54fa82e67540251e099417dc77c95a22068fdc Mon Sep 17 00:00:00 2001 From: Link Dupont Date: Thu, 25 Jan 2024 09:17:37 -0500 Subject: [PATCH] refactor: update identity to v2.0.0-beta.2 beta.2 uses pointers for empty struct fields, enabling a smaller output when marshaling to JSON. The New*Identity functions need to be updated to use the pointers correctly, and example output needs to be updated to demonstrate the smaller JSON output. --- README.md | 6 +++--- example_test.go | 12 ++++++------ go.mod | 2 +- go.sum | 3 +++ identity.go | 10 +++++----- identity_test.go | 12 ------------ 6 files changed, 18 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 1b2aeb5..8e1ef89 100644 --- a/README.md +++ b/README.md @@ -44,12 +44,12 @@ FLAGS ``` $ xrhidgen user -email someuser@redhat.com -{"identity":{"org_id":"45841","internal":{"org_id":"45841"},"user":{"username":"backbencher","email":"someuser@redhat.com","first_name":"Cira","last_name":"Roob","is_active":false,"is_org_admin":true,"is_internal":false,"locale":"li","user_id":"nicole"},"system":{},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"","issuer_dn":""},"service_account":{"client_id":"","username":""},"type":"User","auth_type":"cert-auth"},"entitlements":null} +{"identity":{"account_number":"71384","org_id":"72467","internal":{"org_id":"72467"},"user":{"username":"rockabilly","email":"someuser@redhat.com","first_name":"Sawyer","last_name":"Ferry","is_active":true,"is_org_admin":true,"is_internal":false,"locale":"ee","user_id":"insurgence"},"type":"User","auth_type":"cert-auth"},"entitlements":null} ``` ``` $ xrhidgen system | base64 -w0 -eyJpZGVudGl0eSI6eyJvcmdfaWQiOiIyMTcwMyIsImludGVybmFsIjp7Im9yZ19pZCI6IjIxNzAzIn0sInVzZXIiOnsidXNlcm5hbWUiOiIiLCJlbWFpbCI6IiIsImZpcnN0X25hbWUiOiIiLCJsYXN0X25hbWUiOiIiLCJpc19hY3RpdmUiOmZhbHNlLCJpc19vcmdfYWRtaW4iOmZhbHNlLCJpc19pbnRlcm5hbCI6ZmFsc2UsImxvY2FsZSI6IiIsInVzZXJfaWQiOiIifSwic3lzdGVtIjp7ImNuIjoiRyIsImNlcnRfdHlwZSI6ImNvbnN1bWVyIiwiY2x1c3Rlcl9pZCI6IkhKbyJ9LCJhc3NvY2lhdGUiOnsiUm9sZSI6bnVsbCwiZW1haWwiOiIiLCJnaXZlbk5hbWUiOiIiLCJyaGF0VVVJRCI6IiIsInN1cm5hbWUiOiIifSwieDUwOSI6eyJzdWJqZWN0X2RuIjoiIiwiaXNzdWVyX2RuIjoiIn0sInNlcnZpY2VfYWNjb3VudCI6eyJjbGllbnRfaWQiOiIiLCJ1c2VybmFtZSI6IiJ9LCJ0eXBlIjoiU3lzdGVtIiwiYXV0aF90eXBlIjoiYmFzaWMtYXV0aCJ9LCJlbnRpdGxlbWVudHMiOm51bGx9Cg== +eyJpZGVudGl0eSI6eyJvcmdfaWQiOiI2NjY1MSIsImludGVybmFsIjp7Im9yZ19pZCI6IjY2NjUxIn0sInN5c3RlbSI6eyJjbiI6ImFFcEdUZSIsImNlcnRfdHlwZSI6ImNvbnN1bWVyIiwiY2x1c3Rlcl9pZCI6ImlUNksifSwidHlwZSI6IlN5c3RlbSIsImF1dGhfdHlwZSI6ImNlcnQtYXV0aCJ9LCJlbnRpdGxlbWVudHMiOm51bGx9Cg== ``` ``` @@ -61,7 +61,7 @@ used to initialize the generator to a deterministic state. ``` $ SEED=100 xrhidgen user -{"identity":{"employee_account_number":"02299","org_id":"41123","internal":{"org_id":"41123"},"user":{"username":"skeptic","email":"winnifredwinning@shred.org","first_name":"Cameron","last_name":"Stehr","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"fi","user_id":"meredeth"},"system":{},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"","issuer_dn":""},"service_account":{"client_id":"","username":""},"type":"User","auth_type":"basic-auth"},"entitlements":null} +{"identity":{"employee_account_number":"02299","org_id":"41123","internal":{"org_id":"41123"},"user":{"username":"skeptic","email":"winnifredwinning@shred.org","first_name":"Cameron","last_name":"Stehr","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"fi","user_id":"meredeth"},"type":"User","auth_type":"basic-auth"},"entitlements":null} ``` ## Go package diff --git a/example_test.go b/example_test.go index a74f30b..f185d11 100644 --- a/example_test.go +++ b/example_test.go @@ -18,7 +18,7 @@ func ExampleAssociate() { panic(err) } fmt.Println(string(data)) - //Output: {"identity":{"employee_account_number":"02299","org_id":"41123","internal":{"org_id":""},"user":{"username":"","email":"","first_name":"","last_name":"","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"","user_id":""},"system":{},"associate":{"Role":null,"email":"winnifredwinning@shred.org","givenName":"Cameron","rhatUUID":"00e3c758-1d7d-4ecd-98a2-997157e2d05c","surname":"Swift"},"x509":{"subject_dn":"","issuer_dn":""},"service_account":{"client_id":"","username":""},"type":"Associate","auth_type":"basic-auth"},"entitlements":null} + //Output: {"identity":{"employee_account_number":"02299","org_id":"41123","internal":{"org_id":""},"associate":{"Role":null,"email":"winnifredwinning@shred.org","givenName":"Cameron","rhatUUID":"00e3c758-1d7d-4ecd-98a2-997157e2d05c","surname":"Swift"},"type":"Associate","auth_type":"basic-auth"},"entitlements":null} } func ExampleInternal() { @@ -32,7 +32,7 @@ func ExampleInternal() { panic(err) } fmt.Println(string(data)) - //Output: {"identity":{"org_id":"03797","internal":{"org_id":"08321","auth_time":-2978345600000000000},"user":{"username":"","email":"","first_name":"","last_name":"","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"","user_id":""},"system":{},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"","issuer_dn":""},"service_account":{"client_id":"","username":""},"type":"Internal","auth_type":"basic-auth"},"entitlements":null} + //Output: {"identity":{"org_id":"03797","internal":{"org_id":"08321","auth_time":-2978345600000000000},"type":"Internal","auth_type":"basic-auth"},"entitlements":null} } func ExampleSystem() { @@ -46,7 +46,7 @@ func ExampleSystem() { panic(err) } fmt.Println(string(data)) - //Output: {"identity":{"account_number":"16398","org_id":"57572","internal":{"org_id":"57572"},"user":{"username":"","email":"","first_name":"","last_name":"","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"","user_id":""},"system":{"cn":"It6P","cert_type":"consumer","cluster_id":"x8LdjPo"},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"","issuer_dn":""},"service_account":{"client_id":"","username":""},"type":"System","auth_type":"basic-auth"},"entitlements":null} + //Output: {"identity":{"account_number":"16398","org_id":"57572","internal":{"org_id":"57572"},"system":{"cn":"It6P","cert_type":"consumer","cluster_id":"x8LdjPo"},"type":"System","auth_type":"basic-auth"},"entitlements":null} } func ExampleUser() { @@ -60,7 +60,7 @@ func ExampleUser() { panic(err) } fmt.Println(string(data)) - //Output: {"identity":{"org_id":"23807","internal":{"org_id":"23807"},"user":{"username":"tycoon","email":"fransen@crump.biz","first_name":"Frankie","last_name":"Collins","is_active":false,"is_org_admin":false,"is_internal":true,"locale":"pi","user_id":"backset"},"system":{},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"","issuer_dn":""},"service_account":{"client_id":"","username":""},"type":"User","auth_type":"cert-auth"},"entitlements":null} + //Output: {"identity":{"org_id":"23807","internal":{"org_id":"23807"},"user":{"username":"tycoon","email":"fransen@crump.biz","first_name":"Frankie","last_name":"Collins","is_active":false,"is_org_admin":false,"is_internal":true,"locale":"pi","user_id":"backset"},"type":"User","auth_type":"cert-auth"},"entitlements":null} } func ExampleX509() { @@ -74,7 +74,7 @@ func ExampleX509() { panic(err) } fmt.Println(string(data)) - //Output: {"identity":{"org_id":"23807","internal":{"org_id":""},"user":{"username":"","email":"","first_name":"","last_name":"","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"","user_id":""},"system":{},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"2","issuer_dn":"3sfSj"},"service_account":{"client_id":"","username":""},"type":"X509","auth_type":"cert-auth"},"entitlements":null} + //Output: {"identity":{"org_id":"23807","internal":{"org_id":""},"x509":{"subject_dn":"2","issuer_dn":"3sfSj"},"type":"X509","auth_type":"cert-auth"},"entitlements":null} } func ExampleServiceAccount() { @@ -88,5 +88,5 @@ func ExampleServiceAccount() { panic(err) } fmt.Println(string(data)) - //Output: {"identity":{"org_id":"23807","internal":{"org_id":""},"user":{"username":"","email":"","first_name":"","last_name":"","is_active":false,"is_org_admin":false,"is_internal":false,"locale":"","user_id":""},"system":{},"associate":{"Role":null,"email":"","givenName":"","rhatUUID":"","surname":""},"x509":{"subject_dn":"","issuer_dn":""},"service_account":{"client_id":"2","username":"crump"},"type":"ServiceAccount","auth_type":"cert-auth"},"entitlements":null} + //Output: {"identity":{"org_id":"23807","internal":{"org_id":""},"service_account":{"client_id":"2","username":"crump"},"type":"ServiceAccount","auth_type":"cert-auth"},"entitlements":null} } diff --git a/go.mod b/go.mod index 4721552..ff2c48c 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/google/go-cmp v0.6.0 github.com/peterbourgon/ff/v3 v3.4.0 github.com/pioz/faker v1.7.3 - github.com/redhatinsights/platform-go-middlewares/v2 v2.0.0-20240118174021-7836c2be5281 + github.com/redhatinsights/platform-go-middlewares/v2 v2.0.0-beta.2 github.com/sgreben/flagvar v1.10.1 go.openly.dev/pointy v1.3.0 ) diff --git a/go.sum b/go.sum index 0fcbfd1..8472891 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,7 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/nxadm/tail v1.4.11 h1:8feyoE3OzPrcshW5/MJ4sGESc5cqmGkGCWlco4l0bqY= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8= +github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo= github.com/peterbourgon/ff/v3 v3.4.0 h1:QBvM/rizZM1cB0p0lGMdmR7HxZeI/ZrBWB4DqLkMUBc= github.com/peterbourgon/ff/v3 v3.4.0/go.mod h1:zjJVUhx+twciwfDl0zBcFzl4dW8axCRyXE/eKY9RztQ= github.com/pioz/faker v1.7.3 h1:Tez8Emuq0UN+/d6mo3a9m/9ZZ/zdfJk0c5RtRatrceM= @@ -16,6 +17,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/redhatinsights/platform-go-middlewares/v2 v2.0.0-20240118174021-7836c2be5281 h1:L20lFlHbO92CNOH+xNM3U+TnSQfnSPIZdUjgIhtCIl0= github.com/redhatinsights/platform-go-middlewares/v2 v2.0.0-20240118174021-7836c2be5281/go.mod h1:GI4AFQc+iHCvHGbbuY1roOY7KXKDqWbldRemReHk7Pk= +github.com/redhatinsights/platform-go-middlewares/v2 v2.0.0-beta.2 h1:pEDiKYqxEoZT6Mx7B57rYqIgmYyly0f8JfzfY38b9T4= +github.com/redhatinsights/platform-go-middlewares/v2 v2.0.0-beta.2/go.mod h1:Ipl1kmvhZSpgRoe7MyJNK+VAvGG1cIqn535TuId5npw= github.com/sgreben/flagvar v1.10.1 h1:ukN3zqVj9T9U7CiKG6owmejxswJYMbAg9Mxkhi1B4tw= github.com/sgreben/flagvar v1.10.1/go.mod h1:AxDmbFDIxZ4dHj2zg8LxuJn5CSwSS28iY/Wy56e+nhI= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/identity.go b/identity.go index d130fc9..30cc88b 100644 --- a/identity.go +++ b/identity.go @@ -71,7 +71,7 @@ func NewAssociateIdentity(identityTemplate Identity, associateTemplate Associate return nil, err } - id.Identity.Associate = *associate + id.Identity.Associate = associate id.Identity.Type = "Associate" @@ -113,7 +113,7 @@ func NewSystemIdentity(identityTemplate Identity, systemTemplate System) (*ident return nil, err } - id.Identity.System = *system + id.Identity.System = system id.Identity.Type = "System" id.Identity.Internal = identity.Internal{ @@ -136,7 +136,7 @@ func NewX509Identity(identityTemplate Identity, x509Template X509) (*identity.XR return nil, err } - id.Identity.X509 = *x509 + id.Identity.X509 = x509 id.Identity.Type = "X509" @@ -156,7 +156,7 @@ func NewUserIdentity(identityTemplate Identity, userTemplate User) (*identity.XR return nil, err } - id.Identity.User = *user + id.Identity.User = user id.Identity.Type = "User" id.Identity.Internal = identity.Internal{ @@ -180,7 +180,7 @@ func NewServiceAccountIdentity(identityTemplate Identity, serviceAccountTemplate return nil, err } - id.Identity.ServiceAccount = *serviceAccount + id.Identity.ServiceAccount = serviceAccount id.Identity.Type = "ServiceAccount" diff --git a/identity_test.go b/identity_test.go index d84f121..79d6de5 100644 --- a/identity_test.go +++ b/identity_test.go @@ -26,15 +26,11 @@ func TestNewIdentity(t *testing.T) { want: &identity.XRHID{ Identity: identity.Identity{ AccountNumber: "", - Associate: identity.Associate{}, AuthType: "basic-auth", EmployeeAccountNumber: "02299", Internal: identity.Internal{}, OrgID: "41123", - System: identity.System{}, Type: "50cQB", - User: identity.User{}, - X509: identity.X509{}, }, }, }, @@ -47,15 +43,11 @@ func TestNewIdentity(t *testing.T) { want: &identity.XRHID{ Identity: identity.Identity{ AccountNumber: "1234", - Associate: identity.Associate{}, AuthType: "cert-auth", EmployeeAccountNumber: "00229", Internal: identity.Internal{}, OrgID: "94112", - System: identity.System{}, Type: "M5", - User: identity.User{}, - X509: identity.X509{}, }, }, }, @@ -72,15 +64,11 @@ func TestNewIdentity(t *testing.T) { want: &identity.XRHID{ Identity: identity.Identity{ AccountNumber: "10001", - Associate: identity.Associate{}, AuthType: "basic-auth", EmployeeAccountNumber: "112233", Internal: identity.Internal{}, OrgID: "111111", - System: identity.System{}, Type: "universal", - User: identity.User{}, - X509: identity.X509{}, }, }, },