-
Notifications
You must be signed in to change notification settings - Fork 360
/
data_service_principal.go
63 lines (59 loc) · 2.39 KB
/
data_service_principal.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package scim
import (
"context"
"fmt"
"github.com/databricks/terraform-provider-databricks/common"
)
// DataSourceServicePrincipal returns information about the spn specified by the application_id
func DataSourceServicePrincipal() common.Resource {
type spnData struct {
ApplicationID string `json:"application_id,omitempty" tf:"computed"`
DisplayName string `json:"display_name,omitempty" tf:"computed"`
SpID string `json:"sp_id,omitempty" tf:"computed"`
ID string `json:"id,omitempty" tf:"computed"`
Home string `json:"home,omitempty" tf:"computed"`
Repos string `json:"repos,omitempty" tf:"computed"`
Active bool `json:"active,omitempty" tf:"computed"`
ExternalID string `json:"external_id,omitempty" tf:"computed"`
AclPrincipalID string `json:"acl_principal_id,omitempty" tf:"computed"`
}
return common.DataResource(spnData{}, func(ctx context.Context, e any, c *common.DatabricksClient) error {
response := e.(*spnData)
spnAPI := NewServicePrincipalsAPI(ctx, c)
var spList []User
var err error
if response.ApplicationID != "" && response.DisplayName != "" {
return fmt.Errorf("please specify only one of application_id or display_name")
}
if response.ApplicationID != "" {
spList, err = spnAPI.Filter(fmt.Sprintf(`applicationId eq "%s"`, response.ApplicationID), true)
} else if response.DisplayName != "" {
spList, err = spnAPI.Filter(fmt.Sprintf(`displayName eq "%s"`, response.DisplayName), true)
} else {
return fmt.Errorf("please specify either application_id or display_name")
}
if err != nil {
return err
}
if len(spList) == 0 {
if response.ApplicationID != "" {
return fmt.Errorf("cannot find SP with ID %s", response.ApplicationID)
} else {
return fmt.Errorf("cannot find SP with name %s", response.DisplayName)
}
} else if len(spList) > 1 {
return fmt.Errorf("there are more than 1 service principal with name %s", response.DisplayName)
}
sp := spList[0]
response.DisplayName = sp.DisplayName
response.ApplicationID = sp.ApplicationID
response.Home = fmt.Sprintf("/Users/%s", sp.ApplicationID)
response.Repos = fmt.Sprintf("/Repos/%s", sp.ApplicationID)
response.AclPrincipalID = fmt.Sprintf("servicePrincipals/%s", sp.ApplicationID)
response.ExternalID = sp.ExternalID
response.Active = sp.Active
response.SpID = sp.ID
response.ID = sp.ID
return nil
})
}