-
Notifications
You must be signed in to change notification settings - Fork 55
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
DXCDT-499: Add abstractions to fetch client resource data in tf cmd #794
Merged
+298
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"regexp" | ||
|
||
"github.com/auth0/go-auth0/management" | ||
|
||
"github.com/auth0/auth0-cli/internal/auth0" | ||
) | ||
|
||
type ( | ||
importDataList []importDataItem | ||
|
||
importDataItem struct { | ||
ResourceName string | ||
ImportID string | ||
} | ||
|
||
resourceDataFetcher interface { | ||
FetchData(ctx context.Context) (importDataList, error) | ||
} | ||
|
||
clientResourceFetcher struct { | ||
api *auth0.API | ||
} | ||
) | ||
|
||
func (f *clientResourceFetcher) FetchData(ctx context.Context) (importDataList, error) { | ||
var data importDataList | ||
|
||
var page int | ||
for { | ||
clients, err := f.api.Client.List( | ||
ctx, | ||
management.Page(page), | ||
management.Parameter("is_global", "false"), | ||
management.IncludeFields("client_id", "name"), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, client := range clients.Clients { | ||
data = append(data, importDataItem{ | ||
ResourceName: "auth0_client." + sanitizeResourceName(client.GetName()), | ||
ImportID: client.GetClientID(), | ||
}) | ||
} | ||
|
||
if !clients.HasNext() { | ||
break | ||
} | ||
|
||
page++ | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
// sanitizeResourceName will return a valid terraform resource name. | ||
// | ||
// A name must start with a letter or underscore and may | ||
// contain only letters, digits, underscores, and dashes. | ||
func sanitizeResourceName(name string) string { | ||
// Regular expression pattern to remove invalid characters. | ||
namePattern := "[^a-zA-Z0-9_-]+" | ||
re := regexp.MustCompile(namePattern) | ||
|
||
sanitizedName := re.ReplaceAllString(name, "") | ||
|
||
// Regular expression pattern to remove leading digits or dashes. | ||
namePattern = "^[0-9-]+" | ||
re = regexp.MustCompile(namePattern) | ||
|
||
sanitizedName = re.ReplaceAllString(sanitizedName, "") | ||
|
||
return sanitizedName | ||
} |
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,142 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/auth0/go-auth0/management" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/auth0/auth0-cli/internal/auth0" | ||
"github.com/auth0/auth0-cli/internal/auth0/mock" | ||
) | ||
|
||
func TestSanitizeResourceName(t *testing.T) { | ||
testCases := []struct { | ||
input string | ||
expected string | ||
}{ | ||
// Test cases with valid names | ||
{"ValidName123", "ValidName123"}, | ||
{"_Another_Valid-Name", "_Another_Valid-Name"}, | ||
{"name_with_123", "name_with_123"}, | ||
{"_start_with_underscore", "_start_with_underscore"}, | ||
|
||
// Test cases with invalid names to be sanitized | ||
{"Invalid@Name", "InvalidName"}, | ||
{"Invalid Name", "InvalidName"}, | ||
{"123StartWithNumber", "StartWithNumber"}, | ||
{"-StartWithDash", "StartWithDash"}, | ||
{"", ""}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.input, func(t *testing.T) { | ||
sanitized := sanitizeResourceName(testCase.input) | ||
assert.Equal(t, testCase.expected, sanitized) | ||
}) | ||
} | ||
} | ||
|
||
func TestClientResourceFetcher_FetchData(t *testing.T) { | ||
t.Run("it successfully retrieves client data", func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
clientAPI := mock.NewMockClientAPI(ctrl) | ||
clientAPI.EXPECT(). | ||
List(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). | ||
Return( | ||
&management.ClientList{ | ||
List: management.List{ | ||
Start: 0, | ||
Limit: 2, | ||
Total: 4, | ||
}, | ||
Clients: []*management.Client{ | ||
{ | ||
ClientID: auth0.String("clientID_1"), | ||
Name: auth0.String("My Test Client 1"), | ||
}, | ||
{ | ||
ClientID: auth0.String("clientID_2"), | ||
Name: auth0.String("My Test Client 2"), | ||
}, | ||
}, | ||
}, | ||
nil, | ||
) | ||
clientAPI.EXPECT(). | ||
List(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). | ||
Return( | ||
&management.ClientList{ | ||
List: management.List{ | ||
Start: 2, | ||
Limit: 4, | ||
Total: 4, | ||
}, | ||
Clients: []*management.Client{ | ||
{ | ||
ClientID: auth0.String("clientID_3"), | ||
Name: auth0.String("My Test Client 3"), | ||
}, | ||
{ | ||
ClientID: auth0.String("clientID_4"), | ||
Name: auth0.String("My Test Client 4"), | ||
}, | ||
}, | ||
}, | ||
nil, | ||
) | ||
|
||
fetcher := clientResourceFetcher{ | ||
api: &auth0.API{ | ||
Client: clientAPI, | ||
}, | ||
} | ||
|
||
expectedData := importDataList{ | ||
{ | ||
ResourceName: "auth0_client.MyTestClient1", | ||
ImportID: "clientID_1", | ||
}, | ||
{ | ||
ResourceName: "auth0_client.MyTestClient2", | ||
ImportID: "clientID_2", | ||
}, | ||
{ | ||
ResourceName: "auth0_client.MyTestClient3", | ||
ImportID: "clientID_3", | ||
}, | ||
{ | ||
ResourceName: "auth0_client.MyTestClient4", | ||
ImportID: "clientID_4", | ||
}, | ||
} | ||
|
||
data, err := fetcher.FetchData(context.Background()) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedData, data) | ||
}) | ||
|
||
t.Run("it returns an error if api call fails", func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
clientAPI := mock.NewMockClientAPI(ctrl) | ||
clientAPI.EXPECT(). | ||
List(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). | ||
Return(nil, fmt.Errorf("failed to list clients")) | ||
|
||
fetcher := clientResourceFetcher{ | ||
api: &auth0.API{ | ||
Client: clientAPI, | ||
}, | ||
} | ||
|
||
_, err := fetcher.FetchData(context.Background()) | ||
assert.EqualError(t, err, "failed to list clients") | ||
}) | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be preferable to match any leading character that is not a letter or underscore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How the sanitize works is that it matches invalid characters and removes them, so it's doing the opposite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it's only matching a subset of invalid characters, not all possible invalid characters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you're already removing all the other possible chars in the step above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic is split in 2 through 2 regexes, the first one removes all invalid characters, and the second one removes digits or - from the start of the string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation. My bad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, happy to clarify 👍🏻