Skip to content

Commit 14b7c82

Browse files
committed
feat(tem): add data_source for offer subscription
1 parent 6fbb350 commit 14b7c82

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ func Provider(config *Config) plugin.ProviderFunc {
315315
"scaleway_secret": secret.DataSourceSecret(),
316316
"scaleway_secret_version": secret.DataSourceVersion(),
317317
"scaleway_tem_domain": tem.DataSourceDomain(),
318+
"scaleway_tem_offer_subscription": tem.DataSourceOfferSubscription(),
318319
"scaleway_vpc": vpc.DataSourceVPC(),
319320
"scaleway_vpc_gateway_network": vpcgw.DataSourceNetwork(),
320321
"scaleway_vpc_private_network": vpc.DataSourcePrivateNetwork(),
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package tem
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
tem "github.com/scaleway/scaleway-sdk-go/api/tem/v1alpha1"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
11+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
12+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
13+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
14+
)
15+
16+
func DataSourceOfferSubscription() *schema.Resource {
17+
return &schema.Resource{
18+
ReadContext: DataSourceOfferSubscriptionRead,
19+
Schema: map[string]*schema.Schema{
20+
"region": regional.Schema(),
21+
"project_id": account.ProjectIDSchema(),
22+
"subscribed_at": {
23+
Type: schema.TypeString,
24+
Computed: true,
25+
Description: "Date and time of the subscription",
26+
},
27+
"cancellation_available_at": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
Description: "Date and time of the end of the offer-subscription commitment",
31+
},
32+
"sla": {
33+
Type: schema.TypeFloat,
34+
Computed: true,
35+
Description: "Service Level Agreement percentage of the offer-subscription",
36+
},
37+
"max_domains": {
38+
Type: schema.TypeInt,
39+
Computed: true,
40+
Description: "Max number of domains that can be associated with the offer-subscription",
41+
},
42+
"max_dedicated_ips": {
43+
Type: schema.TypeInt,
44+
Computed: true,
45+
Description: "Max number of dedicated IPs that can be associated with the offer-subscription",
46+
},
47+
"max_webhooks_per_domain": {
48+
Type: schema.TypeInt,
49+
Computed: true,
50+
Description: "Max number of webhooks that can be associated with the offer-subscription",
51+
},
52+
"max_custom_blocklists_per_domain": {
53+
Type: schema.TypeInt,
54+
Computed: true,
55+
Description: "Max number of custom blocklists that can be associated with the offer-subscription",
56+
},
57+
"included_monthly_emails": {
58+
Type: schema.TypeInt,
59+
Computed: true,
60+
Description: "Number of emails included in the offer-subscription per month",
61+
},
62+
},
63+
}
64+
}
65+
66+
func DataSourceOfferSubscriptionRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
67+
api, region, projectID, err := NewAPIWithRegionAndID(m, d.Get("project_id").(string))
68+
if err != nil {
69+
return diag.FromErr(err)
70+
}
71+
72+
offer, err := api.ListOfferSubscriptions(&tem.ListOfferSubscriptionsRequest{
73+
Region: region,
74+
ProjectID: projectID,
75+
}, scw.WithContext(ctx))
76+
if err != nil {
77+
if httperrors.Is404(err) {
78+
d.SetId("")
79+
80+
return nil
81+
}
82+
83+
return diag.FromErr(err)
84+
}
85+
86+
if len(offer.OfferSubscriptions) == 0 {
87+
d.SetId("")
88+
89+
return nil
90+
}
91+
92+
offerSubscription := offer.OfferSubscriptions[0]
93+
d.SetId(regional.NewIDString(region, offerSubscription.ProjectID))
94+
_ = d.Set("project_id", offerSubscription.ProjectID)
95+
_ = d.Set("region", region)
96+
_ = d.Set("subscribed_at", offerSubscription.SubscribedAt.Format(time.RFC3339))
97+
_ = d.Set("cancellation_available_at", offerSubscription.CancellationAvailableAt.Format(time.RFC3339))
98+
_ = d.Set("sla", offerSubscription.SLA)
99+
_ = d.Set("max_domains", offerSubscription.MaxDomains)
100+
_ = d.Set("max_dedicated_ips", offerSubscription.MaxDedicatedIPs)
101+
_ = d.Set("max_webhooks_per_domain", offerSubscription.MaxWebhooksPerDomain)
102+
_ = d.Set("max_custom_blocklists_per_domain", offerSubscription.MaxCustomBlocklistsPerDomain)
103+
_ = d.Set("included_monthly_emails", offerSubscription.IncludedMonthlyEmails)
104+
105+
return nil
106+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package tem_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
8+
)
9+
10+
func TestAccDataSourceOfferSubscription_Basic(t *testing.T) {
11+
tt := acctest.NewTestTools(t)
12+
defer tt.Cleanup()
13+
14+
resource.ParallelTest(t, resource.TestCase{
15+
PreCheck: func() { acctest.PreCheck(t) },
16+
ProviderFactories: tt.ProviderFactories,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: `
20+
data "scaleway_tem_offer_subscription" "test" {
21+
project_id = "your_project_id"
22+
}
23+
`,
24+
Check: resource.ComposeTestCheckFunc(
25+
resource.TestCheckResourceAttrSet("data.scaleway_tem_offer_subscription.test", "project_id"),
26+
resource.TestCheckResourceAttr("data.scaleway_tem_offer_subscription.test", "name", "scale"),
27+
),
28+
},
29+
},
30+
})
31+
}

0 commit comments

Comments
 (0)