/
resource_scim_config.go
169 lines (146 loc) · 4.81 KB
/
resource_scim_config.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package resources
import (
"context"
"fmt"
"log"
"github.com/MaterializeInc/terraform-provider-materialize/pkg/frontegg"
"github.com/MaterializeInc/terraform-provider-materialize/pkg/utils"
"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"
)
var resourceSCIM2ConfigurationsSchema = map[string]*schema.Schema{
"source": {
Type: schema.TypeString,
Required: true,
Description: "The source of the SCIM 2.0 configuration. Supported values are `okta`, `azure-ad`, and `other`.",
ForceNew: true,
ValidateFunc: validation.StringInSlice(scim2ConfigSources, true),
},
"connection_name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the SCIM 2.0 connection. It must be unique.",
ForceNew: true,
},
"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
"sync_to_user_management": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether automatic synchronization of data with the IdP is enabled, ensuring that changes in details or status in the IdP are updated accordingly.",
},
"token": {
Type: schema.TypeString,
Computed: true,
Description: "The token of the SCIM 2.0 configuration.",
Sensitive: true,
},
"provisioning_url": {
Type: schema.TypeString,
Computed: true,
Description: "The provisioning URL of the SCIM 2.0 configuration.",
Sensitive: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The creation timestamp of the SCIM 2.0 configuration.",
},
}
func SCIM2Configuration() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSCIM2ConfigurationsCreate,
ReadContext: resourceSCIM2ConfigurationsRead,
DeleteContext: resourceSCIM2ConfigurationsDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: resourceSCIM2ConfigurationsSchema,
Description: "The SCIM 2.0 configurations resource allows you to create, read, and delete the SCIM 2.0 configurations.",
}
}
func resourceSCIM2ConfigurationsCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerMeta, err := utils.GetProviderMeta(meta)
if err != nil {
return diag.FromErr(err)
}
client := providerMeta.Frontegg
config := frontegg.SCIM2Configuration{
Source: d.Get("source").(string),
ConnectionName: d.Get("connection_name").(string),
SyncToUserManagement: true,
}
newConfig, err := frontegg.CreateSCIM2Configuration(ctx, client, config)
if err != nil {
return diag.FromErr(err)
}
if err := d.Set("token", newConfig.Token); err != nil {
return diag.FromErr(err)
}
// Construct and set the Provisioning URL
provisioningURL := fmt.Sprintf("%s/frontegg/directory/resources/scim/v2.0/%s", client.Endpoint, newConfig.ID)
if err := d.Set("provisioning_url", provisioningURL); err != nil {
return diag.FromErr(err)
}
d.SetId(newConfig.ID)
return resourceSCIM2ConfigurationsRead(ctx, d, meta)
}
// Delete an existing SCIM 2.0 configuration
func resourceSCIM2ConfigurationsDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerMeta, err := utils.GetProviderMeta(meta)
if err != nil {
return diag.FromErr(err)
}
client := providerMeta.Frontegg
err = frontegg.DeleteSCIM2Configuration(ctx, client, d.Id())
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
}
func resourceSCIM2ConfigurationsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerMeta, err := utils.GetProviderMeta(meta)
if err != nil {
return diag.FromErr(err)
}
client := providerMeta.Frontegg
configurations, err := frontegg.FetchSCIM2Configurations(ctx, client)
if err != nil {
return diag.FromErr(err)
}
// Find the configuration with the matching ID
var config frontegg.SCIM2Configuration
for _, configuration := range configurations {
if configuration.ID == d.Id() {
config = configuration
break
}
}
if config.ID == "" {
d.SetId("")
log.Printf("[WARN] SCIM 2.0 configuration with ID %s not found", d.Id())
return nil
}
if err := d.Set("source", config.Source); err != nil {
return diag.FromErr(err)
}
if err := d.Set("connection_name", config.ConnectionName); err != nil {
return diag.FromErr(err)
}
if err := d.Set("tenant_id", config.TenantID); err != nil {
return diag.FromErr(err)
}
if err := d.Set("sync_to_user_management", config.SyncToUserManagement); err != nil {
return diag.FromErr(err)
}
if err := d.Set("created_at", config.CreatedAt.String()); err != nil {
return diag.FromErr(err)
}
// Log the response
log.Printf("[DEBUG] SCIM 2.0 configuration: %+v", config)
return nil
}