Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter by cluster uuid in subnet datasource #323

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
500cc1b
Add filter by cluster uuid in subnet datasource
shreevari Jan 12, 2022
2ae79a2
Fix lint errors
shreevari Jan 13, 2022
32c324e
Add filter by cluster uuid to documentation
shreevari Jan 13, 2022
165c563
add client side filtering
shreevari Feb 2, 2022
f6d6262
Rename ExtraFilter=>AdditionalFilter and move out base search paths
shreevari Feb 3, 2022
0a863e8
Remove specific filter from subnet datasource
shreevari Feb 3, 2022
8753d04
Remove unnecessary documentation changes
shreevari Feb 3, 2022
e9724c3
Change extra to additional
shreevari Feb 3, 2022
248cfec
Handle case where base search paths are not provided
shreevari Feb 3, 2022
612d6a1
Add API filter to filter by name
shreevari Feb 3, 2022
0123168
Format and lint changes
shreevari Feb 3, 2022
d63f12f
Move filtering to its own function for better testability
shreevari Feb 3, 2022
57cd1b1
Ignore interfacer lint since suggested changes fail to compile
shreevari Feb 3, 2022
f526d78
Add tests for filters
shreevari Feb 3, 2022
285d6e0
Lint code
shreevari Feb 3, 2022
9aaf37b
Handle errors during filter
shreevari Feb 7, 2022
e09eeaf
Use jsonpath instead of own implementation
shreevari Feb 7, 2022
f9e3e6f
Modify test
shreevari Feb 7, 2022
85c23c3
Rename additional filters to client side filters
shreevari Feb 7, 2022
8adfe04
Add example for subnet client side filters
shreevari Feb 7, 2022
77490fe
Add documentation for additional filters
shreevari Feb 7, 2022
43aecfe
Fix lint errors
shreevari Feb 7, 2022
23e0d2b
Merge remote-tracking branch 'origin/master' into bugfix/fix-308-subn…
shreevari Feb 9, 2022
24e7d82
Replace filter attributes to match terraform schema names
shreevari Feb 9, 2022
cc935a6
Fix lint error
shreevari Feb 9, 2022
209baa2
Add cluster_uuid mapping to base selector in subnet datasource filter
shreevari Feb 9, 2022
bfb614a
Remove unnecessary newlines
shreevari Feb 10, 2022
8e41d4b
Add an acceptance test for subnet datasource filters
shreevari Feb 10, 2022
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
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ issues:
linters:
- testpackage
# part of the golangci govet package is picking up things that go vet doesn't. Seems flaky, shutting that specific error off
- path: client/client.go
linters:
- interfacer
# interfacer lint on `filter` func suggests lint changes that dont compile
# Details: linter suggests to convert `body` param to io.Reader since we don't use Close method, but return type requires
## io.ReadCloser thus failing to compile

run:
# which dirs to skip: they won't be analyzed;
Expand Down
128 changes: 128 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/http"
"net/url"

"github.com/PaesslerAG/jsonpath"
"github.com/hashicorp/terraform-plugin-sdk/helper/logging"
)

Expand Down Expand Up @@ -61,6 +62,12 @@ type Credentials struct {
ProxyURL string
}

// AdditionalFilter specification for client side filters
type AdditionalFilter struct {
Name string
Values []string
}

// NewClient returns a new Nutanix API client.
func NewClient(credentials *Credentials, userAgent string, absolutePath string) (*Client, error) {
if userAgent == "" {
Expand Down Expand Up @@ -221,7 +228,61 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) error
_, err = io.Copy(w, resp.Body)
if err != nil {
fmt.Printf("Error io.Copy %s", err)
return err
}
} else {
err = json.NewDecoder(resp.Body).Decode(v)
if err != nil {
return fmt.Errorf("error unmarshalling json: %s", err)
}
}
}

if c.onRequestCompleted != nil {
c.onRequestCompleted(req, resp, v)
}

return err
}

func searchSlice(slice []string, key string) bool {
for _, v := range slice {
if v == key {
return true
}
}
return false
}

// DoWithFilters performs request passed and filters entities in json response
func (c *Client) DoWithFilters(ctx context.Context, req *http.Request, v interface{}, filters []*AdditionalFilter, baseSearchPaths []string) error {
req = req.WithContext(ctx)
resp, err := c.client.Do(req)
if err != nil {
return err
}

defer func() {
yannickstruyf3 marked this conversation as resolved.
Show resolved Hide resolved
if rerr := resp.Body.Close(); err == nil {
err = rerr
}
}()

err = CheckResponse(resp)
if err != nil {
return err
}

resp.Body, err = filter(resp.Body, filters, baseSearchPaths)
if err != nil {
return err
}

if v != nil {
if w, ok := v.(io.Writer); ok {
_, err = io.Copy(w, resp.Body)
if err != nil {
fmt.Printf("Error io.Copy %s", err)
return err
}
} else {
Expand All @@ -239,6 +300,73 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) error
return err
}

func filter(body io.ReadCloser, filters []*AdditionalFilter, baseSearchPaths []string) (io.ReadCloser, error) {
if filters == nil {
return body, nil
}
if len(baseSearchPaths) == 0 {
baseSearchPaths = []string{"$."}
}

var res map[string]interface{}
b, err := io.ReadAll(body)
if err != nil {
return body, err
}
json.Unmarshal(b, &res)

// Full search paths
searchPaths := map[string][]string{}
filterMap := map[string]*AdditionalFilter{}
for _, filter := range filters {
filterMap[filter.Name] = filter
//Build search paths by appending target search paths to base paths
filterSearchPaths := []string{}
for _, baseSearchPath := range baseSearchPaths {
searchPath := fmt.Sprintf("%s.%s", baseSearchPath, filter.Name)
filterSearchPaths = append(filterSearchPaths, searchPath)
}
searchPaths[filter.Name] = filterSearchPaths
}

// Entities that pass filters
var filteredEntities []interface{}

entities := res["entities"].([]interface{})
shreevari marked this conversation as resolved.
Show resolved Hide resolved
for _, entity := range entities {
shreevari marked this conversation as resolved.
Show resolved Hide resolved
filtersPassed := 0
filter_loop:
for filter, filterSearchPaths := range searchPaths {
for _, searchPath := range filterSearchPaths {
searchTarget := entity.(map[string]interface{})
val, err := jsonpath.Get(searchPath, searchTarget)
if err != nil {
continue
}
// Stringify leaf value since we support only string values in filter
value := fmt.Sprint(val)
if searchSlice(filterMap[filter].Values, value) {
filtersPassed++
continue filter_loop
}
}
}

// Value must pass all filters since we perform logical AND b/w filters
if filtersPassed == len(filters) {
filteredEntities = append(filteredEntities, entity)
}
}
res["entities"] = filteredEntities

// Convert filtered result back to io.ReadCloser
filteredBody, jsonErr := json.Marshal(res)
if jsonErr != nil {
return body, jsonErr
}
return io.NopCloser(bytes.NewReader(filteredBody)), nil
}

// CheckResponse checks errors if exist errors in request
func CheckResponse(r *http.Response) error {
c := r.StatusCode
Expand Down
62 changes: 62 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package client
import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"regexp"
"strings"
"testing"
)
Expand Down Expand Up @@ -241,6 +243,66 @@ func TestDo_redirectLoop(t *testing.T) {
// }
// }

// *********** Filters tests ***********

func getEntity(name string, vlanID string, uuid string) string {
return fmt.Sprintf(`{"spec":{"cluster_reference":{"uuid":"%s"},"name":"%s","resources":{"vlan_id":%s}}}`, uuid, name, vlanID)
}

func removeWhiteSpace(input string) string {
whitespacePattern := regexp.MustCompile(`\s+`)
return whitespacePattern.ReplaceAllString(input, "")
}

func getFilter(name string, values []string) []*AdditionalFilter {
return []*AdditionalFilter{
{
Name: name,
Values: values,
},
}
}

func runTest(filters []*AdditionalFilter, inputString string, expected string) bool {
input := io.NopCloser(strings.NewReader(inputString))
fmt.Println(expected)
baseSearchPaths := []string{"spec", "spec.resources"}
filteredBody, err := filter(input, filters, baseSearchPaths)
if err != nil {
panic(err)
}
actualBytes, _ := io.ReadAll(filteredBody)
actual := string(actualBytes)
fmt.Println(actual)
return actual == expected
}

func TestDoWithFilters_filter(t *testing.T) {
entity1 := getEntity("subnet-01", "111", "012345-111")
entity2 := getEntity("subnet-01", "112", "012345-112")
entity3 := getEntity("subnet-02", "112", "012345-111")
input := fmt.Sprintf(`{"entities":[%s,%s,%s]}`, entity1, entity2, entity3)

filtersList := [][]*AdditionalFilter{
getFilter("name", []string{"subnet-01", "subnet-03"}),
getFilter("vlan_id", []string{"111", "subnet-03"}),
getFilter("cluster_reference.uuid", []string{"111", "012345-112"}),
}
expectedList := []string{
removeWhiteSpace(fmt.Sprintf(`{"entities":[%s,%s]}`, entity1, entity2)),
removeWhiteSpace(fmt.Sprintf(`{"entities":[%s]}`, entity1)),
removeWhiteSpace(fmt.Sprintf(`{"entities":[%s]}`, entity2)),
}

for i := 0; i < len(filtersList); i++ {
if ok := runTest(filtersList[i], input, expectedList[i]); !ok {
t.Fatal()
}
}
}

// *************************************

func TestClient_NewRequest(t *testing.T) {
type fields struct {
Credentials *Credentials
Expand Down
14 changes: 8 additions & 6 deletions client/v3/v3_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type Service interface {
DeleteVolumeGroup(uuid string) error
CreateVolumeGroup(request *VolumeGroupInput) (*VolumeGroupResponse, error)
ListAllVM(filter string) (*VMListIntentResponse, error)
ListAllSubnet(filter string) (*SubnetListIntentResponse, error)
ListAllSubnet(filter string, clientSideFilters []*client.AdditionalFilter) (*SubnetListIntentResponse, error)
ListAllNetworkSecurityRule(filter string) (*NetworkSecurityRuleListIntentResponse, error)
ListAllImage(filter string) (*ImageListIntentResponse, error)
ListAllCluster(filter string) (*ClusterListIntentResponse, error)
Expand Down Expand Up @@ -293,8 +293,9 @@ func (op Operations) ListSubnet(getEntitiesRequest *DSMetadata) (*SubnetListInte
if err != nil {
return nil, err
}
baseSearchPaths := []string{"metadata", "status", "status.resources"}

return subnetListIntentResponse, op.client.Do(ctx, req, subnetListIntentResponse)
return subnetListIntentResponse, op.client.DoWithFilters(ctx, req, subnetListIntentResponse, getEntitiesRequest.ClientSideFilters, baseSearchPaths)
}

/*UpdateSubnet Updates a subnet
Expand Down Expand Up @@ -938,13 +939,14 @@ func (op Operations) ListAllVM(filter string) (*VMListIntentResponse, error) {
}

// ListAllSubnet ...
func (op Operations) ListAllSubnet(filter string) (*SubnetListIntentResponse, error) {
func (op Operations) ListAllSubnet(filter string, clientSideFilters []*client.AdditionalFilter) (*SubnetListIntentResponse, error) {
entities := make([]*SubnetIntentResponse, 0)

resp, err := op.ListSubnet(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("subnet"),
Length: utils.Int64Ptr(itemsPerPage),
Filter: &filter,
Kind: utils.StringPtr("subnet"),
Length: utils.Int64Ptr(itemsPerPage),
ClientSideFilters: clientSideFilters,
})

if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions client/v3/v3_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package v3

import (
"time"

"github.com/terraform-providers/terraform-provider-nutanix/client"
)

// Reference ...
Expand Down Expand Up @@ -583,6 +585,9 @@ type DSMetadata struct {

// The sort order in which results are returned
SortOrder *string `json:"sort_order,omitempty" mapstructure:"sort_order,omitempty"`

// Additional filters for client side filtering api response
ClientSideFilters []*client.AdditionalFilter `json:"-"`
}

// VMIntentResource Response object for intentful operations on a vm
Expand Down
41 changes: 41 additions & 0 deletions examples/client-side-filters.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
terraform {
required_providers {
nutanix = {
source = "nutanix/nutanix"
version = "1.3"
}
}
}

provider "nutanix" {
username = "admin"
password = "password"
endpoint = "pc-ip"
insecure = true
port = 9440
}

data "nutanix_clusters" "clusters" {}

output "subnet" {
value = data.nutanix_subnet.test
}

data "nutanix_subnet" "test" {
subnet_name = "nutanix-subnet"
additional_filter {
name = "name"
values = ["vlan.154", "nutanix-subnet"]
}

additional_filter {
name = "vlan_id"
values = ["154", "123"]
}

additional_filter {
name = "cluster_reference.uuid"
values = ["0005d304-e60e-43c3-3507-ac1f6b60292f"]
}
}

3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module github.com/terraform-providers/terraform-provider-nutanix

require (
github.com/PaesslerAG/jsonpath v0.1.1
github.com/client9/misspell v0.3.4
github.com/golangci/golangci-lint v1.25.0
github.com/hashicorp/terraform-plugin-sdk v1.7.0
github.com/magiconair/properties v1.8.1
github.com/magiconair/properties v1.8.1 // indirect
github.com/mitchellh/gox v1.0.1
github.com/spf13/cast v1.3.1
github.com/stretchr/testify v1.6.1
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/OpenPeeDeeP/depguard v1.0.1 h1:VlW4R6jmBIv3/u1JNlawEvJMM4J+dPORPaZasQee8Us=
github.com/OpenPeeDeeP/depguard v1.0.1/go.mod h1:xsIw86fROiiwelg+jB2uM9PiKihMMmUx/1V+TNhjQvM=
github.com/PaesslerAG/gval v1.0.0 h1:GEKnRwkWDdf9dOmKcNrar9EA1bz1z9DqPIO1+iLzhd8=
github.com/PaesslerAG/gval v1.0.0/go.mod h1:y/nm5yEyTeX6av0OfKJNp9rBNj2XrGhAf5+v24IBN1I=
github.com/PaesslerAG/jsonpath v0.1.0/go.mod h1:4BzmtoM/PI8fPO4aQGIusjGxGir2BzcV0grWtFzq1Y8=
github.com/PaesslerAG/jsonpath v0.1.1 h1:c1/AToHQMVsduPAa4Vh6xp2U0evy4t8SWp8imEsylIk=
github.com/PaesslerAG/jsonpath v0.1.1/go.mod h1:lVboNxFGal/VwW6d9JzIy56bUsYAP6tH/x80vjnCseY=
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE=
Expand Down
Loading