Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Named containers #351

Merged
merged 3 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions pkg/container/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,64 @@ func (a Attributes) ToV2() []*container.Attribute {

return attrs
}

// sets value of the attribute by key.
func setAttribute(c *Container, key, value string) {
var a *Attribute

iterateAttributes(c, func(a_ *Attribute) bool {
if a_.Key() == key {
a = a_
}

return a != nil
})

if a == nil {
a = NewAttribute()
a.SetKey(key)

c.SetAttributes(append(c.Attributes(), a))
}

a.SetValue(value)
}

// iterates over container attributes. Stops at f's true return.
//
// Handler must not be nil.
func iterateAttributes(c *Container, f func(*Attribute) bool) {
for _, a := range c.Attributes() {
if f(a) {
return
}
}
}

// SetNativeNameWithZone sets container native name and its zone.
//
// Use SetNativeName to set default zone.
func SetNativeNameWithZone(c *Container, name, zone string) {
setAttribute(c, container.SysAttributeName, name)
setAttribute(c, container.SysAttributeZone, zone)
}

// SetNativeName sets container native name with default zone (container).
func SetNativeName(c *Container, name string) {
SetNativeNameWithZone(c, name, container.SysAttributeZoneDefault)
}

// GetNativeNameWithZone returns container native name and its zone.
func GetNativeNameWithZone(c *Container) (name string, zone string) {
iterateAttributes(c, func(a *Attribute) bool {
if key := a.Key(); key == container.SysAttributeName {
name = a.Value()
} else if key == container.SysAttributeZone {
zone = a.Value()
}

return name != "" && zone != ""
})

return
}
35 changes: 35 additions & 0 deletions pkg/container/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,38 @@ func TestNewAttributeFromV2(t *testing.T) {
require.Nil(t, container.NewAttributeFromV2(x))
})
}

func TestGetNameWithZone(t *testing.T) {
c := container.New()

for _, item := range [...]struct {
name, zone string
}{
{"name1", ""},
{"name1", "zone1"},
{"name2", "zone1"},
{"name2", "zone2"},
{"", "zone2"},
{"", ""},
} {
container.SetNativeNameWithZone(c, item.name, item.zone)

name, zone := container.GetNativeNameWithZone(c)

require.Equal(t, item.name, name, item.name)
require.Equal(t, item.zone, zone, item.zone)
}
}

func TestSetNativeName(t *testing.T) {
c := container.New()

const nameDefZone = "some name"

container.SetNativeName(c, nameDefZone)

name, zone := container.GetNativeNameWithZone(c)

require.Equal(t, nameDefZone, name)
require.Equal(t, containerv2.SysAttributeZoneDefault, zone)
}
9 changes: 9 additions & 0 deletions v2/container/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@ const SysAttributePrefix = "__NEOFS__"
const (
// SysAttributeSubnet is a string ID of container's storage subnet.
SysAttributeSubnet = SysAttributePrefix + "SUBNET"

// SysAttributeName is a string of human-friendly container name registered as the domain in NNS contract.
SysAttributeName = SysAttributePrefix + "NAME"

// SysAttributeZone is a string of zone for container name.
SysAttributeZone = SysAttributePrefix + "ZONE"
)

// SysAttributeZoneDefault is a default value for SysAttributeZone attribute.
const SysAttributeZoneDefault = "container"
6 changes: 6 additions & 0 deletions v2/container/grpc/types.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.