Skip to content
Open
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
16 changes: 8 additions & 8 deletions mmv1/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2197,23 +2197,23 @@ func (r Resource) TestSampleSetUp(sysfs fs.FS) {
}
}

// TestServiceDependencies returns a map of service names to import aliases that are required
// TestDependencies returns a map of service names to import aliases that are required
// by this resource's samples.
func (r Resource) TestServiceDependencies() map[string]string {
func (r Resource) TestDependencies() map[string]string {
deps := map[string]string{}
for _, s := range r.TestSamples() {
for service, alias := range s.TestServiceDependencies(r.Runtime.ResourcePrefixServiceMap) {
if depsAlias, ok := deps[service]; ok && alias != depsAlias {
for pkg, alias := range s.TestDependencies(r.Runtime.ResourcePrefixPkgMap) {
if depsAlias, ok := deps[pkg]; ok && alias != depsAlias {
if (alias == "_" && depsAlias == "") || (alias == "" && depsAlias == "_") {
deps[service] = ""
deps[pkg] = ""
continue
}
log.Fatalf("Conflicting aliases (%s vs %s) for service dependency %s for resource %s", depsAlias, alias, service, r.ApiName)
log.Fatalf("Conflicting aliases (%s vs %s) for pkg dependency %s for resource %s", depsAlias, alias, pkg, r.ApiName)
}
deps[service] = alias
deps[pkg] = alias
}
}
delete(deps, strings.ToLower(r.ProductMetadata.Name))
delete(deps, "services/"+strings.ToLower(r.ProductMetadata.Name))
return deps
}

Expand Down
16 changes: 8 additions & 8 deletions mmv1/api/resource/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,23 @@ func (s *Sample) TestSteps() []*Step {
})
}

// TestServiceDependencies returns a map of service names to import aliases that are required
// TestDependencies returns a map of service names to import aliases that are required
// by this sample's steps.
func (s *Sample) TestServiceDependencies(resourcePrefixServiceMap map[string]string) map[string]string {
func (s *Sample) TestDependencies(resourcePrefixPkgMap map[string]string) map[string]string {
deps := map[string]string{}
if len(s.BootstrapIam) > 0 {
deps["resourcemanager"] = ""
deps["services/resourcemanager"] = ""
}
for _, step := range s.TestSteps() {
for service, alias := range step.TestServiceDependencies(resourcePrefixServiceMap) {
if depsAlias, ok := deps[service]; ok && alias != depsAlias {
for pkg, alias := range step.TestDependencies(resourcePrefixPkgMap) {
if depsAlias, ok := deps[pkg]; ok && alias != depsAlias {
if (alias == "_" && depsAlias == "") || (alias == "" && depsAlias == "_") {
deps[service] = ""
deps[pkg] = ""
continue
}
log.Fatalf("Conflicting aliases (%s vs %s) for service dependency %s for sample %s", depsAlias, alias, service, s.Name)
log.Fatalf("Conflicting aliases (%s vs %s) for pkg dependency %s for sample %s", depsAlias, alias, pkg, s.Name)
}
deps[service] = alias
deps[pkg] = alias
}
}
return deps
Expand Down
18 changes: 9 additions & 9 deletions mmv1/api/resource/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/resource"
)

func TestSample_TestServiceDependencies(t *testing.T) {
func TestSample_TestDependencies(t *testing.T) {
cases := []struct {
name string
sample resource.Sample
Expand Down Expand Up @@ -40,7 +40,7 @@ func TestSample_TestServiceDependencies(t *testing.T) {
},
resourcePrefixServiceMap: map[string]string{},
want: map[string]string{
"resourcemanager": "",
"services/resourcemanager": "",
},
},
{
Expand All @@ -63,7 +63,7 @@ func TestSample_TestServiceDependencies(t *testing.T) {
},
resourcePrefixServiceMap: map[string]string{},
want: map[string]string{
"compute": "",
"services/compute": "",
},
},
{
Expand All @@ -83,10 +83,10 @@ func TestSample_TestServiceDependencies(t *testing.T) {
},
},
resourcePrefixServiceMap: map[string]string{
"google_compute_": "compute",
"google_compute_": "services/compute",
},
want: map[string]string{
"compute": "",
"services/compute": "",
},
},
{
Expand All @@ -106,19 +106,19 @@ func TestSample_TestServiceDependencies(t *testing.T) {
},
},
resourcePrefixServiceMap: map[string]string{
"google_project": "resourcemanager",
"google_project": "services/resourcemanager",
},
want: map[string]string{
"resourcemanager": "",
"services/resourcemanager": "",
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := tc.sample.TestServiceDependencies(tc.resourcePrefixServiceMap)
got := tc.sample.TestDependencies(tc.resourcePrefixServiceMap)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("TestServiceDependencies() mismatch (-want +got:\n%s", diff)
t.Errorf("TestDependencies() mismatch (-want +got:\n%s", diff)
}
})
}
Expand Down
20 changes: 10 additions & 10 deletions mmv1/api/resource/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/golang/glog"
)

var hclResourceRegexp = regexp.MustCompile(`resource "(?P<resource>google_[^"]+)"`)
var hclResourceRegexp = regexp.MustCompile(`(?:resource|data|list|ephemeral) "(?P<resource>google_[^"]+)"`)

type Step struct {
Name string `yaml:"name,omitempty"`
Expand Down Expand Up @@ -124,19 +124,19 @@ func (s *Step) TestStepSlug(productName, resourceName string) string {
return ret
}

// TestServiceDependencies returns a map of service names to import aliases that are required
// TestDependencies returns a map of service names to import aliases that are required
// by this step.
func (s *Step) TestServiceDependencies(resourcePrefixServiceMap map[string]string) map[string]string {
func (s *Step) TestDependencies(resourcePrefixPkgMap map[string]string) map[string]string {
deps := map[string]string{}
for _, val := range s.TestContextVars {
if strings.HasPrefix(val, "compute.") {
deps["compute"] = ""
deps["services/compute"] = ""
}
if strings.HasPrefix(val, "kms.") {
deps["kms"] = ""
deps["services/kms"] = ""
}
if strings.HasPrefix(val, "servicenetworking.") {
deps["servicenetworking"] = ""
deps["services/servicenetworking"] = ""
}
}
matches := hclResourceRegexp.FindAllStringSubmatch(s.TestHCLText, -1)
Expand All @@ -147,15 +147,15 @@ func (s *Step) TestServiceDependencies(resourcePrefixServiceMap map[string]strin

for r, _ := range resources {
longestPrefix := ""
for prefix, _ := range resourcePrefixServiceMap {
for prefix, _ := range resourcePrefixPkgMap {
if strings.HasPrefix(r, prefix) && len(prefix) > len(longestPrefix) {
longestPrefix = prefix
}
}
if longestPrefix != "" {
service := resourcePrefixServiceMap[longestPrefix]
if _, ok := deps[service]; !ok {
deps[service] = "_"
pkg := resourcePrefixPkgMap[longestPrefix]
if _, ok := deps[pkg]; !ok {
deps[pkg] = "_"
}
}
}
Expand Down
32 changes: 16 additions & 16 deletions mmv1/api/resource/step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/resource"
)

func TestStep_TestServiceDependencies(t *testing.T) {
func TestStep_TestDependencies(t *testing.T) {
cases := []struct {
name string
step resource.Step
Expand All @@ -34,7 +34,7 @@ func TestStep_TestServiceDependencies(t *testing.T) {
},
resourcePrefixServiceMap: map[string]string{},
want: map[string]string{
"compute": "",
"services/compute": "",
},
},
{
Expand All @@ -47,7 +47,7 @@ func TestStep_TestServiceDependencies(t *testing.T) {
},
resourcePrefixServiceMap: map[string]string{},
want: map[string]string{
"kms": "",
"services/kms": "",
},
},
{
Expand All @@ -60,7 +60,7 @@ func TestStep_TestServiceDependencies(t *testing.T) {
},
resourcePrefixServiceMap: map[string]string{},
want: map[string]string{
"servicenetworking": "",
"services/servicenetworking": "",
},
},
{
Expand All @@ -83,8 +83,8 @@ resource "google_kms_crypto_key" "foobar" {
TestHCLText: "",
},
resourcePrefixServiceMap: map[string]string{
"google_compute_": "compute",
"google_kms_": "kms",
"google_compute_": "services/compute",
"google_kms_": "services/kms",
},
want: map[string]string{},
},
Expand All @@ -99,12 +99,12 @@ resource "google_kms_crypto_key" "foobar" {
}`,
},
resourcePrefixServiceMap: map[string]string{
"google_compute_": "compute",
"google_kms_": "kms",
"google_compute_": "services/compute",
"google_kms_": "services/kms",
},
want: map[string]string{
"compute": "_",
"kms": "_",
"services/compute": "_",
"services/kms": "_",
},
},
{
Expand All @@ -121,21 +121,21 @@ resource "google_kms_crypto_key" "foobar" {
}`,
},
resourcePrefixServiceMap: map[string]string{
"google_compute_": "compute",
"google_kms_": "kms",
"google_compute_": "services/compute",
"google_kms_": "services/kms",
},
want: map[string]string{
"compute": "",
"kms": "",
"services/compute": "",
"services/kms": "",
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := tc.step.TestServiceDependencies(tc.resourcePrefixServiceMap)
got := tc.step.TestDependencies(tc.resourcePrefixServiceMap)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("TestServiceDependencies() mismatch (-want +got:\n%s", diff)
t.Errorf("TestDependencies() mismatch (-want +got:\n%s", diff)
}
})
}
Expand Down
20 changes: 10 additions & 10 deletions mmv1/api/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ func TestResourceAddExtraFields(t *testing.T) {
})
}

func TestSample_TestServiceDependencies(t *testing.T) {
func TestResource_TestDependencies(t *testing.T) {
cases := []struct {
name string
resource api.Resource
Expand All @@ -778,7 +778,7 @@ func TestSample_TestServiceDependencies(t *testing.T) {
},
},
Runtime: api.Runtime{
ResourcePrefixServiceMap: map[string]string{},
ResourcePrefixPkgMap: map[string]string{},
},
},
want: map[string]string{},
Expand Down Expand Up @@ -810,11 +810,11 @@ func TestSample_TestServiceDependencies(t *testing.T) {
},
},
Runtime: api.Runtime{
ResourcePrefixServiceMap: map[string]string{},
ResourcePrefixPkgMap: map[string]string{},
},
},
want: map[string]string{
"compute": "",
"services/compute": "",
},
},
{
Expand Down Expand Up @@ -842,13 +842,13 @@ func TestSample_TestServiceDependencies(t *testing.T) {
},
},
Runtime: api.Runtime{
ResourcePrefixServiceMap: map[string]string{
"google_compute_": "compute",
ResourcePrefixPkgMap: map[string]string{
"google_compute_": "services/compute",
},
},
},
want: map[string]string{
"compute": "",
"services/compute": "",
},
},
{
Expand All @@ -868,7 +868,7 @@ func TestSample_TestServiceDependencies(t *testing.T) {
},
},
Runtime: api.Runtime{
ResourcePrefixServiceMap: map[string]string{},
ResourcePrefixPkgMap: map[string]string{},
},
},
want: map[string]string{},
Expand All @@ -877,9 +877,9 @@ func TestSample_TestServiceDependencies(t *testing.T) {
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := tc.resource.TestServiceDependencies()
got := tc.resource.TestDependencies()
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("TestServiceDependencies() mismatch (-want +got:\n%s", diff)
t.Errorf("TestDependencies() mismatch (-want +got:\n%s", diff)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions mmv1/api/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package api

// Runtime holds metadata about the current generation runtime.
type Runtime struct {
// ResourcePrefixServiceMap contains entries mapping product resource prefixes (like google_compute_) to
// ResourcePrefixPkgMap contains entries mapping product resource prefixes (like google_compute_) to
// the service package that the resource is in.
ResourcePrefixServiceMap map[string]string
ResourcePrefixPkgMap map[string]string
}
Loading
Loading