split github_team_supplier

main
Martin Guibert 2021-06-30 20:42:02 +02:00
parent 8fbc06b12c
commit d9d735615e
13 changed files with 168 additions and 5518 deletions

View File

@ -0,0 +1,50 @@
package github
import (
"fmt"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
tf "github.com/cloudskiff/driftctl/pkg/remote/terraform"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/github"
)
type GithubTeamEnumerator struct {
repository GithubRepository
factory resource.ResourceFactory
providerConfig tf.TerraformProviderConfig
}
func NewGithubTeamEnumerator(repo GithubRepository, factory resource.ResourceFactory, providerConfig tf.TerraformProviderConfig) *GithubTeamEnumerator {
return &GithubTeamEnumerator{
repository: repo,
factory: factory,
providerConfig: providerConfig,
}
}
func (g *GithubTeamEnumerator) SupportedType() resource.ResourceType {
return github.GithubTeamResourceType
}
func (g *GithubTeamEnumerator) Enumerate() ([]resource.Resource, error) {
resourceList, err := g.repository.ListTeams()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, string(g.SupportedType()))
}
results := make([]resource.Resource, len(resourceList))
for _, team := range resourceList {
results = append(
results,
g.factory.CreateAbstractResource(
string(g.SupportedType()),
fmt.Sprintf("%d", team.DatabaseId),
map[string]interface{}{},
),
)
}
return results, err
}

View File

@ -1,59 +0,0 @@
package github
import (
"fmt"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
resourcegithub "github.com/cloudskiff/driftctl/pkg/resource/github"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
)
type GithubTeamSupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
repository GithubRepository
runner *terraform.ParallelResourceReader
}
func NewGithubTeamSupplier(provider *GithubTerraformProvider, repository GithubRepository, deserializer *resource.Deserializer) *GithubTeamSupplier {
return &GithubTeamSupplier{
provider,
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s GithubTeamSupplier) Resources() ([]resource.Resource, error) {
resourceList, err := s.repository.ListTeams()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, resourcegithub.GithubTeamResourceType)
}
for _, team := range resourceList {
team := team
s.runner.Run(func() (cty.Value, error) {
completeResource, err := s.reader.ReadResource(terraform.ReadResourceArgs{
Ty: resourcegithub.GithubTeamResourceType,
ID: fmt.Sprintf("%d", team.DatabaseId),
})
if err != nil {
logrus.Warnf("Error reading %d[%s]: %+v", team.DatabaseId, resourcegithub.GithubTeamResourceType, err)
return cty.NilVal, err
}
return *completeResource, nil
})
}
results, err := s.runner.Wait()
if err != nil {
return nil, err
}
return s.deserializer.Deserialize(resourcegithub.GithubTeamResourceType, results)
}

View File

@ -1,86 +0,0 @@
package github
import (
"context"
"testing"
"github.com/cloudskiff/driftctl/pkg/parallel"
"github.com/cloudskiff/driftctl/pkg/resource"
resourcegithub "github.com/cloudskiff/driftctl/pkg/resource/github"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/goldenfile"
dritftctlmocks "github.com/cloudskiff/driftctl/test/mocks"
testresource "github.com/cloudskiff/driftctl/test/resource"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestGithubTeamSupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *MockGithubRepository)
err error
}{
{
test: "no github teams",
dirName: "github_teams_empty",
mocks: func(client *MockGithubRepository) {
client.On("ListTeams").Return([]Team{}, nil)
},
err: nil,
},
{
test: "Multiple github teams with parent",
dirName: "github_teams_multiple",
mocks: func(client *MockGithubRepository) {
client.On("ListTeams").Return([]Team{
{DatabaseId: 4556811}, // github_team.team1
{DatabaseId: 4556812}, // github_team.team2
{DatabaseId: 4556814}, // github_team.with_parent
}, nil)
},
err: nil,
},
}
for _, c := range cases {
shouldUpdate := c.dirName == *goldenfile.Update
providerLibrary := terraform.NewProviderLibrary()
supplierLibrary := resource.NewSupplierLibrary()
repo := testresource.InitFakeSchemaRepository(terraform.GITHUB, "4.4.0")
resourcegithub.InitResourcesMetadata(repo)
factory := terraform.NewTerraformResourceFactory(repo)
deserializer := resource.NewDeserializer(factory)
mockedRepo := MockGithubRepository{}
c.mocks(&mockedRepo)
if shouldUpdate {
provider, err := InitTestGithubProvider(providerLibrary)
if err != nil {
t.Fatal(err)
}
supplierLibrary.AddSupplier(NewGithubTeamSupplier(provider, &mockedRepo, deserializer))
}
t.Run(c.test, func(tt *testing.T) {
provider := dritftctlmocks.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.GITHUB), shouldUpdate)
s := &GithubTeamSupplier{
provider,
deserializer,
&mockedRepo,
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
}
got, err := s.Resources()
assert.Equal(tt, c.err, err)
mock.AssertExpectationsForObjects(tt)
test.CtyTestDiff(got, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}

View File

@ -44,8 +44,10 @@ func Init(version string, alerter *alerter.Alerter,
deserializer := resource.NewDeserializer(factory)
providerLibrary.AddProvider(terraform.GITHUB, provider)
remoteLibrary.AddEnumerator(NewGithubTeamEnumerator(repository, factory, provider.Config))
remoteLibrary.AddDetailsFetcher(github.GithubTeamResourceType, common.NewGenericDetailFetcher(github.GithubTeamResourceType, provider, deserializer))
supplierLibrary.AddSupplier(NewGithubRepositorySupplier(provider, repository, deserializer))
supplierLibrary.AddSupplier(NewGithubTeamSupplier(provider, repository, deserializer))
supplierLibrary.AddSupplier(NewGithubMembershipSupplier(provider, repository, deserializer))
supplierLibrary.AddSupplier(NewGithubTeamMembershipSupplier(provider, repository, deserializer))
supplierLibrary.AddSupplier(NewGithubBranchProtectionSupplier(provider, repository, deserializer))

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,102 @@
package remote
import (
"testing"
"github.com/cloudskiff/driftctl/mocks"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
"github.com/cloudskiff/driftctl/pkg/remote/common"
"github.com/cloudskiff/driftctl/pkg/remote/github"
tf "github.com/cloudskiff/driftctl/pkg/remote/terraform"
githubres "github.com/cloudskiff/driftctl/pkg/resource/github"
"github.com/cloudskiff/driftctl/pkg/terraform"
testresource "github.com/cloudskiff/driftctl/test/resource"
tftest "github.com/cloudskiff/driftctl/test/terraform"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/goldenfile"
"github.com/stretchr/testify/assert"
)
func TestScanGithubTeam(t *testing.T) {
tests := []struct {
test string
dirName string
mocks func(repository *github.MockGithubRepository)
err error
}{
{
test: "no github teams",
dirName: "github_teams_empty",
mocks: func(client *github.MockGithubRepository) {
client.On("ListTeams").Return([]github.Team{}, nil)
},
err: nil,
},
{
test: "Multiple github teams with parent",
dirName: "github_teams_multiple",
mocks: func(client *github.MockGithubRepository) {
client.On("ListTeams").Return([]github.Team{
{DatabaseId: 4556811}, // github_team.team1
{DatabaseId: 4556812}, // github_team.team2
{DatabaseId: 4556814}, // github_team.with_parent
}, nil)
},
err: nil,
},
}
schemaRepository := testresource.InitFakeSchemaRepository("github", "4.4.0")
githubres.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
deserializer := resource.NewDeserializer(factory)
alerter := &mocks.AlerterInterface{}
for _, c := range tests {
t.Run(c.test, func(tt *testing.T) {
shouldUpdate := c.dirName == *goldenfile.Update
scanOptions := ScannerOptions{Deep: true}
providerLibrary := terraform.NewProviderLibrary()
remoteLibrary := common.NewRemoteLibrary()
mockedRepo := github.MockGithubRepository{}
c.mocks(&mockedRepo)
var repo github.GithubRepository = &mockedRepo
realProvider, err := tftest.InitTestGithubProvider(providerLibrary, "4.4.0")
if err != nil {
t.Fatal(err)
}
provider := tftest.NewFakeTerraformProvider(realProvider)
provider.WithResponse(c.dirName)
if shouldUpdate {
err := realProvider.Init()
if err != nil {
t.Fatal(err)
}
provider.ShouldUpdate()
repo = github.NewGithubRepository(realProvider.GetConfig(), cache.New(0))
}
remoteLibrary.AddEnumerator(github.NewGithubTeamEnumerator(repo, factory, tf.TerraformProviderConfig{
Name: "test",
DefaultAlias: "eu-west-3",
}))
remoteLibrary.AddDetailsFetcher(githubres.GithubTeamResourceType, common.NewGenericDetailFetcher(githubres.GithubTeamResourceType, provider, deserializer))
s := NewScanner(nil, remoteLibrary, alerter, scanOptions)
got, err := s.Resources()
assert.Equal(tt, err, c.err)
if err != nil {
return
}
test.TestAgainstGoldenFile(got, githubres.GithubTeamResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}

View File

@ -5,6 +5,7 @@ import (
"github.com/cloudskiff/driftctl/pkg/output"
"github.com/cloudskiff/driftctl/pkg/remote/aws"
"github.com/cloudskiff/driftctl/pkg/remote/github"
"github.com/cloudskiff/driftctl/pkg/terraform"
)
@ -18,3 +19,15 @@ func InitTestAwsProvider(providerLibrary *terraform.ProviderLibrary, version str
providerLibrary.AddProvider(terraform.AWS, provider)
return provider, nil
}
func InitTestGithubProvider(providerLibrary *terraform.ProviderLibrary, version string) (*github.GithubTerraformProvider, error) {
progress := &output.MockProgress{}
progress.On("Inc").Maybe().Return()
provider, err := github.NewGithubTerraformProvider(version, progress, "")
if err != nil {
return nil, err
}
providerLibrary.AddProvider(terraform.GITHUB, provider)
return provider, nil
}