Skip to content

Commit 5ee169a

Browse files
authored
feat(webhosting): add webhosting resource (#2043)
* add support for webhosting, first try * fixes, doc, update cassette
1 parent 0de7674 commit 5ee169a

6 files changed

+1197
-0
lines changed

docs/resources/webhosting.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
subcategory: "Webhosting"
3+
page_title: "Scaleway: scaleway_webhosting"
4+
---
5+
6+
# scaleway_webhosting
7+
8+
Creates and manages Scaleway Web Hostings.
9+
For more information, see [the documentation](https://www.scaleway.com/en/developers/api/webhosting/).
10+
11+
## Example
12+
13+
```hcl
14+
data "scaleway_webhosting_offer" "by_name" {
15+
name = "lite"
16+
}
17+
18+
resource "scaleway_webhosting" "main" {
19+
offer_id = data.scaleway_webhosting_offer.by_name.offer_id
20+
21+
domain = "yourdomain.com"
22+
tags = ["webhosting", "provider", "terraform"]
23+
}
24+
```
25+
26+
## Arguments Reference
27+
28+
The following arguments are supported:
29+
30+
- `offer_id` - (Required) The ID of the selected offer for the hosting.
31+
- `email` - (Required) The contact email of the client for the hosting.
32+
- `domain` - (Required) TThe domain name of the hosting.
33+
- `option_ids` - (Optional) The IDs of the selected options for the hosting.
34+
- `tags` - (Optional) The tags associated with the hosting.
35+
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) of the Hosting.
36+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the VPC is associated with.
37+
38+
## Attributes Reference
39+
40+
In addition to all above arguments, the following attributes are exported:
41+
42+
- `id` - The ID of the hosting.
43+
44+
~> **Important:** Hostings' IDs are [regional](../guides/regions_and_zones.md#resource-ids), which means they are of the form `{region}/{id}`, e.g. `fr-par/11111111-1111-1111-1111-111111111111
45+
46+
- `status` - The hosting status.
47+
- `created_at` - Date and time of hosting's creation (RFC 3339 format).
48+
- `updated_at` - Date and time of hosting's last update (RFC 3339 format).
49+
- `platform_hostname` - The hostname of the host platform.
50+
- `platform_number` - The number of the host platform.
51+
- `offer_name` - The name of the active offer.
52+
- `options` - The active options of the hosting.
53+
- `id` - The option ID.
54+
- `name` - The option name.
55+
- `dns_status` - The DNS status of the hosting.
56+
- `cpanel_urls` - The URL to connect to cPanel Dashboard and to Webmail interface.
57+
- `dashboard` - The URL of the Dashboard.
58+
- `webmail` - The URL of the Webmail interface.
59+
- `username` - The main hosting cPanel username.
60+
- `organization_id` - The organization ID the hosting is associated with.
61+
62+
## Import
63+
64+
Hostings can be imported using the `{region}/{id}`, e.g.
65+
66+
```bash
67+
$ terraform import scaleway_webhosting.hosting01 fr-par/11111111-1111-1111-1111-111111111111
68+
```

scaleway/helpers_webhosting.go

+66
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package scaleway
22

33
import (
4+
"context"
5+
"time"
6+
47
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
58
webhosting "github.com/scaleway/scaleway-sdk-go/api/webhosting/v1alpha1"
69
"github.com/scaleway/scaleway-sdk-go/scw"
710
)
811

12+
const (
13+
defaultWebhostingTimeout = 5 * time.Minute
14+
hostingRetryInterval = 5 * time.Second
15+
)
16+
917
// webhostingAPIWithRegion returns a new Webhosting API and the region for a Create request
1018
func webhostingAPIWithRegion(d *schema.ResourceData, m interface{}) (*webhosting.API, scw.Region, error) {
1119
meta := m.(*Meta)
@@ -18,6 +26,18 @@ func webhostingAPIWithRegion(d *schema.ResourceData, m interface{}) (*webhosting
1826
return api, region, nil
1927
}
2028

29+
// webhostingAPIWithRegionAndID returns a Webhosting API with region and ID extracted from the state
30+
func webhostingAPIWithRegionAndID(m interface{}, id string) (*webhosting.API, scw.Region, string, error) {
31+
meta := m.(*Meta)
32+
api := webhosting.NewAPI(meta.scwClient)
33+
34+
region, id, err := parseRegionalID(id)
35+
if err != nil {
36+
return nil, "", "", err
37+
}
38+
return api, region, id, nil
39+
}
40+
2141
func flattenOfferProduct(product *webhosting.OfferProduct) interface{} {
2242
return []map[string]interface{}{
2343
{
@@ -37,3 +57,49 @@ func flattenOfferProduct(product *webhosting.OfferProduct) interface{} {
3757
func flattenOfferPrice(price *scw.Money) interface{} {
3858
return price.String()
3959
}
60+
61+
func flattenHostingCpanelUrls(cpanelURL *webhosting.HostingCpanelURLs) []map[string]interface{} {
62+
return []map[string]interface{}{
63+
{
64+
"dashboard": cpanelURL.Dashboard,
65+
"webmail": cpanelURL.Webmail,
66+
},
67+
}
68+
}
69+
70+
func flattenHostingOptions(options []*webhosting.HostingOption) []map[string]interface{} {
71+
if options == nil {
72+
return nil
73+
}
74+
flattenedOptions := []map[string]interface{}(nil)
75+
for _, option := range options {
76+
flattenedOptions = append(flattenedOptions, map[string]interface{}{
77+
"id": option.ID,
78+
"name": option.Name,
79+
})
80+
}
81+
return flattenedOptions
82+
}
83+
84+
func waitForHosting(ctx context.Context, api *webhosting.API, region scw.Region, hostingID string, timeout time.Duration) (*webhosting.Hosting, error) {
85+
retryInterval := hostingRetryInterval
86+
if DefaultWaitRetryInterval != nil {
87+
retryInterval = *DefaultWaitRetryInterval
88+
}
89+
90+
return api.WaitForHosting(&webhosting.WaitForHostingRequest{
91+
HostingID: hostingID,
92+
Region: region,
93+
Timeout: scw.TimeDurationPtr(timeout),
94+
RetryInterval: &retryInterval,
95+
}, scw.WithContext(ctx))
96+
}
97+
98+
func containsTag(tags []string, tag string) bool {
99+
for _, t := range tags {
100+
if t == tag {
101+
return true
102+
}
103+
}
104+
return false
105+
}

scaleway/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
165165
"scaleway_vpc_public_gateway_ip_reverse_dns": resourceScalewayVPCPublicGatewayIPReverseDNS(),
166166
"scaleway_vpc_public_gateway_pat_rule": resourceScalewayVPCPublicGatewayPATRule(),
167167
"scaleway_vpc_private_network": resourceScalewayVPCPrivateNetwork(),
168+
"scaleway_webhosting": resourceScalewayWebhosting(),
168169
},
169170

170171
DataSourcesMap: map[string]*schema.Resource{

0 commit comments

Comments
 (0)