Skip to content

feat(tem): add data_source for offer subscription #2984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions docs/data-sources/tem_offer_subscription.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
subcategory: "Transactional Email"
page_title: "Scaleway: scaleway_tem_offer_subscription"
---

# scaleway_tem_offer_subscription

Gets information about a transactional email offer subscription.

## Example Usage

```hcl
// Retrieve offer subscription information
data "scaleway_tem_offer_subscription" "test" {}
```

## Argument Reference

- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) where the offer subscription exists.
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the offer subscription is associated with.

## Attributes Reference

The following attributes are exported:

- `offer_name` - The name of the offer associated with the subscription (e.g., `scale`).
- `subscribed_at` - The date and time of the subscription.
- `cancellation_available_at` - The date and time when cancellation becomes available for the subscription.
- `sla` - The Service Level Agreement (SLA) percentage of the offer subscription.
- `max_domains` - The maximum number of domains that can be associated with the offer subscription.
- `max_dedicated_ips` - The maximum number of dedicated IPs that can be associated with the offer subscription.
- `max_webhooks_per_domain` - The maximum number of webhooks that can be associated with the offer subscription per domain.
- `max_custom_blocklists_per_domain` - The maximum number of custom blocklists that can be associated with the offer subscription per domain.
- `included_monthly_emails` - The number of emails included in the offer subscription per month.

## Import

This data source is read-only and cannot be imported.

1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ func Provider(config *Config) plugin.ProviderFunc {
"scaleway_secret": secret.DataSourceSecret(),
"scaleway_secret_version": secret.DataSourceVersion(),
"scaleway_tem_domain": tem.DataSourceDomain(),
"scaleway_tem_offer_subscription": tem.DataSourceOfferSubscription(),
"scaleway_vpc": vpc.DataSourceVPC(),
"scaleway_vpc_gateway_network": vpcgw.DataSourceNetwork(),
"scaleway_vpc_private_network": vpc.DataSourcePrivateNetwork(),
Expand Down
17 changes: 17 additions & 0 deletions internal/services/tem/helpers.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package tem

import (
"context"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
accountSDK "github.com/scaleway/scaleway-sdk-go/api/account/v3"
tem "github.com/scaleway/scaleway-sdk-go/api/tem/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
)

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

return api, region, id, nil
}

func getDefaultProjectID(ctx context.Context, m interface{}) (string, error) {
accountAPI := account.NewProjectAPI(m)

res, err := accountAPI.ListProjects(&accountSDK.ProjectAPIListProjectsRequest{
Name: types.ExpandStringPtr("default"),
}, scw.WithContext(ctx))
if err != nil {
return "", err
}

return res.Projects[0].ID, nil
}
123 changes: 123 additions & 0 deletions internal/services/tem/offer_subscription_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package tem

import (
"context"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
tem "github.com/scaleway/scaleway-sdk-go/api/tem/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
)

func DataSourceOfferSubscription() *schema.Resource {
return &schema.Resource{
ReadContext: DataSourceOfferSubscriptionRead,
Schema: map[string]*schema.Schema{
"region": regional.Schema(),
"project_id": account.ProjectIDSchema(),
"offer_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the offer",
},
"subscribed_at": {
Type: schema.TypeString,
Computed: true,
Description: "Date and time of the subscription",
},
"cancellation_available_at": {
Type: schema.TypeString,
Computed: true,
Description: "Date and time of the end of the offer-subscription commitment",
},
"sla": {
Type: schema.TypeFloat,
Computed: true,
Description: "Service Level Agreement percentage of the offer-subscription",
},
"max_domains": {
Type: schema.TypeInt,
Computed: true,
Description: "Max number of domains that can be associated with the offer-subscription",
},
"max_dedicated_ips": {
Type: schema.TypeInt,
Computed: true,
Description: "Max number of dedicated IPs that can be associated with the offer-subscription",
},
"max_webhooks_per_domain": {
Type: schema.TypeInt,
Computed: true,
Description: "Max number of webhooks that can be associated with the offer-subscription",
},
"max_custom_blocklists_per_domain": {
Type: schema.TypeInt,
Computed: true,
Description: "Max number of custom blocklists that can be associated with the offer-subscription",
},
"included_monthly_emails": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of emails included in the offer-subscription per month",
},
},
}
}

func DataSourceOfferSubscriptionRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api, region, err := temAPIWithRegion(d, m)
if err != nil {
return diag.FromErr(err)
}

var projectID string

if _, ok := d.GetOk("project_id"); !ok {
projectID, err = getDefaultProjectID(ctx, m)
if err != nil {
return diag.FromErr(err)
}
} else {
projectID = d.Get("project_id").(string)
}

offer, err := api.ListOfferSubscriptions(&tem.ListOfferSubscriptionsRequest{
Region: region,
ProjectID: projectID,
}, scw.WithContext(ctx))
if err != nil {
if httperrors.Is404(err) {
d.SetId("")

return nil
}

return diag.FromErr(err)
}

if len(offer.OfferSubscriptions) == 0 {
d.SetId("")

return nil
}

offerSubscription := offer.OfferSubscriptions[0]
d.SetId(regional.NewIDString(region, offerSubscription.ProjectID))
_ = d.Set("project_id", offerSubscription.ProjectID)
_ = d.Set("region", region)
_ = d.Set("offer_name", offerSubscription.OfferName)
_ = d.Set("subscribed_at", offerSubscription.SubscribedAt.Format(time.RFC3339))
_ = d.Set("cancellation_available_at", offerSubscription.CancellationAvailableAt.Format(time.RFC3339))
_ = d.Set("sla", offerSubscription.SLA)
_ = d.Set("max_domains", offerSubscription.MaxDomains)
_ = d.Set("max_dedicated_ips", offerSubscription.MaxDedicatedIPs)
_ = d.Set("max_webhooks_per_domain", offerSubscription.MaxWebhooksPerDomain)
_ = d.Set("max_custom_blocklists_per_domain", offerSubscription.MaxCustomBlocklistsPerDomain)
_ = d.Set("included_monthly_emails", offerSubscription.IncludedMonthlyEmails)

return nil
}
43 changes: 43 additions & 0 deletions internal/services/tem/offer_subscription_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package tem_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
)

func TestAccDataSourceOfferSubscription_Basic(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()
orgID, orgIDExists := tt.Meta.ScwClient().GetDefaultOrganizationID()

if !orgIDExists {
orgID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`

data scaleway_account_project "project" {
name = "default"
organization_id = "%s"
}

data "scaleway_tem_offer_subscription" "test" {
project_id = data.scaleway_account_project.project.id
}
`, orgID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.scaleway_tem_offer_subscription.test", "project_id"),
resource.TestCheckResourceAttr("data.scaleway_tem_offer_subscription.test", "offer_name", "scale"),
),
},
},
})
}
Loading
Loading