/
resource_okta_app_bookmark.go
145 lines (136 loc) · 4.93 KB
/
resource_okta_app_bookmark.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
package okta
import (
"context"
"time"
"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 resourceAppBookmark() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAppBookmarkCreate,
ReadContext: resourceAppBookmarkRead,
UpdateContext: resourceAppBookmarkUpdate,
DeleteContext: resourceAppBookmarkDelete,
Importer: &schema.ResourceImporter{
StateContext: appImporter,
},
// For those familiar with Terraform schemas be sure to check the base application schema and/or
// the examples in the documentation
Description: `This resource allows you to create and configure a Bookmark Application.
-> During an apply if there is change in status the app will first be
activated or deactivated in accordance with the status change. Then, all
other arguments that changed will be applied.`,
Schema: buildAppSchemaWithVisibility(map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Required: true,
Description: "The URL of the bookmark.",
},
"request_integration": {
Type: schema.TypeBool,
Default: false,
Optional: true,
Description: "Would you like Okta to add an integration for this app?",
},
"authentication_policy": {
Type: schema.TypeString,
Optional: true,
Description: `The ID of the associated app_signon_policy. If this property is removed from the application the default sign-on-policy will be associated with this application.`,
},
}),
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(1 * time.Hour),
Read: schema.DefaultTimeout(1 * time.Hour),
Update: schema.DefaultTimeout(1 * time.Hour),
},
}
}
func resourceAppBookmarkCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := getOktaClientFromMetadata(m)
app := buildAppBookmark(d)
activate := d.Get("status").(string) == statusActive
params := &query.Params{Activate: &activate}
_, _, err := client.Application.CreateApplication(ctx, app, params)
if err != nil {
return diag.Errorf("failed to create bookmark application: %v", err)
}
d.SetId(app.Id)
err = handleAppLogo(ctx, d, m, app.Id, app.Links)
if err != nil {
return diag.Errorf("failed to upload logo for bookmark application: %v", err)
}
err = createOrUpdateAuthenticationPolicy(ctx, d, m, app.Id)
if err != nil {
return diag.Errorf("failed to set authentication policy for bookmark application: %v", err)
}
return resourceAppBookmarkRead(ctx, d, m)
}
func resourceAppBookmarkRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
app := sdk.NewBookmarkApplication()
err := fetchApp(ctx, d, m, app)
if err != nil {
return diag.Errorf("failed to get bookmark application: %v", err)
}
if app.Id == "" {
d.SetId("")
return nil
}
setAuthenticationPolicy(d, app.Links)
_ = d.Set("url", app.Settings.App.Url)
_ = d.Set("request_integration", app.Settings.App.RequestIntegration)
appRead(d, app.Name, app.Status, app.SignOnMode, app.Label, app.Accessibility, app.Visibility, app.Settings.Notes)
_ = d.Set("logo_url", linksValue(app.Links, "logo", "href"))
return nil
}
func resourceAppBookmarkUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
additionalChanges, err := appUpdateStatus(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
if !additionalChanges {
return nil
}
client := getOktaClientFromMetadata(m)
app := buildAppBookmark(d)
_, _, err = client.Application.UpdateApplication(ctx, d.Id(), app)
if err != nil {
return diag.Errorf("failed to update bookmark application: %v", err)
}
if d.HasChange("logo") {
err = handleAppLogo(ctx, d, m, app.Id, app.Links)
if err != nil {
o, _ := d.GetChange("logo")
_ = d.Set("logo", o)
return diag.Errorf("failed to upload logo for bookmark application: %v", err)
}
}
err = createOrUpdateAuthenticationPolicy(ctx, d, m, app.Id)
if err != nil {
return diag.Errorf("failed to set authentication policy for bookmark application: %v", err)
}
return resourceAppBookmarkRead(ctx, d, m)
}
func resourceAppBookmarkDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
err := deleteApplication(ctx, d, m)
if err != nil {
return diag.Errorf("failed to delete bookmark application: %v", err)
}
return nil
}
func buildAppBookmark(d *schema.ResourceData) *sdk.BookmarkApplication {
app := sdk.NewBookmarkApplication()
integration := d.Get("request_integration").(bool)
app.Label = d.Get("label").(string)
app.Settings = &sdk.BookmarkApplicationSettings{
App: &sdk.BookmarkApplicationSettingsApplication{
RequestIntegration: &integration,
Url: d.Get("url").(string),
},
Notes: buildAppNotes(d),
}
app.Visibility = buildAppVisibility(d)
app.Accessibility = buildAppAccessibility(d)
return app
}