Sessions Read/List/Cancel API and SDK (#369)

This commit is contained in:
Todd Knight authored and GitHub committed 2020-09-20 20:22:57 -04:00
1 parent f7e48ec836
commit e937b0ea27
22 files changed
+2852 -130

No files matched your search

+20
View File
@@ -13,6 +13,7 @@ import (
"github.com/hashicorp/boundary/internal/gen/controller/api/resources/hostsets"
"github.com/hashicorp/boundary/internal/gen/controller/api/resources/roles"
"github.com/hashicorp/boundary/internal/gen/controller/api/resources/scopes"
"github.com/hashicorp/boundary/internal/gen/controller/api/resources/sessions"
"github.com/hashicorp/boundary/internal/gen/controller/api/resources/targets"
"github.com/hashicorp/boundary/internal/gen/controller/api/resources/users"
"google.golang.org/protobuf/proto"
@@ -356,6 +357,25 @@ var inputStructs = []*structInfo{
outFile: "targets/tcp_target_attributes.gen.go",
subtypeName: "TcpTarget",
},
{
inProto: &sessions.Session{},
outFile: "sessions/session.gen.go",
templates: []*template.Template{
clientTemplate,
readTemplate,
listTemplate,
},
pathArgs: []string{"session"},
createResponseTypes: true,
},
{
inProto: &sessions.SessionState{},
outFile: "sessions/state.gen.go",
},
{
inProto: &sessions.WorkerInfo{},
outFile: "sessions/workers.gen.go",
},
{
inProto: &targets.SessionAuthorization{},
outFile: "targets/session_authorization.gen.go",
+75
View File
@@ -0,0 +1,75 @@
package sessions
import (
"context"
"errors"
"fmt"
"net/url"
"github.com/hashicorp/boundary/api"
"github.com/kr/pretty"
)
func (c *Client) Cancel(ctx context.Context, sessionId string, version uint32, opt ...Option) (*SessionUpdateResult, *api.Error, error) {
if sessionId == "" {
return nil, nil, fmt.Errorf("empty sessionId value passed into Cancel request")
}
if c.client == nil {
return nil, nil, errors.New("nil client")
}
opts, apiOpts := getOpts(opt...)
if version == 0 {
if !opts.withAutomaticVersioning {
return nil, nil, errors.New("zero version number passed into Cancel request")
}
existingSession, existingApiErr, existingErr := c.Read(ctx, sessionId, opt...)
if existingErr != nil {
return nil, nil, fmt.Errorf("error performing initial check-and-set read: %w", existingErr)
}
if existingApiErr != nil {
return nil, nil, fmt.Errorf("error from controller when performing initial check-and-set read: %s", pretty.Sprint(existingApiErr))
}
if existingSession == nil {
return nil, nil, errors.New("nil resource response found when performing initial check-and-set read")
}
if existingSession.Item == nil {
return nil, nil, errors.New("nil resource found when performing initial check-and-set read")
}
version = existingSession.Item.Version
}
opts.postMap["version"] = version
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("sessions/%s:cancel", sessionId), opts.postMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Cancel request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Cancel call: %w", err)
}
target := new(SessionUpdateResult)
target.Item = new(Session)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Cancel response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
}
+46
View File
@@ -0,0 +1,46 @@
package sessions
import (
"github.com/hashicorp/boundary/api"
)
// Option is a func that sets optional attributes for a call. This does not need
// to be used directly, but instead option arguments are built from the
// functions in this package. WithX options set a value to that given in the
// argument; DefaultX options indicate that the value should be set to its
// default. When an API call is made options are processed in ther order they
// appear in the function call, so for a given argument X, a succession of WithX
// or DefaultX calls will result in the last call taking effect.
type Option func(*options)
type options struct {
postMap map[string]interface{}
queryMap map[string]string
withAutomaticVersioning bool
}
func getDefaultOptions() options {
return options{
postMap: make(map[string]interface{}),
queryMap: make(map[string]string),
}
}
func getOpts(opt ...Option) (options, []api.Option) {
opts := getDefaultOptions()
for _, o := range opt {
o(&opts)
}
var apiOpts []api.Option
return opts, apiOpts
}
// If set, and if the version is zero during an update, the API will perform a
// fetch to get the current version of the resource and populate it during the
// update call. This is convenient but opens up the possibility for subtle
// order-of-modification issues, so use carefully.
func WithAutomaticVersioning(enable bool) Option {
return func(o *options) {
o.withAutomaticVersioning = enable
}
}
+200
View File
@@ -0,0 +1,200 @@
// Code generated by "make api"; DO NOT EDIT.
package sessions
import (
"bytes"
"context"
"fmt"
"net/url"
"time"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/scopes"
)
type Session struct {
Id string `json:"id,omitempty"`
TargetId string `json:"target_id,omitempty"`
Scope *scopes.ScopeInfo `json:"scope,omitempty"`
CreatedTime time.Time `json:"created_time,omitempty"`
UpdatedTime time.Time `json:"updated_time,omitempty"`
ExpirationTime time.Time `json:"expiration_time,omitempty"`
Version uint32 `json:"version,omitempty"`
Type string `json:"type,omitempty"`
AuthTokenId string `json:"auth_token_id,omitempty"`
UserId string `json:"user_id,omitempty"`
HostSetId string `json:"host_set_id,omitempty"`
HostId string `json:"host_id,omitempty"`
ScopeId string `json:"scope_id,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
States []*SessionState `json:"states,omitempty"`
Status string `json:"status,omitempty"`
WorkerInfo []*WorkerInfo `json:"worker_info,omitempty"`
Certificate []byte `json:"certificate,omitempty"`
PrivateKey []byte `json:"private_key,omitempty"`
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n Session) ResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n Session) ResponseMap() map[string]interface{} {
return n.responseMap
}
type SessionReadResult struct {
Item *Session
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n SessionReadResult) GetItem() interface{} {
return n.Item
}
func (n SessionReadResult) GetResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n SessionReadResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
type SessionCreateResult = SessionReadResult
type SessionUpdateResult = SessionReadResult
type SessionDeleteResult struct {
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n SessionDeleteResult) GetResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n SessionDeleteResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
type SessionListResult struct {
Items []*Session
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n SessionListResult) GetItems() interface{} {
return n.Items
}
func (n SessionListResult) GetResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n SessionListResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
// Client is a client for this collection
type Client struct {
client *api.Client
}
// Creates a new client for this collection. The submitted API client is cloned;
// modifications to it after generating this client will not have effect. If you
// need to make changes to the underlying API client, use ApiClient() to access
// it.
func NewClient(c *api.Client) *Client {
return &Client{client: c.Clone()}
}
// ApiClient returns the underlying API client
func (c *Client) ApiClient() *api.Client {
return c.client
}
func (c *Client) Read(ctx context.Context, sessionId string, opt ...Option) (*SessionReadResult, *api.Error, error) {
if sessionId == "" {
return nil, nil, fmt.Errorf("empty sessionId value passed into Read request")
}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("sessions/%s", sessionId), nil, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Read request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Read call: %w", err)
}
target := new(SessionReadResult)
target.Item = new(Session)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Read response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
}
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) (*SessionListResult, *api.Error, error) {
if scopeId == "" {
return nil, nil, fmt.Errorf("empty scopeId value passed into List request")
}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
opts.queryMap["scope_id"] = scopeId
req, err := c.client.NewRequest(ctx, "GET", "sessions", nil, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating List request: %w", err)
}
if len(opts.queryMap) > 0 {
q := url.Values{}
for k, v := range opts.queryMap {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during List call: %w", err)
}
target := new(SessionListResult)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding List response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
}
+12
View File
@@ -0,0 +1,12 @@
// Code generated by "make api"; DO NOT EDIT.
package sessions
import (
"time"
)
type SessionState struct {
Status string `json:"status,omitempty"`
StartTime time.Time `json:"start_time,omitempty"`
EndTime time.Time `json:"end_time,omitempty"`
}
+6
View File
@@ -0,0 +1,6 @@
// Code generated by "make api"; DO NOT EDIT.
package sessions
type WorkerInfo struct {
Address string `json:"address,omitempty"`
}