-
Notifications
You must be signed in to change notification settings - Fork 669
/
resource_ibm_appid_action_url.go
145 lines (115 loc) · 3.97 KB
/
resource_ibm_appid_action_url.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 appid
import (
"context"
"fmt"
"log"
"strings"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
appid "github.com/IBM/appid-management-go-sdk/appidmanagementv4"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func ResourceIBMAppIDActionURL() *schema.Resource {
return &schema.Resource{
Description: "The custom url to redirect to when Cloud Directory action is executed.",
CreateContext: resourceIBMAppIDActionURLCreate,
ReadContext: resourceIBMAppIDActionURLRead,
DeleteContext: resourceIBMAppIDActionURLDelete,
UpdateContext: resourceIBMAppIDActionURLUpdate,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"tenant_id": {
Description: "The AppID instance GUID",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"action": {
Description: "The type of the action: `on_user_verified` - the URL of your custom user verified page, `on_reset_password` - the URL of your custom reset password page",
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{"on_user_verified", "on_reset_password"}, false),
Required: true,
ForceNew: true,
},
"url": {
Description: "The action URL",
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceIBMAppIDActionURLRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
appIDClient, err := meta.(conns.ClientSession).AppIDAPI()
if err != nil {
return diag.FromErr(err)
}
id := d.Id()
idParts := strings.Split(id, "/")
if len(idParts) < 2 {
return diag.Errorf("Incorrect ID %s: AppID action URL ID should be a combination of tenantID/action", id)
}
tenantID := idParts[0]
action := idParts[1]
cfg, resp, err := appIDClient.GetCloudDirectoryActionURLWithContext(ctx, &appid.GetCloudDirectoryActionURLOptions{
TenantID: &tenantID,
Action: &action,
})
if err != nil {
if resp != nil && resp.StatusCode == 404 {
log.Printf("[WARN] AppID instance '%s' is not found, removing Action URL from state", tenantID)
d.SetId("")
return nil
}
return diag.Errorf("Error getting AppID actionURL: %s\n%s", err, resp)
}
if cfg.ActionURL != nil {
d.Set("url", *cfg.ActionURL)
}
d.Set("tenant_id", tenantID)
d.Set("action", action)
return nil
}
func resourceIBMAppIDActionURLCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
appIDClient, err := meta.(conns.ClientSession).AppIDAPI()
if err != nil {
return diag.FromErr(err)
}
tenantID := d.Get("tenant_id").(string)
action := d.Get("action").(string)
actionURL := d.Get("url").(string)
input := &appid.SetCloudDirectoryActionOptions{
TenantID: &tenantID,
Action: &action,
ActionURL: &actionURL,
}
_, resp, err := appIDClient.SetCloudDirectoryActionWithContext(ctx, input)
if err != nil {
return diag.Errorf("Error setting AppID Cloud Directory action URL: %s\n%s", err, resp)
}
d.SetId(fmt.Sprintf("%s/%s", tenantID, action))
return resourceIBMAppIDActionURLRead(ctx, d, meta)
}
func resourceIBMAppIDActionURLDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
appIDClient, err := meta.(conns.ClientSession).AppIDAPI()
if err != nil {
return diag.FromErr(err)
}
tenantID := d.Get("tenant_id").(string)
action := d.Get("action").(string)
resp, err := appIDClient.DeleteActionURLWithContext(ctx, &appid.DeleteActionURLOptions{
TenantID: &tenantID,
Action: &action,
})
if err != nil {
return diag.Errorf("Error deleting AppID Cloud Directory action URL: %s\n%s", err, resp)
}
d.SetId("")
return nil
}
func resourceIBMAppIDActionURLUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return resourceIBMAppIDActionURLCreate(ctx, d, m)
}