diff --git a/client/GroupTrackContainer.go b/client/GroupTrackContainer.go index c82ab94..37fdff3 100644 --- a/client/GroupTrackContainer.go +++ b/client/GroupTrackContainer.go @@ -100,6 +100,46 @@ func AddContainerToGroup(ContainerID string, GroupID string) (*Group, error) { return group, nil } +// RemoveContainerGroup Remove Container from the group ID specified +func RemoveContainerGroup(ContainerID string, GroupID string) (*Group,error) { + // Get container information based on container ID provided + containerInfo, err := GetContainerInformation(ContainerID) + if err != nil { + return nil,err + } + // Gets group information based on the group ID provided + group, err := GetGroup(GroupID) + if err != nil { + return nil,err + } + // Remove container from the appropriate group + err = group.RemoveContainerGroup(containerInfo) + if err != nil { + return nil,err + } + // Get Groups information from reading the grouptrackcontainer.json file + groups, err := ReadGroup() + if err != nil { + return nil, err + } + // Updating specific element in the group list with the remove container + groups.GroupList[group.index] = group + // Write groups information on the grouptrackcontainer.json file + err = groups.WriteGroup() + if err != nil { + return nil, err + } + + return group, nil +} + +// RemoveContainerGroups Remove Container from groups (i.e which ever groups has information +// about that container). This is mostly called when a container is deleted or removed from +// the tracked container list +func RemoveContainerGroups(ContainerID string) { + +} + // GetGroup Gets group information based on // group id provided func GetGroup(GroupID string) (*Group,error) { @@ -196,4 +236,16 @@ func (grp *Groups)WriteGroup() error { func (grp *Group)AddContainer(Container *TrackContainer) error { grp.TrackContainerList = append(grp.TrackContainerList, Container) return nil +} + +// RemoveContainerGroup Removes container information from the group +func (grp *Group)RemoveContainerGroup(Container *TrackContainer) error { + // Iterating through all container in the Group of Tracked Container + for i, container := range grp.TrackContainerList { + // If the container ID matches then remove the container from the group + if container.Id == Container.Id { + grp.TrackContainerList = append(grp.TrackContainerList[:i], grp.TrackContainerList[i+1:]...) + } + } + return nil } \ No newline at end of file diff --git a/client/GroupTrackContainer_test.go b/client/GroupTrackContainer_test.go index 901235f..23f6e8d 100644 --- a/client/GroupTrackContainer_test.go +++ b/client/GroupTrackContainer_test.go @@ -97,3 +97,74 @@ func TestAddContainerToGroup(t *testing.T) { } } + +// Testing if the container information is removed from the group +func TestGroup_RemoveContainerGroup(t *testing.T) { + // Creates a new group + group, err := CreateGroup() + if err != nil { + fmt.Println(err) + t.Fail() + } + + // Creating and adding the container to the + // tracked list + container1 ,err := docker.BuildRunContainer(0,"false","") + if err != nil { + fmt.Println(err) + t.Fail() + } + + // Testing the AddTrackContainer Function and adding the first container created + err = AddTrackContainer(container1,"0.0.0.0") + if err != nil { + // Killing docker container created + err = docker.StopAndRemoveContainer(container1.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } + fmt.Println(err) + t.Fail() + } + + // Adds container information to the group + Group, err := AddContainerToGroup(container1.ID,group.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } + fmt.Println("Container added") + PrettyPrint(Group) + + // Removing docker container from the group + Group, err = RemoveContainerGroup(container1.ID,group.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } + fmt.Println("Container removed") + PrettyPrint(Group) + + // Killing docker container created + err = docker.StopAndRemoveContainer(container1.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } + + // Removing container 1 from the tracked list + err = RemoveTrackedContainer(container1.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } + + // Removes the new group + // it created + err = RemoveGroup(group.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } +}