Skip to content

Commit

Permalink
Merge pull request #90 from IntaSlapna/feature/59
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterMX authored Sep 12, 2023
2 parents cb89678 + c3c6a0d commit ae5ea6d
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 47 deletions.
8 changes: 6 additions & 2 deletions apis/projects/v1alpha1/member_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ type MemberParameters struct {
ProjectIDSelector *xpv1.Selector `json:"projectIdSelector,omitempty"`

// The user ID of the member.
// +immutable
UserID int `json:"userID"`
// +optional
UserID *int `json:"userID,omitempty"`

// The username of the member.
// +optional
UserName *string `json:"userName,omitempty"`

// A valid access level.
// +immutable
Expand Down
10 changes: 10 additions & 0 deletions apis/projects/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/projects/member.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ spec:
name: gitlab-provider
writeConnectionSecretToRef:
name: gitlab-project-example-member
namespace: crossplane-system
namespace: crossplane-system
4 changes: 3 additions & 1 deletion package/crds/projects.gitlab.crossplane.io_members.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,11 @@ spec:
userID:
description: The user ID of the member.
type: integer
userName:
description: The username of the member.
type: string
required:
- accessLevel
- userID
type: object
providerConfigRef:
default:
Expand Down
7 changes: 7 additions & 0 deletions pkg/clients/projects/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type MockClient struct {
MockCreatePipelineScheduleVariable func(pid interface{}, schedule int, opt *gitlab.CreatePipelineScheduleVariableOptions, options ...gitlab.RequestOptionFunc) (*gitlab.PipelineVariable, *gitlab.Response, error)
MockEditPipelineScheduleVariable func(pid interface{}, schedule int, key string, opt *gitlab.EditPipelineScheduleVariableOptions, options ...gitlab.RequestOptionFunc) (*gitlab.PipelineVariable, *gitlab.Response, error)
MockDeletePipelineScheduleVariable func(pid interface{}, schedule int, key string, options ...gitlab.RequestOptionFunc) (*gitlab.PipelineVariable, *gitlab.Response, error)

MockListUsers func(opt *gitlab.ListUsersOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.User, *gitlab.Response, error)
}

// GetPipelineSchedule calls the underlying MockGetPipelineSchedule method.
Expand Down Expand Up @@ -245,3 +247,8 @@ func (c *MockClient) CreateProjectAccessToken(pid interface{}, opt *gitlab.Creat
func (c *MockClient) RevokeProjectAccessToken(pid interface{}, id int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
return c.MockRevokeProjectAccessToken(pid, id)
}

// ListUsers calls the underlying MockListUsers method.
func (c *MockClient) ListUsers(opt *gitlab.ListUsersOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.User, *gitlab.Response, error) {
return c.MockListUsers(opt)
}
2 changes: 1 addition & 1 deletion pkg/clients/projects/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func GenerateMemberObservation(projectMember *gitlab.ProjectMember) v1alpha1.Mem
// GenerateAddMemberOptions generates project member add options
func GenerateAddMemberOptions(p *v1alpha1.MemberParameters) *gitlab.AddProjectMemberOptions {
projectMember := &gitlab.AddProjectMemberOptions{
UserID: &p.UserID,
UserID: p.UserID,
AccessLevel: accessLevelValueV1alpha1ToGitlab(&p.AccessLevel),
}
if p.ExpiresAt != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/clients/projects/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestGenerateAddMemberOptions(t *testing.T) {
args: args{
parameters: &v1alpha1.MemberParameters{
ProjectID: &projectID,
UserID: userID,
UserID: &userID,
AccessLevel: v1alpha1AccessLevelValue,
ExpiresAt: &expiresAt,
},
Expand All @@ -109,7 +109,7 @@ func TestGenerateAddMemberOptions(t *testing.T) {
args: args{
parameters: &v1alpha1.MemberParameters{
ProjectID: &projectID,
UserID: userID,
UserID: &userID,
AccessLevel: v1alpha1.AccessLevelValue(accessLevel),
},
},
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestGenerateEditMemberOptions(t *testing.T) {
args: args{
parameters: &v1alpha1.MemberParameters{
ProjectID: &projectID,
UserID: userID,
UserID: &userID,
AccessLevel: v1alpha1AccessLevelValue,
ExpiresAt: &expiresAt,
},
Expand All @@ -156,7 +156,7 @@ func TestGenerateEditMemberOptions(t *testing.T) {
args: args{
parameters: &v1alpha1.MemberParameters{
ProjectID: &projectID,
UserID: userID,
UserID: &userID,
AccessLevel: v1alpha1AccessLevelValue,
},
},
Expand Down
40 changes: 33 additions & 7 deletions pkg/controller/projects/members/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/crossplane-contrib/provider-gitlab/apis/projects/v1alpha1"
"github.com/crossplane-contrib/provider-gitlab/pkg/clients"
"github.com/crossplane-contrib/provider-gitlab/pkg/clients/projects"
"github.com/crossplane-contrib/provider-gitlab/pkg/clients/users"
)

const (
Expand All @@ -41,6 +42,8 @@ const (
errDeleteFailed = "cannot delete Gitlab Project Member"
errObserveFailed = "cannot observe Gitlab Project Member"
errProjectIDMissing = "ProjectID is missing"
errUserInfoMissing = "UserID or UserName is missing"
errFetchFailed = "can not fetch userID by UserName"
)

// SetupMember adds a controller that reconciles Project Members.
Expand All @@ -52,7 +55,10 @@ func SetupMember(mgr ctrl.Manager, l logging.Logger) error {
For(&v1alpha1.Member{}).
Complete(managed.NewReconciler(mgr,
resource.ManagedKind(v1alpha1.MemberGroupVersionKind),
managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), newGitlabClientFn: projects.NewMemberClient}),
managed.WithExternalConnecter(&connector{
kube: mgr.GetClient(),
newGitlabClientFn: projects.NewMemberClient,
newUserClientFn: users.NewUserClient}),
managed.WithInitializers(managed.NewDefaultProviderConfig(mgr.GetClient())),
managed.WithLogger(l.WithValues("controller", name)),
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name)))))
Expand All @@ -61,6 +67,7 @@ func SetupMember(mgr ctrl.Manager, l logging.Logger) error {
type connector struct {
kube client.Client
newGitlabClientFn func(cfg clients.Config) projects.MemberClient
newUserClientFn func(cfg clients.Config) users.UserClient
}

func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
Expand All @@ -72,12 +79,13 @@ func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.E
if err != nil {
return nil, err
}
return &external{kube: c.kube, client: c.newGitlabClientFn(*cfg)}, nil
return &external{kube: c.kube, client: c.newGitlabClientFn(*cfg), userClient: c.newUserClientFn(*cfg)}, nil
}

type external struct {
kube client.Client
client projects.MemberClient
kube client.Client
client projects.MemberClient
userClient users.UserClient
}

func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.ExternalObservation, error) {
Expand All @@ -89,9 +97,21 @@ func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, errors.New(errProjectIDMissing)
}

userID, err := cr.Spec.ForProvider.UserID, error(nil)
if cr.Spec.ForProvider.UserID == nil {
if cr.Spec.ForProvider.UserName == nil {
return managed.ExternalObservation{}, errors.New(errUserInfoMissing)
}
userID, err = users.GetUserID(e.userClient, *cr.Spec.ForProvider.UserName)
if err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, errFetchFailed)
}
}
cr.Spec.ForProvider.UserID = userID

projectMember, res, err := e.client.GetProjectMember(
*cr.Spec.ForProvider.ProjectID,
cr.Spec.ForProvider.UserID,
*cr.Spec.ForProvider.UserID,
)

if err != nil {
Expand Down Expand Up @@ -140,10 +160,13 @@ func (e *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext
if cr.Spec.ForProvider.ProjectID == nil {
return managed.ExternalUpdate{}, errors.New(errProjectIDMissing)
}
if cr.Spec.ForProvider.UserID == nil {
return managed.ExternalUpdate{}, errors.New(errUserInfoMissing)
}

_, _, err := e.client.EditProjectMember(
*cr.Spec.ForProvider.ProjectID,
cr.Spec.ForProvider.UserID,
*cr.Spec.ForProvider.UserID,
projects.GenerateEditMemberOptions(&cr.Spec.ForProvider),
gitlab.WithContext(ctx),
)
Expand All @@ -158,10 +181,13 @@ func (e *external) Delete(ctx context.Context, mg resource.Managed) error {
if cr.Spec.ForProvider.ProjectID == nil {
return errors.New(errProjectIDMissing)
}
if cr.Spec.ForProvider.UserID == nil {
return errors.New(errUserInfoMissing)
}

_, err := e.client.DeleteProjectMember(
*cr.Spec.ForProvider.ProjectID,
cr.Spec.ForProvider.UserID,
*cr.Spec.ForProvider.UserID,
gitlab.WithContext(ctx),
)
return errors.Wrap(err, errDeleteFailed)
Expand Down
Loading

0 comments on commit ae5ea6d

Please sign in to comment.