|
| 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 | +} |
0 commit comments