Skip to content

Commit

Permalink
added func to get edge hosts using tags
Browse files Browse the repository at this point in the history
  • Loading branch information
MGamya committed Jun 25, 2024
1 parent cfbbb09 commit 4af332c
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions client/cluster_edge_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"errors"
"fmt"

clientV1 "github.com/spectrocloud/palette-api-go/client/v1"
"github.com/spectrocloud/palette-api-go/models"
Expand Down Expand Up @@ -68,6 +69,124 @@ func (h *V1Client) ListEdgeHosts() ([]*models.V1EdgeHostsMetadata, error) {
return items, nil
}

func (h *V1Client) GetEdgeHostsByTags(tags map[string]string) ([]*models.V1EdgeHostsMetadata, error) {
continueToken := ""
var items []*models.V1EdgeHostsMetadata
filter := getEdgeFilter(nil, tags)
for ok := true; ok; ok = (continueToken != "") {
params := clientV1.NewV1DashboardEdgehostsSearchParamsWithContext(h.ctx).
WithBody(&models.V1SearchFilterSummarySpec{
Filter: filter,
Sort: nil,
})
resp, err := h.Client.V1DashboardEdgehostsSearch(params)
if err != nil {
return nil, err
}
continueToken = resp.Payload.Listmeta.Continue
items = append(items, resp.Payload.Items...)
}

return items, nil
}

func getEdgeFilter(extraFilters []*models.V1SearchFilterItem, tags map[string]string) *models.V1SearchFilterSpec {
filter := &models.V1SearchFilterSpec{
Conjunction: and(),
FilterGroups: []*models.V1SearchFilterGroup{
{
Conjunction: and(),
Filters: []*models.V1SearchFilterItem{},
},
},
}

// Tags filter
if len(tags) > 0 {
var tagValues []string
for key, value := range tags {
tagValues = append(tagValues, fmt.Sprintf("%s:%s", key, value))
}
tagsFilter := &models.V1SearchFilterItem{
Condition: &models.V1SearchFilterCondition{
String: &models.V1SearchFilterStringCondition{
Match: &models.V1SearchFilterStringConditionMatch{
Conjunction: or(),
Values: tagValues,
},
Operator: models.V1SearchFilterStringOperatorEq,
Negation: false,
IgnoreCase: false,
},
},
Property: "tags",
Type: models.V1SearchFilterPropertyTypeString,
}
filter.FilterGroups[0].Filters = append(filter.FilterGroups[0].Filters, tagsFilter)
}

// // State filter
// if state != "" {
// stateFilter := &models.V1SearchFilterItem{
// Condition: &models.V1SearchFilterCondition{
// String: &models.V1SearchFilterStringCondition{
// Match: &models.V1SearchFilterStringConditionMatch{
// Conjunction: or(),
// Values: []string{state},
// },
// Operator: models.V1SearchFilterStringOperatorEq,
// },
// },
// Property: "state",
// Type: models.V1SearchFilterPropertyTypeString,
// }
// filter.FilterGroups[0].Filters = append(filter.FilterGroups[0].Filters, stateFilter)
// }

// // Health state filter
// if healthState != "" {
// healthStateFilter := &models.V1SearchFilterItem{
// Condition: &models.V1SearchFilterCondition{
// String: &models.V1SearchFilterStringCondition{
// Match: &models.V1SearchFilterStringConditionMatch{
// Conjunction: or(),
// Values: []string{healthState},
// },
// Operator: models.V1SearchFilterStringOperatorEq,
// },
// },
// Property: "healthState",
// Type: models.V1SearchFilterPropertyTypeString,
// }
// filter.FilterGroups[0].Filters = append(filter.FilterGroups[0].Filters, healthStateFilter)
// }

// // Architecture filter
// if architecture != "" {
// architectureFilter := &models.V1SearchFilterItem{
// Condition: &models.V1SearchFilterCondition{
// String: &models.V1SearchFilterStringCondition{
// Match: &models.V1SearchFilterStringConditionMatch{
// Conjunction: or(),
// Values: []string{architecture},
// },
// Operator: models.V1SearchFilterStringOperatorEq,
// },
// },
// Property: "architecture",
// Type: models.V1SearchFilterPropertyTypeString,
// }
// filter.FilterGroups[0].Filters = append(filter.FilterGroups[0].Filters, architectureFilter)
// }

// Append extra filters if provided
if extraFilters != nil {
filter.FilterGroups = append(filter.FilterGroups, &models.V1SearchFilterGroup{Conjunction: and(), Filters: extraFilters})
}

return filter
}

func (h *V1Client) CreateClusterEdgeNative(cluster *models.V1SpectroEdgeNativeClusterEntity) (string, error) {
params := clientV1.NewV1SpectroClustersEdgeNativeCreateParamsWithContext(h.ctx).
WithBody(cluster)
Expand Down

0 comments on commit 4af332c

Please sign in to comment.