Merge pull request #790 from cloudskiff/fea/split_github_membership_supplier

split github membership supplier
main
Elie 2021-07-05 15:33:35 +02:00 committed by GitHub
commit b7d1d5dc74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 145 additions and 5515 deletions

View File

@ -0,0 +1,45 @@
package github
import (
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/github"
)
type GithubMembershipEnumerator struct {
Membership GithubRepository
factory resource.ResourceFactory
}
func NewGithubMembershipEnumerator(repo GithubRepository, factory resource.ResourceFactory) *GithubMembershipEnumerator {
return &GithubMembershipEnumerator{
Membership: repo,
factory: factory,
}
}
func (g *GithubMembershipEnumerator) SupportedType() resource.ResourceType {
return github.GithubMembershipResourceType
}
func (g *GithubMembershipEnumerator) Enumerate() ([]resource.Resource, error) {
ids, err := g.Membership.ListMembership()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, string(g.SupportedType()))
}
results := make([]resource.Resource, len(ids))
for _, id := range ids {
results = append(
results,
g.factory.CreateAbstractResource(
string(g.SupportedType()),
id,
map[string]interface{}{},
),
)
}
return results, err
}

View File

@ -1,57 +0,0 @@
package github
import (
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 GithubMembershipSupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
repository GithubRepository
runner *terraform.ParallelResourceReader
}
func NewGithubMembershipSupplier(provider *GithubTerraformProvider, repository GithubRepository, deserializer *resource.Deserializer) *GithubMembershipSupplier {
return &GithubMembershipSupplier{
provider,
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s GithubMembershipSupplier) Resources() ([]resource.Resource, error) {
resourceList, err := s.repository.ListMembership()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, resourcegithub.GithubMembershipResourceType)
}
for _, id := range resourceList {
id := id
s.runner.Run(func() (cty.Value, error) {
completeResource, err := s.reader.ReadResource(terraform.ReadResourceArgs{
Ty: resourcegithub.GithubMembershipResourceType,
ID: id,
})
if err != nil {
logrus.Warnf("Error reading %s[%s]: %+v", id, resourcegithub.GithubMembershipResourceType, err)
return cty.NilVal, err
}
return *completeResource, nil
})
}
results, err := s.runner.Wait()
if err != nil {
return nil, err
}
return s.deserializer.Deserialize(resourcegithub.GithubMembershipResourceType, results)
}

View File

@ -1,85 +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 TestGithubMembershipupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *MockGithubRepository)
err error
}{
{
test: "no members",
dirName: "github_membership_empty",
mocks: func(client *MockGithubRepository) {
client.On("ListMembership").Return([]string{}, nil)
},
err: nil,
},
{
test: "Multiple membership with admin and member roles",
dirName: "github_membership_multiple",
mocks: func(client *MockGithubRepository) {
client.On("ListMembership").Return([]string{
"driftctl-test:driftctl-acceptance-tester",
"driftctl-test:eliecharra",
}, 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(NewGithubMembershipSupplier(provider, &mockedRepo, deserializer))
}
t.Run(c.test, func(tt *testing.T) {
provider := dritftctlmocks.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.GITHUB), shouldUpdate)
s := &GithubMembershipSupplier{
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

@ -50,7 +50,9 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewGithubRepositoryEnumerator(repository, factory, provider.Config))
remoteLibrary.AddDetailsFetcher(github.GithubRepositoryResourceType, common.NewGenericDetailsFetcher(github.GithubRepositoryResourceType, provider, deserializer))
supplierLibrary.AddSupplier(NewGithubMembershipSupplier(provider, repository, deserializer))
remoteLibrary.AddEnumerator(NewGithubMembershipEnumerator(repository, factory))
remoteLibrary.AddDetailsFetcher(github.GithubMembershipResourceType, common.NewGenericDetailsFetcher(github.GithubMembershipResourceType, provider, deserializer))
supplierLibrary.AddSupplier(NewGithubTeamMembershipSupplier(provider, repository, deserializer))
supplierLibrary.AddSupplier(NewGithubBranchProtectionSupplier(provider, repository, deserializer))

View File

@ -0,0 +1,97 @@
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"
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 TestScanGithubMembership(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *github.MockGithubRepository)
err error
}{
{
test: "no members",
dirName: "github_membership_empty",
mocks: func(client *github.MockGithubRepository) {
client.On("ListMembership").Return([]string{}, nil)
},
err: nil,
},
{
test: "Multiple membership with admin and member roles",
dirName: "github_membership_multiple",
mocks: func(client *github.MockGithubRepository) {
client.On("ListMembership").Return([]string{
"driftctl-test:driftctl-acceptance-tester",
"driftctl-test:eliecharra",
}, 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 cases {
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.NewGithubMembershipEnumerator(repo, factory))
remoteLibrary.AddDetailsFetcher(githubres.GithubMembershipResourceType, common.NewGenericDetailsFetcher(githubres.GithubMembershipResourceType, 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.GithubMembershipResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}