Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Data Source Check For Resource #623

Draft
wants to merge 2 commits into
base: moduli-generate-datasources
Choose a base branch
from
Draft
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
25 changes: 25 additions & 0 deletions scripts/generate_data_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ func Main() error {
var rs []string
if r == "" {
for path := range swagger.Paths.Paths {
resourceName := strings.Split(path, "/")[2]
log.Printf("Resource: %s", resourceName)
if !resourceExist(resourceName) {
continue
}
if strings.HasSuffix(path, "{id}") {
rs = append(rs, strings.Split(path, "/")[2])
}
Expand Down Expand Up @@ -687,3 +692,23 @@ func write(data string, filename string) error {
f.Write([]byte(data))
return nil
}

// Check if terraform resource exists
func resourceExist(rs string) bool {
// Format resource properly to match the file name
name := strings.ReplaceAll(rs, "-", "_")

if !strings.HasPrefix(name, "credentials") {
name = strings.TrimSuffix(name, "s")
}

resourceFilePath := fmt.Sprintf("internal/provider/resource_%s*.go", name)

// Assuming the resource files are in the internal/provider directory
// Utilize glob as some resources are not matched 1-1 such as credential_stores data sources vs credential_store_static resource
matches, err := filepath.Glob(resourceFilePath)
if err != nil || len(matches) == 0 {
return false
}
return true
}
Loading