Skip to content

Commit

Permalink
[FAB-17680] Support removing an org
Browse files Browse the repository at this point in the history
Removing an organization from a consortium, application, and orderer.

Signed-off-by: xu wu <wuxu1103@163.com>
  • Loading branch information
wuxuer committed Apr 4, 2020
1 parent 85d55b4 commit e232a36
Show file tree
Hide file tree
Showing 3 changed files with 281 additions and 1 deletion.
60 changes: 59 additions & 1 deletion pkg/config/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,39 @@ func Example_systemChannel() {
panic(err)
}

orgToAdd := config.Organization{
Name: "Org3",
Policies: map[string]config.Policy{
config.AdminsPolicyKey: {
Type: config.ImplicitMetaPolicyType,
Rule: "MAJORITY Admins",
},
config.EndorsementPolicyKey: {
Type: config.ImplicitMetaPolicyType,
Rule: "MAJORITY Endorsement",
},
config.ReadersPolicyKey: {
Type: config.ImplicitMetaPolicyType,
Rule: "ANY Readers",
},
config.WritersPolicyKey: {
Type: config.ImplicitMetaPolicyType,
Rule: "ANY Writers",
},
},
MSP: baseMSP(&testing.T{}),
}

err = c.AddOrgToConsortium(orgToAdd, "SampleConsortium")
if err != nil {
panic(err)
}

err = c.RemoveConsortiumOrg("SampleConsortium", "Org3")
if err != nil {
panic(err)
}

// Compute the delta
configUpdate, err := c.ComputeUpdate("testsyschannel")
if err != nil {
Expand Down Expand Up @@ -352,6 +385,11 @@ func Example_organization() {
panic(err)
}

err = c.RemoveApplicationOrg("Org2")
if err != nil {
panic(err)
}

err = c.AddApplicationOrgPolicy("Org1", config.AdminsPolicyKey, "TestPolicy", config.Policy{
Type: config.ImplicitMetaPolicyType,
Rule: "MAJORITY Endorsement",
Expand All @@ -375,6 +413,11 @@ func Example_organization() {
panic(err)
}

err = c.RemoveOrdererOrg("OrdererOrg2")
if err != nil {
panic(err)
}

err = c.RemoveOrdererOrgPolicy("OrdererOrg", config.WritersPolicyKey)
if err != nil {
panic(err)
Expand Down Expand Up @@ -1367,7 +1410,22 @@ func fetchSystemChannelConfig() *cb.Config {
config.ConsortiumsGroupKey: {
Groups: map[string]*cb.ConfigGroup{
"SampleConsortium": {
Groups: map[string]*cb.ConfigGroup{},
Groups: map[string]*cb.ConfigGroup{
"Org1": {
Groups: map[string]*cb.ConfigGroup{},
Policies: map[string]*cb.ConfigPolicy{},
Values: map[string]*cb.ConfigValue{},
ModPolicy: "Admins",
Version: 0,
},
"Org2": {
Groups: map[string]*cb.ConfigGroup{},
Policies: map[string]*cb.ConfigPolicy{},
Values: map[string]*cb.ConfigValue{},
ModPolicy: "Admins",
Version: 0,
},
},
Values: map[string]*cb.ConfigValue{
config.ChannelCreationPolicyKey: {
ModPolicy: "/Channel/Orderer/Admins",
Expand Down
39 changes: 39 additions & 0 deletions pkg/config/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ func (c *ConfigTx) ApplicationOrg(orgName string) (Organization, error) {
return getOrganization(orgGroup, orgName)
}

// RemoveApplicationOrg remove an org from Application groups
func (c *ConfigTx) RemoveApplicationOrg(orgName string) error {
applicationGroups := c.updated.ChannelGroup.Groups[ApplicationGroupKey].Groups
if _, ok := applicationGroups[orgName]; !ok {
return fmt.Errorf("application org %s does not exist in channel config", orgName)
}

delete(applicationGroups, orgName)

return nil
}

// OrdererOrg retrieves an existing org from an orderer organization config group.
func (c *ConfigTx) OrdererOrg(orgName string) (Organization, error) {
orgGroup, ok := c.base.ChannelGroup.Groups[OrdererGroupKey].Groups[orgName]
Expand Down Expand Up @@ -55,6 +67,18 @@ func (c *ConfigTx) OrdererOrg(orgName string) (Organization, error) {
return org, err
}

// RemoveOredererOrg remove an org from Orderer groups
func (c *ConfigTx) RemoveOrdererOrg(orgName string) error {
ordererGroups := c.updated.ChannelGroup.Groups[OrdererGroupKey].Groups
if _, ok := ordererGroups[orgName]; !ok {
return fmt.Errorf("orderer org %s does not exist in channel config", orgName)
}

delete(ordererGroups, orgName)

return nil
}

// ConsortiumOrg retrieves an existing org from a consortium organization config group.
func (c *ConfigTx) ConsortiumOrg(consortiumName, orgName string) (Organization, error) {
consortium, ok := c.base.ChannelGroup.Groups[ConsortiumsGroupKey].Groups[consortiumName]
Expand All @@ -77,6 +101,21 @@ func (c *ConfigTx) ConsortiumOrg(consortiumName, orgName string) (Organization,
return org, err
}

// RemoveConsortiumOrg remove an org in a consortium organization from Consortiums groups
func (c *ConfigTx) RemoveConsortiumOrg(consortiumName, orgName string) error {
consortium, ok := c.updated.ChannelGroup.Groups[ConsortiumsGroupKey].Groups[consortiumName]
if !ok {
return fmt.Errorf("consortium %s does not exist in channel config", consortiumName)
}
if _, ok := consortium.Groups[orgName]; !ok {
return fmt.Errorf("consortium org %s does not exist in channel config", orgName)
}

delete(consortium.Groups, orgName)

return nil
}

// newOrgConfigGroup returns an config group for an organization.
// It defines the crypto material for the organization (its MSP).
// It sets the mod_policy of all elements to "Admins".
Expand Down
183 changes: 183 additions & 0 deletions pkg/config/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,68 @@ func TestApplicationOrg(t *testing.T) {
}
}

func TestRemoveApplicationOrg(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)

channel := Channel{
Consortium: "SampleConsortium",
Application: Application{
Policies: standardPolicies(),
Organizations: []Organization{baseApplicationOrg(t)},
},
}
channelGroup, err := newChannelGroup(channel)
gt.Expect(err).NotTo(HaveOccurred())
orgGroup, err := newOrgConfigGroup(channel.Application.Organizations[0])
gt.Expect(err).NotTo(HaveOccurred())
channelGroup.Groups[ApplicationGroupKey].Groups["Org1"] = orgGroup

config := &cb.Config{
ChannelGroup: channelGroup,
}

c := ConfigTx{
base: config,
updated: config,
}

err = c.RemoveApplicationOrg("Org1")
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(c.updated.ChannelGroup.Groups[ApplicationGroupKey].Groups["Org1"]).To(BeNil())
}

func TestRemoveApplicationOrgFailures(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)

channel := Channel{
Consortium: "SampleConsortium",
Application: Application{
Policies: standardPolicies(),
Organizations: []Organization{baseApplicationOrg(t)},
},
}
channelGroup, err := newChannelGroup(channel)
gt.Expect(err).NotTo(HaveOccurred())
orgGroup, err := newOrgConfigGroup(channel.Application.Organizations[0])
gt.Expect(err).NotTo(HaveOccurred())
channelGroup.Groups[ApplicationGroupKey].Groups["Org1"] = orgGroup

config := &cb.Config{
ChannelGroup: channelGroup,
}

c := ConfigTx{
base: config,
updated: config,
}

err = c.RemoveApplicationOrg("BadOrg")
gt.Expect(err).To(MatchError("application org BadOrg does not exist in channel config"))
gt.Expect(c.updated.ChannelGroup.Groups[ApplicationGroupKey].Groups["Org1"]).NotTo(BeNil())
}

func TestOrdererOrg(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)
Expand Down Expand Up @@ -147,6 +209,50 @@ func TestOrdererOrg(t *testing.T) {
}
}

func TestRemoveOrdererOrg(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)

channel := baseSystemChannelProfile(t)
channelGroup, err := newSystemChannelGroup(channel)
gt.Expect(err).NotTo(HaveOccurred())

config := &cb.Config{
ChannelGroup: channelGroup,
}

c := ConfigTx{
base: config,
updated: config,
}

err = c.RemoveOrdererOrg("OrdererOrg")
gt.Expect(err).ToNot(HaveOccurred())
gt.Expect(c.updated.ChannelGroup.Groups[OrdererGroupKey].Groups["OrdererOrg"]).To(BeNil())
}

func TestRemoveOrdererOrgFailures(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)

channel := baseSystemChannelProfile(t)
channelGroup, err := newSystemChannelGroup(channel)
gt.Expect(err).NotTo(HaveOccurred())

config := &cb.Config{
ChannelGroup: channelGroup,
}

c := ConfigTx{
base: config,
updated: config,
}

err = c.RemoveOrdererOrg("BadOrg")
gt.Expect(err).To(MatchError("orderer org BadOrg does not exist in channel config"))
gt.Expect(c.updated.ChannelGroup.Groups[OrdererGroupKey].Groups["OrdererOrg"]).NotTo(BeNil())
}

func TestConsortiumOrg(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)
Expand Down Expand Up @@ -210,6 +316,83 @@ func TestConsortiumOrg(t *testing.T) {
}
}

func TestRemoveConsortiumOrg(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)

channel := baseSystemChannelProfile(t)
channelGroup, err := newSystemChannelGroup(channel)
gt.Expect(err).NotTo(HaveOccurred())

config := &cb.Config{
ChannelGroup: channelGroup,
}

c := ConfigTx{
base: config,
updated: config,
}

err = c.RemoveConsortiumOrg("Consortium1", "Org1")
gt.Expect(err).ToNot(HaveOccurred())
gt.Expect(c.updated.ChannelGroup.Groups[ConsortiumsGroupKey].Groups["Consortium1"].Groups["Org1"]).To(BeNil())
}

func TestRemoveConsortiumOrgFailures(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)

channel := baseSystemChannelProfile(t)
channelGroup, err := newSystemChannelGroup(channel)
gt.Expect(err).NotTo(HaveOccurred())

config := &cb.Config{
ChannelGroup: channelGroup,
}

c := ConfigTx{
base: config,
updated: config,
}

tests := []struct {
name string
consortiumName string
orgName string
expectedErr string
}{
{
name: "consortium not defined",
consortiumName: "BadConsortium",
orgName: "Org1",
expectedErr: "consortium BadConsortium does not exist in channel config",
},
{
name: "organization not defined",
consortiumName: "Consortium1",
orgName: "BadOrg",
expectedErr: "consortium org BadOrg does not exist in channel config",
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)

err := c.RemoveConsortiumOrg(tc.consortiumName, tc.orgName)
if tc.expectedErr != "" {
gt.Expect(err).To(MatchError(tc.expectedErr))
gt.Expect(c.updated.ChannelGroup.Groups[ConsortiumsGroupKey].Groups["Consortium1"].Groups["Org1"]).NotTo(BeNil())
} else {
gt.Expect(err).ToNot(HaveOccurred())
gt.Expect(c.updated.ChannelGroup.Groups[ConsortiumsGroupKey].Groups[tc.consortiumName].Groups[tc.orgName]).To(BeNil())
}
})
}
}

func TestNewOrgConfigGroup(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit e232a36

Please sign in to comment.