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

find fastly service revursively in terraform planned JSON #375

Merged
merged 1 commit into from
Oct 21, 2024
Merged
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
87 changes: 44 additions & 43 deletions terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ type TerraformPlannedResource struct {
Values json.RawMessage `json:"values"`
}

type TerraformModule struct {
Resources []*TerraformPlannedResource `json:"resources"`
ChildModules []*TerraformModule `json:"child_modules"`
}

type TerraformPlannedInput struct {
PlannedValues *struct {
RootModule *struct {
Resources []*TerraformPlannedResource `json:"resources"`
ChildModules []*struct {
Resources []*TerraformPlannedResource `json:"resources"`
} `json:"child_modules"`
} `json:"root_module"`
RootModule *TerraformModule `json:"root_module"`
} `json:"planned_values"`
}

Expand All @@ -135,58 +135,59 @@ func UnmarshalTerraformPlannedInput(buf []byte) ([]*FastlyService, error) {
return nil, errors.New(`Input does not seem to terraform planned JSON: "root_module" field does not exist`)
}

var services []*FastlyService
var serviceValues *FastlyServiceValues
// Case: service is declared in root module
if len(root.PlannedValues.RootModule.Resources) > 0 {
for _, v := range root.PlannedValues.RootModule.Resources {
if !isFastlyVCLServiceResource(v) {
continue
}

if err := json.Unmarshal(v.Values, &serviceValues); err != nil {
return nil, errors.Wrap(err, "Failed to unmarshal values")
}
services, err := findFastlyServicesInTerraformModule(root.PlannedValues.RootModule)
if err != nil {
return nil, errors.WithStack(err)
}

services = append(services, &FastlyService{
Name: serviceValues.Name,
Vcls: serviceValues.Vcl,
Acls: serviceValues.Acl,
Backends: serviceValues.Backend,
Dictionaries: serviceValues.Dictionary,
Directors: serviceValues.Director,
Snippets: serviceValues.Snippets,
LoggingEndpoints: factoryLoggingEndpoints(serviceValues),
})
}
if len(services) == 0 {
return nil, errors.New(`Fastly service does not exist. Did you plan with fastly terraform provider?`)
}

// Case: service is declared in child module
for _, v := range root.PlannedValues.RootModule.ChildModules {
for _, v := range v.Resources {
return services, nil
}

func findFastlyServicesInTerraformModule(mod *TerraformModule) ([]*FastlyService, error) {
var services []*FastlyService

// Find services in module resources
if len(mod.Resources) > 0 {
// v is *TerraformPlannedResource
for _, v := range mod.Resources {
if !isFastlyVCLServiceResource(v) {
continue
}

if err := json.Unmarshal(v.Values, &serviceValues); err != nil {
var s *FastlyServiceValues
if err := json.Unmarshal(v.Values, &s); err != nil {
return nil, errors.Wrap(err, "Failed to unmarshal values")
}

services = append(services, &FastlyService{
Name: serviceValues.Name,
Vcls: serviceValues.Vcl,
Acls: serviceValues.Acl,
Backends: serviceValues.Backend,
Dictionaries: serviceValues.Dictionary,
Directors: serviceValues.Director,
Snippets: serviceValues.Snippets,
LoggingEndpoints: factoryLoggingEndpoints(serviceValues),
Name: s.Name,
Vcls: s.Vcl,
Acls: s.Acl,
Backends: s.Backend,
Dictionaries: s.Dictionary,
Directors: s.Director,
Snippets: s.Snippets,
LoggingEndpoints: factoryLoggingEndpoints(s),
})
}
}

if len(services) == 0 {
return nil, errors.New(`Fastly service does not exist. Did you plan with fastly terraform provider?`)
// Check child_modules existence and return found services if not found
if mod.ChildModules == nil || len(mod.ChildModules) == 0 {
return services, nil
}
// If module has child_modules, find Fastly service recursively
for _, child := range mod.ChildModules {
// child is *TerraformModule
childSerivices, err := findFastlyServicesInTerraformModule(child)
if err != nil {
return nil, errors.WithStack(err)
}
services = append(services, childSerivices...)
}

return services, nil
Expand Down