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

Upgrade SDK to v2 and add debugging #23

Merged
merged 4 commits into from
Jun 23, 2021
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ website/node_modules
*~
.*.swp
.idea
.vscode
*.iml
*.test
*.iml
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go:
- 1.12
- 1.16
services:
- docker
script:
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ test: fmtcheck
testacc: fmtcheck
go test -v ./auth0 -run="TestAcc"

build: fmtcheck vet testacc
build-only:
@go install
@mkdir -p ~/.terraform.d/plugins/
@cp $(GOPATH)/bin/terraform-provider-auth0 ~/.terraform.d/plugins/terraform-provider-auth0
@echo "Build succeeded"

build: fmtcheck vet testacc build-only

clean:
rm -rf pkg/

fmt:
gofmt -w $(GOFMT_FILES)

Expand All @@ -31,4 +34,4 @@ vet:
exit 1; \
fi

.PHONY: build test testacc vet fmt fmtcheck errcheck vendor-status test-compile
.PHONY: build build-only test testacc vet fmt fmtcheck errcheck vendor-status test-compile
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# terraform-provider-auth0 [![Build Status](https://travis-ci.org/kevholditch/terraform-provider-auth0.svg?branch=master)](https://travis-ci.org/kevholditch/terraform-provider-auth0)
A terraform provider for Auth0

# Debugging

In order to debug the provider, first make sure the target workspace is running at least terraform v0.14. Execute following steps:
* run `./debug.sh` script in this repo root and init target workspace (`terraform init`)
* attach with your debugger to port `2345`, this will also print required gRPC debug info to stdout
* note the env variable from the above output (you'll need it in the next step)
* set breakpoints in code and run a plan (`TF_REATTACH_PROVIDERS='{...}' terraform plan`)
* re-run the above command as many times as needed (provider process keeps running after the plan has finished)

You may also want to edit your delve config to increase string truncation limit in the debugger (set `max-string-len` in `~/.config/dlv/config.yml`)
11 changes: 10 additions & 1 deletion auth0/auth0_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/parnurzeal/gorequest"
"net/http"

"github.com/parnurzeal/gorequest"
)

type AuthClient struct {
Expand Down Expand Up @@ -185,6 +186,10 @@ func (authClient *AuthClient) GetUserById(id string) (*User, error) {
return nil, fmt.Errorf("could not parse auth0 get user response, error: %v %s", err, body)
}

TfLogString("[GetUserById-raw]", body)
TfLogJson("[GetUserById-unmarshalled-user]", user)
TfLogJson("[GetUserById-unmarshalled-user_metadata]", user.UserMetaData)

if user.UserId == "" {
return nil, nil
}
Expand Down Expand Up @@ -240,6 +245,10 @@ func (authClient *AuthClient) UpdateUserById(id string, userRequest *UserRequest
return nil, fmt.Errorf("could not parse auth0 user update response, error: %v", err)
}

TfLogString("[UpdateUserById-raw]", body)
TfLogJson("[UpdateUserById-unmarshalled-user]", updatedUser)
TfLogJson("[UpdateUserById-unmarshalled-user_metadata]", updatedUser.UserMetaData)

if updatedUser.UserId == "" {
return nil, fmt.Errorf("could not update auth0 user, error: %v", body)
}
Expand Down
18 changes: 15 additions & 3 deletions auth0/auth0_client_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package auth0

import (
"github.com/google/uuid"
"os"
"sync"
"testing"
"time"

"github.com/google/uuid"
)

// 1. Create Token
Expand Down Expand Up @@ -70,19 +71,30 @@ func TestAccGetUserByIdIsNotRateLimited(t *testing.T) {
}()

var done sync.WaitGroup
errs := make(chan error, numberOfGoRoutines*numberOfRequests)

for i := 0; i < numberOfGoRoutines; i++ {
done.Add(1)
go func() {
defer done.Done()

for i := 1; i <= numberOfRequests; i++ {
_, err := client.GetUserById(createdUser.UserId)
if err != nil {
t.Fatalf("failed to get user %v", err)
errs <- err
}
}
}()
}

done.Wait()
go func() {
done.Wait()
close(errs)
}()

for err := range errs {
if err != nil {
t.Fatalf("failed to get user %v", err)
}
}
}
34 changes: 34 additions & 0 deletions auth0/debug_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package auth0

import (
"encoding/json"
"log"
"os"
)

func shouldLog() bool {
tflog := os.Getenv("TF_LOG_PROVIDER")
if len(tflog) == 0 {
tflog = os.Getenv("TF_LOG")
}

return tflog == "TRACE"
}

func TfLogString(context string, message string) {
if shouldLog() {
log.Printf("\n\n[%s] %s\n\n", context, message)
}
}

func TfLogJson(context string, boxed interface{}) {
if shouldLog() {
jsonBytes, err := json.Marshal(boxed)

if err != nil {
log.Printf("[debug_logger] failed to marshal log: %v", err)
} else {
log.Printf("\n\n[%s] %s\n\n", context, string(jsonBytes))
}
}
}
6 changes: 3 additions & 3 deletions auth0/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package auth0

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"log"
"time"

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

func Provider() terraform.ResourceProvider {
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"domain": &schema.Schema{
Expand Down
14 changes: 7 additions & 7 deletions auth0/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package auth0

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"os"
"testing"

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

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

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

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

func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
var _ schema.Provider = *Provider()
}

func testAccPreCheck(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion auth0/resource_auth0_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package auth0

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

func resourceAuth0Api() *schema.Resource {
Expand Down
4 changes: 2 additions & 2 deletions auth0/resource_auth0_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package auth0

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"testing"
)

Expand Down
2 changes: 1 addition & 1 deletion auth0/resource_auth0_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package auth0

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

func resourceAuth0Client() *schema.Resource {
Expand Down
2 changes: 1 addition & 1 deletion auth0/resource_auth0_client_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package auth0

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

func resourceAuth0ClientGrant() *schema.Resource {
Expand Down
4 changes: 2 additions & 2 deletions auth0/resource_auth0_client_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package auth0
import (
"fmt"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"testing"
)

Expand Down
4 changes: 2 additions & 2 deletions auth0/resource_auth0_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package auth0

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"testing"
)

Expand Down
23 changes: 20 additions & 3 deletions auth0/resource_auth0_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package auth0

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

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

func resourceAuth0User() *schema.Resource {
Expand Down Expand Up @@ -36,8 +37,9 @@ func resourceAuth0User() *schema.Resource {
Optional: true,
},
"password": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
"user_metadata": &schema.Schema{
Type: schema.TypeMap,
Expand All @@ -60,6 +62,7 @@ func resourceAuth0UserCreate(d *schema.ResourceData, meta interface{}) error {
userRequest := createUserRequestFromResourceData(d)

user, err := auth0Client.CreateUser(userRequest)
TfLogJson("[resourceAuth0UserCreate]", userRequest)

if err != nil {
return fmt.Errorf("failed to create auth0 user: %v error: %v", userRequest, err)
Expand All @@ -77,6 +80,7 @@ func resourceAuth0UserUpdate(d *schema.ResourceData, meta interface{}) error {
userId := d.Id()

for _, update := range updateUserRequests {
TfLogJson("[resourceAuth0UserUpdate]", update)
_, err := auth0Client.UpdateUserById(userId, update)

if err != nil {
Expand All @@ -99,6 +103,7 @@ func resourceAuth0UserRead(d *schema.ResourceData, meta interface{}) error {

if user == nil {
d.SetId("")
TfLogString("[resourceAuth0UserRead]", "User is nil")
} else {
d.Set("user_id", user.UserId)
d.Set("email", user.Email)
Expand All @@ -111,6 +116,8 @@ func resourceAuth0UserRead(d *schema.ResourceData, meta interface{}) error {
if len(user.Identities) > 0 {
d.Set("connection_type", user.Identities[0].Connection)
}

TfLogJson("[resourceAuth0UserRead]", user)
}

return nil
Expand All @@ -126,6 +133,8 @@ func resourceAuth0UserDelete(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("could not delete auth0 user: %v", err)
}

TfLogString("[resourceAuth0UserDelete]", d.Id())

return nil
}

Expand All @@ -142,14 +151,20 @@ func createUserUpdatesFromResourceData(d *schema.ResourceData) []*UserRequest {
userRequestA.Name = readStringFromResource(d, "name")
userRequestA.UserMetaData = readMapFromResource(d, "user_metadata")

TfLogJson("[createUserUpdatesFromResourceData-userRequestA]", userRequestA)

// Second contains only the password
userRequestB := &UserRequest{}
userRequestB.Password = readStringFromResource(d, "password")

TfLogJson("[createUserUpdatesFromResourceData-userRequestB]", userRequestB)

// Final updates the email_verified state
userRequestC := &UserRequest{}
userRequestC.EmailVerified = readBoolFromResource(d, "email_verified")

TfLogJson("[createUserUpdatesFromResourceData-userRequestC]", userRequestC)

return []*UserRequest{userRequestA, userRequestB, userRequestC}
}

Expand All @@ -164,5 +179,7 @@ func createUserRequestFromResourceData(d *schema.ResourceData) *UserRequest {
userRequest.UserMetaData = readMapFromResource(d, "user_metadata")
userRequest.EmailVerified = readBoolFromResource(d, "email_verified")

TfLogJson("[createUserRequestFromResourceData-userRequest]", userRequest)

return userRequest
}
5 changes: 3 additions & 2 deletions auth0/resource_auth0_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package auth0

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"testing"

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

func TestAccAuth0User(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion auth0/resource_reader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package auth0

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

func readStringFromResource(d *schema.ResourceData, key string) string {
if attr, ok := d.GetOk(key); ok {
Expand Down
Loading