Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/development' into feature/custom…
Browse files Browse the repository at this point in the history
…er-group-resource
  • Loading branch information
incentrotolgaakyazi committed Sep 16, 2022
2 parents 5571ac1 + b31cbcc commit e4816fd
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 33 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ test:
go test -v ./...

build:
go build ./...

go build ./...
12 changes: 0 additions & 12 deletions commercelayer/resource_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,6 @@ func resourceAddress() *schema.Resource {
},
},
},
"relationships": {
Description: "Resource relationships",
Type: schema.TypeList,
MaxItems: 1,
MinItems: 1,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// TODO: implement geocoder relation
},
},
},
},
}
}
Expand Down
60 changes: 51 additions & 9 deletions commercelayer/resource_merchants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commercelayer

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
commercelayer "github.com/incentro-dc/go-commercelayer-sdk/api"
Expand Down Expand Up @@ -71,7 +70,7 @@ func resourceMerchant() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address": {
Description: "The associated address",
Description: "The related address",
Type: schema.TypeString,
Required: true,
},
Expand All @@ -83,16 +82,29 @@ func resourceMerchant() *schema.Resource {
}

func resourceMerchantReadFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
return diag.Errorf("Not implemented")
c := i.(*commercelayer.APIClient)

resp, _, err := c.MerchantsApi.GETMerchantsMerchantId(ctx, d.Id()).Execute()
if err != nil {
return diagErr(err)
}

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

d.SetId(merchant.GetId())

return nil
}

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

attributes := d.Get("attributes").([]interface{})[0].(map[string]interface{})
relationships := d.Get("relationships").([]interface{})[0].(map[string]interface{})

fmt.Println(relationships)
attributes := d.Get("attributes").([]any)[0].(map[string]any)
relationships := d.Get("relationships").([]any)[0].(map[string]any)

merchantCreate := commercelayer.MerchantCreate{
Data: commercelayer.MerchantCreateData{
Expand Down Expand Up @@ -125,9 +137,39 @@ func resourceMerchantCreateFunc(ctx context.Context, d *schema.ResourceData, i i
}

func resourceMerchantDeleteFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
return diag.Errorf("Not implemented")
c := i.(*commercelayer.APIClient)
_, err := c.MerchantsApi.DELETEMerchantsMerchantId(ctx, d.Id()).Execute()
return diag.FromErr(err)
}

func resourceMerchantUpdateFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
return diag.Errorf("Not implemented")
c := i.(*commercelayer.APIClient)

attributes := d.Get("attributes").([]any)[0].(map[string]any)
relationships := d.Get("relationships").([]any)[0].(map[string]any)

var merchantUpdate = commercelayer.MerchantUpdate{
Data: commercelayer.MerchantUpdateData{
Type: merchantType,
Id: d.Id(),
Attributes: commercelayer.PATCHMerchantsMerchantId200ResponseDataAttributes{
Name: stringRef(attributes["name"].(string)),
Reference: stringRef(attributes["reference"]),
ReferenceOrigin: stringRef(attributes["reference_origin"]),
Metadata: keyValueRef(attributes["metadata"]),
},
Relationships: &commercelayer.MerchantUpdateDataRelationships{
Address: &commercelayer.BingGeocoderDataRelationshipsAddresses{
Data: commercelayer.BingGeocoderDataRelationshipsAddressesData{
Type: stringRef(addressType),
Id: stringRef(relationships["address"].(string)),
},
},
},
},
}

_, _, err := c.MerchantsApi.PATCHMerchantsMerchantId(ctx, d.Id()).MerchantUpdate(merchantUpdate).Execute()

return diag.FromErr(err)
}
4 changes: 0 additions & 4 deletions examples/full/addresses.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,4 @@ resource "commercelayer_address" "incentro_address" {
foo: "bar"
}
}

relationships {

}
}
3 changes: 1 addition & 2 deletions examples/full/merchants.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ resource "commercelayer_merchant" "incentro_merchant" {
attributes {
name = "Incentro Merchant"
metadata = {
foo : "bar"
foo: "bar"
}
}


relationships {
address = commercelayer_address.incentro_address.id
}
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ go 1.18

require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.1
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220708125716-60e688b00bc9
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220909135040-a967fe9433aa
golang.org/x/oauth2 v0.0.0-20220630143837-2104d58473e0
)

replace github.com/incentro-dc/go-commercelayer-sdk => ./../go-commercelayer-sdk

require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220708125716-60e688b00bc9 h1:5eEVLpTxW2FeIbwYkXzMayek9KPPrPJBmUBu067krl4=
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220708125716-60e688b00bc9/go.mod h1:7o1L5RRkJ6Fw04+Hvjqt/dCDonYA+it1Leug5rtE+/s=
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220708134853-5c71991be907 h1:YGbTuJ2+Xczz6qnWBdOH85OgX12nVHn4sxQvm6ohCsQ=
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220708134853-5c71991be907/go.mod h1:7o1L5RRkJ6Fw04+Hvjqt/dCDonYA+it1Leug5rtE+/s=
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220909135040-a967fe9433aa h1:1LNePHoMZhlXGbxii+nmS8ke4eMPuzTxz3JIk4XZCuM=
github.com/incentro-dc/go-commercelayer-sdk v0.0.0-20220909135040-a967fe9433aa/go.mod h1:7o1L5RRkJ6Fw04+Hvjqt/dCDonYA+it1Leug5rtE+/s=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/incentro-dc/terraform-provider-commercelayer/commercelayer"
"log"
)

func main() {
var debugMode bool

Expand Down

0 comments on commit e4816fd

Please sign in to comment.