mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Add utility function to retrieve a Docker resource's Portainer access control (if any)
This commit is contained in:
@ -2,6 +2,7 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/greenled/portainer-stack-utils/client"
|
"github.com/greenled/portainer-stack-utils/client"
|
||||||
@ -19,6 +20,7 @@ const (
|
|||||||
ErrSeveralEndpointsAvailable = Error("Several endpoints available")
|
ErrSeveralEndpointsAvailable = Error("Several endpoints available")
|
||||||
ErrNoEndpointsAvailable = Error("No endpoints available")
|
ErrNoEndpointsAvailable = Error("No endpoints available")
|
||||||
ErrUserNotFound = Error("User not found")
|
ErrUserNotFound = Error("User not found")
|
||||||
|
ErrAccessControlNotFound = Error("Access control not found")
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -241,3 +243,43 @@ func GetUserByName(name string) (user portainer.User, err error) {
|
|||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetDockerResourcePortainerAccessControl retrieves a Docker resource's Portainer access control (if any)
|
||||||
|
func GetDockerResourcePortainerAccessControl(endpointID portainer.EndpointID, resourceID string, resourceControlType client.ResourceType) (resourceControl portainer.ResourceControl, err error) {
|
||||||
|
portainerClient, err := GetClient()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pddr := portainerDecoratedDockerResource{}
|
||||||
|
|
||||||
|
err = portainerClient.DoJSONWithToken(fmt.Sprintf("endpoints/%d/docker/%ss/%s", endpointID, resourceControlType, resourceID), http.MethodGet, http.Header{}, nil, &pddr)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if pddr.hasAccessControl() {
|
||||||
|
resourceControl = pddr.Portainer.ResourceControl
|
||||||
|
} else {
|
||||||
|
err = ErrAccessControlNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// portainerDecoratedDockerResource represents a Docker resource decorated by Portainer
|
||||||
|
type portainerDecoratedDockerResource struct {
|
||||||
|
Portainer struct {
|
||||||
|
ResourceControl portainer.ResourceControl
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// hasAccessControl checks if a decorated Docker resource has an access control.
|
||||||
|
// Access control is a Portainer thing, not a Docker one.
|
||||||
|
func (ddr *portainerDecoratedDockerResource) hasAccessControl() bool {
|
||||||
|
// Docker resources returned by Portainer API may have a Portainer
|
||||||
|
// field with Portainer-related data. Such Portainer field may have a
|
||||||
|
// ResourceControl field, and when such ResourceControl field's ID field
|
||||||
|
// equals 0 it means the docker resource has no access control set.
|
||||||
|
return ddr.Portainer.ResourceControl.ID != 0
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user