Skip to content

Commit 3b3dca5

Browse files
authored
feat(tem): add data_source for offer subscription (#2984)
* feat(tem): add data_source for offer subscription * feat(tem): add data source for offer subscription * add test * fix project id * fix test * fix test * add default project_id
1 parent 6fbb350 commit 3b3dca5

7 files changed

+1192
-231
lines changed

Diff for: docs/data-sources/tem_offer_subscription.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
subcategory: "Transactional Email"
3+
page_title: "Scaleway: scaleway_tem_offer_subscription"
4+
---
5+
6+
# scaleway_tem_offer_subscription
7+
8+
Gets information about a transactional email offer subscription.
9+
10+
## Example Usage
11+
12+
```hcl
13+
// Retrieve offer subscription information
14+
data "scaleway_tem_offer_subscription" "test" {}
15+
```
16+
17+
## Argument Reference
18+
19+
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) where the offer subscription exists.
20+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the offer subscription is associated with.
21+
22+
## Attributes Reference
23+
24+
The following attributes are exported:
25+
26+
- `offer_name` - The name of the offer associated with the subscription (e.g., `scale`).
27+
- `subscribed_at` - The date and time of the subscription.
28+
- `cancellation_available_at` - The date and time when cancellation becomes available for the subscription.
29+
- `sla` - The Service Level Agreement (SLA) percentage of the offer subscription.
30+
- `max_domains` - The maximum number of domains that can be associated with the offer subscription.
31+
- `max_dedicated_ips` - The maximum number of dedicated IPs that can be associated with the offer subscription.
32+
- `max_webhooks_per_domain` - The maximum number of webhooks that can be associated with the offer subscription per domain.
33+
- `max_custom_blocklists_per_domain` - The maximum number of custom blocklists that can be associated with the offer subscription per domain.
34+
- `included_monthly_emails` - The number of emails included in the offer subscription per month.
35+
36+
## Import
37+
38+
This data source is read-only and cannot be imported.
39+

Diff for: internal/provider/provider.go

+1
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(),

Diff for: internal/services/tem/helpers.go

+17
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package tem
22

33
import (
4+
"context"
45
"time"
56

67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
accountSDK "github.com/scaleway/scaleway-sdk-go/api/account/v3"
79
tem "github.com/scaleway/scaleway-sdk-go/api/tem/v1alpha1"
810
"github.com/scaleway/scaleway-sdk-go/scw"
911
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1012
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
13+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
14+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1115
)
1216

1317
const (
@@ -39,3 +43,16 @@ func NewAPIWithRegionAndID(m interface{}, id string) (*tem.API, scw.Region, stri
3943

4044
return api, region, id, nil
4145
}
46+
47+
func getDefaultProjectID(ctx context.Context, m interface{}) (string, error) {
48+
accountAPI := account.NewProjectAPI(m)
49+
50+
res, err := accountAPI.ListProjects(&accountSDK.ProjectAPIListProjectsRequest{
51+
Name: types.ExpandStringPtr("default"),
52+
}, scw.WithContext(ctx))
53+
if err != nil {
54+
return "", err
55+
}
56+
57+
return res.Projects[0].ID, nil
58+
}
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
"offer_name": {
23+
Type: schema.TypeString,
24+
Computed: true,
25+
Description: "Name of the offer",
26+
},
27+
"subscribed_at": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
Description: "Date and time of the subscription",
31+
},
32+
"cancellation_available_at": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
Description: "Date and time of the end of the offer-subscription commitment",
36+
},
37+
"sla": {
38+
Type: schema.TypeFloat,
39+
Computed: true,
40+
Description: "Service Level Agreement percentage of the offer-subscription",
41+
},
42+
"max_domains": {
43+
Type: schema.TypeInt,
44+
Computed: true,
45+
Description: "Max number of domains that can be associated with the offer-subscription",
46+
},
47+
"max_dedicated_ips": {
48+
Type: schema.TypeInt,
49+
Computed: true,
50+
Description: "Max number of dedicated IPs that can be associated with the offer-subscription",
51+
},
52+
"max_webhooks_per_domain": {
53+
Type: schema.TypeInt,
54+
Computed: true,
55+
Description: "Max number of webhooks that can be associated with the offer-subscription",
56+
},
57+
"max_custom_blocklists_per_domain": {
58+
Type: schema.TypeInt,
59+
Computed: true,
60+
Description: "Max number of custom blocklists that can be associated with the offer-subscription",
61+
},
62+
"included_monthly_emails": {
63+
Type: schema.TypeInt,
64+
Computed: true,
65+
Description: "Number of emails included in the offer-subscription per month",
66+
},
67+
},
68+
}
69+
}
70+
71+
func DataSourceOfferSubscriptionRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
72+
api, region, err := temAPIWithRegion(d, m)
73+
if err != nil {
74+
return diag.FromErr(err)
75+
}
76+
77+
var projectID string
78+
79+
if _, ok := d.GetOk("project_id"); !ok {
80+
projectID, err = getDefaultProjectID(ctx, m)
81+
if err != nil {
82+
return diag.FromErr(err)
83+
}
84+
} else {
85+
projectID = d.Get("project_id").(string)
86+
}
87+
88+
offer, err := api.ListOfferSubscriptions(&tem.ListOfferSubscriptionsRequest{
89+
Region: region,
90+
ProjectID: projectID,
91+
}, scw.WithContext(ctx))
92+
if err != nil {
93+
if httperrors.Is404(err) {
94+
d.SetId("")
95+
96+
return nil
97+
}
98+
99+
return diag.FromErr(err)
100+
}
101+
102+
if len(offer.OfferSubscriptions) == 0 {
103+
d.SetId("")
104+
105+
return nil
106+
}
107+
108+
offerSubscription := offer.OfferSubscriptions[0]
109+
d.SetId(regional.NewIDString(region, offerSubscription.ProjectID))
110+
_ = d.Set("project_id", offerSubscription.ProjectID)
111+
_ = d.Set("region", region)
112+
_ = d.Set("offer_name", offerSubscription.OfferName)
113+
_ = d.Set("subscribed_at", offerSubscription.SubscribedAt.Format(time.RFC3339))
114+
_ = d.Set("cancellation_available_at", offerSubscription.CancellationAvailableAt.Format(time.RFC3339))
115+
_ = d.Set("sla", offerSubscription.SLA)
116+
_ = d.Set("max_domains", offerSubscription.MaxDomains)
117+
_ = d.Set("max_dedicated_ips", offerSubscription.MaxDedicatedIPs)
118+
_ = d.Set("max_webhooks_per_domain", offerSubscription.MaxWebhooksPerDomain)
119+
_ = d.Set("max_custom_blocklists_per_domain", offerSubscription.MaxCustomBlocklistsPerDomain)
120+
_ = d.Set("included_monthly_emails", offerSubscription.IncludedMonthlyEmails)
121+
122+
return nil
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package tem_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
9+
)
10+
11+
func TestAccDataSourceOfferSubscription_Basic(t *testing.T) {
12+
tt := acctest.NewTestTools(t)
13+
defer tt.Cleanup()
14+
orgID, orgIDExists := tt.Meta.ScwClient().GetDefaultOrganizationID()
15+
16+
if !orgIDExists {
17+
orgID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
18+
}
19+
20+
resource.ParallelTest(t, resource.TestCase{
21+
PreCheck: func() { acctest.PreCheck(t) },
22+
ProviderFactories: tt.ProviderFactories,
23+
Steps: []resource.TestStep{
24+
{
25+
Config: fmt.Sprintf(`
26+
27+
data scaleway_account_project "project" {
28+
name = "default"
29+
organization_id = "%s"
30+
}
31+
32+
data "scaleway_tem_offer_subscription" "test" {
33+
project_id = data.scaleway_account_project.project.id
34+
}
35+
`, orgID),
36+
Check: resource.ComposeTestCheckFunc(
37+
resource.TestCheckResourceAttrSet("data.scaleway_tem_offer_subscription.test", "project_id"),
38+
resource.TestCheckResourceAttr("data.scaleway_tem_offer_subscription.test", "offer_name", "scale"),
39+
),
40+
},
41+
},
42+
})
43+
}

0 commit comments

Comments
 (0)