From 009eeef47438676086bff6266b7368c8aeb0d817 Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Mon, 18 Nov 2024 22:23:24 +0530 Subject: [PATCH] PLT-1493: Added support for permissions (#142) * PLT-1493: Added support for permissions * changed permission sciope to const * added comments * fix --- client/permission.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 client/permission.go diff --git a/client/permission.go b/client/permission.go new file mode 100644 index 00000000..c04a9709 --- /dev/null +++ b/client/permission.go @@ -0,0 +1,41 @@ +package client + +import ( + "fmt" + + clientv1 "github.com/spectrocloud/palette-sdk-go/api/client/v1" + "github.com/spectrocloud/palette-sdk-go/api/models" +) + +// PermissionScope defines the scope of a permission. +type PermissionScope string + +const ( + // PermissionScopeProject represents a project-level scope. + PermissionScopeProject PermissionScope = "project" + + // PermissionScopeTenant represents a tenant-level scope. + PermissionScopeTenant PermissionScope = "tenant" + + // PermissionScopeResource represents a resource-level scope. + PermissionScopeResource PermissionScope = "resource" +) + +// GetPermissionByName retrieves an existing permission by name and permissionScope(project, tenant & resource). +func (h *V1Client) GetPermissionByName(permissionName string, permissionScope PermissionScope) (*models.V1Permission, error) { + // ACL scoped to tenant only + permScope := string(permissionScope) + params := clientv1.NewV1PermissionsListParams().WithScope(&permScope) + resp, err := h.Client.V1PermissionsList(params) + if err != nil { + return nil, err + } + + for _, permission := range resp.Payload { + if permission.Name == permissionName { + return permission, nil + } + } + + return nil, fmt.Errorf("permission name '%s' not found in scope '%s'", permissionName, permissionScope) +}