Skip to content

Commit

Permalink
Create consul_intention when deleted outside Terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Lapeyre committed Dec 27, 2018
1 parent 53c97f4 commit b3083bd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions consul/resource_consul_intention.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ func resourceConsulIntentionRead(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Failed to retrieve intention (dc: '%s'): %v", dc, err)
}

if intention == nil {
d.SetId("")
return nil
}

d.Set("source_name", intention.SourceName)
d.Set("destination_name", intention.DestinationName)
d.Set("description", intention.Description)
Expand Down
31 changes: 31 additions & 0 deletions consul/resource_consul_intention_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ func TestAccConsulIntention_basic(t *testing.T) {
resource.TestCheckResourceAttr("consul_intention.example", "meta.baz", "bat"),
),
},
resource.TestStep{
PreConfig: deleteIntention(t),
Config: testAccConsulIntentionConfigBasic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("consul_intention.example", "source_name", "api"),
resource.TestCheckResourceAttr("consul_intention.example", "destination_name", "db"),
),
},
},
})
}
Expand All @@ -45,6 +53,29 @@ func TestAccConsulIntention_badAction(t *testing.T) {
})
}

func deleteIntention(t *testing.T) func() {
return func() {
connect := testAccProvider.Meta().(*consulapi.Client).Connect()
match := &consulapi.IntentionMatch{
By: consulapi.IntentionMatchSource,
Names: []string{"api"},
}
qOpts := &consulapi.QueryOptions{}
intentions, _, err := connect.IntentionMatch(match, qOpts)
if err != nil {
t.Fatalf("err: %v", err)
}
if len(intentions) != 1 {
t.Fatalf("Should found 1 intention (%v found)", len(intentions))
}
wOpts := &consulapi.WriteOptions{}
_, err = connect.IntentionDelete(intentions["api"][0].ID, wOpts)
if err != nil {
t.Fatalf("err: %v", err)
}
}
}

func testAccCheckConsulIntentionDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*consulapi.Client)

Expand Down

0 comments on commit b3083bd

Please sign in to comment.