This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
/
data_source_app.go
82 lines (74 loc) 路 2.11 KB
/
data_source_app.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
package okta
import (
"errors"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceApp() *schema.Resource {
return &schema.Resource{
Read: dataSourceAppRead,
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"label", "label_prefix"},
},
"label": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"id", "label_prefix"},
},
"label_prefix": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"id", "label"},
},
"active_only": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Search only ACTIVE applications.",
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceAppRead(d *schema.ResourceData, m interface{}) error {
id := d.Get("id").(string)
label := d.Get("label").(string)
labelPrefix := d.Get("label_prefix").(string)
filters := &appFilters{ID: id, Label: label, LabelPrefix: labelPrefix}
if d.Get("active_only").(bool) {
filters.ApiFilter = `status eq "ACTIVE"`
}
if id == "" && label == "" && labelPrefix == "" {
return errors.New("you must provide either an label_prefix, id, or label to search with")
}
appList, err := listApps(m.(*Config), filters)
if err != nil {
return err
}
if len(appList) < 1 {
return fmt.Errorf(`No application found with provided filter. id: "%s", label: "%s", label_prefix: "%s"`, id, label, labelPrefix)
} else if len(appList) > 1 {
fmt.Println("Found multiple applications with the criteria supplied, using the first one, sorted by creation date.")
}
app := appList[0]
d.SetId(app.ID)
d.Set("label", app.Label)
d.Set("description", app.Description)
d.Set("name", app.Name)
d.Set("status", app.Status)
return nil
}