Skip to content
Closed
33 changes: 33 additions & 0 deletions doc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ <h2>Table of Contents</h2>
<a href="#metalstack%2fapi%2fv2%2fcommon.proto-extensions"><span class="badge">X</span>File-level Extensions</a>
</li>

<li>
<a href="#metalstack%2fapi%2fv2%2fcommon.proto-extensions"><span class="badge">X</span>File-level Extensions</a>
</li>


</ul>
</li>
Expand Down Expand Up @@ -1805,6 +1809,14 @@ <h3 id="metalstack/api/v2/common.proto-extensions">File-level Extensions</h3>
<td><p>StringValue which can be set to a enum</p></td>
</tr>

<tr>
<td>treat_as_id</td>
<td><a href="#bool">bool</a></td>
<td><a href="#google.protobuf.FieldOptions">.google.protobuf.FieldOptions</a></td>
<td>53000</td>
<td><p>TreatAsId if defined this field is treated as primary key of this entity</p></td>
</tr>

<tr>
<td>admin_roles</td>
<td><a href="#metalstack.api.v2.AdminRole">AdminRole</a></td>
Expand Down Expand Up @@ -2830,6 +2842,13 @@ <h3 id="metalstack.admin.v2.FilesystemServiceUpdateRequest">FilesystemServiceUpd
</thead>
<tbody>

<tr>
<td>id</td>
<td><a href="#string">string</a></td>
<td></td>
<td><p>ID of the filesystem to update </p></td>
</tr>

<tr>
<td>filesystem_layout</td>
<td><a href="#metalstack.api.v2.FilesystemLayout">metalstack.api.v2.FilesystemLayout</a></td>
Expand Down Expand Up @@ -3447,6 +3466,13 @@ <h3 id="metalstack.admin.v2.ImageServiceUpdateRequest">ImageServiceUpdateRequest
</thead>
<tbody>

<tr>
<td>id</td>
<td><a href="#string">string</a></td>
<td></td>
<td><p>ID of the image to update </p></td>
</tr>

<tr>
<td>image</td>
<td><a href="#metalstack.api.v2.Image">metalstack.api.v2.Image</a></td>
Expand Down Expand Up @@ -6414,6 +6440,13 @@ <h3 id="metalstack.admin.v2.PartitionServiceUpdateRequest">PartitionServiceUpdat
</thead>
<tbody>

<tr>
<td>id</td>
<td><a href="#string">string</a></td>
<td></td>
<td><p>ID of the partition to update </p></td>
</tr>

<tr>
<td>partition</td>
<td><a href="#metalstack.api.v2.Partition">metalstack.api.v2.Partition</a></td>
Expand Down
46 changes: 46 additions & 0 deletions go/id/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package id

import (
"fmt"
"reflect"

apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)

func Get[M proto.Message](m M) (string, error) {
var primaryKey string

if reflect.ValueOf(m).Kind() == reflect.Pointer {
if reflect.ValueOf(m).IsNil() {
return primaryKey, fmt.Errorf("given message is a nil pointer")
}
}

var (
value protoreflect.Value
field protoreflect.FieldDescriptor
)
pm := m.ProtoReflect()
field = pm.Descriptor().Fields().ByTextName("id")
if field == nil {
pm.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
if fd.Kind() != protoreflect.StringKind {
return true
}
opts := fd.Options()
if opts != nil && proto.HasExtension(opts, apiv2.E_TreatAsId) {
field = fd
return true
}
return false
})
}
if field == nil {
return primaryKey, fmt.Errorf("unable to fetch primaryKey from %v", m)
}
value = pm.Get(field)
primaryKey = value.String()
return primaryKey, nil
}
68 changes: 68 additions & 0 deletions go/id/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package id

import (
"testing"

apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)

func TestGet(t *testing.T) {

tur := &apiv2.TenantServiceUpdateMemberRequest{
Login: "john@google",
}
nur := &apiv2.NetworkServiceUpdateRequest{
Id: "internet",
}
pur := &apiv2.ProjectServiceUpdateRequest{
Project: "p1",
}
tests := []struct {
name string
m proto.Message
want string
wantErr bool
}{
{
name: "network",
m: nur,
want: "internet",
},
{
name: "tenant",
m: tur,
want: "john@google",
},
{
name: "project",
m: pur,
want: "p1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Get(tt.m)
if (err != nil) != tt.wantErr {
t.Errorf("GetPrimaryKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("GetPrimaryKey() = %v, want %v", got, tt.want)
}
})
}
}

func BenchmarkGet(b *testing.B) {
tur := &apiv2.TenantServiceUpdateMemberRequest{
Role: apiv2.TenantRole_TENANT_ROLE_EDITOR,
Login: "john@google",
}
for b.Loop() {
got, err := Get(tur)
require.NoError(b, err)
require.NotNil(b, got)
}
}
19 changes: 15 additions & 4 deletions go/metalstack/admin/v2/filesystem.pb.go

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

19 changes: 15 additions & 4 deletions go/metalstack/admin/v2/image.pb.go

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

19 changes: 15 additions & 4 deletions go/metalstack/admin/v2/partition.pb.go

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

43 changes: 31 additions & 12 deletions go/metalstack/api/v2/common.pb.go

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

Loading