Skip to content

Commit

Permalink
feat: reworked market creation to use response body for shared secret
Browse files Browse the repository at this point in the history
  • Loading branch information
demeyerthom committed Jan 17, 2025
1 parent 93d3ac3 commit e8f656c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
73 changes: 52 additions & 21 deletions commercelayer/resource_market.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package commercelayer

import (
"bytes"
"context"
"encoding/json"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
commercelayer "github.com/labd/go-commercelayer-sdk/api"
"net/http"
)

func resourceMarket() *schema.Resource {
Expand All @@ -29,12 +32,6 @@ func resourceMarket() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"shared_secret": {
Description: "The shared secret generated at the market level that can be used to verify that callbacks are coming from CommerceLayer.",
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"attributes": {
Description: "Resource attributes",
Type: schema.TypeList,
Expand All @@ -53,6 +50,12 @@ func resourceMarket() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"shared_secret": {
Description: "The shared secret generated at the market level that can be used to verify that callbacks are coming from CommerceLayer.",
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"facebook_pixel_id": {
Description: "The Facebook Pixed ID",
Type: schema.TypeString,
Expand Down Expand Up @@ -142,6 +145,34 @@ func resourceMarket() *schema.Resource {
}
}

func appendSharedSecret(d *schema.ResourceData, sharedSecret string) error {
aSlice := d.Get("attributes").([]interface{})
attr := aSlice[0].(map[string]any)

attr["shared_secret"] = sharedSecret

aSlice[0] = attr

return d.Set("attributes", aSlice)
}

func unMarshalMarket(res *http.Response) (*commercelayer.GETMarketsMarketId200Response, error) {
b := new(bytes.Buffer)
_, err := b.ReadFrom(res.Body)
if err != nil {
return nil, err
}

market := &commercelayer.GETMarketsMarketId200Response{}

err = json.Unmarshal(b.Bytes(), market)
if err != nil {
return nil, err
}

return market, nil
}

func resourceMarketReadFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)

Expand All @@ -150,17 +181,18 @@ func resourceMarketReadFunc(ctx context.Context, d *schema.ResourceData, i inter
return diagErr(err)
}

Market, ok := resp.GetDataOk()
market, ok := resp.GetDataOk()
if !ok {
d.SetId("")
return nil
}

d.SetId(Market.GetId().(string))

MarketAttributes := Market.GetAttributes()
d.SetId(market.GetId().(string))

d.Set("shared_secret", MarketAttributes.GetSharedSecret().(string))
err = appendSharedSecret(d, market.Attributes.GetSharedSecret().(string))
if err != nil {
return diagErr(err)
}

return nil
}
Expand Down Expand Up @@ -232,24 +264,23 @@ func resourceMarketCreateFunc(ctx context.Context, d *schema.ResourceData, i int
return diagErr(err)
}

market, _, err := c.MarketsApi.POSTMarkets(ctx).MarketCreate(marketCreate).Execute()
_, pRes, err := c.MarketsApi.POSTMarkets(ctx).MarketCreate(marketCreate).Execute()
if err != nil {
return diagErr(err)
}

d.SetId(market.Data.GetId().(string))
market, err := unMarshalMarket(pRes)
if err != nil {
return diagErr(err)
}

// Market needs to be fetched again to get the shared secret. The create call does return it but the CL
// OpenApi spec does not have it in the response so the SDK does not have a method to retrieve it.
refetchedMarket, _, refetchMarketErr := c.MarketsApi.GETMarketsMarketId(ctx, market.Data.GetId()).Execute()
d.SetId(market.Data.GetId().(string))

if refetchMarketErr != nil {
return diagErr(refetchMarketErr)
err = appendSharedSecret(d, market.Data.Attributes.GetSharedSecret().(string))
if err != nil {
return diagErr(err)
}

marketAttributes := refetchedMarket.Data.GetAttributes()
d.Set("shared_secret", marketAttributes.GetSharedSecret().(string))

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions commercelayer/resource_market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (s *AcceptanceSuite) TestAccMarket_basic() {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "attributes.0.name", "labd Market"),
resource.TestCheckResourceAttr(resourceName, "attributes.0.facebook_pixel_id", "pixel"),
resource.TestCheckResourceAttrSet(resourceName, "attributes.0.shared_secret"),
),
},
{
Expand All @@ -65,6 +66,7 @@ func (s *AcceptanceSuite) TestAccMarket_basic() {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "attributes.0.name", "labd Market Changed"),
resource.TestCheckResourceAttr(resourceName, "attributes.0.facebook_pixel_id", "pixelchanged"),
resource.TestCheckResourceAttrSet(resourceName, "attributes.0.shared_secret"),
),
},
},
Expand Down

0 comments on commit e8f656c

Please sign in to comment.