access capabilities; minor alignment and refactoring (#34)

This commit is contained in:
Michael Quigley 2023-07-18 15:30:46 -04:00
parent 7dc14445ad
commit 7a3533209a
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 69 additions and 6 deletions

52
sdk/access.go Normal file
View File

@ -0,0 +1,52 @@
package sdk
import (
httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti/zrok/environment/env_core"
"github.com/openziti/zrok/rest_client_zrok/share"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/pkg/errors"
)
func CreateAccess(root env_core.Root, request *AccessRequest) (*Access, error) {
out := share.NewAccessParams()
out.Body = &rest_model_zrok.AccessRequest{
ShrToken: request.ShareToken,
EnvZID: root.Environment().Token,
}
zrok, err := root.Client()
if err != nil {
return nil, errors.Wrap(err, "error getting zrok client")
}
auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().Token)
in, err := zrok.Share.Access(out, auth)
if err != nil {
return nil, errors.Wrap(err, "unable to create access")
}
return &Access{Token: in.Payload.FrontendToken, ShareToken: request.ShareToken, BackendMode: BackendMode(in.Payload.BackendMode)}, nil
}
func DeleteAccess(root env_core.Root, acc *Access) error {
out := share.NewUnaccessParams()
out.Body = &rest_model_zrok.UnaccessRequest{
FrontendToken: acc.Token,
ShrToken: acc.ShareToken,
EnvZID: root.Environment().ZitiIdentity,
}
zrok, err := root.Client()
if err != nil {
return errors.Wrap(err, "error getting zrok client")
}
auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().Token)
_, err = zrok.Share.Unaccess(out, auth)
if err != nil {
return errors.Wrap(err, "error deleting access")
}
return nil
}

View File

@ -24,6 +24,20 @@ type ShareRequest struct {
Target string
}
type Share struct {
Token string
}
type AccessRequest struct {
ShareToken string
}
type Access struct {
Token string
ShareToken string
BackendMode BackendMode
}
type Metrics struct {
Namespace string
Sessions map[string]SessionMetrics

View File

@ -9,10 +9,6 @@ import (
"strings"
)
type Share struct {
Token string
}
func CreateShare(root env_core.Root, request *ShareRequest) (*Share, error) {
var err error
var out *share.ShareParams
@ -77,11 +73,11 @@ func newPublicShare(root env_core.Root, request *ShareRequest) *share.ShareParam
return req
}
func DeleteShare(root env_core.Root, shrToken string) error {
func DeleteShare(root env_core.Root, shr *Share) error {
req := share.NewUnshareParams()
req.Body = &rest_model_zrok.UnshareRequest{
EnvZID: root.Environment().ZitiIdentity,
ShrToken: shrToken,
ShrToken: shr.Token,
}
zrok, err := root.Client()
@ -94,5 +90,6 @@ func DeleteShare(root env_core.Root, shrToken string) error {
if err != nil {
return errors.Wrap(err, "error deleting share")
}
return nil
}