Skip to content

Commit

Permalink
gcs: Implement managing ethernet profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
johanstokking committed Jul 12, 2024
1 parent 03c8cd9 commit 049786b
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 7 deletions.
118 changes: 111 additions & 7 deletions pkg/gatewayconfigurationserver/managed/grpc_ethernet_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ package managed

import (
"context"
"strconv"

northboundv1 "go.thethings.industries/pkg/api/gen/tti/gateway/controller/northbound/v1"
"go.thethings.network/lorawan-stack/v3/pkg/ttgc"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/types/known/emptypb"
)

Expand All @@ -36,37 +38,139 @@ func (w *managedGatewayEthernetProfileServer) Create(
ctx context.Context,
req *ttnpb.CreateManagedGatewayEthernetProfileRequest,
) (*ttnpb.ManagedGatewayEthernetProfile, error) {
return nil, status.Error(codes.Unimplemented, "not implemented")
if err := requireProfileRights(ctx, req.Collaborator); err != nil {
return nil, err
}
if req.Profile.ProfileName == "" {
return nil, errNoProfileName.New()
}
profile := req.Profile
res, err := northboundv1.NewEthernetProfileServiceClient(w.client).Create(
ctx,
&northboundv1.EthernetProfileServiceCreateRequest{
Domain: w.client.Domain(ctx),
Group: group(req.Collaborator),
EthernetProfile: fromEthernetProfile(profile),
},
)
if err != nil {
return nil, err
}
profile.ProfileId = toProfileID(res.ProfileId)
return profile, nil
}

// Delete implements ttnpb.ManagedGatewayEthernetProfileConfigurationServiceServer.
func (w *managedGatewayEthernetProfileServer) Delete(
ctx context.Context,
req *ttnpb.DeleteManagedGatewayEthernetProfileRequest,
) (*emptypb.Empty, error) {
return nil, status.Error(codes.Unimplemented, "not implemented")
if err := requireProfileRights(ctx, req.Collaborator); err != nil {
return nil, err
}
_, err := northboundv1.NewEthernetProfileServiceClient(w.client).Delete(
ctx,
&northboundv1.EthernetProfileServiceDeleteRequest{
Domain: w.client.Domain(ctx),
Group: group(req.Collaborator),
ProfileId: fromProfileID(req.ProfileId),
},
)
if err != nil {
return nil, err
}
return ttnpb.Empty, nil
}

// Get implements ttnpb.ManagedGatewayEthernetProfileConfigurationServiceServer.
func (w *managedGatewayEthernetProfileServer) Get(
ctx context.Context,
req *ttnpb.GetManagedGatewayEthernetProfileRequest,
) (*ttnpb.ManagedGatewayEthernetProfile, error) {
return nil, status.Error(codes.Unimplemented, "not implemented")
if err := requireProfileRights(ctx, req.Collaborator); err != nil {
return nil, err
}
profileID := fromProfileID(req.ProfileId)
res, err := northboundv1.NewEthernetProfileServiceClient(w.client).Get(
ctx,
&northboundv1.EthernetProfileServiceGetRequest{
Domain: w.client.Domain(ctx),
Group: group(req.Collaborator),
ProfileId: profileID,
},
)
if err != nil {
return nil, err
}
profile := toEthernetProfile(profileID, res.EthernetProfile)
return profile, nil
}

// List implements ttnpb.ManagedGatewayEthernetProfileConfigurationServiceServer.
func (w *managedGatewayEthernetProfileServer) List(
ctx context.Context,
req *ttnpb.ListManagedGatewayEthernetProfilesRequest,
) (*ttnpb.ManagedGatewayEthernetProfiles, error) {
return nil, status.Error(codes.Unimplemented, "not implemented")
if err := requireProfileRights(ctx, req.Collaborator); err != nil {
return nil, err
}
page := req.Page
if page == 0 {
page = 1
}
res, err := northboundv1.NewEthernetProfileServiceClient(w.client).List(
ctx,
&northboundv1.EthernetProfileServiceListRequest{
Domain: w.client.Domain(ctx),
Group: group(req.Collaborator),
Limit: req.Limit,
Offset: (page - 1) * req.Limit,
},
)
if err != nil {
return nil, err
}
profiles := make([]*ttnpb.ManagedGatewayEthernetProfile, 0, len(res.Entries))
for _, entry := range res.Entries {
profile := toEthernetProfile(entry.ProfileId, entry.EthernetProfile)
profiles = append(profiles, profile)
}
grpc.SetHeader(ctx, metadata.Pairs("x-total-count", strconv.FormatInt(int64(res.Total), 10))) //nolint:errcheck
return &ttnpb.ManagedGatewayEthernetProfiles{
Profiles: profiles,
}, nil
}

// Update implements ttnpb.ManagedGatewayEthernetProfileConfigurationServiceServer.
func (w *managedGatewayEthernetProfileServer) Update(
ctx context.Context,
req *ttnpb.UpdateManagedGatewayEthernetProfileRequest,
) (*ttnpb.ManagedGatewayEthernetProfile, error) {
return nil, status.Error(codes.Unimplemented, "not implemented")
if err := requireProfileRights(ctx, req.Collaborator); err != nil {
return nil, err
}
client := northboundv1.NewEthernetProfileServiceClient(w.client)
profileID := fromProfileID(req.Profile.ProfileId)
getRes, err := client.Get(ctx, &northboundv1.EthernetProfileServiceGetRequest{
Domain: w.client.Domain(ctx),
Group: group(req.Collaborator),
ProfileId: profileID,
})
if err != nil {
return nil, err
}
profile := toEthernetProfile(profileID, getRes.EthernetProfile)
if err := profile.SetFields(req.Profile, req.FieldMask.GetPaths()...); err != nil {
return nil, err
}
_, err = client.Update(ctx, &northboundv1.EthernetProfileServiceUpdateRequest{
Domain: w.client.Domain(ctx),
Group: group(req.Collaborator),
ProfileId: profileID,
EthernetProfile: fromEthernetProfile(profile),
})
if err != nil {
return nil, err
}
return profile, nil
}
22 changes: 22 additions & 0 deletions pkg/gatewayconfigurationserver/managed/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,25 @@ func fromWiFiProfile(profile *ttnpb.ManagedGatewayWiFiProfile) *northboundv1.Wif
NetworkInterfaceAddresses: fromNetworkInterfaceAddresses(profile.NetworkInterfaceAddresses),
}
}

func toEthernetProfile(profileID []byte, profile *northboundv1.EthernetProfile) *ttnpb.ManagedGatewayEthernetProfile {
if profile == nil {
return nil
}
return &ttnpb.ManagedGatewayEthernetProfile{
ProfileId: toProfileID(profileID),
ProfileName: profile.ProfileName,
NetworkInterfaceAddresses: toNetworkInterfaceAddresses(profile.NetworkInterfaceAddresses),
}
}

func fromEthernetProfile(profile *ttnpb.ManagedGatewayEthernetProfile) *northboundv1.EthernetProfile {
if profile == nil {
return nil
}
return &northboundv1.EthernetProfile{
ProfileName: profile.ProfileName,
Shared: true,
NetworkInterfaceAddresses: fromNetworkInterfaceAddresses(profile.NetworkInterfaceAddresses),
}
}

0 comments on commit 049786b

Please sign in to comment.