@@ -3,12 +3,14 @@ package webhosting
33import (
44 "context"
55 "fmt"
6+ "strings"
67
78 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
89 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9- webhosting "github.com/scaleway/scaleway-sdk-go/api/webhosting/v1alpha1 "
10+ webhosting "github.com/scaleway/scaleway-sdk-go/api/webhosting/v1 "
1011 "github.com/scaleway/scaleway-sdk-go/scw"
1112 "github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
13+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
1214 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1315)
1416
@@ -22,19 +24,27 @@ func DataSourceOffer() *schema.Resource {
2224 Description : "Exact name of the desired offer" ,
2325 ConflictsWith : []string {"offer_id" },
2426 },
27+ "control_panel" : {
28+ Type : schema .TypeString ,
29+ Optional : true ,
30+ Description : "Name of the control panel.(Cpanel or Plesk)" ,
31+ DiffSuppressFunc : dsf .IgnoreCase ,
32+ ConflictsWith : []string {"offer_id" },
33+ },
2534 "offer_id" : {
2635 Type : schema .TypeString ,
2736 Optional : true ,
2837 Description : "ID of the desired offer" ,
29- ConflictsWith : []string {"name" },
38+ ConflictsWith : []string {"name" , "control_panel" },
3039 },
3140 "billing_operation_path" : {
3241 Computed : true ,
3342 Type : schema .TypeString ,
3443 },
3544 "product" : {
36- Type : schema .TypeList ,
37- Computed : true ,
45+ Type : schema .TypeList ,
46+ Computed : true ,
47+ Deprecated : "The product field is deprecated. Please use the offer field instead." ,
3848 Elem : & schema.Resource {
3949 Schema : map [string ]* schema.Schema {
4050 "name" : {
@@ -46,32 +56,113 @@ func DataSourceOffer() *schema.Resource {
4656 Computed : true ,
4757 },
4858 "email_accounts_quota" : {
49- Computed : true ,
5059 Type : schema .TypeInt ,
60+ Computed : true ,
5161 },
5262 "email_storage_quota" : {
53- Computed : true ,
5463 Type : schema .TypeInt ,
64+ Computed : true ,
5565 },
5666 "databases_quota" : {
57- Computed : true ,
5867 Type : schema .TypeInt ,
68+ Computed : true ,
5969 },
6070 "hosting_storage_quota" : {
61- Computed : true ,
6271 Type : schema .TypeInt ,
72+ Computed : true ,
6373 },
6474 "support_included" : {
65- Computed : true ,
6675 Type : schema .TypeBool ,
76+ Computed : true ,
6777 },
6878 "v_cpu" : {
69- Computed : true ,
7079 Type : schema .TypeInt ,
80+ Computed : true ,
7181 },
7282 "ram" : {
73- Computed : true ,
7483 Type : schema .TypeInt ,
84+ Computed : true ,
85+ },
86+ },
87+ },
88+ },
89+ "offer" : {
90+ Type : schema .TypeList ,
91+ Computed : true ,
92+ Description : "The offer details of the hosting" ,
93+ Elem : & schema.Resource {
94+ Schema : map [string ]* schema.Schema {
95+ "id" : {
96+ Type : schema .TypeString ,
97+ Computed : true ,
98+ },
99+ "name" : {
100+ Type : schema .TypeString ,
101+ Computed : true ,
102+ },
103+ "billing_operation_path" : {
104+ Type : schema .TypeString ,
105+ Computed : true ,
106+ },
107+ "available" : {
108+ Type : schema .TypeBool ,
109+ Computed : true ,
110+ },
111+ "control_panel_name" : {
112+ Type : schema .TypeString ,
113+ Computed : true ,
114+ },
115+ "end_of_life" : {
116+ Type : schema .TypeBool ,
117+ Computed : true ,
118+ },
119+ "quota_warning" : {
120+ Type : schema .TypeString ,
121+ Computed : true ,
122+ },
123+ "price" : {
124+ Type : schema .TypeString ,
125+ Computed : true ,
126+ },
127+ "options" : {
128+ Type : schema .TypeList ,
129+ Computed : true ,
130+ Elem : & schema.Resource {
131+ Schema : map [string ]* schema.Schema {
132+ "id" : {
133+ Type : schema .TypeString ,
134+ Computed : true ,
135+ },
136+ "name" : {
137+ Type : schema .TypeString ,
138+ Computed : true ,
139+ },
140+ "billing_operation_path" : {
141+ Type : schema .TypeString ,
142+ Computed : true ,
143+ },
144+ "min_value" : {
145+ Type : schema .TypeInt ,
146+ Computed : true ,
147+ },
148+ "current_value" : {
149+ Type : schema .TypeInt ,
150+ Computed : true ,
151+ },
152+ "max_value" : {
153+ Type : schema .TypeInt ,
154+ Computed : true ,
155+ },
156+ "quota_warning" : {
157+ Type : schema .TypeString ,
158+ Computed : true ,
159+ },
160+ "price" : {
161+ Type : schema .TypeString ,
162+ Computed : true ,
163+ },
164+ },
165+ },
75166 },
76167 },
77168 },
@@ -86,12 +177,12 @@ func DataSourceOffer() *schema.Resource {
86177}
87178
88179func dataSourceOfferRead (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
89- api , region , err := newAPIWithRegion (d , m )
180+ api , region , err := newOfferAPIWithRegion (d , m )
90181 if err != nil {
91182 return diag .FromErr (err )
92183 }
93184
94- res , err := api .ListOffers (& webhosting.ListOffersRequest {
185+ res , err := api .ListOffers (& webhosting.OfferAPIListOffersRequest {
95186 Region : region ,
96187 }, scw .WithContext (ctx ))
97188 if err != nil {
@@ -105,8 +196,11 @@ func dataSourceOfferRead(ctx context.Context, d *schema.ResourceData, m interfac
105196 var filteredOffer * webhosting.Offer
106197
107198 for _ , offer := range res .Offers {
108- if offer .ID == d .Get ("offer_id" ) || offer .Product .Name == d .Get ("name" ) {
199+ cp , _ := d .Get ("control_panel" ).(string )
200+ if offer .ID == d .Get ("offer_id" ) || (offer .Name == d .Get ("name" ) && (cp == "" || strings .EqualFold (offer .ControlPanelName , cp ))) {
109201 filteredOffer = offer
202+
203+ break
110204 }
111205 }
112206
@@ -117,10 +211,11 @@ func dataSourceOfferRead(ctx context.Context, d *schema.ResourceData, m interfac
117211 regionalID := datasource .NewRegionalID (filteredOffer .ID , region )
118212 d .SetId (regionalID )
119213 _ = d .Set ("offer_id" , regionalID )
120- _ = d .Set ("name" , filteredOffer .Product . Name )
214+ _ = d .Set ("name" , filteredOffer .Name )
121215 _ = d .Set ("region" , region )
122216 _ = d .Set ("billing_operation_path" , filteredOffer .BillingOperationPath )
123- _ = d .Set ("product" , flattenOfferProduct (filteredOffer .Product ))
217+ _ = d .Set ("product" , nil )
218+ _ = d .Set ("offer" , flattenOffer (filteredOffer ))
124219 _ = d .Set ("price" , flattenOfferPrice (filteredOffer .Price ))
125220
126221 return nil
0 commit comments