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

DXCDT-509: Adding auth0_connections export support #797

Merged
merged 3 commits into from
Aug 10, 2023
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ vendor

# Binary output folder
out/

# Terraform export command artifacts
*.tf
3 changes: 3 additions & 0 deletions internal/cli/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (i *terraformInputs) parseResourceFetchers(api *auth0.API) []resourceDataFe
&clientResourceFetcher{
api: api,
},
&connectionResourceFetcher{
api: api,
},
}
}

Expand Down
35 changes: 35 additions & 0 deletions internal/cli/terraform_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type (
clientResourceFetcher struct {
api *auth0.API
}

connectionResourceFetcher struct {
api *auth0.API
}
)

func (f *clientResourceFetcher) FetchData(ctx context.Context) (importDataList, error) {
Expand Down Expand Up @@ -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
Expand Down
101 changes: 101 additions & 0 deletions internal/cli/terraform_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}