Skip to content
Merged
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
11 changes: 10 additions & 1 deletion cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Plan struct {
ResourceDrift []ResourceChange `json:"resource_drift,omitempty"`
ResourceChanges []ResourceChange `json:"resource_changes,omitempty"`
OutputChanges map[string]Change `json:"output_changes,omitempty"`
PriorState json.RawMessage `json:"prior_state,omitempty"`
PriorState State `json:"prior_state,omitempty"`
Config config `json:"configuration,omitempty"`
RelevantAttributes []ResourceAttr `json:"relevant_attributes,omitempty"`
Checks json.RawMessage `json:"checks,omitempty"`
Expand Down Expand Up @@ -609,3 +609,12 @@ type ResourceAttr struct {
Resource string `json:"resource"`
Attr json.RawMessage `json:"attribute"`
}

// State is the top-level representation of the json format of a terraform
// state.
type State struct {
FormatVersion string `json:"format_version,omitempty"`
TerraformVersion string `json:"terraform_version,omitempty"`
Values *StateValues `json:"values,omitempty"`
Checks json.RawMessage `json:"checks,omitempty"`
}
25 changes: 14 additions & 11 deletions cmd/submitplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,18 @@ func changingItemQueriesFromPlan(ctx context.Context, planJSON []byte, lf log.Fi

var currentResource *Resource
for _, mapData := range mappings {
currentResource = plan.PlannedValues.RootModule.DigResource(resourceChange.Address)
// Look for the resource in the prior values first, since this is
// the *previous* state we're like to be able to find it in the
// actual infra
if plan.PriorState.Values != nil {
currentResource = plan.PriorState.Values.RootModule.DigResource(resourceChange.Address)
}

// If we didn't find it, look in the planned values
if currentResource == nil {
currentResource = plan.PlannedValues.RootModule.DigResource(resourceChange.Address)
}

if currentResource == nil {
log.WithContext(ctx).
WithFields(lf).
Expand All @@ -106,21 +117,13 @@ func changingItemQueriesFromPlan(ctx context.Context, planJSON []byte, lf log.Fi
dataMap["values"] = currentResource.AttributeValues

if overmindMappings, ok := plan.PlannedValues.Outputs["overmind_mappings"]; ok {
// TODO: Check for provider mappings
//
// This will need to follow the logic form the readme. We now have
// the entire plan parsed in a typesafe manner so it shouldn't be
// terribly hard. We just need to map from the changing resource to
// the provider, which probably should be its own function. Once we
// have that we can check the outputs for mappings

configResource := plan.Config.RootModule.DigResource(resourceChange.Address)

if configResource == nil {
log.WithContext(ctx).
WithFields(lf).
WithField("terraform-address", resourceChange.Address).
Warn("skipping resource without config")
Debug("skipping provider mapping for resource without config")
} else {
// Look up the provider config key in the mappings
mappings := make(map[string]map[string]string)
Expand All @@ -141,7 +144,7 @@ func changingItemQueriesFromPlan(ctx context.Context, planJSON []byte, lf log.Fi
WithFields(lf).
WithField("terraform-address", resourceChange.Address).
WithField("provider-config-key", configResource.ProviderConfigKey).
Debug("found provider mappings")
Info("found provider mappings")

// We have mappings for this provider, so set them
// in the `provider_mapping` value
Expand Down
38 changes: 26 additions & 12 deletions cmd/submitplan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,45 @@ func TestChangingItemQueriesFromPlan(t *testing.T) {
t.Error(err)
}

if len(queries) != 2 {
t.Errorf("Expected 1 queries, got %v", len(queries))
if len(queries) != 3 {
t.Errorf("Expected 3 queries, got %v", len(queries))
}

if queries[0].Type != "Deployment" {
t.Errorf("Expected query type to be Deployment, got %v", queries[0].Type)
}

if queries[0].Query != "api-server" {
t.Errorf("Expected query to be api-server, got %v", queries[0].Query)
if queries[0].Query != "nats-box" {
t.Errorf("Expected query to be nats-box, got %v", queries[0].Query)
}

if queries[0].Scope != "dogfood.default" {
t.Errorf("Expected query scope to be dogfood.default, got %v", queries[0].Scope)
// Since this resource is being deleted it doesn't have any config so we
// can't determine the scope from mappings
if queries[0].Scope != "*" {
t.Errorf("Expected query scope to be *, got %v", queries[0].Scope)
}

if queries[1].Type != "iam-policy" {
t.Errorf("Expected query type to be iam-policy, got %v", queries[1].Type)
if queries[1].Type != "Deployment" {
t.Errorf("Expected query type to be Deployment, got %v", queries[1].Type)
}

if queries[1].Query != "arn:aws:iam::123456789012:policy/test-alb-ingress" {
t.Errorf("Expected query to be arn:aws:iam::123456789012:policy/test-alb-ingress, got %v", queries[1].Query)
if queries[1].Query != "api-server" {
t.Errorf("Expected query to be api-server, got %v", queries[1].Query)
}

if queries[1].Scope != "*" {
t.Errorf("Expected query scope to be *, got %v", queries[1].Scope)
if queries[1].Scope != "dogfood.default" {
t.Errorf("Expected query scope to be dogfood.default, got %v", queries[1].Scope)
}

if queries[2].Type != "iam-policy" {
t.Errorf("Expected query type to be iam-policy, got %v", queries[2].Type)
}

if queries[2].Query != "arn:aws:iam::123456789012:policy/test-alb-ingress" {
t.Errorf("Expected query to be arn:aws:iam::123456789012:policy/test-alb-ingress, got %v", queries[2].Query)
}

if queries[2].Scope != "*" {
t.Errorf("Expected query scope to be *, got %v", queries[2].Scope)
}
}
Loading