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

Conversation

jac
Copy link
Member

@jac jac commented Nov 7, 2024

Add the client methods for the new RolesService.
This includes the RegisterRoleResources method used to register resources with SAMS for the SAMS+Entitle integration.

Test plan

CI

Ran locally with the following client code

package main

import (
	"context"
	"fmt"
	"os"
	"strings"

	"github.com/davecgh/go-spew/spew"
	sams "github.com/sourcegraph/sourcegraph-accounts-sdk-go"
	clientsv1 "github.com/sourcegraph/sourcegraph-accounts-sdk-go/clients/v1"
	"github.com/sourcegraph/sourcegraph-accounts-sdk-go/roles"
	"golang.org/x/oauth2/clientcredentials"

	"sourcegraph-accounts/internal/strutil"
)

var (
	server                  = fmt.Sprintf("http://localhost:%s", os.Getenv("PORT"))
	clientCredentialsConfig = clientcredentials.Config{
		ClientID:     os.Getenv("ACCOUNTS_CLIENT_EXAMPLE_CLIENT_ID"),
		ClientSecret: os.Getenv("ACCOUNTS_CLIENT_EXAMPLE_CLIENT_SECRET"),
		TokenURL:     fmt.Sprintf("%s/oauth/token", server),
		Scopes: strings.Split(
			strutil.Coalesce(
				os.Getenv("ACCOUNTS_CLIENT_EXAMPLE_CLIENT_SCOPES"),
				"openid,profile,email",
			),
			","),
	}
)

func main() {
	ctx := context.Background()
	client, err := sams.NewClientV1(
		sams.ClientV1Config{
			ConnConfig: sams.ConnConfig{
				ExternalURL: server,
			},
			TokenSource: clientCredentialsConfig.TokenSource(ctx),
		},
	)
	if err != nil {
		spew.Dump(err)
		return
	}

	resources := [][]*clientsv1.RoleResource{
		{
			{
				ResourceId:  "example-resource-1",
				DisplayName: "Example Resource 1",
			},
			{
				ResourceId:  "example-resource-2",
				DisplayName: "Example Resource 2",
			},
			{
				ResourceId:  "example-resource-3",
				DisplayName: "Example Resource 3",
			},
		},
		{
			{
				ResourceId:  "example-resource-10",
				DisplayName: "Example Resource 10",
			},
			{
				ResourceId:  "example-resource-20",
				DisplayName: "Example Resource 20",
			},
			{
				ResourceId:  "example-resource-30",
				DisplayName: "Example Resource 30",
			},
		},
		{
			{
				ResourceId:  "example-resource-100",
				DisplayName: "Example Resource 100",
			},
			{
				ResourceId:  "example-resource-200",
				DisplayName: "Example Resource 200",
			},
			{
				ResourceId:  "example-resource-300",
				DisplayName: "Example Resource 300",
			},
		},
	}
	index := 0
	count, err := client.Roles().RegisterRoleResources(ctx, sams.RegisterResourcesMetadata{
		ResourceType: roles.EnterpriseSubscription,
	}, func() ([]*clientsv1.RoleResource, error) {
		if index >= len(resources) {
			return nil, nil
		}
		result := resources[index]
		index++
		return result, nil
	})
	spew.Dump(count, err)
}

Output:

(uint64) 9
(interface {}) <nil>

image

@jac jac force-pushed the jac/core-397-client branch 2 times, most recently from 796e3d7 to bfd146e Compare November 8, 2024 13:55
@jac jac force-pushed the jac/core-397-client branch from bfd146e to 46ef1c7 Compare November 8, 2024 14:06
@jac jac marked this pull request as ready for review November 8, 2024 14:11
@jac jac requested a review from a team as a code owner November 8, 2024 14:11
@jac jac requested a review from bobheadxi November 8, 2024 18:54
Copy link
Member

@unknwon unknwon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request changes because let's make sure some important details are described correctly.

@jac jac requested a review from unknwon November 12, 2024 22:52
Copy link
Member

@unknwon unknwon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@jac jac merged commit f4cd2d7 into main Nov 13, 2024
2 checks passed
@jac jac deleted the jac/core-397-client branch November 13, 2024 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants