|
| 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 | +} |
0 commit comments