Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Exposes deleting a tribe agreement through REST
Browse files Browse the repository at this point in the history
  • Loading branch information
jcooklin authored and pittma committed Oct 29, 2015
1 parent d8314fd commit 0581e55
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 9 deletions.
2 changes: 2 additions & 0 deletions mgmt/rest/rbody/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ func UnmarshalBody(t string, b []byte) (Body, error) {
return unmarshalAndHandleError(b, &TribeAgreementList{})
case TribeAddAgreementType:
return unmarshalAndHandleError(b, &TribeAddAgreement{})
case TribeDeleteAgreementType:
return unmarshalAndHandleError(b, &TribeDeleteAgreement{})
case TribeMemberShowType:
return unmarshalAndHandleError(b, &TribeMemberShow{})
case TribeJoinAgreementType:
Expand Down
29 changes: 21 additions & 8 deletions mgmt/rest/rbody/tribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ package rbody
import "github.com/intelsdi-x/pulse/mgmt/tribe/agreement"

const (
TribeAgreementListType = "tribe_agreement_list_returned"
TribeGetAgreementType = "tribe_agreement_returned"
TribeAddAgreementType = "tribe_agreement_created"
TribeAddMemberType = "tribe_member_added"
TribeJoinAgreementType = "tribe_agreement_joined"
TribeLeaveAgreementType = "tribe_agreement_left"
TribeMemberListType = "tribe_member_list_returned"
TribeMemberShowType = "tribe_member_details_returned"
TribeAgreementListType = "tribe_agreement_list_returned"
TribeGetAgreementType = "tribe_agreement_returned"
TribeAddAgreementType = "tribe_agreement_created"
TribeDeleteAgreementType = "tribe_agreement_deleted"
TribeAddMemberType = "tribe_member_added"
TribeJoinAgreementType = "tribe_agreement_joined"
TribeLeaveAgreementType = "tribe_agreement_left"
TribeMemberListType = "tribe_member_list_returned"
TribeMemberShowType = "tribe_member_details_returned"
)

type TribeAddAgreement struct {
Expand All @@ -56,6 +57,18 @@ func (t *TribeGetAgreement) ResponseBodyType() string {
return TribeGetAgreementType
}

type TribeDeleteAgreement struct {
Agreements map[string]*agreement.Agreement `json:"agreements"`
}

func (t *TribeDeleteAgreement) ResponseBodyMessage() string {
return "Tribe agreement deleted"
}

func (t *TribeDeleteAgreement) ResponseBodyType() string {
return TribeDeleteAgreementType
}

type TribeAgreementList struct {
Agreements map[string]*agreement.Agreement `json:"agreements"`
}
Expand Down
2 changes: 2 additions & 0 deletions mgmt/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type managesTribe interface {
GetAgreement(name string) (*agreement.Agreement, perror.PulseError)
GetAgreements() map[string]*agreement.Agreement
AddAgreement(name string) perror.PulseError
RemoveAgreement(name string) perror.PulseError
JoinAgreement(agreementName, memberName string) perror.PulseError
LeaveAgreement(agreementName, memberName string) perror.PulseError
GetMembers() []string
Expand Down Expand Up @@ -221,6 +222,7 @@ func (s *Server) addRoutes() {
s.r.GET("/v1/tribe/agreements", s.getAgreements)
s.r.POST("/v1/tribe/agreements", s.addAgreement)
s.r.GET("/v1/tribe/agreements/:name", s.getAgreement)
s.r.DELETE("/v1/tribe/agreements/:name", s.deleteAgreement)
s.r.PUT("/v1/tribe/agreements/:name/join", s.joinAgreement)
s.r.DELETE("/v1/tribe/agreements/:name/leave", s.leaveAgreement)
s.r.GET("/v1/tribe/members", s.getMembers)
Expand Down
25 changes: 24 additions & 1 deletion mgmt/rest/tribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *Server) getAgreements(w http.ResponseWriter, r *http.Request, _ httprou
}

func (s *Server) getAgreement(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
tribeLogger = tribeLogger.WithField("_block", "joinAgreement")
tribeLogger = tribeLogger.WithField("_block", "getAgreement")
name := p.ByName("name")
if _, ok := s.tr.GetAgreements()[name]; !ok {
fields := map[string]interface{}{
Expand All @@ -70,6 +70,29 @@ func (s *Server) getAgreement(w http.ResponseWriter, r *http.Request, p httprout
respond(200, a, w)
}

func (s *Server) deleteAgreement(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
tribeLogger = tribeLogger.WithField("_block", "deleteAgreement")
name := p.ByName("name")
if _, ok := s.tr.GetAgreements()[name]; !ok {
fields := map[string]interface{}{
"agreement_name": name,
}
tribeLogger.WithFields(fields).Error(ErrAgreementDoesNotExist)
respond(400, rbody.FromPulseError(perror.New(ErrAgreementDoesNotExist, fields)), w)
return
}
a := &rbody.TribeDeleteAgreement{}
var perr perror.PulseError
perr = s.tr.RemoveAgreement(name)
if perr != nil {
tribeLogger.Error(perr)
respond(400, rbody.FromPulseError(perr), w)
return
}
a.Agreements = s.tr.GetAgreements()
respond(200, a, w)
}

func (s *Server) joinAgreement(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
tribeLogger = tribeLogger.WithField("_block", "joinAgreement")
name := p.ByName("name")
Expand Down
48 changes: 48 additions & 0 deletions mgmt/rest/tribe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ func getAgreement(port int, name string) *rbody.APIResponse {
return getAPIResponse(resp)
}

func deleteAgreement(port int, name string) *rbody.APIResponse {
client := &http.Client{}
uri := fmt.Sprintf("http://127.0.0.1:%d/v1/tribe/agreements/%s", port, name)
req, err := http.NewRequest("DELETE", uri, nil)
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
return getAPIResponse(resp)
}

func joinAgreement(port int, memberName, agreementName string) *rbody.APIResponse {
ja, err := json.Marshal(struct {
MemberName string `json:"member_name"`
Expand Down Expand Up @@ -470,6 +482,42 @@ func TestTribePluginAgreements(t *testing.T) {
}
wg.Wait()
So(timedOut, ShouldEqual, false)

Convey("The agreement is deleted", func() {
d := deleteAgreement(mgtPorts[0], aName)
So(d.Body, ShouldHaveSameTypeAs, new(rbody.TribeDeleteAgreement))
So(d.Meta.Code, ShouldEqual, 200)

Convey("All members delete the agreement", func(c C) {
var wg sync.WaitGroup
timedOut := false
for _, i := range mgtPorts {
timer := time.After(15 * time.Second)
wg.Add(1)
go func(port int) {
defer wg.Done()
for {
select {
case <-timer:
timedOut = true
return
default:
resp := getAgreements(port)
if resp.Meta.Code == 200 {
c.So(resp.Body.(*rbody.TribeAgreementList), ShouldHaveSameTypeAs, new(rbody.TribeAgreementList))
if len(resp.Body.(*rbody.TribeAgreementList).Agreements) == 0 {
return
}
}
time.Sleep(200 * time.Millisecond)
}
}
}(i)
}
wg.Wait()
So(timedOut, ShouldEqual, false)
})
})
})
})
})
Expand Down
2 changes: 2 additions & 0 deletions pulse.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ type managesTribe interface {
GetAgreement(name string) (*agreement.Agreement, perror.PulseError)
GetAgreements() map[string]*agreement.Agreement
AddAgreement(name string) perror.PulseError
RemoveAgreement(name string) perror.PulseError
JoinAgreement(agreementName, memberName string) perror.PulseError
LeaveAgreement(agreementName, memberName string) perror.PulseError
GetMembers() []string
GetMember(name string) *agreement.Member
}
Expand Down

0 comments on commit 0581e55

Please sign in to comment.