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

Added filter block to databricks_instance_profiles data source #2988

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
61 changes: 53 additions & 8 deletions aws/data_instance_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,81 @@ package aws

import (
"context"
"encoding/json"
"fmt"
"reflect"
"regexp"
"strings"

"github.com/databricks/databricks-sdk-go"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/exp/slices"
)

func instanceProfileMatchesFilter(ip *instanceProfileData, filter *instanceProfileFilter) bool {
var ipMap map[string]interface{}
m, _ := json.Marshal(ip)
_ = json.Unmarshal(m, &ipMap)
val := ipMap[filter.Name]
stringVal := fmt.Sprint(val)
re := regexp.MustCompile(filter.Pattern)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use .Compile and check for the error. Also, it's very inefficient - it's better to check validity of the regexp earlier, around line 46, compile regexp, and pass it explicitly. Otherwise we'll compile it on each function invocation.

return re.Match([]byte(regexp.QuoteMeta(stringVal)))
}

type instanceProfileData struct {
Name string `json:"name"`
Arn string `json:"arn"`
RoleArn string `json:"role_arn"`
IsMeta bool `json:"is_meta"`
}

type instanceProfileFilter struct {
Name string `json:"name"`
Pattern string `json:"pattern"`
}

func DataSourceInstanceProfiles() *schema.Resource {
type instanceProfileData struct {
Name string `json:"name,omitempty" tf:"computed"`
Arn string `json:"arn,omitempty" tf:"computed"`
RoleArn string `json:"role_arn,omitempty" tf:"computed"`
IsMeta bool `json:"is_meta,omitempty" tf:"computed"`
}
return common.WorkspaceData(func(ctx context.Context, data *struct {
InstanceProfiles []instanceProfileData `json:"instance_profiles,omitempty" tf:"computed"`
Filter instanceProfileFilter `json:"filter,omitempty" tf:"optional"`
}, w *databricks.WorkspaceClient) error {

if data.Filter != (instanceProfileFilter{}) {
if data.Filter.Pattern == "" {
return fmt.Errorf("field `pattern` cannot be empty")
}
var fieldNames []string
val := reflect.ValueOf(instanceProfileData{})
for i := 0; i < val.Type().NumField(); i++ {
fieldNames = append(fieldNames, val.Type().Field(i).Tag.Get("json"))
}
Comment on lines +49 to +53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid having this if we expose listAllFields function implemented in #3044 - in this case it will handle inherited fields correctly

if !slices.Contains(fieldNames, data.Filter.Name) {
if data.Filter.Name == "" {
return fmt.Errorf("field `name` cannot be empty")
}
Comment on lines +55 to +57
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we can move this condition after the line 48

return fmt.Errorf("`%s` is not a valid value for the name field. Must be one of [%s]", data.Filter.Name, strings.Join(fieldNames, ", "))
}
}

instanceProfiles, err := w.InstanceProfiles.ListAll(ctx)
if err != nil {
return err
}

for _, v := range instanceProfiles {
arnSlices := strings.Split(v.InstanceProfileArn, "/")
name := arnSlices[len(arnSlices)-1]
data.InstanceProfiles = append(data.InstanceProfiles, instanceProfileData{
ipData := instanceProfileData{
Name: name,
Arn: v.InstanceProfileArn,
RoleArn: v.IamRoleArn,
IsMeta: v.IsMetaInstanceProfile,
})
}
if data.Filter != (instanceProfileFilter{}) && !instanceProfileMatchesFilter(&ipData, &data.Filter) {
continue
}
data.InstanceProfiles = append(data.InstanceProfiles, ipData)
}
return nil
})
Expand Down
Loading
Loading