mirror of
https://github.com/openziti/zrok.git
synced 2024-11-25 17:43:53 +01:00
57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
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) {
|
|
if !root.IsEnabled() {
|
|
return nil, errors.New("environment is not enabled; enable with 'zrok enable' first!")
|
|
}
|
|
|
|
out := share.NewAccessParams()
|
|
out.Body = &rest_model_zrok.AccessRequest{
|
|
ShrToken: request.ShareToken,
|
|
EnvZID: root.Environment().ZitiIdentity,
|
|
}
|
|
|
|
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
|
|
}
|