/
resource_custom_domain_verify.go
260 lines (212 loc) · 8.44 KB
/
resource_custom_domain_verify.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package base
import (
"context"
"fmt"
"net/http"
"regexp"
"time"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/patrickcping/pingone-go-sdk-v2/management"
"github.com/patrickcping/pingone-go-sdk-v2/pingone/model"
"github.com/pingidentity/terraform-provider-pingone/internal/framework"
"github.com/pingidentity/terraform-provider-pingone/internal/sdk"
)
// Types
type CustomDomainVerifyResource serviceClientType
type CustomDomainVerifyResourceModel struct {
Id types.String `tfsdk:"id"`
EnvironmentId types.String `tfsdk:"environment_id"`
CustomDomainId types.String `tfsdk:"custom_domain_id"`
DomainName types.String `tfsdk:"domain_name"`
Status types.String `tfsdk:"status"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
}
// Framework interfaces
var (
_ resource.Resource = &CustomDomainVerifyResource{}
_ resource.ResourceWithConfigure = &CustomDomainVerifyResource{}
)
// New Object
func NewCustomDomainVerifyResource() resource.Resource {
return &CustomDomainVerifyResource{}
}
// Metadata
func (r *CustomDomainVerifyResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_custom_domain_verify"
}
// Schema
func (r *CustomDomainVerifyResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
statusDescription := framework.SchemaAttributeDescriptionFromMarkdown(
"A string that specifies the status of the custom domain.",
).AllowedValuesEnum(management.AllowedEnumCustomDomainStatusEnumValues)
const attrMinLength = 2
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
Description: framework.SchemaDescriptionFromMarkdown("Resource to create and manage PingOne Custom Domain verification.").Description,
Attributes: map[string]schema.Attribute{
"id": framework.Attr_ID(),
"environment_id": framework.Attr_LinkID(
framework.SchemaAttributeDescriptionFromMarkdown("The ID of the environment to verify the custom domain in."),
),
"custom_domain_id": framework.Attr_LinkID(
framework.SchemaAttributeDescriptionFromMarkdown("The ID of the custom domain to verify."),
),
"domain_name": schema.StringAttribute{
Description: framework.SchemaAttributeDescriptionFromMarkdown("A string that specifies the domain name in use.").Description,
Computed: true,
},
"status": schema.StringAttribute{
MarkdownDescription: statusDescription.MarkdownDescription,
Description: statusDescription.Description,
Computed: true,
},
},
Blocks: map[string]schema.Block{
"timeouts": timeouts.Block(ctx, timeouts.Opts{
Create: true,
}),
},
}
}
func (r *CustomDomainVerifyResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.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 *CustomDomainVerifyResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan, state CustomDomainVerifyResourceModel
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 plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
timeoutValue := 60
// Run the API call
var response *management.CustomDomain
resp.Diagnostics.Append(framework.ParseResponseWithCustomTimeout(
ctx,
func() (any, *http.Response, error) {
fO, fR, fErr := r.Client.ManagementAPIClient.CustomDomainsApi.UpdateDomain(ctx, plan.EnvironmentId.ValueString(), plan.CustomDomainId.ValueString()).ContentType(management.ENUMCUSTOMDOMAINPOSTHEADER_DOMAIN_NAME_VERIFYJSON).Execute()
return framework.CheckEnvironmentExistsOnPermissionsError(ctx, r.Client.ManagementAPIClient, plan.EnvironmentId.ValueString(), fO, fR, fErr)
},
"UpdateDomain",
func(error model.P1Error) diag.Diagnostics {
var diags diag.Diagnostics
// Cannot validate against the authoritative name service
if details, ok := error.GetDetailsOk(); ok && details != nil && len(details) > 0 {
m, _ := regexp.MatchString("^Error response from authoritative name servers: NXDOMAIN", details[0].GetMessage())
if m {
diags.AddError(
fmt.Sprintf("Cannot verify the domain - %s", details[0].GetMessage()),
"Please check the domain authority exists or is reachable.",
)
return diags
}
m, _ = regexp.MatchString("^No CNAME records found", details[0].GetMessage())
if m {
diags.AddError(
fmt.Sprintf("Cannot verify the domain - %s", details[0].GetMessage()),
"Please check the domain authority has the correct CNAME value set (hint: if using the \"pingone_custom_domain\" resource, the CNAME value to use is returned in the \"canonical_name\" attribute.)",
)
return diags
}
}
return nil
},
sdk.DefaultCreateReadRetryable,
&response,
time.Duration(timeoutValue)*time.Minute, // 60 mins
)...)
if resp.Diagnostics.HasError() {
return
}
// Create the state to save
state = plan
// Save updated data into Terraform state
resp.Diagnostics.Append(state.toState(response)...)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
func (r *CustomDomainVerifyResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data *CustomDomainVerifyResourceModel
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.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// Run the API call
var response *management.CustomDomain
resp.Diagnostics.Append(framework.ParseResponse(
ctx,
func() (any, *http.Response, error) {
fO, fR, fErr := r.Client.ManagementAPIClient.CustomDomainsApi.ReadOneDomain(ctx, data.EnvironmentId.ValueString(), data.Id.ValueString()).Execute()
return framework.CheckEnvironmentExistsOnPermissionsError(ctx, r.Client.ManagementAPIClient, data.EnvironmentId.ValueString(), fO, fR, fErr)
},
"ReadOneDomain",
framework.CustomErrorResourceNotFoundWarning,
sdk.DefaultCreateReadRetryable,
&response,
)...)
if resp.Diagnostics.HasError() {
return
}
// Remove from state if resource is not found
if response == nil {
resp.State.RemoveResource(ctx)
return
}
// Save updated data into Terraform state
resp.Diagnostics.Append(data.toState(response)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *CustomDomainVerifyResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
}
func (r *CustomDomainVerifyResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
}
func (p *CustomDomainVerifyResourceModel) toState(apiObject *management.CustomDomain) diag.Diagnostics {
var diags diag.Diagnostics
if apiObject == 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(apiObject.GetIdOk())
p.EnvironmentId = framework.StringToTF(*apiObject.GetEnvironment().Id)
p.DomainName = framework.StringOkToTF(apiObject.GetDomainNameOk())
p.Status = framework.EnumOkToTF(apiObject.GetStatusOk())
return diags
}