Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: set Cloudian Group and User ids from Crossplane external-name #102

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions apis/user/v1alpha1/group_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ type GroupParameters struct {
//+optional
//+kubebuilder:default=true
Active bool `json:"active"`
// GroupID is the group ID (known as Name in the GUI).
//+kubebuilder:validation:MinLength=1
//+kubebuilder:validation:MaxLength=64
//+kubebuilder:validation:Pattern=`^[A-Za-z0-9_-]*$`
GroupID string `json:"groupId"`
tenstad marked this conversation as resolved.
Show resolved Hide resolved
// GroupName is the group name (known as Description in the GUI).
//+optional
//+kubebuilder:validation:MinLength=1
Expand Down
1 change: 0 additions & 1 deletion apis/user/v1alpha1/user_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
// UserParameters are the configurable fields of a User.
type UserParameters struct {
GroupID string `json:"groupId"`
UserID string `json:"userId"`
}

// UserObservation are the observable fields of a User.
Expand Down
19 changes: 10 additions & 9 deletions internal/controller/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/connection"
"github.com/crossplane/crossplane-runtime/pkg/controller"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/ratelimiter"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
Expand Down Expand Up @@ -147,7 +148,7 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, errors.New(errNotGroup)
}

observedGroup, err := c.cloudianService.GetGroup(ctx, cr.Spec.ForProvider.GroupID)
observedGroup, err := c.cloudianService.GetGroup(ctx, meta.GetExternalName(mg))
if err != nil {
if errors.Is(err, cloudian.ErrNotFound) {
return managed.ExternalObservation{ResourceExists: false}, nil
Expand All @@ -166,7 +167,7 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
// Return false when the external resource exists, but it not up to date
// with the desired managed resource state. This lets the managed
// resource reconciler know that it needs to call Update.
ResourceUpToDate: isUpToDate(cr.Spec.ForProvider, *observedGroup),
ResourceUpToDate: isUpToDate(meta.GetExternalName(mg), cr.Spec.ForProvider, *observedGroup),

// Return any details that may be required to connect to the external
// resource. These will be stored as the connection secret.
Expand All @@ -182,7 +183,7 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext

cr.SetConditions(xpv1.Creating())

if err := c.cloudianService.CreateGroup(ctx, newCloudianGroup(cr.Spec.ForProvider)); err != nil {
if err := c.cloudianService.CreateGroup(ctx, newCloudianGroup(meta.GetExternalName(mg), cr.Spec.ForProvider)); err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, errCreateGroup)
}

Expand All @@ -199,7 +200,7 @@ func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext
return managed.ExternalUpdate{}, errors.New(errNotGroup)
}

if err := c.cloudianService.UpdateGroup(ctx, newCloudianGroup(cr.Spec.ForProvider)); err != nil {
if err := c.cloudianService.UpdateGroup(ctx, newCloudianGroup(meta.GetExternalName(mg), cr.Spec.ForProvider)); err != nil {
return managed.ExternalUpdate{}, errors.Wrap(err, errUpdateGroup)
}

Expand All @@ -218,7 +219,7 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) (managed.Ext

cr.SetConditions(xpv1.Deleting())

if err := c.cloudianService.DeleteGroup(ctx, cr.Spec.ForProvider.GroupID); err != nil {
if err := c.cloudianService.DeleteGroup(ctx, meta.GetExternalName(mg)); err != nil {
return managed.ExternalDelete{}, errors.Wrap(err, errDeleteGroup)
}

Expand All @@ -229,14 +230,14 @@ func (c *external) Disconnect(ctx context.Context) error {
return nil
}

func isUpToDate(desired v1alpha1.GroupParameters, observed cloudian.Group) bool {
return newCloudianGroup(desired) == observed
func isUpToDate(name string, desired v1alpha1.GroupParameters, observed cloudian.Group) bool {
return newCloudianGroup(name, desired) == observed
}

func newCloudianGroup(gp v1alpha1.GroupParameters) cloudian.Group {
func newCloudianGroup(name string, gp v1alpha1.GroupParameters) cloudian.Group {
return cloudian.Group{
Active: gp.Active,
GroupID: gp.GroupID,
GroupID: name,
GroupName: gp.GroupName,
LDAPEnabled: ptr.Deref(gp.LDAPEnabled, false),
LDAPGroup: ptr.Deref(gp.LDAPGroup, ""),
Expand Down
11 changes: 6 additions & 5 deletions internal/controller/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/connection"
"github.com/crossplane/crossplane-runtime/pkg/controller"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/ratelimiter"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
Expand Down Expand Up @@ -151,7 +152,7 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, errors.Wrap(err, errListUsers)
}

upToDate := isUpToDate(cr.Spec.ForProvider, users)
upToDate := isUpToDate(meta.GetExternalName(mg), users)

return managed.ExternalObservation{
// Return false when the external resource does not exist. This lets
Expand All @@ -170,9 +171,9 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
}, nil
}

func isUpToDate(spec v1alpha1.UserParameters, users []cloudian.User) bool {
func isUpToDate(userId string, users []cloudian.User) bool {
for _, user := range users {
if user.UserID == spec.UserID {
if user.UserID == userId {
return true
}
}
Expand All @@ -187,7 +188,7 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext

user := cloudian.User{
GroupID: cr.Spec.ForProvider.GroupID,
UserID: cr.Spec.ForProvider.UserID,
UserID: meta.GetExternalName(mg),
}
if err := c.cloudianService.CreateUser(ctx, user); err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, errCreateUser)
Expand Down Expand Up @@ -223,7 +224,7 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) (managed.Ext

user := cloudian.User{
GroupID: cr.Spec.ForProvider.GroupID,
UserID: cr.Spec.ForProvider.UserID,
UserID: meta.GetExternalName(mg),
}
if err := c.cloudianService.DeleteUser(ctx, user); err != nil {
return managed.ExternalDelete{}, errors.Wrap(err, errDeleteUser)
Expand Down
8 changes: 0 additions & 8 deletions package/crds/user.cloudian.crossplane.io_groups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ spec:
description: Active determines whether the group is enabled (true)
or disabled (false) in the system.
type: boolean
groupId:
description: GroupID is the group ID (known as Name in the GUI).
maxLength: 64
minLength: 1
pattern: ^[A-Za-z0-9_-]*$
type: string
groupName:
description: GroupName is the group name (known as Description
in the GUI).
Expand Down Expand Up @@ -117,8 +111,6 @@ spec:
group will be authenticated against the LDAP system when they
log into the CMC.
type: string
required:
- groupId
type: object
managementPolicies:
default:
Expand Down
3 changes: 0 additions & 3 deletions package/crds/user.cloudian.crossplane.io_users.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,8 @@ spec:
properties:
groupId:
type: string
userId:
type: string
required:
- groupId
- userId
type: object
managementPolicies:
default:
Expand Down
Loading