Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clients/v1: add RolesService client methods #70

Merged
merged 8 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions clients/v1/clients.pb.go

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

92 changes: 92 additions & 0 deletions clientsv1_roles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package sams

import (
"context"
"slices"

"connectrpc.com/connect"
"golang.org/x/oauth2"

"github.com/google/uuid"
clientsv1 "github.com/sourcegraph/sourcegraph-accounts-sdk-go/clients/v1"
"github.com/sourcegraph/sourcegraph-accounts-sdk-go/clients/v1/clientsv1connect"
"github.com/sourcegraph/sourcegraph-accounts-sdk-go/roles"
"github.com/sourcegraph/sourcegraph/lib/errors"
)

// RolesServiceV1 provides client methods to interact with the
// RolesService API v1.
type RolesServiceV1 struct {
client *ClientV1
}

func (s *RolesServiceV1) newClient(ctx context.Context) clientsv1connect.RolesServiceClient {
return clientsv1connect.NewRolesServiceClient(
oauth2.NewClient(ctx, s.client.tokenSource),
s.client.gRPCURL(),
connect.WithInterceptors(s.client.defaultInterceptors...),
)
}

// RegisterResourcesMetadata is the metadata for registering resources.
type RegisterResourcesMetadata struct {
ResourceType roles.ResourceType
Revision uuid.UUID
}

func (r RegisterResourcesMetadata) validate() error {
if !slices.Contains(roles.ResourceTypes(), r.ResourceType) {
return errors.New("invalid resource type")
}
return nil
}

// RegisterRoleResources registers the resources for a given resource type.
// `resourcesIterator` is a function that returns a list of resources to register.
// The function is invoked repeatedly until it produces an empty slice or an error.
//
// Required scope: sams::roles.resources::write
func (s *RolesServiceV1) RegisterRoleResources(ctx context.Context, metadata RegisterResourcesMetadata, resourcesIterator func() ([]*clientsv1.RoleResource, error)) (uint64, error) {
err := metadata.validate()
if err != nil {
return 0, errors.Wrap(err, "invalid metadata")
}

client := s.newClient(ctx)
stream := client.RegisterRoleResources(ctx)
err = stream.Send(&clientsv1.RegisterRoleResourcesRequest{
Payload: &clientsv1.RegisterRoleResourcesRequest_Metadata{
Metadata: &clientsv1.RegisterRoleResourcesRequestMetadata{
ResourceType: string(metadata.ResourceType),
Revision: metadata.Revision.String(),
},
},
})
if err != nil {
return 0, errors.Wrap(err, "failed to send metadata")
}
for {
resources, err := resourcesIterator()
if err != nil {
return 0, errors.Wrap(err, "failed to get resources")
}
if len(resources) == 0 {
break
}
err = stream.Send(&clientsv1.RegisterRoleResourcesRequest{
Payload: &clientsv1.RegisterRoleResourcesRequest_Resources_{
Resources: &clientsv1.RegisterRoleResourcesRequest_Resources{
Resources: resources,
},
},
})
if err != nil {
return 0, errors.Wrap(err, "failed to send resources")
}
}
resp, err := stream.CloseAndReceive()
if err != nil {
return 0, errors.Wrap(err, "failed to close stream")
}
return resp.Msg.GetResourceCount(), nil
}
4 changes: 4 additions & 0 deletions clientv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ func (c *ClientV1) Tokens() *TokensServiceV1 {
return &TokensServiceV1{client: c}
}

func (c *ClientV1) Roles() *RolesServiceV1 {
return &RolesServiceV1{client: c}
}

var (
ErrNotFound = errors.New("not found")
ErrRecordMismatch = errors.New("record mismatch")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
connectrpc.com/otelconnect v0.7.1
github.com/coreos/go-oidc/v3 v3.11.0
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
github.com/hexops/autogold/v2 v2.2.1
github.com/hexops/valast v1.4.4
github.com/lestrrat-go/jwx/v2 v2.1.1
Expand Down Expand Up @@ -45,7 +46,6 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/s2a-go v0.1.8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
Expand Down
Loading