Skip to content

Commit

Permalink
Adding provider information to module dependencies (#275)
Browse files Browse the repository at this point in the history
* adding provider information to module dependencies

Signed-off-by: Diogenes Fernandes <[email protected]>

* cleanup

Signed-off-by: Diogenes Fernandes <[email protected]>

* remove fmt and change to test version constraint instead

Signed-off-by: Diogenes Fernandes <[email protected]>

* generating openAPI yml

Signed-off-by: Diogenes Fernandes <[email protected]>

---------

Signed-off-by: Diogenes Fernandes <[email protected]>
  • Loading branch information
diofeher authored Jan 17, 2025
1 parent f1d440f commit 964ac49
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
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
6 changes: 6 additions & 0 deletions backend/internal/server/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ definitions:
type: string
name:
type: string
version_constraint:
type: string
type: object
ModuleResource:
properties:
Expand Down Expand Up @@ -353,6 +355,10 @@ definitions:
additionalProperties:
$ref: '#/definitions/ModuleOutput'
type: object
provider_config:
additionalProperties:
$ref: '#/definitions/ModuleProviderConfigSchema'
type: object
resources:
items:
$ref: '#/definitions/ModuleResource'
Expand Down

0 comments on commit 964ac49

Please sign in to comment.