2021-10-13 15:37:27 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-12-06 13:29:39 +00:00
|
|
|
remoteerror "github.com/snyk/driftctl/pkg/remote/error"
|
|
|
|
"github.com/snyk/driftctl/pkg/remote/google/repository"
|
|
|
|
"github.com/snyk/driftctl/pkg/resource"
|
|
|
|
"github.com/snyk/driftctl/pkg/resource/google"
|
2021-10-13 15:37:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type GoogleComputeInstanceGroupEnumerator struct {
|
|
|
|
repository repository.AssetRepository
|
|
|
|
factory resource.ResourceFactory
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGoogleComputeInstanceGroupEnumerator(repo repository.AssetRepository, factory resource.ResourceFactory) *GoogleComputeInstanceGroupEnumerator {
|
|
|
|
return &GoogleComputeInstanceGroupEnumerator{
|
|
|
|
repository: repo,
|
|
|
|
factory: factory,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GoogleComputeInstanceGroupEnumerator) SupportedType() resource.ResourceType {
|
|
|
|
return google.GoogleComputeInstanceGroupResourceType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GoogleComputeInstanceGroupEnumerator) Enumerate() ([]*resource.Resource, error) {
|
|
|
|
groups, err := e.repository.SearchAllInstanceGroups()
|
|
|
|
if err != nil {
|
|
|
|
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
|
|
|
|
}
|
|
|
|
|
|
|
|
results := make([]*resource.Resource, 0, len(groups))
|
|
|
|
for _, res := range groups {
|
|
|
|
splittedName := strings.Split(res.GetName(), "/")
|
|
|
|
if len(splittedName) != 9 {
|
|
|
|
logrus.WithField("name", res.GetName()).Error("Unable to decode project from instance group name")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
project := splittedName[4]
|
|
|
|
results = append(
|
|
|
|
results,
|
|
|
|
e.factory.CreateAbstractResource(
|
|
|
|
string(e.SupportedType()),
|
|
|
|
trimResourceName(res.GetName()),
|
|
|
|
map[string]interface{}{
|
2021-10-28 13:55:29 +00:00
|
|
|
"name": res.GetDisplayName(),
|
|
|
|
"project": project,
|
|
|
|
"location": res.GetLocation(),
|
2021-10-13 15:37:27 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, err
|
|
|
|
}
|