This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
/
resource_three_field_app.go
150 lines (128 loc) 路 4.46 KB
/
resource_three_field_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package okta
import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/okta/okta-sdk-golang/okta"
"github.com/okta/okta-sdk-golang/okta/query"
)
func resourceThreeFieldApp() *schema.Resource {
return &schema.Resource{
Create: resourceThreeFieldAppCreate,
Read: resourceThreeFieldAppRead,
Update: resourceThreeFieldAppUpdate,
Delete: resourceThreeFieldAppDelete,
Exists: resourceAppExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
// For those familiar with Terraform schemas be sure to check the base application schema and/or
// the examples in the documentation
Schema: buildSwaAppSchema(map[string]*schema.Schema{
"button_selector": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Login button field CSS selector",
},
"password_selector": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Login password field CSS selector",
},
"username_selector": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Login username field CSS selector",
},
"extra_field_selector": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Extra field CSS selector",
},
"extra_field_value": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Value for extra form field",
},
"url": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Login URL",
ValidateFunc: validateIsURL,
},
"url_regex": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "A regex that further restricts URL to the specified regex",
},
}),
}
}
func resourceThreeFieldAppCreate(d *schema.ResourceData, m interface{}) error {
client := getOktaClientFromMetadata(m)
app := buildThreeFieldApplication(d, m)
activate := d.Get("status").(string) == "ACTIVE"
params := &query.Params{Activate: &activate}
_, _, err := client.Application.CreateApplication(app, params)
if err != nil {
return err
}
d.SetId(app.Id)
return resourceThreeFieldAppRead(d, m)
}
func resourceThreeFieldAppRead(d *schema.ResourceData, m interface{}) error {
app := okta.NewSwaThreeFieldApplication()
err := fetchApp(d, m, app)
if err != nil {
return err
}
d.Set("button_selector", app.Settings.App.ButtonSelector)
d.Set("password_selector", app.Settings.App.PasswordSelector)
d.Set("username_selector", app.Settings.App.UserNameSelector)
d.Set("extra_field_selector", app.Settings.App.ExtraFieldSelector)
d.Set("extra_field_value", app.Settings.App.ExtraFieldValue)
d.Set("url", app.Settings.App.TargetURL)
d.Set("url_regex", app.Settings.App.LoginUrlRegex)
d.Set("user_name_template", app.Credentials.UserNameTemplate.Template)
d.Set("user_name_template_type", app.Credentials.UserNameTemplate.Type)
appRead(d, app.Name, app.Status, app.SignOnMode, app.Label, app.Accessibility, app.Visibility)
return nil
}
func resourceThreeFieldAppUpdate(d *schema.ResourceData, m interface{}) error {
client := getOktaClientFromMetadata(m)
app := buildThreeFieldApplication(d, m)
_, _, err := client.Application.UpdateApplication(d.Id(), app)
if err != nil {
return err
}
desiredStatus := d.Get("status").(string)
err = setAppStatus(d, client, app.Status, desiredStatus)
if err != nil {
return err
}
return resourceThreeFieldAppRead(d, m)
}
func resourceThreeFieldAppDelete(d *schema.ResourceData, m interface{}) error {
client := getOktaClientFromMetadata(m)
_, err := client.Application.DeactivateApplication(d.Id())
if err != nil {
return err
}
_, err = client.Application.DeleteApplication(d.Id())
return err
}
func buildThreeFieldApplication(d *schema.ResourceData, m interface{}) *okta.SwaThreeFieldApplication {
app := okta.NewSwaThreeFieldApplication()
app.Label = d.Get("label").(string)
app.Settings = &okta.SwaThreeFieldApplicationSettings{
App: &okta.SwaThreeFieldApplicationSettingsApplication{
TargetURL: d.Get("url").(string),
ButtonSelector: d.Get("button_selector").(string),
UserNameSelector: d.Get("username_selector").(string),
PasswordSelector: d.Get("password_selector").(string),
ExtraFieldSelector: d.Get("extra_field_selector").(string),
ExtraFieldValue: d.Get("extra_field_value").(string),
LoginUrlRegex: d.Get("url_regex").(string),
},
}
app.Visibility = buildVisibility(d)
return app
}