-
Notifications
You must be signed in to change notification settings - Fork 386
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can avoid having this if we expose |
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
}) | ||
|
There was a problem hiding this comment.
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.