Skip to content

Commit

Permalink
feat: Support generating service_account records
Browse files Browse the repository at this point in the history
Add support for generating ServiceAccount type identity records.
  • Loading branch information
subpop committed Jan 18, 2024
1 parent 19bd7e1 commit cac4ce6
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
21 changes: 21 additions & 0 deletions identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
33 changes: 33 additions & 0 deletions service_account.go
Original file line number Diff line number Diff line change
@@ -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
}
76 changes: 76 additions & 0 deletions service_account_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
})
}
}

0 comments on commit cac4ce6

Please sign in to comment.