Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Feat: Add Group SSH certificates API functionality #1838

Merged
merged 2 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ type Client struct {
GroupMilestones *GroupMilestonesService
GroupProtectedEnvironments *GroupProtectedEnvironmentsService
GroupRepositoryStorageMove *GroupRepositoryStorageMoveService
GroupSSHCertificates *GroupSSHCertificatesService
GroupVariables *GroupVariablesService
GroupWikis *GroupWikisService
Groups *GroupsService
Expand Down Expand Up @@ -379,6 +380,7 @@ func newClient(options ...ClientOptionFunc) (*Client, error) {
c.GroupMilestones = &GroupMilestonesService{client: c}
c.GroupProtectedEnvironments = &GroupProtectedEnvironmentsService{client: c}
c.GroupRepositoryStorageMove = &GroupRepositoryStorageMoveService{client: c}
c.GroupSSHCertificates = &GroupSSHCertificatesService{client: c}
c.GroupVariables = &GroupVariablesService{client: c}
c.GroupWikis = &GroupWikisService{client: c}
c.Groups = &GroupsService{client: c}
Expand Down
105 changes: 105 additions & 0 deletions group_ssh_certificates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package gitlab

import (
"fmt"
"net/http"
"time"
)

// GroupSSHCertificatesService handles communication with the group
// SSH certificate related methods of the GitLab API.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/group_ssh_certificates.html
type GroupSSHCertificatesService struct {
client *Client
}

// GroupSSHCertificate represents a GitLab Group SSH certificate.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/member_roles.html
type GroupSSHCertificate struct {
ID int `json:"id"`
Title string `json:"title"`
Key string `json:"key"`
CreatedAt *time.Time `json:"created_at"`
}

// ListGroupSSHCertificates gets a list of SSH certificates for a specified
// group.
//
// Gitlab API docs:
// https://docs.gitlab.com/ee/api/group_ssh_certificates.html#get-all-ssh-certificates-for-a-particular-group
func (s *GroupSSHCertificatesService) ListGroupSSHCertificates(gid interface{}, options ...RequestOptionFunc) ([]*GroupSSHCertificate, *Response, error) {
group, err := parseID(gid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("groups/%s/ssh_certificates", PathEscape(group))

req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}

var certs []*GroupSSHCertificate
resp, err := s.client.Do(req, &certs)
if err != nil {
return nil, resp, err
}

return certs, resp, nil
}

// CreateGroupSSHCertificateOptions represents the available
// CreateGroupSSHCertificate() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_ssh_certificates.html#create-ssh-certificate
type CreateGroupSSHCertificateOptions struct {
Key *string `url:"key,omitempty" json:"key,omitempty"`
Title *string `url:"title,omitempty" json:"title,omitempty"`
}

// CreateMemberRole creates a new member role for a specified group.
//
// Gitlab API docs:
// https://docs.gitlab.com/ee/api/group_ssh_certificates.html#create-ssh-certificate
func (s *GroupSSHCertificatesService) CreateGroupSSHCertificate(gid interface{}, opt *CreateGroupSSHCertificateOptions, options ...RequestOptionFunc) (*GroupSSHCertificate, *Response, error) {
group, err := parseID(gid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("groups/%s/ssh_certificates", PathEscape(group))

req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
if err != nil {
return nil, nil, err
}

cert := new(GroupSSHCertificate)
resp, err := s.client.Do(req, cert)
if err != nil {
return nil, resp, err
}

return cert, resp, nil
}

// DeleteGroupSSHCertificate deletes a SSH certificate from a specified group.
//
// Gitlab API docs:
// https://docs.gitlab.com/ee/api/group_ssh_certificates.html#delete-group-ssh-certificate
func (s *GroupSSHCertificatesService) DeleteGroupSSHCertificate(gid interface{}, cert int, options ...RequestOptionFunc) (*Response, error) {
group, err := parseID(gid)
if err != nil {
return nil, err
}
u := fmt.Sprintf("groups/%s/ssh_certificates/%d", PathEscape(group), cert)

req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
if err != nil {
return nil, err
}

return s.client.Do(req, nil)
}
72 changes: 72 additions & 0 deletions group_ssh_certificates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package gitlab

import (
"net/http"
"testing"
"time"

"github.com/stretchr/testify/require"
)

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

path := "/api/v4/groups/1/ssh_certificates"

mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
mustWriteHTTPResponse(t, w, "testdata/list_group_ssh_certificates.json")
})

certificates, _, err := client.GroupSSHCertificates.ListGroupSSHCertificates(1)
require.NoError(t, err)

want := []*GroupSSHCertificate{
{
ID: 1876,
Title: "SSH Certificate",
Key: "ssh-rsa ssh-rsa AAAAB3NzaC1ea2dAAAADAQABAAAAgQDGbLkF44ScxRQi2FfA7VsHgGqptguSbmW26jkJhEiRZpGS4/+UzaaSqc8Psw2OhSsKc5QwfrB/ANpO4LhOjDzhf2FuD8ACkv3R7XtaJ+rN6PlyzoBfLAiSyzxhEoMFDBprTgaiZKgg2yQ9dRH55w3f6XMZ4hnaUae53nQgfQLxFw== [email protected]",
CreatedAt: Ptr(time.Date(2022, time.March, 20, 20, 42, 40, 221000000, time.UTC)),
},
}

require.Equal(t, want, certificates)
}

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

path := "/api/v4/groups/84/ssh_certificates"

mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
mustWriteHTTPResponse(t, w, "testdata/create_group_ssh_certificates.json")
})

cert, _, err := client.GroupSSHCertificates.CreateGroupSSHCertificate(84, &CreateGroupSSHCertificateOptions{
Key: Ptr("ssh-rsa ssh-rsa AAAAB3NzaC1ea2dAAAADAQABAAAAgQDGbLkF44ScxRQi2FfA7VsHgGqptguSbmW26jkJhEiRZpGS4/+UzaaSqc8Psw2OhSsKc5QwfrB/ANpO4LhOjDzhf2FuD8ACkv3R7XtaJ+rN6PlyzoBfLAiSyzxhEoMFDBprTgaiZKgg2yQ9dRH55w3f6XMZ4hnaUae53nQgfQLxFw== [email protected]"),
Title: Ptr("SSH Certificate"),
})
require.NoError(t, err)

want := &GroupSSHCertificate{
ID: 1876,
Title: "SSH Certificate",
Key: "ssh-rsa ssh-rsa AAAAB3NzaC1ea2dAAAADAQABAAAAgQDGbLkF44ScxRQi2FfA7VsHgGqptguSbmW26jkJhEiRZpGS4/+UzaaSqc8Psw2OhSsKc5QwfrB/ANpO4LhOjDzhf2FuD8ACkv3R7XtaJ+rN6PlyzoBfLAiSyzxhEoMFDBprTgaiZKgg2yQ9dRH55w3f6XMZ4hnaUae53nQgfQLxFw== [email protected]",
CreatedAt: Ptr(time.Date(2022, time.March, 20, 20, 42, 40, 221000000, time.UTC)),
}

require.Equal(t, want, cert)
}
func TestDeleteGroupSSHCertificate(t *testing.T) {
mux, client := setup(t)

path := "/api/v4/groups/1/ssh_certificates/1876"

mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})

_, err := client.GroupSSHCertificates.DeleteGroupSSHCertificate(1, 1876)
require.NoError(t, err)
}
6 changes: 6 additions & 0 deletions testdata/create_group_ssh_certificates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": 1876,
"title": "SSH Certificate",
"key": "ssh-rsa ssh-rsa AAAAB3NzaC1ea2dAAAADAQABAAAAgQDGbLkF44ScxRQi2FfA7VsHgGqptguSbmW26jkJhEiRZpGS4/+UzaaSqc8Psw2OhSsKc5QwfrB/ANpO4LhOjDzhf2FuD8ACkv3R7XtaJ+rN6PlyzoBfLAiSyzxhEoMFDBprTgaiZKgg2yQ9dRH55w3f6XMZ4hnaUae53nQgfQLxFw== [email protected]",
"created_at": "2022-03-20T20:42:40.221Z"
}
8 changes: 8 additions & 0 deletions testdata/list_group_ssh_certificates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"id": 1876,
"title": "SSH Certificate",
"key": "ssh-rsa ssh-rsa AAAAB3NzaC1ea2dAAAADAQABAAAAgQDGbLkF44ScxRQi2FfA7VsHgGqptguSbmW26jkJhEiRZpGS4/+UzaaSqc8Psw2OhSsKc5QwfrB/ANpO4LhOjDzhf2FuD8ACkv3R7XtaJ+rN6PlyzoBfLAiSyzxhEoMFDBprTgaiZKgg2yQ9dRH55w3f6XMZ4hnaUae53nQgfQLxFw== [email protected]",
"created_at": "2022-03-20T20:42:40.221Z"
}
]