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

Adding provider information to module dependencies #275

Merged
merged 4 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 17 additions & 0 deletions backend/internal/moduleindex/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ func (g generator) extractModuleSchema(ctx context.Context, directory string, d
g.extractModuleVariables(rootModuleSchema, &d.BaseDetails)
g.extractModuleOutputs(rootModuleSchema, &d.BaseDetails)
g.extractModuleDependencies(rootModuleSchema, d)
g.extractProviderDependencies(moduleSchema, d)
g.extractModuleResources(rootModuleSchema, d)
return nil
}
Expand Down Expand Up @@ -814,6 +815,22 @@ func (g generator) extractModuleDependencies(moduleSchema moduleschema.ModuleSch
d.Dependencies = result
}

func (g generator) extractProviderDependencies(schema moduleschema.Schema, d *Details) {
result := make([]ProviderDependency, len(schema.ProviderConfig))
i := 0
for providerCallName, providerCall := range schema.ProviderConfig {
result[i] = ProviderDependency{
Name: providerCallName,
FullName: providerCall.FullName,
VersionConstraint: providerCall.VersionConstraint,
Alias: "",
}
i++
}

d.Providers = result
}

func (g generator) extractModuleResources(moduleSchema moduleschema.ModuleSchema, d *Details) {
result := make([]Resource, len(moduleSchema.Resources))
for i, resource := range moduleSchema.Resources {
Expand Down
8 changes: 8 additions & 0 deletions backend/internal/moduleindex/moduleschema/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,12 @@ func TestModuleCall(t *testing.T) {
if metadata.RootModule.ModuleCalls["foo"].Source != "./module" {
t.Fatalf("Incorrect module source: %s", metadata.RootModule.ModuleCalls["foo"].Source)
}

_, ok = metadata.ProviderConfig["opentofu"]
if !ok {
t.Fatalf("Provider call not found.")
}
if metadata.ProviderConfig["opentofu"].VersionConstraint != "1.6.0" {
t.Fatalf("Incorrect provider name: %s != %s", metadata.ProviderConfig["opentofu"].VersionConstraint, "1.6.0")
}
}
14 changes: 8 additions & 6 deletions backend/internal/moduleindex/moduleschema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ type Schema struct {

// swagger:model ModuleProviderConfigSchema
type ProviderConfigSchema struct {
Name string `json:"name"`
FullName string `json:"full_name"`
Name string `json:"name"`
FullName string `json:"full_name"`
VersionConstraint string `json:"version_constraint"`
}

// swagger:model ModuleSchema
type ModuleSchema struct {
Resources []Resource `json:"resources,omitempty"`
ModuleCalls map[string]ModuleCall `json:"module_calls,omitempty"`
Variables map[string]Variable `json:"variables,omitempty"`
Outputs map[string]Output `json:"outputs,omitempty"`
Resources []Resource `json:"resources,omitempty"`
ModuleCalls map[string]ModuleCall `json:"module_calls,omitempty"`
ProviderConfig map[string]ProviderConfigSchema `json:"provider_config,omitempty"`
Variables map[string]Variable `json:"variables,omitempty"`
Outputs map[string]Output `json:"outputs,omitempty"`
}

// swagger:model ModuleResource
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
required_providers {
opentofu = {
source = "opentofu/opentofu"
version = "1.6.0"
}

ad = {
source = "opentofu/ad"
version = "0.5.0"
}
}
}
1 change: 1 addition & 0 deletions backend/internal/moduleindex/moduleschema/tofu.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (e *externalTofuExtractor) Extract(ctx context.Context, moduleDirectory str
}
}
}

var result Schema
outputBytes := output.Bytes()
if err := json.Unmarshal(outputBytes, &result); err != nil {
Expand Down