Skip to content

Commit

Permalink
Add environment deletion (back end)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Baecher committed Mar 2, 2018
1 parent 24a9f8d commit b1ca976
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## ?.?.?

### New features

* Environments can be deleted through the API.

### Bugfixes and minor changes

* Slack notifier is now less spammy.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The command endpoint is for manipulating data.
| `POST` | `/api/events/feature_toggled` | `{"feature":"feature1","environment":"staging","status":true}` | Toggle a feature. |
| `POST` | `/api/events/feature_deleted` | `{"name":"feature1"}` | Delete a feature. |
| `POST` | `/api/events/environments_ordered`| `{"order":["dev","staging"]}` | Change env display order. |
| `POST` | `/api/events/environment_deleted` | `{"name":"staging"}` | Delete an environment. |

## Client

Expand Down
44 changes: 44 additions & 0 deletions models/environment_deleted.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package models

import (
"errors"
"time"

"github.com/MEDIGO/laika/notifier"
)

type EnvironmentDeleted struct {
Name string `json:"name"`
}

func (e *EnvironmentDeleted) Validate(s *State) (error, error) {
if s.getEnvByName(e.Name) == nil {
return errors.New("Bad environment"), nil
}

return nil, nil
}

func (e *EnvironmentDeleted) Update(s *State, t time.Time) *State {
state := *s
state.Environments = []Environment{}

for _, env := range s.Environments {
if env.Name != e.Name {
state.Environments = append(state.Environments, env)
}
}

for _, feature := range s.Features {
delete(state.Enabled, EnvFeature{e.Name, feature.Name})
}

return &state
}

func (e *EnvironmentDeleted) PrePersist(*State) (Event, error) {
return e, nil
}

func (*EnvironmentDeleted) Notify(*State, notifier.Notifier) {
}
40 changes: 40 additions & 0 deletions models/environment_deleted_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package models

import (
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestEnvironmentDeleted(t *testing.T) {
var time time.Time
s := NewState()

s = requireValid(t, s, &EnvironmentCreated{Name: "e1"}).Update(s, time)
s = requireValid(t, s, &EnvironmentCreated{Name: "e2"}).Update(s, time)
s = requireValid(t, s, &FeatureCreated{Name: "f1"}).Update(s, time)
s = requireValid(t, s, &FeatureCreated{Name: "f2"}).Update(s, time)
s = requireValid(t, s, &FeatureCreated{Name: "f3"}).Update(s, time)
s = requireValid(t, s, &FeatureToggled{Environment: "e1", Feature: "f2", Status: true}).Update(s, time)

// successful deletion
s = requireValid(t, s, &EnvironmentDeleted{Name: "e1"}).Update(s, time)

require.Len(t, s.Environments, 1)
require.Equal(t, "e2", s.Environments[0].Name)

_, ok := s.Enabled[EnvFeature{Env: "e1", Feature: "f2"}]
require.False(t, ok)

// non-existing feature is invalid
fd := &EnvironmentDeleted{Name: "e1"}
valErr, err := fd.Validate(s)
require.NoError(t, err)
require.Error(t, valErr)

// non-existing environment should not cause harm
before := *s
s = fd.Update(s, time)
require.Equal(t, before, *s)
}
1 change: 1 addition & 0 deletions models/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Event interface {

var types = map[string](func() Event){
"environment_created": func() Event { return &EnvironmentCreated{} },
"environment_deleted": func() Event { return &EnvironmentDeleted{} },
"environments_ordered": func() Event { return &EnvironmentsOrdered{} },
"feature_created": func() Event { return &FeatureCreated{} },
"feature_toggled": func() Event { return &FeatureToggled{} },
Expand Down

0 comments on commit b1ca976

Please sign in to comment.