From cac4ce66a2780e2e786f38097058e99d695862cd Mon Sep 17 00:00:00 2001 From: Link Dupont Date: Thu, 18 Jan 2024 13:01:08 -0500 Subject: [PATCH] feat: Support generating service_account records Add support for generating ServiceAccount type identity records. --- identity.go | 21 ++++++++++++ service_account.go | 33 ++++++++++++++++++ service_account_test.go | 76 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 service_account.go create mode 100644 service_account_test.go diff --git a/identity.go b/identity.go index 1def4a8..d130fc9 100644 --- a/identity.go +++ b/identity.go @@ -165,3 +165,24 @@ func NewUserIdentity(identityTemplate Identity, userTemplate User) (*identity.XR return id, nil } + +// NewServiceAccountIdentity will build and return a fully populated +// ServiceAccount identity record, using any values that are present in +// identityTemplate and serviceAccountTemplate. +func NewServiceAccountIdentity(identityTemplate Identity, serviceAccountTemplate ServiceAccount) (*identity.XRHID, error) { + id, err := NewIdentity(identityTemplate) + if err != nil { + return nil, err + } + + serviceAccount, err := NewServiceAccount(serviceAccountTemplate) + if err != nil { + return nil, err + } + + id.Identity.ServiceAccount = *serviceAccount + + id.Identity.Type = "ServiceAccount" + + return id, nil +} diff --git a/service_account.go b/service_account.go new file mode 100644 index 0000000..c05cdfe --- /dev/null +++ b/service_account.go @@ -0,0 +1,33 @@ +package xrhidgen + +import ( + "github.com/pioz/faker" + "github.com/redhatinsights/platform-go-middlewares/v2/identity" +) + +// ServiceAccount holds values to be used as input when generating a service +// account identity record. +type ServiceAccount struct { + ClientID *string + Username *string +} + +// NewServiceAccount will build and return a fully populated ServiceAccount data +// structure, using any values that are present in template. +func NewServiceAccount(template ServiceAccount) (*identity.ServiceAccount, error) { + var id identity.ServiceAccount + + if template.ClientID != nil { + id.ClientId = *template.ClientID + } else { + id.ClientId = faker.String() + } + + if template.Username != nil { + id.Username = *template.Username + } else { + id.Username = faker.Username() + } + + return &id, nil +} diff --git a/service_account_test.go b/service_account_test.go new file mode 100644 index 0000000..93a6278 --- /dev/null +++ b/service_account_test.go @@ -0,0 +1,76 @@ +package xrhidgen + +import ( + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/pioz/faker" + "github.com/redhatinsights/platform-go-middlewares/v2/identity" + "go.openly.dev/pointy" +) + +func TestNewServiceAccount(t *testing.T) { + type Tests struct { + description string + seed int64 + input ServiceAccount + want *identity.ServiceAccount + wantError error + } + tests := []Tests{ + { + description: "empty template", + seed: 100, + input: ServiceAccount{}, + want: &identity.ServiceAccount{ + ClientId: "gimwu7Re", + Username: "boltrope", + }, + }, + { + description: "partial template", + seed: 100, + input: ServiceAccount{ + ClientID: pointy.String("12345"), + }, + want: &identity.ServiceAccount{ + ClientId: "12345", + Username: "ammon", + }, + }, + { + description: "full template", + seed: 100, + input: ServiceAccount{ + ClientID: pointy.String("12345"), + Username: pointy.String("jsmith"), + }, + want: &identity.ServiceAccount{ + ClientId: "12345", + Username: "jsmith", + }, + }, + } + + for _, test := range tests { + t.Run(test.description, func(t *testing.T) { + faker.SetSeed(test.seed) + + got, err := NewServiceAccount(test.input) + + if test.wantError != nil { + if !cmp.Equal(err, test.wantError, cmpopts.EquateErrors()) { + t.Errorf("%#v != %#v", err, test.wantError) + } + } else { + if err != nil { + t.Fatal(err) + } + if !cmp.Equal(got, test.want) { + t.Errorf("%v", cmp.Diff(got, test.want)) + } + } + }) + } +}