diff --git a/ccloud/iam_users_test.go b/ccloud/iam_users_test.go index 1b44a30..7ebcd54 100644 --- a/ccloud/iam_users_test.go +++ b/ccloud/iam_users_test.go @@ -1,7 +1,9 @@ package ccloud_test import ( + "fmt" "os" + "testing" "github.com/electric-saw/ccloud-client-go/ccloud" @@ -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{ diff --git a/ccloud/rbac.go b/ccloud/rbac.go new file mode 100644 index 0000000..d8ef353 --- /dev/null +++ b/ccloud/rbac.go @@ -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 +} \ No newline at end of file diff --git a/ccloud/rbac_test.go b/ccloud/rbac_test.go new file mode 100644 index 0000000..e246df6 --- /dev/null +++ b/ccloud/rbac_test.go @@ -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) + + +} \ No newline at end of file