Skip to content
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

Fix webhook management issue #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BIGCOMMERCE_STORE_HASH=
PREV_BIGCOMMERCE_CLIENT_ID=
PREV_BIGCOMMERCE_ACCESS_TOKEN=
BIGCOMMERCE_CLIENT_ID=
BIGCOMMERCE_ACCESS_TOKEN=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.env
terraform-provider-bigcommerce
bin

Expand Down
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
TEST?=$$(go list ./... | grep -v 'vendor')
HOSTNAME=ashsmith.io
NAMESPACE=test
HOSTNAME=space48.com
NAMESPACE=local
NAME=bigcommerce
BINARY=terraform-provider-${NAME}
VERSION=0.2
VERSION=0.1
OS_ARCH=darwin_amd64

include .env
export

default: install

build:
Expand Down Expand Up @@ -34,4 +37,4 @@ test:
echo $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4

testacc:
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
TF_ACC=1 go test -v ./...
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Terraform Provider for BigCommerce

- [Bigcommerce Provider Documentation on Terraform](https://registry.terraform.io/providers/ashsmith/bigcommerce/latest)
- [Bigcommerce Provider Documentation on Terraform](https://registry.terraform.io/providers/space48/bigcommerce/latest)

## Resources:

Expand Down Expand Up @@ -30,20 +30,20 @@ go generate
terraform {
required_providers {
bigcommerce = {
source = "ashsmith/bigcommerce"
source = "space48/bigcommerce"
version = "0.1.0"
}
}
}

provider "bigcommerce" {
store_hash = "your-hash"
client_id = "your-client-id"
access_token = "your-access-token"
}

resource "bigcommerce_webhook" "example" {
scope = "store/customer/*"
client_id = "your-client-id"
access_token = "your-access-token"
destination = "https://foo.bar/webhook"
is_active = true

Expand Down
48 changes: 14 additions & 34 deletions bigcommerce/data_source_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bigcommerce

import (
"context"
"fmt"
"strconv"

"github.com/ashsmith/bigcommerce-api-go"
Expand All @@ -15,41 +14,33 @@ func dataSourceWebhook() *schema.Resource {
Description: "Provides information about a webhook ",
ReadContext: dataSourceWebhookRead,
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
},
"client_id": &schema.Schema{
"client_id": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Required: true,
},
"store_hash": &schema.Schema{
"access_token": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Required: true,
},
"created_at": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"updated_at": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"scope": &schema.Schema{
"scope": {
Type: schema.TypeString,
Computed: true,
},
"destination": &schema.Schema{
"destination": {
Type: schema.TypeString,
Computed: true,
},
"is_active": &schema.Schema{
"is_active": {
Type: schema.TypeBool,
Computed: true,
},
"header": &schema.Schema{
"header": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Expand All @@ -72,14 +63,15 @@ func dataSourceWebhook() *schema.Resource {
func dataSourceWebhookRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
storeHash := m.(string)
clientId := d.Get("client_id").(string)
accessToken := d.Get("access_token").(string)

c := m.(*bigcommerce.Client)
client := createBigCommerceClient(storeHash, clientId, accessToken)
hookID := d.Get("id").(string)

fmt.Println(c)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gabrieldagama any idea why this print statement was here in the upstream branch?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea... maybe some debug? not sure!


webhookID, _ := strconv.ParseInt(hookID, 10, 64)
webhook, whErr := c.Webhooks.Get(webhookID)
webhook, whErr := client.Webhooks.Get(webhookID)

if whErr != nil {
return diag.FromErr(whErr)
Expand All @@ -99,18 +91,6 @@ func setWebhookData(webhook bigcommerce.Webhook, d *schema.ResourceData) diag.Di
if err := d.Set("id", strconv.FormatInt(webhook.ID, 10)); err != nil {
return diag.FromErr(err)
}
if err := d.Set("client_id", webhook.ClientID); err != nil {
return diag.FromErr(err)
}
if err := d.Set("store_hash", webhook.StoreHash); err != nil {
return diag.FromErr(err)
}
if err := d.Set("created_at", int(webhook.CreatedAt)); err != nil {
return diag.FromErr(err)
}
if err := d.Set("updated_at", int(webhook.UpdatedAt)); err != nil {
return diag.FromErr(err)
}
if err := d.Set("scope", webhook.Scope); err != nil {
return diag.FromErr(err)
}
Expand Down
47 changes: 3 additions & 44 deletions bigcommerce/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package bigcommerce

import (
"context"
"net/http"

"github.com/ashsmith/bigcommerce-api-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand All @@ -13,23 +11,11 @@ import (
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"store_hash": &schema.Schema{
"store_hash": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("BIGCOMMERCE_STORE_HASH", nil),
},
"client_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("BIGCOMMERCE_CLIENT_ID", nil),
},
"access_token": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("BIGCOMMERCE_ACCESS_TOKEN", nil),
},
},
ResourcesMap: map[string]*schema.Resource{
"bigcommerce_webhook": resourceWebhook(),
Expand All @@ -42,28 +28,10 @@ func Provider() *schema.Provider {
}

func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
clientID := d.Get("client_id").(string)
accessToken := d.Get("access_token").(string)
storeHash := d.Get("store_hash").(string)
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics

if clientID == "" {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Missing client_id from provider configuration",
Detail: "client_id is a required parmeter and must be defined, you can also use BIGCOMMERCE_CLIENT_ID environment variable.",
})
}

if accessToken == "" {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Missing access_token from provider configuration",
Detail: "access_token is a required parmeter and must be defined, you can also use BIGCOMMERCE_ACCESS_TOKEN environment variable.",
})
}

if storeHash == "" {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Expand All @@ -72,18 +40,9 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
})
}

if (clientID == "") || (accessToken == "") || (storeHash == "") {
if storeHash == "" {
return nil, diags
}

app := bigcommerce.App{
ClientID: clientID,
StoreHash: storeHash,
AccessToken: accessToken,
}

httpClient := http.Client{}
bcClient := app.NewClient(httpClient)

return bcClient, diags
return storeHash, diags
}
30 changes: 30 additions & 0 deletions bigcommerce/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package bigcommerce

import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var testAccProviders map[string]*schema.Provider
var testAccProvider *schema.Provider

func init() {
testAccProvider = Provider()
testAccProviders = map[string]*schema.Provider{
"bigcommerce": testAccProvider,
}
}

func TestProvider(t *testing.T) {
if err := Provider().InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("BIGCOMMERCE_STORE_HASH"); v == "" {
t.Fatal("BIGCOMMERCE_STORE_HASH must be set for acceptance tests")
}
}
Loading