-
Notifications
You must be signed in to change notification settings - Fork 367
/
data_service_principals.go
39 lines (34 loc) · 1.14 KB
/
data_service_principals.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package scim
import (
"context"
"fmt"
"sort"
"github.com/databricks/terraform-provider-databricks/common"
)
// DataSourceServicePrincipals searches for service principals based on display_name
func DataSourceServicePrincipals() common.Resource {
type spnsData struct {
DisplayNameContains string `json:"display_name_contains,omitempty" tf:"computed"`
ApplicationIDs []string `json:"application_ids,omitempty" tf:"computed,slice_set"`
}
return common.DataResource(spnsData{}, func(ctx context.Context, e any, c *common.DatabricksClient) error {
response := e.(*spnsData)
spnAPI := NewServicePrincipalsAPI(ctx, c)
var filter string
if response.DisplayNameContains != "" {
filter = fmt.Sprintf(`displayName co "%s"`, response.DisplayNameContains)
}
spList, err := spnAPI.Filter(filter, true)
if err != nil {
return err
}
if len(spList) == 0 {
return fmt.Errorf("cannot find SPs with display name containing %s", response.DisplayNameContains)
}
for _, sp := range spList {
response.ApplicationIDs = append(response.ApplicationIDs, sp.ApplicationID)
}
sort.Strings(response.ApplicationIDs)
return nil
})
}