Skip to content

Commit

Permalink
DXCDT-360: Move organization resources to dedicated pkg (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught authored Feb 8, 2023
1 parent 758e4ba commit f11337f
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 186 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider
package organization

import (
"context"
Expand All @@ -13,7 +13,8 @@ import (
"github.com/auth0/terraform-provider-auth0/internal/value"
)

func newOrganization() *schema.Resource {
// NewResource will return a new auth0_organization resource.
func NewResource() *schema.Resource {
return &schema.Resource{
CreateContext: createOrganization,
ReadContext: readOrganization,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package provider
package organization

import (
"context"
"fmt"
"net/http"
"strings"

"github.com/auth0/go-auth0/management"
"github.com/hashicorp/go-multierror"
Expand All @@ -20,7 +19,8 @@ var (
errInvalidOrganizationConnectionIDFormat = fmt.Errorf("ID must be formated as <organizationID>:<connectionID>")
)

func newOrganizationConnection() *schema.Resource {
// NewConnectionResource will return a new auth0_organization_connection resource.
func NewConnectionResource() *schema.Resource {
return &schema.Resource{
Description: "With this resource, you can manage enabled connections on an organization.",
CreateContext: createOrganizationConnection,
Expand Down Expand Up @@ -63,35 +63,6 @@ func newOrganizationConnection() *schema.Resource {
}
}

func importOrganizationConnection(
_ context.Context,
data *schema.ResourceData,
_ interface{},
) ([]*schema.ResourceData, error) {
rawID := data.Id()
if rawID == "" {
return nil, errEmptyOrganizationConnectionID
}

if !strings.Contains(rawID, ":") {
return nil, errInvalidOrganizationConnectionIDFormat
}

idPair := strings.Split(rawID, ":")
if len(idPair) != 2 {
return nil, errInvalidOrganizationConnectionIDFormat
}

result := multierror.Append(
data.Set("organization_id", idPair[0]),
data.Set("connection_id", idPair[1]),
)

data.SetId(resource.UniqueId())

return []*schema.ResourceData{data}, result.ErrorOrNil()
}

func createOrganizationConnection(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*management.Management)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package provider
package organization_test

import (
"context"
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/stretchr/testify/assert"

"github.com/auth0/terraform-provider-auth0/internal/provider"
"github.com/auth0/terraform-provider-auth0/internal/recorder"
"github.com/auth0/terraform-provider-auth0/internal/template"
)
Expand Down Expand Up @@ -46,7 +43,7 @@ func TestAccOrganizationConnection(t *testing.T) {
httpRecorder := recorder.New(t)

resource.Test(t, resource.TestCase{
ProviderFactories: TestFactories(httpRecorder),
ProviderFactories: provider.TestFactories(httpRecorder),
Steps: []resource.TestStep{
{
Config: template.ParseTestName(TestAccOrganizationConnectionCreate, strings.ToLower(t.Name())),
Expand Down Expand Up @@ -95,54 +92,3 @@ func TestAccOrganizationConnection(t *testing.T) {
},
})
}

func TestImportOrganizationConnection(t *testing.T) {
var testCases = []struct {
testName string
givenID string
expectedOrganizationID string
expectedConnectionID string
expectedError error
}{
{
testName: "it correctly parses the resource ID",
givenID: "org_1234:conn_5678",
expectedOrganizationID: "org_1234",
expectedConnectionID: "conn_5678",
},
{
testName: "it fails when the given ID is empty",
givenID: "",
expectedError: fmt.Errorf("ID cannot be empty"),
},
{
testName: "it fails when the given ID does not have \":\" as a separator",
givenID: "org_1234conn_5678",
expectedError: fmt.Errorf("ID must be formated as <organizationID>:<connectionID>"),
},
{
testName: "it fails when the given ID has too many separators",
givenID: "org_1234:conn_5678:",
expectedError: fmt.Errorf("ID must be formated as <organizationID>:<connectionID>"),
},
}

for _, testCase := range testCases {
t.Run(testCase.testName, func(t *testing.T) {
data := schema.TestResourceDataRaw(t, newOrganizationConnection().Schema, nil)
data.SetId(testCase.givenID)

actualData, err := importOrganizationConnection(context.Background(), data, nil)

if testCase.expectedError != nil {
assert.EqualError(t, err, testCase.expectedError.Error())
assert.Nil(t, actualData)
return
}

assert.Equal(t, actualData[0].Get("organization_id").(string), testCase.expectedOrganizationID)
assert.Equal(t, actualData[0].Get("connection_id").(string), testCase.expectedConnectionID)
assert.NotEqual(t, actualData[0].Id(), testCase.givenID)
})
}
}
68 changes: 68 additions & 0 deletions internal/auth0/organization/resource_import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package organization

import (
"context"
"strings"

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

func importOrganizationConnection(
_ context.Context,
data *schema.ResourceData,
_ interface{},
) ([]*schema.ResourceData, error) {
rawID := data.Id()
if rawID == "" {
return nil, errEmptyOrganizationConnectionID
}

if !strings.Contains(rawID, ":") {
return nil, errInvalidOrganizationConnectionIDFormat
}

idPair := strings.Split(rawID, ":")
if len(idPair) != 2 {
return nil, errInvalidOrganizationConnectionIDFormat
}

result := multierror.Append(
data.Set("organization_id", idPair[0]),
data.Set("connection_id", idPair[1]),
)

data.SetId(resource.UniqueId())

return []*schema.ResourceData{data}, result.ErrorOrNil()
}

func importOrganizationMember(
_ context.Context,
data *schema.ResourceData,
_ interface{},
) ([]*schema.ResourceData, error) {
rawID := data.Id()
if rawID == "" {
return nil, errEmptyOrganizationMemberID
}

if !strings.Contains(rawID, ":") {
return nil, errInvalidOrganizationMemberIDFormat
}

idPair := strings.Split(rawID, ":")
if len(idPair) != 2 {
return nil, errInvalidOrganizationMemberIDFormat
}

result := multierror.Append(
data.Set("organization_id", idPair[0]),
data.Set("user_id", idPair[1]),
)

data.SetId(resource.UniqueId())

return []*schema.ResourceData{data}, result.ErrorOrNil()
}
112 changes: 112 additions & 0 deletions internal/auth0/organization/resource_import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package organization

import (
"context"
"fmt"
"testing"

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

func TestImportOrganizationConnection(t *testing.T) {
var testCases = []struct {
testName string
givenID string
expectedOrganizationID string
expectedConnectionID string
expectedError error
}{
{
testName: "it correctly parses the resource ID",
givenID: "org_1234:conn_5678",
expectedOrganizationID: "org_1234",
expectedConnectionID: "conn_5678",
},
{
testName: "it fails when the given ID is empty",
givenID: "",
expectedError: fmt.Errorf("ID cannot be empty"),
},
{
testName: "it fails when the given ID does not have \":\" as a separator",
givenID: "org_1234conn_5678",
expectedError: fmt.Errorf("ID must be formated as <organizationID>:<connectionID>"),
},
{
testName: "it fails when the given ID has too many separators",
givenID: "org_1234:conn_5678:",
expectedError: fmt.Errorf("ID must be formated as <organizationID>:<connectionID>"),
},
}

for _, testCase := range testCases {
t.Run(testCase.testName, func(t *testing.T) {
data := schema.TestResourceDataRaw(t, NewConnectionResource().Schema, nil)
data.SetId(testCase.givenID)

actualData, err := importOrganizationConnection(context.Background(), data, nil)

if testCase.expectedError != nil {
assert.EqualError(t, err, testCase.expectedError.Error())
assert.Nil(t, actualData)
return
}

assert.Equal(t, actualData[0].Get("organization_id").(string), testCase.expectedOrganizationID)
assert.Equal(t, actualData[0].Get("connection_id").(string), testCase.expectedConnectionID)
assert.NotEqual(t, actualData[0].Id(), testCase.givenID)
})
}
}

func TestImportOrganizationMember(t *testing.T) {
var testCases = []struct {
testName string
givenID string
expectedOrganizationID string
expectedUserID string
expectedError error
}{
{
testName: "it correctly parses the resource ID",
givenID: "org_1234:auth0|62d82",
expectedOrganizationID: "org_1234",
expectedUserID: "auth0|62d82",
},
{
testName: "it fails when the given ID is empty",
givenID: "",
expectedError: fmt.Errorf("ID cannot be empty"),
},
{
testName: "it fails when the given ID does not have \":\" as a separator",
givenID: "org_1234auth0|62d82",
expectedError: fmt.Errorf("ID must be formated as <organizationID>:<userID>"),
},
{
testName: "it fails when the given ID has too many separators",
givenID: "org_1234:auth0|62d82:",
expectedError: fmt.Errorf("ID must be formated as <organizationID>:<userID>"),
},
}

for _, testCase := range testCases {
t.Run(testCase.testName, func(t *testing.T) {
data := schema.TestResourceDataRaw(t, NewMemberResource().Schema, nil)
data.SetId(testCase.givenID)

actualData, err := importOrganizationMember(context.Background(), data, nil)

if testCase.expectedError != nil {
assert.EqualError(t, err, testCase.expectedError.Error())
assert.Nil(t, actualData)
return
}

assert.Equal(t, actualData[0].Get("organization_id").(string), testCase.expectedOrganizationID)
assert.Equal(t, actualData[0].Get("user_id").(string), testCase.expectedUserID)
assert.NotEqual(t, actualData[0].Id(), testCase.givenID)
})
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package provider
package organization

import (
"context"
"fmt"
"net/http"
"strings"

"github.com/auth0/go-auth0/management"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -21,7 +19,8 @@ var (
errInvalidOrganizationMemberIDFormat = fmt.Errorf("ID must be formated as <organizationID>:<userID>")
)

func newOrganizationMember() *schema.Resource {
// NewMemberResource will return a new auth0_organization_member resource.
func NewMemberResource() *schema.Resource {
return &schema.Resource{
Description: "This resource is used to manage the assignment of members and their roles within an organization.",
CreateContext: createOrganizationMember,
Expand Down Expand Up @@ -52,35 +51,6 @@ func newOrganizationMember() *schema.Resource {
}
}

func importOrganizationMember(
_ context.Context,
data *schema.ResourceData,
_ interface{},
) ([]*schema.ResourceData, error) {
rawID := data.Id()
if rawID == "" {
return nil, errEmptyOrganizationMemberID
}

if !strings.Contains(rawID, ":") {
return nil, errInvalidOrganizationMemberIDFormat
}

idPair := strings.Split(rawID, ":")
if len(idPair) != 2 {
return nil, errInvalidOrganizationMemberIDFormat
}

result := multierror.Append(
data.Set("organization_id", idPair[0]),
data.Set("user_id", idPair[1]),
)

data.SetId(resource.UniqueId())

return []*schema.ResourceData{data}, result.ErrorOrNil()
}

func createOrganizationMember(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

Expand Down
Loading

0 comments on commit f11337f

Please sign in to comment.