Skip to content

Commit 9604461

Browse files
committed
chore: migrate webhosting to v1
1 parent 09a924a commit 9604461

File tree

8 files changed

+1291
-1162
lines changed

8 files changed

+1291
-1162
lines changed

internal/services/webhosting/helpers.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"time"
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8-
webhosting "github.com/scaleway/scaleway-sdk-go/api/webhosting/v1alpha1"
8+
"github.com/scaleway/scaleway-sdk-go/api/webhosting/v1"
99
"github.com/scaleway/scaleway-sdk-go/scw"
1010
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1111
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
@@ -17,9 +17,8 @@ const (
1717
hostingRetryInterval = 5 * time.Second
1818
)
1919

20-
// newAPIWithRegion returns a new Webhosting API and the region for a Create request
21-
func newAPIWithRegion(d *schema.ResourceData, m interface{}) (*webhosting.API, scw.Region, error) {
22-
api := webhosting.NewAPI(meta.ExtractScwClient(m))
20+
func newOfferAPIWithRegion(d *schema.ResourceData, m interface{}) (*webhosting.OfferAPI, scw.Region, error) {
21+
api := webhosting.NewOfferAPI(meta.ExtractScwClient(m))
2322

2423
region, err := meta.ExtractRegion(d, m)
2524
if err != nil {
@@ -29,9 +28,21 @@ func newAPIWithRegion(d *schema.ResourceData, m interface{}) (*webhosting.API, s
2928
return api, region, nil
3029
}
3130

32-
// NewAPIWithRegionAndID returns a Webhosting API with region and ID extracted from the state
33-
func NewAPIWithRegionAndID(m interface{}, id string) (*webhosting.API, scw.Region, string, error) {
34-
api := webhosting.NewAPI(meta.ExtractScwClient(m))
31+
// newHostingAPIWithRegion returns a new Hosting API and the region for a Create request.
32+
func newHostingAPIWithRegion(d *schema.ResourceData, m interface{}) (*webhosting.HostingAPI, scw.Region, error) {
33+
api := webhosting.NewHostingAPI(meta.ExtractScwClient(m))
34+
35+
region, err := meta.ExtractRegion(d, m)
36+
if err != nil {
37+
return nil, "", err
38+
}
39+
40+
return api, region, nil
41+
}
42+
43+
// NewAPIWithRegionAndID returns a Hosting API with region and ID extracted from the state.
44+
func NewAPIWithRegionAndID(m interface{}, id string) (*webhosting.HostingAPI, scw.Region, string, error) {
45+
api := webhosting.NewHostingAPI(meta.ExtractScwClient(m))
3546

3647
region, id, err := regional.ParseID(id)
3748
if err != nil {
@@ -41,7 +52,7 @@ func NewAPIWithRegionAndID(m interface{}, id string) (*webhosting.API, scw.Regio
4152
return api, region, id, nil
4253
}
4354

44-
func waitForHosting(ctx context.Context, api *webhosting.API, region scw.Region, hostingID string, timeout time.Duration) (*webhosting.Hosting, error) {
55+
func waitForHosting(ctx context.Context, api *webhosting.HostingAPI, region scw.Region, hostingID string, timeout time.Duration) (*webhosting.Hosting, error) {
4556
retryInterval := hostingRetryInterval
4657
if transport.DefaultWaitRetryInterval != nil {
4758
retryInterval = *transport.DefaultWaitRetryInterval

internal/services/webhosting/offer_data_source.go

Lines changed: 99 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9-
webhosting "github.com/scaleway/scaleway-sdk-go/api/webhosting/v1alpha1"
9+
webhosting "github.com/scaleway/scaleway-sdk-go/api/webhosting/v1"
1010
"github.com/scaleway/scaleway-sdk-go/scw"
1111
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
1212
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
@@ -33,8 +33,9 @@ func DataSourceOffer() *schema.Resource {
3333
Type: schema.TypeString,
3434
},
3535
"product": {
36-
Type: schema.TypeList,
37-
Computed: true,
36+
Type: schema.TypeList,
37+
Computed: true,
38+
Deprecated: "The product field is deprecated. Please use the offer field instead.",
3839
Elem: &schema.Resource{
3940
Schema: map[string]*schema.Schema{
4041
"name": {
@@ -46,32 +47,113 @@ func DataSourceOffer() *schema.Resource {
4647
Computed: true,
4748
},
4849
"email_accounts_quota": {
49-
Computed: true,
5050
Type: schema.TypeInt,
51+
Computed: true,
5152
},
5253
"email_storage_quota": {
53-
Computed: true,
5454
Type: schema.TypeInt,
55+
Computed: true,
5556
},
5657
"databases_quota": {
57-
Computed: true,
5858
Type: schema.TypeInt,
59+
Computed: true,
5960
},
6061
"hosting_storage_quota": {
61-
Computed: true,
6262
Type: schema.TypeInt,
63+
Computed: true,
6364
},
6465
"support_included": {
65-
Computed: true,
6666
Type: schema.TypeBool,
67+
Computed: true,
6768
},
6869
"v_cpu": {
69-
Computed: true,
7070
Type: schema.TypeInt,
71+
Computed: true,
7172
},
7273
"ram": {
73-
Computed: true,
7474
Type: schema.TypeInt,
75+
Computed: true,
76+
},
77+
},
78+
},
79+
},
80+
"offer": {
81+
Type: schema.TypeList,
82+
Computed: true,
83+
Description: "The offer details of the hosting",
84+
Elem: &schema.Resource{
85+
Schema: map[string]*schema.Schema{
86+
"id": {
87+
Type: schema.TypeString,
88+
Computed: true,
89+
},
90+
"name": {
91+
Type: schema.TypeString,
92+
Computed: true,
93+
},
94+
"billing_operation_path": {
95+
Type: schema.TypeString,
96+
Computed: true,
97+
},
98+
"available": {
99+
Type: schema.TypeBool,
100+
Computed: true,
101+
},
102+
"control_panel_name": {
103+
Type: schema.TypeString,
104+
Computed: true,
105+
},
106+
"end_of_life": {
107+
Type: schema.TypeBool,
108+
Computed: true,
109+
},
110+
"quota_warning": {
111+
Type: schema.TypeString,
112+
Computed: true,
113+
},
114+
"price": {
115+
Type: schema.TypeString,
116+
Computed: true,
117+
},
118+
"options": {
119+
Type: schema.TypeList,
120+
Computed: true,
121+
Elem: &schema.Resource{
122+
Schema: map[string]*schema.Schema{
123+
"id": {
124+
Type: schema.TypeString,
125+
Computed: true,
126+
},
127+
"name": {
128+
Type: schema.TypeString,
129+
Computed: true,
130+
},
131+
"billing_operation_path": {
132+
Type: schema.TypeString,
133+
Computed: true,
134+
},
135+
"min_value": {
136+
Type: schema.TypeInt,
137+
Computed: true,
138+
},
139+
"current_value": {
140+
Type: schema.TypeInt,
141+
Computed: true,
142+
},
143+
"max_value": {
144+
Type: schema.TypeInt,
145+
Computed: true,
146+
},
147+
"quota_warning": {
148+
Type: schema.TypeString,
149+
Computed: true,
150+
},
151+
"price": {
152+
Type: schema.TypeString,
153+
Computed: true,
154+
},
155+
},
156+
},
75157
},
76158
},
77159
},
@@ -86,12 +168,12 @@ func DataSourceOffer() *schema.Resource {
86168
}
87169

88170
func dataSourceOfferRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
89-
api, region, err := newAPIWithRegion(d, m)
171+
api, region, err := newOfferAPIWithRegion(d, m)
90172
if err != nil {
91173
return diag.FromErr(err)
92174
}
93175

94-
res, err := api.ListOffers(&webhosting.ListOffersRequest{
176+
res, err := api.ListOffers(&webhosting.OfferAPIListOffersRequest{
95177
Region: region,
96178
}, scw.WithContext(ctx))
97179
if err != nil {
@@ -105,8 +187,9 @@ func dataSourceOfferRead(ctx context.Context, d *schema.ResourceData, m interfac
105187
var filteredOffer *webhosting.Offer
106188

107189
for _, offer := range res.Offers {
108-
if offer.ID == d.Get("offer_id") || offer.Product.Name == d.Get("name") {
190+
if offer.ID == d.Get("offer_id") || offer.Name == d.Get("name") {
109191
filteredOffer = offer
192+
break
110193
}
111194
}
112195

@@ -117,10 +200,11 @@ func dataSourceOfferRead(ctx context.Context, d *schema.ResourceData, m interfac
117200
regionalID := datasource.NewRegionalID(filteredOffer.ID, region)
118201
d.SetId(regionalID)
119202
_ = d.Set("offer_id", regionalID)
120-
_ = d.Set("name", filteredOffer.Product.Name)
203+
_ = d.Set("name", filteredOffer.Name)
121204
_ = d.Set("region", region)
122205
_ = d.Set("billing_operation_path", filteredOffer.BillingOperationPath)
123-
_ = d.Set("product", flattenOfferProduct(filteredOffer.Product))
206+
_ = d.Set("product", nil)
207+
_ = d.Set("offer", flattenOffer(filteredOffer))
124208
_ = d.Set("price", flattenOfferPrice(filteredOffer.Price))
125209

126210
return nil

0 commit comments

Comments
 (0)