Verified Commit b7ba2a65 authored by Andreas Kunze's avatar Andreas Kunze Committed by GitLab
Browse files

feat: Get single group and project service accounts

Changelog: Improvements
parent 30b6a88b
Loading
Loading
Loading
Loading
+11 −0
Changes for group_serviceaccounts.go: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -64,6 +64,17 @@ func (s *GroupsService) ListServiceAccounts(gid any, opt *ListServiceAccountsOpt
	)
}

// GetServiceAccount retrieves a single service account in a specified group.
//
// GitLab API docs:
// https://docs.gitlab.com/api/service_accounts/#retrieve-a-group-service-account
func (s *GroupsService) GetServiceAccount(gid any, serviceAccount int64, options ...RequestOptionFunc) (*GroupServiceAccount, *Response, error) {
	return do[*GroupServiceAccount](s.client,
		withPath(routeGroupsIDServiceAccountsID, GroupID{gid}, serviceAccount),
		withRequestOpts(options...),
	)
}

// CreateServiceAccountOptions represents the available CreateServiceAccount() options.
//
// GitLab API docs:
+30 −0
Changes for group_serviceaccounts_test.go: 30 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -65,6 +65,36 @@ func TestListGroupServiceAccounts(t *testing.T) {
	assert.Equal(t, want, serviceAccounts)
}

func TestGetGroupServiceAccount(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	mux.HandleFunc("/api/v4/groups/1/service_accounts/57", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		fmt.Fprint(w, `
      {
	      "id": 57,
	      "username": "service_account_group_345_6018816a18e515214e0c34c2b33523fc",
		  "public_email": null,
	      "name": "Service account user",
		  "email": "service_account_group_345_6018816a18e515214e0c34c2b33523fc@noreply.gitlab.com"
      }`)
	})

	sa, resp, err := client.Groups.GetServiceAccount(1, 57)
	assert.NoError(t, err)
	assert.NotNil(t, resp)

	want := &GroupServiceAccount{
		ID:          57,
		UserName:    "service_account_group_345_6018816a18e515214e0c34c2b33523fc",
		PublicEmail: "",
		Name:        "Service account user",
		Email:       "service_account_group_345_6018816a18e515214e0c34c2b33523fc@noreply.gitlab.com",
	}
	assert.Equal(t, want, sa)
}

func TestCreateServiceAccount(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)
+1 −0
Changes for groups.go: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -284,6 +284,7 @@ type (

		// group_serviceaccounts.go
		ListServiceAccounts(gid any, opt *ListServiceAccountsOptions, options ...RequestOptionFunc) ([]*GroupServiceAccount, *Response, error)
		GetServiceAccount(gid any, serviceAccount int64, options ...RequestOptionFunc) (*GroupServiceAccount, *Response, error)
		CreateServiceAccount(gid any, opt *CreateServiceAccountOptions, options ...RequestOptionFunc) (*GroupServiceAccount, *Response, error)
		UpdateServiceAccount(gid any, serviceAccount int64, opt *UpdateServiceAccountOptions, options ...RequestOptionFunc) (*GroupServiceAccount, *Response, error)
		DeleteServiceAccount(gid any, serviceAccount int64, opt *DeleteServiceAccountOptions, options ...RequestOptionFunc) (*Response, error)
+12 −0
Changes for project_serviceaccounts.go: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -137,6 +137,18 @@ func (s *ProjectsService) ListProjectServiceAccounts(pid any, opt *ListProjectSe
	)
}

// GetProjectServiceAccount retrieves a single service account
// in a specified project.
//
// GitLab API docs:
// https://docs.gitlab.com/api/service_accounts/#retrieve-a-project-service-account
func (s *ProjectsService) GetProjectServiceAccount(pid any, serviceAccount int64, options ...RequestOptionFunc) (*ProjectServiceAccount, *Response, error) {
	return do[*ProjectServiceAccount](s.client,
		withPath(routeProjectsIDServiceAccountsID, ProjectID{pid}, serviceAccount),
		withRequestOpts(options...),
	)
}

// CreateProjectServiceAccount creates a project service account user.
//
// GitLab API docs:
+28 −0
Changes for project_serviceaccounts_test.go: 28 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -57,6 +57,34 @@ func TestListProjectServiceAccounts(t *testing.T) {
	assert.Equal(t, want, serviceAccounts)
}

func TestGetProjectServiceAccount(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	mux.HandleFunc("/api/v4/projects/1/service_accounts/42", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		fmt.Fprint(w, `
      {
	      "id": 42,
          "username": "service_account_project_345_6018816a18e515214e0c34c2b33523fc",
	      "name": "Service account user",
		  "email": "service_account_project_345_6018816a18e515214e0c34c2b33523fc@noreply.gitlab.com"
      }`)
	})

	sa, resp, err := client.Projects.GetProjectServiceAccount(1, 42)
	assert.NoError(t, err)
	assert.NotNil(t, resp)

	want := &ProjectServiceAccount{
		ID:       42,
		Username: "service_account_project_345_6018816a18e515214e0c34c2b33523fc",
		Name:     "Service account user",
		Email:    "service_account_project_345_6018816a18e515214e0c34c2b33523fc@noreply.gitlab.com",
	}
	assert.Equal(t, want, sa)
}

func TestCreateProjectServiceAccount(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)
Loading