-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DXCDT-360: Move organization resources to dedicated pkg (#472)
- Loading branch information
Showing
9 changed files
with
208 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.