Skip to content

Commit 7133976

Browse files
authored
feat: update okta-org-captcha resource (#2885)
1 parent 5a1e9ed commit 7133976

22 files changed

Lines changed: 2464 additions & 121 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resource "okta_captcha" "test" {
2+
name = "testAcc_replace_with_uuid"
3+
type = "HCAPTCHA"
4+
site_key = "random_key"
5+
secret_key = "random_secret_key"
6+
}
7+
8+
data "okta_captcha" "test" {
9+
id = okta_captcha.test.id
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
resource "okta_captcha" "test" {
2+
name = "testAcc_replace_with_uuid"
3+
type = "HCAPTCHA"
4+
site_key = "random_key"
5+
secret_key = "random_secret_key"
6+
}
7+
8+
resource "okta_org_captcha" "test" {
9+
captcha_id = okta_captcha.test.id
10+
enabled_pages = ["SIGN_IN"]
11+
}
12+
13+
data "okta_org_captcha" "test" {
14+
depends_on = [okta_org_captcha.test]
15+
}

examples/resources/okta_captcha/basic.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ resource "okta_captcha" "test" {
22
name = "testAcc_replace_with_uuid"
33
type = "HCAPTCHA"
44
site_key = "random_key"
5-
secret_key = "random_key"
5+
secret_key = "random_secret_key"
66
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
resource "okta_captcha" "test_captcha" {
2+
name = "testAcc_replace_with_uuid"
3+
type = "HCAPTCHA"
4+
site_key = "random_key"
5+
secret_key = "random_secret_key"
6+
}
7+
8+
resource "okta_org_captcha" "test" {
9+
captcha_id = okta_captcha.test_captcha.id
10+
enabled_pages = ["SIGN_IN"]
11+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
resource "okta_org_captcha" "test" {
2+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
resource "okta_captcha" "test_captcha" {
2+
name = "testAcc_replace_with_uuid"
3+
type = "HCAPTCHA"
4+
site_key = "random_key"
5+
secret_key = "random_secret_key"
6+
}
7+
8+
resource "okta_org_captcha" "test" {
9+
captcha_id = okta_captcha.test_captcha.id
10+
enabled_pages = ["SSO", "SSPR", "SIGN_IN"]
11+
}

okta/resources/resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const (
5353
OktaIDaaSBrand = "okta_brand"
5454
OktaIDaaSBrands = "okta_brands"
5555
OktaIDaaSCaptcha = "okta_captcha"
56+
OktaIDaaSCaptchaOrgWide = "okta_org_captcha"
5657
OktaIDaaSCaptchaOrgWideSettings = "okta_captcha_org_wide_settings"
5758
OktaIDaaSDefaultPolicy = "okta_default_policy"
5859
OktaIDaaSDomain = "okta_domain"
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright 2025 - Present Okta, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package idaas
16+
17+
import (
18+
"context"
19+
"net/http"
20+
21+
"github.com/hashicorp/terraform-plugin-framework/datasource"
22+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
23+
"github.com/hashicorp/terraform-plugin-framework/types"
24+
25+
"github.com/okta/terraform-provider-okta/okta/config"
26+
)
27+
28+
var (
29+
_ datasource.DataSource = &captchaDataSource{}
30+
_ datasource.DataSourceWithConfigure = &captchaDataSource{}
31+
)
32+
33+
// CaptchaDataSource defines the data source implementation.
34+
type captchaDataSource struct {
35+
Config *config.Config
36+
}
37+
38+
// CaptchaDataSourceModel describes the data source data model.
39+
type captchaDataSourceModel struct {
40+
ID types.String `tfsdk:"id"`
41+
Name types.String `tfsdk:"name"`
42+
SiteKey types.String `tfsdk:"site_key"`
43+
Type types.String `tfsdk:"type"`
44+
}
45+
46+
func newCaptchaDataSource() datasource.DataSource {
47+
return &captchaDataSource{}
48+
}
49+
50+
func (d *captchaDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
51+
resp.TypeName = req.ProviderTypeName + "_captcha"
52+
}
53+
54+
func (d *captchaDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
55+
d.Config = dataSourceConfiguration(req, resp)
56+
}
57+
58+
func (d *captchaDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
59+
resp.Schema = schema.Schema{
60+
MarkdownDescription: "Retrieves the properties of a specified CAPTCHA instance",
61+
Attributes: map[string]schema.Attribute{
62+
"id": schema.StringAttribute{
63+
MarkdownDescription: "Unique identifier of the captcha.",
64+
Required: true,
65+
},
66+
"name": schema.StringAttribute{
67+
MarkdownDescription: "The name of the CAPTCHA instance",
68+
Computed: true,
69+
},
70+
"site_key": schema.StringAttribute{
71+
MarkdownDescription: "The site key issued from the CAPTCHA provider to render a CAPTCHA on a page",
72+
Computed: true,
73+
},
74+
"type": schema.StringAttribute{
75+
MarkdownDescription: "The type of CAPTCHA provider",
76+
Computed: true,
77+
},
78+
},
79+
}
80+
}
81+
82+
func (d *captchaDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
83+
var state captchaDataSourceModel
84+
85+
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
86+
if resp.Diagnostics.HasError() {
87+
return
88+
}
89+
90+
client := d.Config.OktaIDaaSClient.OktaSDKClientV6()
91+
id := state.ID.ValueString()
92+
result, httpResp, err := client.CAPTCHAAPI.GetCaptchaInstance(ctx, id).Execute()
93+
if err != nil {
94+
if httpResp != nil && httpResp.StatusCode == http.StatusNotFound {
95+
resp.Diagnostics.AddError("Not Found", "captcha with the given ID was not found.")
96+
return
97+
}
98+
resp.Diagnostics.AddError("Error reading captcha", err.Error())
99+
return
100+
}
101+
state.ID = types.StringValue(string(result.GetId()))
102+
state.Name = types.StringValue(string(result.GetName()))
103+
state.SiteKey = types.StringValue(string(result.GetSiteKey()))
104+
state.Type = types.StringValue(string(result.GetType()))
105+
106+
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
107+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package idaas_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
"github.com/okta/terraform-provider-okta/okta/acctest"
8+
)
9+
10+
func TestAccDataSourceOktaCaptcha_read(t *testing.T) {
11+
mgr := newFixtureManager("data-sources", "okta_captcha", t.Name())
12+
config := mgr.GetFixtures("datasource.tf", t)
13+
resourceName := "data.okta_captcha.test"
14+
15+
acctest.OktaResourceTest(t, resource.TestCase{
16+
PreCheck: acctest.AccPreCheck(t),
17+
ErrorCheck: testAccErrorChecks(t),
18+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactoriesForTestAcc(t),
19+
Steps: []resource.TestStep{
20+
{
21+
Config: config,
22+
Check: resource.ComposeTestCheckFunc(
23+
resource.TestCheckResourceAttrSet(resourceName, "id"),
24+
resource.TestCheckResourceAttrSet(resourceName, "name"),
25+
resource.TestCheckResourceAttrSet(resourceName, "site_key"),
26+
resource.TestCheckResourceAttrSet(resourceName, "type"),
27+
),
28+
},
29+
},
30+
})
31+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright 2025 - Present Okta, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package idaas
16+
17+
import (
18+
"context"
19+
"net/http"
20+
21+
"github.com/hashicorp/terraform-plugin-framework/datasource"
22+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
23+
"github.com/hashicorp/terraform-plugin-framework/types"
24+
25+
"github.com/okta/terraform-provider-okta/okta/config"
26+
)
27+
28+
var (
29+
_ datasource.DataSource = &orgCaptchaDataSource{}
30+
_ datasource.DataSourceWithConfigure = &orgCaptchaDataSource{}
31+
)
32+
33+
// OrgCaptchaDataSource defines the data source implementation.
34+
type orgCaptchaDataSource struct {
35+
Config *config.Config
36+
}
37+
38+
// OrgCaptchaDataSourceModel describes the data source data model.
39+
type orgCaptchaDataSourceModel struct {
40+
ID types.String `tfsdk:"id"`
41+
CaptchaId types.String `tfsdk:"captcha_id"`
42+
EnabledPages types.List `tfsdk:"enabled_pages"`
43+
}
44+
45+
func newOrgCaptchaDataSource() datasource.DataSource {
46+
return &orgCaptchaDataSource{}
47+
}
48+
49+
func (d *orgCaptchaDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
50+
resp.TypeName = req.ProviderTypeName + "_org_captcha"
51+
}
52+
53+
func (d *orgCaptchaDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
54+
d.Config = dataSourceConfiguration(req, resp)
55+
}
56+
57+
func (d *orgCaptchaDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
58+
resp.Schema = schema.Schema{
59+
MarkdownDescription: "Retrieves the CAPTCHA settings object for your organization > **Note**: If the current organization hasn't configured CAPTCHA Settings, the request returns an empty object.",
60+
Attributes: map[string]schema.Attribute{
61+
"id": schema.StringAttribute{
62+
MarkdownDescription: "Unique identifier of the org_captcha.",
63+
Optional: true,
64+
Computed: true,
65+
},
66+
"captcha_id": schema.StringAttribute{
67+
MarkdownDescription: "The unique key of the associated CAPTCHA instance",
68+
Computed: true,
69+
},
70+
"enabled_pages": schema.ListAttribute{
71+
MarkdownDescription: "An array of pages that have CAPTCHA enabled",
72+
ElementType: types.StringType,
73+
Computed: true,
74+
},
75+
},
76+
}
77+
}
78+
79+
func (d *orgCaptchaDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
80+
var state orgCaptchaDataSourceModel
81+
82+
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
83+
if resp.Diagnostics.HasError() {
84+
return
85+
}
86+
87+
client := d.Config.OktaIDaaSClient.OktaSDKClientV6()
88+
result, httpResp, err := client.CAPTCHAAPI.GetOrgCaptchaSettings(ctx).Execute()
89+
if err != nil {
90+
if httpResp != nil && httpResp.StatusCode == http.StatusNotFound {
91+
resp.Diagnostics.AddError("Not Found", "org_captcha with the given ID was not found.")
92+
return
93+
}
94+
resp.Diagnostics.AddError("Error reading org_captcha", err.Error())
95+
return
96+
}
97+
state.ID = types.StringValue("org_captcha")
98+
_ = result // IDExpr may not reference result; prevent "declared and not used"
99+
state.CaptchaId = types.StringValue(string(result.GetCaptchaId()))
100+
101+
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
102+
}

0 commit comments

Comments
 (0)