Skip to content

Commit

Permalink
feature: fetch currency data by code, since users do not know ID (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
yurymikhailenko authored Jun 15, 2021
1 parent 9190107 commit 07970f3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
4 changes: 2 additions & 2 deletions docs/data-sources/currency.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Represents the EPCC API [Currency Object](https://documentation.elasticpath.com/

### Required

- **id** (String) The ID of this resource.
- **code** (String) Currency 3-letter unique code

### Read-only

- **code** (String)
- **id** (String) The ID of this resource.
- **decimal_places** (Number)
- **decimal_point** (String)
- **default** (Boolean)
Expand Down
10 changes: 6 additions & 4 deletions examples/data-sources/currency_data_source/datasource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ terraform {
}
}
}

provider "epcc" {
beta_features = "account-management"
}
data "epcc_currency" "example" {
id = "d8339926-3ee0-4d69-acd5-af107f7da42f"

data "epcc_currency" "USD" {
code = "USD"
}

output "currency_code" {
value = data.epcc_currency.example.code
output "usd_currency_id" {
value = data.epcc_currency.USD.id
}

60 changes: 47 additions & 13 deletions internal/provider/data_source_epcc_currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package provider
import (
"context"
"github.com/elasticpath/terraform-provider-epcc/external/sdk/epcc"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceEpccCurrency() *schema.Resource {
return &schema.Resource{
ReadContext: addDiagToContext(dataSourceEpccCurrencyRead),
Schema: map[string]*schema.Schema{
"id": {Type: schema.TypeString, Required: true},
"code": {Type: schema.TypeString, Computed: true},
"id": {Type: schema.TypeString, Computed: true},
"code": {Type: schema.TypeString, Required: true},
"exchange_rate": {Type: schema.TypeInt, Computed: true},
"format": {Type: schema.TypeString, Computed: true},
"decimal_point": {Type: schema.TypeString, Computed: true},
Expand All @@ -26,19 +27,52 @@ func dataSourceEpccCurrency() *schema.Resource {
func dataSourceEpccCurrencyRead(ctx context.Context, d *schema.ResourceData, m interface{}) {
client := m.(*epcc.Client)

currencyId := d.Get("id").(string)
currency, err := epcc.Currencies.Get(&ctx, client, currencyId)
currencyCode := d.Get("code").(string)
currencies, err := epcc.Currencies.GetAll(&ctx, client)
if err != nil {
ReportAPIError(ctx, err)
} else {
d.Set("code", currency.Data.Code)
d.Set("exchange_rate", currency.Data.ExchangeRate)
d.Set("format", currency.Data.Format)
d.Set("decimal_point", currency.Data.DecimalPoint)
d.Set("thousand_separator", currency.Data.ThousandSeparator)
d.Set("decimal_places", currency.Data.DecimalPlaces)
d.Set("default", currency.Data.Default)
d.Set("enabled", currency.Data.Enabled)
d.SetId(currency.Data.Id)
var currency *epcc.Currency
for i, next := range currencies.Data {
if next.Code == currencyCode {
currency = &currencies.Data[i]
break
}
}
if currency == nil {
addToDiag(ctx, diag.Errorf("currency with code %v not found", currencyCode))
return
}

if err := d.Set("exchange_rate", currency.ExchangeRate); err != nil {
addToDiag(ctx, diag.FromErr(err))
return
}
if err := d.Set("format", currency.Format); err != nil {
addToDiag(ctx, diag.FromErr(err))
return
}
if err := d.Set("decimal_point", currency.DecimalPoint); err != nil {
addToDiag(ctx, diag.FromErr(err))
return
}
if err := d.Set("thousand_separator", currency.ThousandSeparator); err != nil {
addToDiag(ctx, diag.FromErr(err))
return
}
if err := d.Set("decimal_places", currency.DecimalPlaces); err != nil {
addToDiag(ctx, diag.FromErr(err))
return
}
if err := d.Set("default", currency.Default); err != nil {
addToDiag(ctx, diag.FromErr(err))
return
}
if err := d.Set("enabled", currency.Enabled); err != nil {
addToDiag(ctx, diag.FromErr(err))
return
}

d.SetId(currency.Id)
}
}

0 comments on commit 07970f3

Please sign in to comment.