[#76] frostfsid: Add GetGroupByName method

Signed-off-by: Marina Biryukova <m.biryukova@yadro.com>
This commit is contained in:
Marina Biryukova 2024-01-29 14:10:43 +03:00
parent 6e72d0b3b4
commit 55a4163951
5 changed files with 45 additions and 0 deletions

View file

@ -114,6 +114,7 @@ const (
getGroupMethod = "getGroup"
getGroupExtendedMethod = "getGroupExtended"
getGroupIDByNameMethod = "getGroupIDByName"
getGroupByNameMethod = "getGroupByName"
setGroupNameMethod = "setGroupName"
setGroupKVMethod = "setGroupKV"
deleteGroupKVMethod = "deleteGroupKV"
@ -482,6 +483,16 @@ func (c Client) GetGroupIDByName(namespace, groupName string) (int64, error) {
return unwrap.Int64(c.act.Call(c.contract, getGroupIDByNameMethod, namespace, groupName))
}
// GetGroupByName gets group by its name (namespace scope).
func (c Client) GetGroupByName(namespace, groupName string) (*Group, error) {
items, err := unwrap.Array(c.act.Call(c.contract, getGroupByNameMethod, namespace, groupName))
if err != nil {
return nil, err
}
return parseGroup(items)
}
// ListGroups gets all groups in specific namespace.
func (c Client) ListGroups(namespace string) ([]*Group, error) {
items, err := commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listGroupsMethod, namespace)

View file

@ -4,6 +4,7 @@ safemethods:
- "getGroup"
- "getGroupExtended"
- "getGroupIDByName"
- "getGroupByName"
- "getNamespace"
- "getNamespaceExtended"
- "getSubject"

View file

@ -585,6 +585,19 @@ func GetGroupIDByName(ns, name string) int {
return std.Deserialize(groupIDRaw).(int)
}
func GetGroupByName(ns, name string) Group {
groupID := GetGroupIDByName(ns, name)
gKey := groupKey(ns, groupID)
ctx := storage.GetReadOnlyContext()
data := storage.Get(ctx, gKey).([]byte)
if data == nil {
panic("group not found")
}
return std.Deserialize(data).(Group)
}
func SetGroupName(ns string, groupID int, name string) {
ctx := storage.GetContext()
checkContractOwner(ctx)

View file

@ -177,6 +177,11 @@ func (c *ContractReader) GetGroup(ns string, groupID *big.Int) ([]stackitem.Item
return unwrap.Array(c.invoker.Call(c.hash, "getGroup", ns, groupID))
}
// GetGroupByName invokes `getGroupByName` method of contract.
func (c *ContractReader) GetGroupByName(ns string, name string) ([]stackitem.Item, error) {
return unwrap.Array(c.invoker.Call(c.hash, "getGroupByName", ns, name))
}
// GetGroupExtended invokes `getGroupExtended` method of contract.
func (c *ContractReader) GetGroupExtended(ns string, groupID *big.Int) ([]stackitem.Item, error) {
return unwrap.Array(c.invoker.Call(c.hash, "getGroupExtended", ns, groupID))

View file

@ -537,6 +537,21 @@ func TestFrostFSID_Client_GetSubjectByName(t *testing.T) {
require.Equal(t, defaultNamespace, subj.Namespace)
}
func TestFrostFSID_Client_GetGroupByName(t *testing.T) {
ffsid, cancel := initFrostfsIFClientTest(t)
defer cancel()
groupName := "group"
actGroupID, err := ffsid.cli.ParseGroupID(ffsid.cli.Wait(ffsid.cli.CreateGroup(defaultNamespace, groupName)))
require.NoError(t, err)
group, err := ffsid.cli.GetGroupByName(defaultNamespace, groupName)
require.NoError(t, err)
require.Equal(t, actGroupID, group.ID)
require.Equal(t, defaultNamespace, group.Namespace)
require.Equal(t, groupName, group.Name)
}
func prettyPrintExtendedSubjects(subjects []*client.SubjectExtended) {
for _, subj := range subjects {
var sb strings.Builder