diff --git a/frostfsid/client/client.go b/frostfsid/client/client.go index ef08267..d13085d 100644 --- a/frostfsid/client/client.go +++ b/frostfsid/client/client.go @@ -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) diff --git a/frostfsid/config.yml b/frostfsid/config.yml index 0da01a3..0beb114 100644 --- a/frostfsid/config.yml +++ b/frostfsid/config.yml @@ -4,6 +4,7 @@ safemethods: - "getGroup" - "getGroupExtended" - "getGroupIDByName" + - "getGroupByName" - "getNamespace" - "getNamespaceExtended" - "getSubject" diff --git a/frostfsid/frostfsid_contract.go b/frostfsid/frostfsid_contract.go index ffb6dd4..9362465 100644 --- a/frostfsid/frostfsid_contract.go +++ b/frostfsid/frostfsid_contract.go @@ -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) diff --git a/rpcclient/frostfsid/client.go b/rpcclient/frostfsid/client.go index 2555b51..ddea629 100644 --- a/rpcclient/frostfsid/client.go +++ b/rpcclient/frostfsid/client.go @@ -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)) diff --git a/tests/frostfsid_client_test.go b/tests/frostfsid_client_test.go index 0ca0572..6516e75 100644 --- a/tests/frostfsid_client_test.go +++ b/tests/frostfsid_client_test.go @@ -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