From ede9a8efc1ee5e8d9c35c5a344b6bc21c0419d69 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Tue, 8 Aug 2023 15:57:16 -0400 Subject: [PATCH 1/2] Implementing connections according to existing pattern --- .gitignore | 3 +++ internal/cli/terraform.go | 3 +++ internal/cli/terraform_fetcher.go | 35 +++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/.gitignore b/.gitignore index 4cbf3db0e..a9fc0216b 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,6 @@ vendor # Binary output folder out/ + +# Terraform export command artifacts +*.tf \ No newline at end of file diff --git a/internal/cli/terraform.go b/internal/cli/terraform.go index 9fbd087a5..2978af99a 100644 --- a/internal/cli/terraform.go +++ b/internal/cli/terraform.go @@ -38,6 +38,9 @@ func (i *terraformInputs) parseResourceFetchers(api *auth0.API) []resourceDataFe &clientResourceFetcher{ api: api, }, + &connectionResourceFetcher{ + api: api, + }, } } diff --git a/internal/cli/terraform_fetcher.go b/internal/cli/terraform_fetcher.go index 55d74af83..be8b8e87b 100644 --- a/internal/cli/terraform_fetcher.go +++ b/internal/cli/terraform_fetcher.go @@ -24,6 +24,10 @@ type ( clientResourceFetcher struct { api *auth0.API } + + connectionResourceFetcher struct { + api *auth0.API + } ) func (f *clientResourceFetcher) FetchData(ctx context.Context) (importDataList, error) { @@ -58,6 +62,37 @@ func (f *clientResourceFetcher) FetchData(ctx context.Context) (importDataList, return data, nil } +func (f *connectionResourceFetcher) FetchData(ctx context.Context) (importDataList, error) { + var data importDataList + + var page int + for { + connections, err := f.api.Connection.List( + ctx, + management.Page(page), + management.IncludeFields("id", "name"), + ) + if err != nil { + return nil, err + } + + for _, connection := range connections.Connections { + data = append(data, importDataItem{ + ResourceName: "auth0_connection." + sanitizeResourceName(connection.GetName()), + ImportID: connection.GetID(), + }) + } + + if !connections.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 From e08a76133e687606119286eb1609809e33fce0be Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Wed, 9 Aug 2023 16:53:33 -0400 Subject: [PATCH 2/2] Adding unit tests --- internal/cli/terraform_fetcher_test.go | 101 +++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/internal/cli/terraform_fetcher_test.go b/internal/cli/terraform_fetcher_test.go index c17732df4..a0969f0b0 100644 --- a/internal/cli/terraform_fetcher_test.go +++ b/internal/cli/terraform_fetcher_test.go @@ -140,3 +140,104 @@ func TestClientResourceFetcher_FetchData(t *testing.T) { assert.EqualError(t, err, "failed to list clients") }) } + +func TestConnectionResourceFetcher_FetchData(t *testing.T) { + t.Run("it successfully retrieves connections data", func(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + connAPI := mock.NewMockConnectionAPI(ctrl) + connAPI.EXPECT(). + List(gomock.Any(), gomock.Any(), gomock.Any()). + Return( + &management.ConnectionList{ + List: management.List{ + Start: 0, + Limit: 2, + Total: 4, + }, + Connections: []*management.Connection{ + { + ID: auth0.String("con_id1"), + Name: auth0.String("Connection 1"), + }, + { + ID: auth0.String("con_id2"), + Name: auth0.String("Connection 2"), + }, + }, + }, + nil, + ) + connAPI.EXPECT(). + List(gomock.Any(), gomock.Any(), gomock.Any()). + Return( + &management.ConnectionList{ + List: management.List{ + Start: 2, + Limit: 4, + Total: 4, + }, + Connections: []*management.Connection{ + { + ID: auth0.String("con_id3"), + Name: auth0.String("Connection 3"), + }, + { + ID: auth0.String("con_id4"), + Name: auth0.String("Connection 4"), + }, + }, + }, + nil, + ) + + fetcher := connectionResourceFetcher{ + api: &auth0.API{ + Connection: connAPI, + }, + } + + expectedData := importDataList{ + { + ResourceName: "auth0_connection.Connection1", + ImportID: "con_id1", + }, + { + ResourceName: "auth0_connection.Connection2", + ImportID: "con_id2", + }, + { + ResourceName: "auth0_connection.Connection3", + ImportID: "con_id3", + }, + { + ResourceName: "auth0_connection.Connection4", + ImportID: "con_id4", + }, + } + + 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() + + connAPI := mock.NewMockConnectionAPI(ctrl) + connAPI.EXPECT(). + List(gomock.Any(), gomock.Any(), gomock.Any()). + Return(nil, fmt.Errorf("failed to list connections")) + + fetcher := connectionResourceFetcher{ + api: &auth0.API{ + Connection: connAPI, + }, + } + + _, err := fetcher.FetchData(context.Background()) + assert.EqualError(t, err, "failed to list connections") + }) +}