Skip to content

Commit

Permalink
feat(tem): refacto ResourceDomainValidationCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
jremy42 committed Apr 10, 2024
1 parent fd2400d commit a6e5904
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions internal/services/tem/domain_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,30 @@ func ResourceDomainValidationCreate(ctx context.Context, d *schema.ResourceData,
return diag.FromErr(err)
}
d.SetId(d.Get("domain_id").(string))
diagnostics := validateDomain(ctx, d, err, api, region)
if diagnostics != nil {
return diagnostics
domain, err := api.GetDomain(&tem.GetDomainRequest{
Region: region,
DomainID: extractAfterSlash(d.Get("domain_id").(string)),
}, scw.WithContext(ctx))
if err != nil {
if httperrors.Is404(err) {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
duration := d.Get("timeout").(int)
timeout := time.Duration(duration) * time.Second
_ = retry.RetryContext(ctx, timeout, func() *retry.RetryError {
domainCheck, _ := api.CheckDomain(&tem.CheckDomainRequest{
Region: region,
DomainID: domain.ID,
})
if domainCheck == nil || domainCheck.Status == "pending" || domainCheck.Status == "unchecked" {
return retry.RetryableError(errors.New("retry"))
}
return nil
})

return ResourceDomainValidationRead(ctx, d, meta)
}

Expand Down Expand Up @@ -105,30 +125,3 @@ func extractAfterSlash(s string) string {
}
return s[lastIndex+1:]
}

func validateDomain(ctx context.Context, d *schema.ResourceData, err error, api *tem.API, region scw.Region) diag.Diagnostics {
domain, err := api.GetDomain(&tem.GetDomainRequest{
Region: region,
DomainID: extractAfterSlash(d.Get("domain_id").(string)),
}, scw.WithContext(ctx))
if err != nil {
if httperrors.Is404(err) {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
duration := d.Get("timeout").(int)
timeout := time.Duration(duration) * time.Second
_ = retry.RetryContext(ctx, timeout, func() *retry.RetryError {
domainCheck, _ := api.CheckDomain(&tem.CheckDomainRequest{
Region: region,
DomainID: domain.ID,
})
if domainCheck == nil || domainCheck.Status == "pending" || domainCheck.Status == "unchecked" {
return retry.RetryableError(errors.New("retry"))
}
return nil
})
return nil
}

0 comments on commit a6e5904

Please sign in to comment.