/
data_source_okta_behavior.go
84 lines (80 loc) · 2.38 KB
/
data_source_okta_behavior.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package okta
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/okta/terraform-provider-okta/sdk"
"github.com/okta/terraform-provider-okta/sdk/query"
)
func dataSourceBehavior() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBehaviorRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"name"},
Description: "Behavior ID.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Behavior name.",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "Behavior status.",
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "Behavior type.",
},
"settings": {
Type: schema.TypeMap,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
Description: "Map of behavior settings.",
},
},
Description: "Get a behavior by name or ID.",
}
}
func dataSourceBehaviorRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var behavior *sdk.Behavior
behaviorID, ok := d.GetOk("id")
if ok {
respBehavior, _, err := getAPISupplementFromMetadata(m).GetBehavior(ctx, behaviorID.(string))
if err != nil {
return diag.Errorf("failed get behavior by ID: %v", err)
}
behavior = respBehavior
} else {
name := d.Get("name").(string)
searchParams := &query.Params{Q: name, Limit: 1}
logger(m).Info("looking for behavior", "query", searchParams.String())
behaviors, _, err := getAPISupplementFromMetadata(m).ListBehaviors(ctx, searchParams)
switch {
case err != nil:
return diag.Errorf("failed to query for behaviors: %v", err)
case len(behaviors) < 1:
return diag.Errorf("behavior with name '%s' does not exist", name)
case behaviors[0].Name != name:
logger(m).Warn("behavior with exact name match was not found: using partial match which contains name as a substring", "name", behaviors[0].Name)
}
behavior = behaviors[0]
}
d.SetId(behavior.ID)
_ = d.Set("type", behavior.Type)
_ = d.Set("status", behavior.Status)
settings := make(map[string]string)
for k, v := range behavior.Settings {
settings[k] = fmt.Sprint(v)
}
_ = d.Set("settings", settings)
return nil
}