Skip to content

feat(webhosting): add dns records and name servers #2962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions internal/services/webhosting/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ func newHostingAPIWithRegion(d *schema.ResourceData, m interface{}) (*webhosting
return api, region, nil
}

func newDNSAPIWithRegion(d *schema.ResourceData, m interface{}) (*webhosting.DnsAPI, scw.Region, error) {
api := webhosting.NewDnsAPI(meta.ExtractScwClient(m))

region, err := meta.ExtractRegion(d, m)
if err != nil {
return nil, "", err
}

return api, region, nil
}

// NewAPIWithRegionAndID returns a Hosting API with region and ID extracted from the state.
func NewAPIWithRegionAndID(m interface{}, id string) (*webhosting.HostingAPI, scw.Region, string, error) {
api := webhosting.NewHostingAPI(meta.ExtractScwClient(m))
Expand All @@ -65,3 +76,32 @@ func waitForHosting(ctx context.Context, api *webhosting.HostingAPI, region scw.
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))
}

func flattenDNSRecords(records []*webhosting.DNSRecord) []map[string]interface{} {
result := []map[string]interface{}{}
for _, r := range records {
result = append(result, map[string]interface{}{
"name": r.Name,
"type": r.Type.String(),
"ttl": r.TTL,
"value": r.Value,
"priority": r.Priority,
"status": r.Status.String(),
})
}

return result
}

func flattenNameServers(servers []*webhosting.Nameserver) []map[string]interface{} {
result := []map[string]interface{}{}
for _, s := range servers {
result = append(result, map[string]interface{}{
"hostname": s.Hostname,
"status": s.Status.String(),
"is_default": s.IsDefault,
})
}

return result
}

Large diffs are not rendered by default.

1,458 changes: 1,072 additions & 386 deletions internal/services/webhosting/testdata/data-source-webhosting-basic.cassette.yaml

Large diffs are not rendered by default.

430 changes: 264 additions & 166 deletions internal/services/webhosting/testdata/webhosting-basic.cassette.yaml

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions internal/services/webhosting/webhosting.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,33 @@ func ResourceWebhosting() *schema.Resource {
Computed: true,
Description: "Main hosting cPanel username",
},
"records": {
Type: schema.TypeList,
Computed: true,
Description: "List of DNS records associated with the webhosting.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {Type: schema.TypeString, Computed: true},
"type": {Type: schema.TypeString, Computed: true},
"ttl": {Type: schema.TypeInt, Computed: true},
"value": {Type: schema.TypeString, Computed: true},
"priority": {Type: schema.TypeInt, Computed: true},
"status": {Type: schema.TypeString, Computed: true},
},
},
},
"name_servers": {
Type: schema.TypeList,
Computed: true,
Description: "List of nameservers associated with the webhosting.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"hostname": {Type: schema.TypeString, Computed: true},
"status": {Type: schema.TypeString, Computed: true},
"is_default": {Type: schema.TypeBool, Computed: true},
},
},
},
"region": regional.Schema(),
"project_id": account.ProjectIDSchema(),
"organization_id": func() *schema.Schema {
Expand Down Expand Up @@ -217,6 +244,11 @@ func resourceWebhostingRead(ctx context.Context, d *schema.ResourceData, m inter
return diag.FromErr(err)
}

dnsAPI, _, err := newDNSAPIWithRegion(d, m)
if err != nil {
return diag.FromErr(err)
}

webhostingResponse, err := waitForHosting(ctx, api, region, id, d.Timeout(schema.TimeoutRead))
if err != nil {
if httperrors.Is404(err) {
Expand All @@ -228,6 +260,16 @@ func resourceWebhostingRead(ctx context.Context, d *schema.ResourceData, m inter
return diag.FromErr(err)
}

dnsRecordsResponse, err := dnsAPI.GetDomainDNSRecords(&webhosting.DNSAPIGetDomainDNSRecordsRequest{
Domain: webhostingResponse.Domain,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

_ = d.Set("records", flattenDNSRecords(dnsRecordsResponse.Records))
_ = d.Set("name_servers", flattenNameServers(dnsRecordsResponse.NameServers))

_ = d.Set("tags", webhostingResponse.Tags)
_ = d.Set("offer_id", regional.NewIDString(region, webhostingResponse.Offer.ID))
_ = d.Set("domain", webhostingResponse.Domain)
Expand Down
9 changes: 9 additions & 0 deletions internal/services/webhosting/webhosting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ func TestAccWebhosting_Basic(t *testing.T) {
resource.TestCheckResourceAttrSet("scaleway_webhosting.main", "updated_at"),
resource.TestCheckResourceAttrSet("scaleway_webhosting.main", "created_at"),
acctest.CheckResourceAttrUUID("scaleway_webhosting.main", "id"),
resource.TestCheckResourceAttrSet("scaleway_webhosting.main", "records.0.name"),
resource.TestCheckResourceAttrSet("scaleway_webhosting.main", "records.0.type"),
resource.TestCheckResourceAttrSet("scaleway_webhosting.main", "records.0.ttl"),
resource.TestCheckResourceAttrSet("scaleway_webhosting.main", "records.0.value"),
resource.TestCheckResourceAttrSet("scaleway_webhosting.main", "records.0.priority"),
resource.TestCheckResourceAttrSet("scaleway_webhosting.main", "records.0.status"),
resource.TestCheckResourceAttrSet("scaleway_webhosting.main", "name_servers.0.hostname"),
resource.TestCheckResourceAttrSet("scaleway_webhosting.main", "name_servers.0.status"),
resource.TestCheckResourceAttrSet("scaleway_webhosting.main", "name_servers.0.is_default"),
),
},
},
Expand Down
Loading