Skip to content

Commit 37f7395

Browse files
authored
feat(webhosting): add datasource (#2060)
* feat(webhosting): add datasource * add doc
1 parent 9229b34 commit 37f7395

6 files changed

+1572
-1
lines changed

docs/data-sources/webhosting.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
subcategory: "Web Hosting"
3+
page_title: "Scaleway: scaleway_webhosting"
4+
---
5+
6+
# scaleway_webhosting
7+
8+
Gets information about a webhosting.
9+
10+
## Example Usage
11+
12+
```hcl
13+
# Get info by offer domain
14+
data "scaleway_webhosting" "by_domain" {
15+
domain = "foobar.com"
16+
}
17+
18+
# Get info by id
19+
data "scaleway_webhosting" "by_id" {
20+
webhosting_id = "11111111-1111-1111-1111-111111111111"
21+
}
22+
```
23+
24+
## Argument Reference
25+
26+
* `domain` - (Optional) The hosting domain name. Only one of `domain` and `webhosting_id` should be specified.
27+
* `webhosting_id` - (Optional) The hosting id. Only one of `domain` and `webhosting_id` should be specified.
28+
* `organization_id` - The ID of the organization the hosting is associated with.
29+
* `project_id` - (Optional. Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the hosting is associated with.
30+
* `region` - (Defaults to [provider](../index.md#zone) `region`) The [region](../guides/regions_and_zones.md#zones) in which hosting exists.
31+
32+
## Attributes Reference
33+
34+
Exported attributes are the ones from `scaleway_webhosting` [resource](../resources/webhosting.md)

docs/resources/webhosting.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The following arguments are supported:
2929

3030
- `offer_id` - (Required) The ID of the selected offer for the hosting.
3131
- `email` - (Required) The contact email of the client for the hosting.
32-
- `domain` - (Required) TThe domain name of the hosting.
32+
- `domain` - (Required) The domain name of the hosting.
3333
- `option_ids` - (Optional) The IDs of the selected options for the hosting.
3434
- `tags` - (Optional) The tags associated with the hosting.
3535
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) of the Hosting.

scaleway/data_source_webhosting.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package scaleway
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
webhosting "github.com/scaleway/scaleway-sdk-go/api/webhosting/v1alpha1"
9+
"github.com/scaleway/scaleway-sdk-go/scw"
10+
)
11+
12+
func dataSourceScalewayWebhosting() *schema.Resource {
13+
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayWebhosting().Schema)
14+
15+
addOptionalFieldsToSchema(dsSchema, "domain")
16+
17+
dsSchema["domain"].ConflictsWith = []string{"webhosting_id"}
18+
dsSchema["webhosting_id"] = &schema.Schema{
19+
Type: schema.TypeString,
20+
Optional: true,
21+
Description: "The ID of the Webhosting",
22+
ValidateFunc: validationUUIDorUUIDWithLocality(),
23+
ConflictsWith: []string{"domain"},
24+
}
25+
dsSchema["organization_id"] = organizationIDOptionalSchema()
26+
dsSchema["project_id"] = &schema.Schema{
27+
Type: schema.TypeString,
28+
Optional: true,
29+
Description: "The project ID the resource is associated to",
30+
ValidateFunc: validationUUID(),
31+
}
32+
33+
return &schema.Resource{
34+
Schema: dsSchema,
35+
ReadContext: dataSourceScalewayWebhostingRead,
36+
}
37+
}
38+
39+
func dataSourceScalewayWebhostingRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
40+
api, region, err := webhostingAPIWithRegion(d, meta)
41+
if err != nil {
42+
return diag.FromErr(err)
43+
}
44+
45+
/* var vpcID interface{}
46+
var ok bool*/
47+
webhostingID, ok := d.GetOk("webhosting_id")
48+
if !ok { // Get IP by region and IP address.
49+
res, err := api.ListHostings(&webhosting.ListHostingsRequest{
50+
Region: region,
51+
Domain: expandStringPtr(d.Get("domain")),
52+
ProjectID: expandStringPtr(d.Get("project_id")),
53+
OrganizationID: expandStringPtr(d.Get("organization_id")),
54+
}, scw.WithContext(ctx))
55+
if err != nil {
56+
return diag.FromErr(err)
57+
}
58+
59+
for _, hosting := range res.Hostings {
60+
if hosting.Domain == d.Get("domain").(string) {
61+
if webhostingID != "" {
62+
return diag.Errorf("more than 1 hosting found with the same domain %s", d.Get("domain"))
63+
}
64+
webhostingID = hosting.ID
65+
}
66+
}
67+
if webhostingID == "" {
68+
return diag.Errorf("no hosting found with the domain %s", d.Get("domain"))
69+
}
70+
}
71+
72+
regionalID := datasourceNewRegionalID(webhostingID, region)
73+
d.SetId(regionalID)
74+
err = d.Set("webhosting_id", regionalID)
75+
if err != nil {
76+
return diag.FromErr(err)
77+
}
78+
79+
diags := resourceScalewayWebhostingRead(ctx, d, meta)
80+
if diags != nil {
81+
return append(diags, diag.Errorf("failed to read hosting")...)
82+
}
83+
84+
if d.Id() == "" {
85+
return diag.Errorf("hosting (%s) not found", regionalID)
86+
}
87+
88+
return nil
89+
}
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package scaleway
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccScalewayDataSourceWebhosting_Basic(t *testing.T) {
10+
tt := NewTestTools(t)
11+
defer tt.Cleanup()
12+
resource.ParallelTest(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
ProviderFactories: tt.ProviderFactories,
15+
CheckDestroy: testAccCheckScalewayWebhostingDestroy(tt),
16+
Steps: []resource.TestStep{
17+
{
18+
Config: `
19+
data "scaleway_webhosting_offer" "by_name" {
20+
name = "lite"
21+
}
22+
23+
resource "scaleway_webhosting" "main" {
24+
offer_id = data.scaleway_webhosting_offer.by_name.offer_id
25+
26+
domain = "foobar.com"
27+
}`,
28+
},
29+
{
30+
Config: `
31+
data "scaleway_webhosting_offer" "by_name" {
32+
name = "lite"
33+
}
34+
35+
resource "scaleway_webhosting" "main" {
36+
offer_id = data.scaleway_webhosting_offer.by_name.offer_id
37+
38+
domain = "foobar.com"
39+
}
40+
41+
data "scaleway_webhosting" "by_domain" {
42+
domain = "foobar.com"
43+
}
44+
45+
data "scaleway_webhosting" "by_id" {
46+
webhosting_id = "${scaleway_webhosting.main.id}"
47+
}`,
48+
Check: resource.ComposeTestCheckFunc(
49+
testAccCheckScalewayWebhostingExists(tt, "scaleway_webhosting.main"),
50+
resource.TestCheckResourceAttrPair("data.scaleway_webhosting.by_domain", "webhosting_id", "scaleway_webhosting.main", "id"),
51+
resource.TestCheckResourceAttrPair("data.scaleway_webhosting.by_id", "domain", "scaleway_webhosting.main", "domain"),
52+
),
53+
},
54+
},
55+
})
56+
}

scaleway/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
237237
"scaleway_vpc_public_gateway_ip": dataSourceScalewayVPCPublicGatewayIP(),
238238
"scaleway_vpc_private_network": dataSourceScalewayVPCPrivateNetwork(),
239239
"scaleway_vpc_public_gateway_pat_rule": dataSourceScalewayVPCPublicGatewayPATRule(),
240+
"scaleway_webhosting": dataSourceScalewayWebhosting(),
240241
"scaleway_webhosting_offer": dataSourceScalewayWebhostingOffer(),
241242
},
242243
}

0 commit comments

Comments
 (0)