/
data_source_trusted_email_domain.go
240 lines (193 loc) · 7.62 KB
/
data_source_trusted_email_domain.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package base
import (
"context"
"fmt"
"net/http"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/patrickcping/pingone-go-sdk-v2/management"
"github.com/pingidentity/terraform-provider-pingone/internal/framework"
"github.com/pingidentity/terraform-provider-pingone/internal/sdk"
"github.com/pingidentity/terraform-provider-pingone/internal/verify"
)
// Types
type TrustedEmailDomainDataSource serviceClientType
type TrustedEmailDomainDataSourceModel struct {
DomainName types.String `tfsdk:"domain_name"`
EmailDomainId types.String `tfsdk:"trusted_email_domain_id"`
EnvironmentId types.String `tfsdk:"environment_id"`
Id types.String `tfsdk:"id"`
}
// Framework interfaces
var (
_ datasource.DataSource = &TrustedEmailDomainDataSource{}
)
// New Object
func NewTrustedEmailDomainDataSource() datasource.DataSource {
return &TrustedEmailDomainDataSource{}
}
// Metadata
func (r *TrustedEmailDomainDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_trusted_email_domain"
}
// Schema
func (r *TrustedEmailDomainDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
primaryLookupFieldExactlyOneOf := []string{"trusted_email_domain_id", "domain_name"}
trustedEmailDomainIdDescription := framework.SchemaAttributeDescriptionFromMarkdown(
"The ID of the trusted email domain. Must be a valid PingOne resource ID.",
).ExactlyOneOf(primaryLookupFieldExactlyOneOf)
domainDescription := framework.SchemaAttributeDescriptionFromMarkdown(
"A string that specifies the domain name to use, which must be provided and must be unique within an environment (for example, `demo.bxretail.org`).",
).ExactlyOneOf(primaryLookupFieldExactlyOneOf)
const emailAddressMaxLength = 5
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
Description: "Datasource to retrieve a Trusted Email Domain.",
Attributes: map[string]schema.Attribute{
"id": framework.Attr_ID(),
"environment_id": framework.Attr_LinkID(
framework.SchemaAttributeDescriptionFromMarkdown("The ID of the environment that is configured with the trusted email domain."),
),
"trusted_email_domain_id": schema.StringAttribute{
MarkdownDescription: trustedEmailDomainIdDescription.MarkdownDescription,
Description: trustedEmailDomainIdDescription.Description,
Optional: true,
Validators: []validator.String{
verify.P1ResourceIDValidator(),
stringvalidator.ExactlyOneOf(path.MatchRelative().AtParent().AtName("domain_name")),
},
},
"domain_name": schema.StringAttribute{
MarkdownDescription: domainDescription.MarkdownDescription,
Description: domainDescription.Description,
Optional: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.MatchRelative().AtParent().AtName("trusted_email_domain_id")),
},
},
},
}
}
func (r *TrustedEmailDomainDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
resourceConfig, ok := req.ProviderData.(framework.ResourceType)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected the provider client, got: %T. Please report this issue to the provider maintainers.", req.ProviderData),
)
return
}
r.Client = resourceConfig.Client.API
if r.Client == nil {
resp.Diagnostics.AddError(
"Client not initialised",
"Expected the PingOne client, got nil. Please report this issue to the provider maintainers.",
)
return
}
}
func (r *TrustedEmailDomainDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data *TrustedEmailDomainDataSourceModel
if r.Client.ManagementAPIClient == nil {
resp.Diagnostics.AddError(
"Client not initialized",
"Expected the PingOne client, got nil. Please report this issue to the provider maintainers.")
return
}
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
var emailDomain management.EmailDomain
if !data.DomainName.IsNull() {
// Run the API call
var entityArray *management.EntityArray
resp.Diagnostics.Append(framework.ParseResponse(
ctx,
func() (any, *http.Response, error) {
fO, fR, fErr := r.Client.ManagementAPIClient.TrustedEmailDomainsApi.ReadAllTrustedEmailDomains(ctx, data.EnvironmentId.ValueString()).Execute()
return framework.CheckEnvironmentExistsOnPermissionsError(ctx, r.Client.ManagementAPIClient, data.EnvironmentId.ValueString(), fO, fR, fErr)
},
"ReadAllTrustedEmailDomains",
framework.DefaultCustomError,
sdk.DefaultCreateReadRetryable,
&entityArray,
)...)
if resp.Diagnostics.HasError() {
return
}
if emailDomains, ok := entityArray.Embedded.GetEmailDomainsOk(); ok {
found := false
for _, emailDomainItem := range emailDomains {
if emailDomainItem.GetDomainName() == data.DomainName.ValueString() {
emailDomain = emailDomainItem
found = true
break
}
}
if !found {
resp.Diagnostics.AddError(
"Cannot find trusted email domain from domain_name",
fmt.Sprintf("The trusted email domain %s for environment %s cannot be found", data.DomainName.String(), data.EnvironmentId.String()),
)
return
}
}
} else if !data.EmailDomainId.IsNull() {
// Run the API call
var response *management.EmailDomain
resp.Diagnostics.Append(framework.ParseResponse(
ctx,
func() (any, *http.Response, error) {
fO, fR, fErr := r.Client.ManagementAPIClient.TrustedEmailDomainsApi.ReadOneTrustedEmailDomain(ctx, data.EnvironmentId.ValueString(), data.EmailDomainId.ValueString()).Execute()
return framework.CheckEnvironmentExistsOnPermissionsError(ctx, r.Client.ManagementAPIClient, data.EnvironmentId.ValueString(), fO, fR, fErr)
},
"ReadOneTrustedEmailDomain",
framework.DefaultCustomError,
sdk.DefaultCreateReadRetryable,
&response,
)...)
if resp.Diagnostics.HasError() {
return
}
emailDomain = *response
} else {
resp.Diagnostics.AddError(
"Missing parameter",
"Cannot find the requested trusted email domain. trusted_email_domain_id or domain_name must be set.",
)
return
}
// Save updated data into Terraform state
resp.Diagnostics.Append(data.toState(&emailDomain)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (p *TrustedEmailDomainDataSourceModel) toState(v *management.EmailDomain) diag.Diagnostics {
var diags diag.Diagnostics
if v == nil {
diags.AddError(
"Data object missing",
"Cannot convert the data object to state as the data object is nil. Please report this to the provider maintainers.",
)
return diags
}
p.Id = framework.StringOkToTF(v.GetIdOk())
if e, ok := v.GetEnvironmentOk(); ok {
p.EnvironmentId = framework.StringOkToTF(e.GetIdOk())
} else {
p.EnvironmentId = types.StringNull()
}
p.EmailDomainId = framework.StringOkToTF(v.GetIdOk())
p.DomainName = framework.StringOkToTF(v.GetDomainNameOk())
return diags
}