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

feat: reconcile user #64

Merged
merged 7 commits into from
Dec 20, 2024
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
2 changes: 2 additions & 0 deletions apis/user/v1alpha1/user_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ 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
41 changes: 34 additions & 7 deletions internal/controller/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ const (
errGetPC = "cannot get ProviderConfig"
errGetCreds = "cannot get credentials"

errNewClient = "cannot create new Service"
errNewClient = "cannot create new Service"
errCreateUser = "cannot create User"
errDeleteUser = "cannot delete User"
errListUsers = "cannot list Users"
errGetUser = "cannot get User"
)

// A NoOpService does nothing.
Expand Down Expand Up @@ -145,8 +149,10 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, errors.New(errNotUser)
}

// These fmt statements should be removed in the real implementation.
fmt.Printf("Observing: %+v", cr)
users, err := c.cloudianService.ListUsers(ctx, cr.Spec.ForProvider.GroupID, nil)
if err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, errListUsers)
}

return managed.ExternalObservation{
// Return false when the external resource does not exist. This lets
Expand All @@ -157,21 +163,36 @@ 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: true,
ResourceUpToDate: isUpToDate(cr.Spec.ForProvider, users),

// Return any details that may be required to connect to the external
// resource. These will be stored as the connection secret.
ConnectionDetails: managed.ConnectionDetails{},
}, nil
}

func isUpToDate(spec v1alpha1.UserParameters, users []cloudian.User) bool {
for _, user := range users {
if user.UserID == spec.UserID {
return true
}
}
return false
}

func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.ExternalCreation, error) {
cr, ok := mg.(*v1alpha1.User)
if !ok {
return managed.ExternalCreation{}, errors.New(errNotUser)
}

fmt.Printf("Creating: %+v", cr)
user := cloudian.User{
GroupID: cr.Spec.ForProvider.GroupID,
UserID: cr.Spec.ForProvider.UserID,
}
if err := c.cloudianService.CreateUser(ctx, user); err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, errCreateUser)
}

return managed.ExternalCreation{
// Optionally return any details that may be required to connect to the
Expand All @@ -186,7 +207,7 @@ func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext
return managed.ExternalUpdate{}, errors.New(errNotUser)
}

fmt.Printf("Updating: %+v", cr)
fmt.Printf("Pretending to Update (no managed fields to update): %+v", cr)

return managed.ExternalUpdate{
// Optionally return any details that may be required to connect to the
Expand All @@ -201,7 +222,13 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) (managed.Ext
return managed.ExternalDelete{}, errors.New(errNotUser)
}

fmt.Printf("Deleting: %+v", cr)
user := cloudian.User{
GroupID: cr.Spec.ForProvider.GroupID,
UserID: cr.Spec.ForProvider.UserID,
}
if err := c.cloudianService.DeleteUser(ctx, user); err != nil {
return managed.ExternalDelete{}, errors.Wrap(err, errDeleteUser)
}

return managed.ExternalDelete{}, nil
}
Expand Down
8 changes: 8 additions & 0 deletions package/crds/user.cloudian.crossplane.io_users.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ spec:
type: string
forProvider:
description: UserParameters are the configurable fields of a User.
properties:
groupId:
type: string
userId:
type: string
required:
- groupId
- userId
type: object
managementPolicies:
default:
Expand Down
Loading