Skip to content

Commit

Permalink
Merge pull request #8 from electric-saw/rbac
Browse files Browse the repository at this point in the history
adding schema registry rbac list support
  • Loading branch information
lucasviecelli authored Dec 14, 2022
2 parents 6cda092 + d58396c commit c0dbdfc
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ccloud/iam_users_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ccloud_test

import (
"fmt"
"os"

"testing"

"github.com/electric-saw/ccloud-client-go/ccloud"
Expand All @@ -16,6 +18,14 @@ func makeClient() *ccloud.ConfluentClient {
return ccloud.NewClient(key, secret)
}

func makeRbacCrn() string {
schemaRegistryCluster := os.Getenv("SCHEMA_REGISTRY_CLUSTER")
organization := os.Getenv("ORGANIZATION")
environment := os.Getenv("ENVIRONMENT")

return fmt.Sprintf("crn://confluent.cloud/organization=%s/environment=%s/schema-registry=%s/subject=*", organization, environment, schemaRegistryCluster)
}

func TestListRoles(t *testing.T) {
c := makeClient()
users, err := c.ListUsers(&common.PaginationOptions{
Expand Down
51 changes: 51 additions & 0 deletions ccloud/rbac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ccloud

import (
"encoding/json"
"fmt"
"net/http"

"github.com/electric-saw/ccloud-client-go/ccloud/common"
)

type Rbac struct {
common.BaseModel
Principal string `json:"principal"`
RoleName string `json:"role_name"`
CrnPattern string `json:"crn_pattern"`
}

type RbacList struct {
common.BaseModel
Data []Rbac `json:"data"`
}

type SchemaRegistryRbacListOptions struct {
common.PaginationOptions
RoleName string `url:"role_name,omitempty"`
CrnPattern string `url:"crn_pattern,omitempty"`
Principal string `url:"principal,omitempty"`
}

func (c *ConfluentClient) ListSchemaRegistryRBAC(opt *SchemaRegistryRbacListOptions) (*RbacList, error) {
urlPath := "/iam/v2/role-bindings"

req, err := c.doRequest(urlPath, http.MethodGet, nil, opt)
if err != nil {
return nil, err
}

if http.StatusOK != req.StatusCode {
return nil, fmt.Errorf("failed to get schema registry rbac: %s", req.Status)
}

defer req.Body.Close()

var rbacList RbacList
err = json.NewDecoder(req.Body).Decode(&rbacList)
if err != nil {
return nil, err
}

return &rbacList, nil
}
36 changes: 36 additions & 0 deletions ccloud/rbac_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ccloud_test

import (
"testing"

"github.com/electric-saw/ccloud-client-go/ccloud"
"github.com/electric-saw/ccloud-client-go/ccloud/common"
"github.com/stretchr/testify/assert"
)

func TestGetSchemaRegistryRbac(t *testing.T) {
c := makeClient()
crn := makeRbacCrn()

rbacList, err := c.ListSchemaRegistryRBAC(
&ccloud.SchemaRegistryRbacListOptions{
PaginationOptions: common.PaginationOptions{
PageSize: 10,
},
CrnPattern: crn,
})

assert.NoError(t, err)
assert.NotNil(t, rbacList)

rbacList, err = c.ListSchemaRegistryRBAC(
&ccloud.SchemaRegistryRbacListOptions{
CrnPattern: rbacList.Data[0].CrnPattern,
Principal: rbacList.Data[0].Principal,
})

assert.NoError(t, err)
assert.NotNil(t, rbacList)


}

0 comments on commit c0dbdfc

Please sign in to comment.