-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for querying delegates
- Loading branch information
1 parent
000600b
commit 9da1c94
Showing
4 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters