From 8983521994e7460dae8f180a65c53ba55486ad01 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Wed, 6 Sep 2023 14:41:41 -0400 Subject: [PATCH] DXCDT-524: `auth0_organization_connections` resource export support (#823) * Adding organization connections resource export support * Fixing unit test --------- Co-authored-by: Will Vedder --- internal/auth/scopes.go | 2 +- internal/auth0/mock/organization_mock.go | 20 ++++++++ internal/auth0/organization.go | 5 ++ internal/cli/terraform_fetcher.go | 16 ++++++- internal/cli/terraform_fetcher_test.go | 60 +++++++++++++++++++++++- 5 files changed, 98 insertions(+), 5 deletions(-) diff --git a/internal/auth/scopes.go b/internal/auth/scopes.go index 1705dcf8a..f6f6d55c2 100644 --- a/internal/auth/scopes.go +++ b/internal/auth/scopes.go @@ -17,7 +17,7 @@ var RequiredScopes = []string{ "read:anomaly_blocks", "delete:anomaly_blocks", "create:log_streams", "delete:log_streams", "read:log_streams", "update:log_streams", "create:actions", "delete:actions", "read:actions", "update:actions", - "create:organizations", "delete:organizations", "read:organizations", "update:organizations", "read:organization_members", "read:organization_member_roles", + "create:organizations", "delete:organizations", "read:organizations", "update:organizations", "read:organization_members", "read:organization_member_roles", "read:organization_connections", "read:prompts", "update:prompts", "read:attack_protection", "update:attack_protection", } diff --git a/internal/auth0/mock/organization_mock.go b/internal/auth0/mock/organization_mock.go index 632f0ee98..cca000203 100644 --- a/internal/auth0/mock/organization_mock.go +++ b/internal/auth0/mock/organization_mock.go @@ -171,3 +171,23 @@ func (mr *MockOrganizationAPIMockRecorder) Update(ctx, id, o interface{}, opts . varargs := append([]interface{}{ctx, id, o}, opts...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockOrganizationAPI)(nil).Update), varargs...) } + +// Connections mocks base method. +func (m *MockOrganizationAPI) Connections(ctx context.Context, id string, opts ...management.RequestOption) (*management.OrganizationConnectionList, error) { + m.ctrl.T.Helper() + varargs := []interface{}{ctx, id} + for _, a := range opts { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "Connections", varargs...) + ret0, _ := ret[0].(*management.OrganizationConnectionList) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Connections indicates an expected call of Connections. +func (mr *MockOrganizationAPIMockRecorder) Connections(ctx, id interface{}, opts ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{ctx, id}, opts...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Connections", reflect.TypeOf((*MockOrganizationAPI)(nil).Connections), varargs...) +} \ No newline at end of file diff --git a/internal/auth0/organization.go b/internal/auth0/organization.go index 1c8e2370f..4f634d8f7 100644 --- a/internal/auth0/organization.go +++ b/internal/auth0/organization.go @@ -43,4 +43,9 @@ type OrganizationAPI interface { // // See: https://auth0.com/docs/api/management/v2#!/Organizations/get_organization_member_roles MemberRoles(ctx context.Context, id string, userID string, opts ...management.RequestOption) (r *management.OrganizationMemberRoleList, err error) + + // Connections retrieves connections enabled for an organization. + // + // See: https://auth0.com/docs/api/management/v2/#!/Organizations/get_enabled_connections + Connections(ctx context.Context, id string, opts ...management.RequestOption) (c *management.OrganizationConnectionList, err error) } diff --git a/internal/cli/terraform_fetcher.go b/internal/cli/terraform_fetcher.go index e2ec4599a..2c25676ac 100644 --- a/internal/cli/terraform_fetcher.go +++ b/internal/cli/terraform_fetcher.go @@ -276,10 +276,22 @@ func (f *organizationResourceFetcher) FetchData(ctx context.Context) (importData } for _, org := range orgs { + organization := org.(*management.Organization) data = append(data, importDataItem{ - ResourceName: "auth0_organization." + sanitizeResourceName(org.(*management.Organization).GetName()), - ImportID: org.(*management.Organization).GetID(), + ResourceName: "auth0_organization." + sanitizeResourceName(organization.GetName()), + ImportID: organization.GetID(), }) + + conns, err := f.api.Organization.Connections(ctx, organization.GetID()) + if err != nil { + return data, err + } + if len(conns.OrganizationConnections) > 0 { + data = append(data, importDataItem{ + ResourceName: "auth0_organization_connections." + sanitizeResourceName(organization.GetName()), + ImportID: organization.GetID(), + }) + } } return data, nil diff --git a/internal/cli/terraform_fetcher_test.go b/internal/cli/terraform_fetcher_test.go index 07f8ef746..50a68ee6a 100644 --- a/internal/cli/terraform_fetcher_test.go +++ b/internal/cli/terraform_fetcher_test.go @@ -668,6 +668,30 @@ func TestOrganizationResourceFetcher_FetchData(t *testing.T) { }, nil, ) + orgAPI.EXPECT(). + Connections(gomock.Any(), gomock.Any(), gomock.Any()). + Return( + &management.OrganizationConnectionList{ + OrganizationConnections: []*management.OrganizationConnection{ + { + ConnectionID: auth0.String("conn-1"), + }, + }, + }, + nil, + ) + orgAPI.EXPECT(). + Connections(gomock.Any(), gomock.Any(), gomock.Any()). + Return( + &management.OrganizationConnectionList{ + OrganizationConnections: []*management.OrganizationConnection{ + { + ConnectionID: auth0.String("conn-1"), + }, + }, + }, + nil, + ) orgAPI.EXPECT(). List(gomock.Any(), gomock.Any(), gomock.Any()). Return( @@ -684,12 +708,32 @@ func TestOrganizationResourceFetcher_FetchData(t *testing.T) { }, { ID: auth0.String("org_4"), - Name: auth0.String("Organization 4"), + Name: auth0.String("Organization 4 - NO CONNECTIONS!"), + }, + }, + }, + nil, + ) + orgAPI.EXPECT(). + Connections(gomock.Any(), gomock.Any(), gomock.Any()). + Return( + &management.OrganizationConnectionList{ + OrganizationConnections: []*management.OrganizationConnection{ + { + ConnectionID: auth0.String("conn-1"), }, }, }, nil, ) + orgAPI.EXPECT(). + Connections(gomock.Any(), gomock.Any(), gomock.Any()). + Return( + &management.OrganizationConnectionList{ + OrganizationConnections: []*management.OrganizationConnection{}, + }, + nil, + ) fetcher := organizationResourceFetcher{ api: &auth0.API{ @@ -702,16 +746,28 @@ func TestOrganizationResourceFetcher_FetchData(t *testing.T) { ResourceName: "auth0_organization.Organization1", ImportID: "org_1", }, + { + ResourceName: "auth0_organization_connections.Organization1", + ImportID: "org_1", + }, { ResourceName: "auth0_organization.Organization2", ImportID: "org_2", }, + { + ResourceName: "auth0_organization_connections.Organization2", + ImportID: "org_2", + }, { ResourceName: "auth0_organization.Organization3", ImportID: "org_3", }, { - ResourceName: "auth0_organization.Organization4", + ResourceName: "auth0_organization_connections.Organization3", + ImportID: "org_3", + }, + { + ResourceName: "auth0_organization.Organization4-NOCONNECTIONS", ImportID: "org_4", }, }