Skip to content

Commit

Permalink
Added support for querying delegates
Browse files Browse the repository at this point in the history
  • Loading branch information
micahlmartin committed Oct 8, 2021
1 parent 000600b commit 9da1c94
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 0 deletions.
126 changes: 126 additions & 0 deletions harness/api/delegate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package api

import (
"fmt"
"strings"

"github.com/harness-io/harness-go-sdk/harness/api/graphql"
)

// Helper type for accessing all delegate related crud methods
type DelegateClient struct {
APIClient *Client
}

// Get the client for interacting with Harness delegates
func (c *Client) Delegates() *DelegateClient {
return &DelegateClient{
APIClient: c,
}
}

func (c *DelegateClient) GetDelegateByName(name string) (*graphql.Delegate, error) {
delegateList, _, err := c.GetDelegateWithFilters(1, 0, name, "", "")

if err != nil {
return nil, err
}

if len(delegateList) == 0 {
return nil, nil
}

return delegateList[0], nil
}

func (c *DelegateClient) GetDelegateWithFilters(limit int, offset int, name string, status graphql.DelegateStatus, delegateType graphql.DelegateType) ([]*graphql.Delegate, *graphql.PageInfo, error) {

filters := []string{}

if name != "" {
filters = append(filters, fmt.Sprintf(`delegateName:"%s"`, name))
}

if status != "" {
filters = append(filters, fmt.Sprintf(`delegateStatus: %s`, status))
}

if delegateType != "" {
filters = append(filters, fmt.Sprintf(`delegateType: %s`, delegateType))
}

filterStr := strings.Join(filters, ", ")

query := &GraphQLQuery{
Query: fmt.Sprintf(`query {
delegateList(limit: %[3]d, offset: %[4]d, filters: [{
accountId: "%[5]s",
%[6]s
}]) {
nodes {
%[1]s
}
%[2]s
}
}`, standardDelegateFields, paginationFields, limit, offset, c.APIClient.AccountId, filterStr),
}

res := struct {
DelegateList struct {
Nodes []*graphql.Delegate `json:"nodes"`
PageInfo *graphql.PageInfo
}
}{}

err := c.APIClient.ExecuteGraphQLQuery(query, &res)

if err != nil {
return nil, nil, err
}

return res.DelegateList.Nodes, res.DelegateList.PageInfo, nil
}

func (c *DelegateClient) ListDelegates(limit int, offset int) ([]*graphql.Delegate, *graphql.PageInfo, error) {
query := &GraphQLQuery{
Query: fmt.Sprintf(`query {
delegateList(limit: %[3]d, offset: %[4]d) {
nodes {
%[1]s
}
%[2]s
}
}`, standardDelegateFields, paginationFields, limit, offset),
}

res := struct {
DelegateList struct {
Nodes []*graphql.Delegate `json:"nodes"`
PageInfo *graphql.PageInfo
}
}{}

err := c.APIClient.ExecuteGraphQLQuery(query, &res)

if err != nil {
return nil, nil, err
}

return res.DelegateList.Nodes, res.DelegateList.PageInfo, nil
}

const standardDelegateFields = `
ip
hostName
delegateName
uuid
accountId
delegateProfileId
delegateType
description
hostName
lastHeartBeat
polllingModeEnabled
status
version
`
51 changes: 51 additions & 0 deletions harness/api/delegate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package api

import (
"testing"

"github.com/harness-io/harness-go-sdk/harness/api/graphql"
"github.com/stretchr/testify/require"
)

func TestListDelegates(t *testing.T) {
client := getClient()
limit := 100
offset := 0
hasMore := true

for hasMore {
delegates, pagination, err := client.Delegates().ListDelegates(limit, offset)
require.NoError(t, err, "Failed to list delegates: %s", err)
require.NotEmpty(t, delegates, "No delegates found")
require.NotNil(t, pagination, "Pagination should not be nil")

hasMore = len(delegates) == limit
offset += limit
}
}

func TestGetDelegateByName(t *testing.T) {
client := getClient()
delegateName := "harness-delegate"

delegate, err := client.Delegates().GetDelegateByName(delegateName)
require.NoError(t, err, "Failed to get delegate: %s", err)
require.NotNil(t, delegate, "Delegate should not be nil")
require.Equal(t, delegateName, delegate.DelegateName, "Delegate name should be %s", delegateName)
}

func TestGetDelegateByName_NotFound(t *testing.T) {
client := getClient()
delegateName := "nodelegate"

delegate, err := client.Delegates().GetDelegateByName(delegateName)
require.NoError(t, err, "Failed to get delegate: %s", err)
require.Nil(t, delegate, "Delegate should be nil")
}

func TestGetDelegateByStatus(t *testing.T) {
client := getClient()
delegateList, _, err := client.Delegates().GetDelegateWithFilters(1, 0, "", graphql.DelegateStatusList.Enabled, "")
require.NoError(t, err, "Failed to get delegate: %s", err)
require.GreaterOrEqual(t, len(delegateList), 1, "Delegate list should have at least 1 delegate")
}
50 changes: 50 additions & 0 deletions harness/api/graphql/enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,53 @@ var SSOTypes = struct {
LDAP: "LDAP",
SAML: "SAML",
}

type DelegateType string

var DelegateTypes = struct {
Docker DelegateType
Ecs DelegateType
Helm DelegateType
Kubernetes DelegateType
Shell DelegateType
}{
Docker: "DOCKER",
Ecs: "ECS",
Helm: "HELM_DELEGATE",
Kubernetes: "KUBERNETES",
Shell: "SHELL_SCRIPT",
}

var DelegateTypesSlice = []string{
"DOCKER",
"ECS",
"HELM_DELEGATE",
"KUBERNETES",
"SHELL_SCRIPT",
}

func (d DelegateType) String() string {
return string(d)
}

type DelegateStatus string

var DelegateStatusList = struct {
Deleted DelegateStatus
Enabled DelegateStatus
WaitingForApproval DelegateStatus
}{
Deleted: "DELETED",
Enabled: "ENABLED",
WaitingForApproval: "WAITING_FOR_APPROVAL",
}

var DelegateStatusSlice = []string{
"DELETED",
"ENABLED",
"WAITING_FOR_APPROVAL",
}

func (d DelegateStatus) String() string {
return string(d)
}
15 changes: 15 additions & 0 deletions harness/api/graphql/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,3 +865,18 @@ type CEClusterHealth struct {
LastEventTimestamp float64 `json:"lastEventTimestamp,omitempty"`
Messages []string `json:"messages"`
}

type Delegate struct {
AccountId string `json:"accountId,omitempty"`
DelegateName string `json:"delegateName,omitempty"`
DelegateProfileId string `json:"delegateProfileId,omitempty"`
DelegateType DelegateType `json:"delegateType,omitempty"`
Description string `json:"description,omitempty"`
HostName string `json:"hostName,omitempty"`
Ip string `json:"ip,omitempty"`
LastHeartBeat string `json:"lastHeartBeat,omitempty"`
PollingModeEnabled bool `json:"pollingModeEnabled,omitempty"`
Status string `json:"status,omitempty"`
UUID string `json:"uuid,omitempty"`
Version string `json:"version,omitempty"`
}

0 comments on commit 9da1c94

Please sign in to comment.