Convert SDK to the new options API (#238)
This commit is contained in:
1 parent
b5acf6afdf
commit
908a299ea3
162 files changed
+6440
-5891
No files matched your search
@@ -1,40 +0,0 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
type AwsEc2HostCatalog struct {
|
||||
*HostCatalog
|
||||
|
||||
// The AWS regions from which this catalog will retrieve the EC2 instances.
|
||||
Regions []string `json:"regions,omitempty"`
|
||||
// The access key used for authenticating with AWS when retrieving EC2 instance details.
|
||||
AccessKey *string `json:"access_key,omitempty"`
|
||||
// Input only.
|
||||
SecretKey *string `json:"secret_key,omitempty"`
|
||||
// This value will never be returned in a response.
|
||||
Rotate *bool `json:"rotate,omitempty"`
|
||||
}
|
||||
|
||||
func (s HostCatalog) AsAwsEc2HostCatalog() (*AwsEc2HostCatalog, error) {
|
||||
out := &AwsEc2HostCatalog{
|
||||
HostCatalog: &s,
|
||||
}
|
||||
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
||||
Result: out,
|
||||
TagName: "json",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating map decoder: %w", err)
|
||||
}
|
||||
|
||||
if err := decoder.Decode(s.Attributes); err != nil {
|
||||
return nil, fmt.Errorf("error decoding attributes map: %w", err)
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
)
|
||||
|
||||
func (s HostCatalog) CreateHost(ctx context.Context, r *Host) (*Host, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in CreateHost request")
|
||||
}
|
||||
|
||||
var opts []api.Option
|
||||
if s.Scope.Id != "" {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
opts = append(opts, api.WithScopeId(s.Scope.Id))
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "POST", fmt.Sprintf("host-catalogs/%s/hosts", s.Id), r, opts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating CreateHost request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during CreateHost call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Host)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding CreateHost repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s HostCatalog) CreateHostSet(ctx context.Context, r *HostSet) (*HostSet, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in CreateHostSet request")
|
||||
}
|
||||
|
||||
var opts []api.Option
|
||||
if s.Scope.Id != "" {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
opts = append(opts, api.WithScopeId(s.Scope.Id))
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "POST", fmt.Sprintf("host-catalogs/%s/host-sets", s.Id), r, opts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating CreateHostSet request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during CreateHostSet call: %w", err)
|
||||
}
|
||||
|
||||
target := new(HostSet)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding CreateHostSet repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
+178
-57
@@ -2,75 +2,196 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/fatih/structs"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/info"
|
||||
"github.com/hashicorp/watchtower/api/internal/strutil"
|
||||
"github.com/hashicorp/watchtower/api/scopes"
|
||||
)
|
||||
|
||||
type Host struct {
|
||||
Client *api.Client `json:"-"`
|
||||
|
||||
defaultFields []string
|
||||
|
||||
// The ID of the host
|
||||
// Output only.
|
||||
Id string `json:"id,omitempty"`
|
||||
// Scope information for this resource
|
||||
// Output only.
|
||||
Scope info.Scope `json:"scope,omitempty"`
|
||||
// The type of the resource, to help differentiate schemas
|
||||
Type *string `json:"type,omitempty"`
|
||||
// Optional name for identification purposes
|
||||
Name *string `json:"name,omitempty"`
|
||||
// Optional user-set description for identification purposes
|
||||
Description *string `json:"description,omitempty"`
|
||||
// The time this resource was created
|
||||
// Output only.
|
||||
CreatedTime time.Time `json:"created_time,omitempty"`
|
||||
// The time this resource was last updated
|
||||
// Output only.
|
||||
UpdatedTime time.Time `json:"updated_time,omitempty"`
|
||||
// Whether the host is disabled
|
||||
Disabled *bool `json:"disabled,omitempty"`
|
||||
// The address (DNS or IP name) used to reach the host
|
||||
Address *string `json:"address,omitempty"`
|
||||
Id string `json:"id,omitempty"`
|
||||
Scope *scopes.ScopeInfo `json:"scope,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
CreatedTime time.Time `json:"created_time,omitempty"`
|
||||
UpdatedTime time.Time `json:"updated_time,omitempty"`
|
||||
Disabled bool `json:"disabled,omitempty"`
|
||||
Address string `json:"address,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Host) SetDefault(key string) {
|
||||
lowerKey := strings.ToLower(key)
|
||||
validMap := map[string]string{"address": "address", "createdtime": "created_time", "description": "description", "disabled": "disabled", "id": "id", "name": "name", "scope": "scope", "type": "type", "updatedtime": "updated_time"}
|
||||
for k, v := range validMap {
|
||||
if k == lowerKey || v == lowerKey {
|
||||
s.defaultFields = strutil.AppendIfMissing(s.defaultFields, v)
|
||||
return
|
||||
}
|
||||
}
|
||||
type hostClient struct {
|
||||
client *api.Client
|
||||
}
|
||||
|
||||
func (s *Host) UnsetDefault(key string) {
|
||||
lowerKey := strings.ToLower(key)
|
||||
validMap := map[string]string{"address": "address", "createdtime": "created_time", "description": "description", "disabled": "disabled", "id": "id", "name": "name", "scope": "scope", "type": "type", "updatedtime": "updated_time"}
|
||||
for k, v := range validMap {
|
||||
if k == lowerKey || v == lowerKey {
|
||||
s.defaultFields = strutil.StrListDelete(s.defaultFields, v)
|
||||
return
|
||||
}
|
||||
}
|
||||
func NewHostClient(c *api.Client) *hostClient {
|
||||
return &hostClient{client: c}
|
||||
}
|
||||
|
||||
func (s Host) MarshalJSON() ([]byte, error) {
|
||||
m := structs.Map(s)
|
||||
if m == nil {
|
||||
m = make(map[string]interface{})
|
||||
func (c *hostClient) Create(ctx context.Context, hostCatalogId string, opt ...Option) (*Host, *api.Error, error) {
|
||||
if hostCatalogId == "" {
|
||||
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into Create request")
|
||||
}
|
||||
for _, k := range s.defaultFields {
|
||||
m[k] = nil
|
||||
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
return json.Marshal(m)
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("host-catalogs/%s/hosts", hostCatalogId), opts.valueMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Create request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during Create call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Host)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Create response: %w", err)
|
||||
}
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *hostClient) Read(ctx context.Context, hostCatalogId string, hostId string, opt ...Option) (*Host, *api.Error, error) {
|
||||
if hostCatalogId == "" {
|
||||
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into Read request")
|
||||
}
|
||||
|
||||
if hostId == "" {
|
||||
return nil, nil, fmt.Errorf("empty hostId value passed into Read request")
|
||||
}
|
||||
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
_, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("host-catalogs/%s/hosts/%s", hostCatalogId, hostId), nil, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Read request: %w", err)
|
||||
}
|
||||
|
||||
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(Host)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Read response: %w", err)
|
||||
}
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *hostClient) Update(ctx context.Context, hostCatalogId string, hostId string, version uint32, opt ...Option) (*Host, *api.Error, error) {
|
||||
if hostCatalogId == "" {
|
||||
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into Update request")
|
||||
}
|
||||
if hostId == "" {
|
||||
return nil, nil, fmt.Errorf("empty hostId value passed into Update request")
|
||||
}
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "PATCH", fmt.Sprintf("host-catalogs/%s/hosts/%s", hostCatalogId, hostId), opts.valueMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Update request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during Update call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Host)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Update response: %w", err)
|
||||
}
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *hostClient) Delete(ctx context.Context, hostCatalogId string, hostId string, opt ...Option) (bool, *api.Error, error) {
|
||||
if hostCatalogId == "" {
|
||||
return false, nil, fmt.Errorf("empty hostCatalogId value passed into Delete request")
|
||||
}
|
||||
|
||||
if hostId == "" {
|
||||
return false, nil, fmt.Errorf("empty hostId value passed into Delete request")
|
||||
}
|
||||
|
||||
if c.client == nil {
|
||||
return false, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
_, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("host-catalogs/%s/hosts/%s", hostCatalogId, hostId), nil, apiOpts...)
|
||||
if err != nil {
|
||||
return false, nil, fmt.Errorf("error creating Delete request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return false, nil, fmt.Errorf("error performing client request during Delete call: %w", err)
|
||||
}
|
||||
|
||||
type deleteResponse struct {
|
||||
Existed bool
|
||||
}
|
||||
target := &deleteResponse{}
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return false, nil, fmt.Errorf("error decoding Delete response: %w", err)
|
||||
}
|
||||
|
||||
return target.Existed, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *hostClient) List(ctx context.Context, hostCatalogId string, opt ...Option) ([]*Host, *api.Error, error) {
|
||||
if hostCatalogId == "" {
|
||||
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into List request")
|
||||
}
|
||||
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
_, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("host-catalogs/%s/hosts", hostCatalogId), nil, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating List request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during List call: %w", err)
|
||||
}
|
||||
|
||||
type listResponse struct {
|
||||
Items []*Host
|
||||
}
|
||||
target := &listResponse{}
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding List response: %w", err)
|
||||
}
|
||||
|
||||
return target.Items, apiErr, nil
|
||||
}
|
||||
+159
-58
@@ -2,76 +2,177 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/fatih/structs"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/info"
|
||||
"github.com/hashicorp/watchtower/api/internal/strutil"
|
||||
"github.com/hashicorp/watchtower/api/scopes"
|
||||
)
|
||||
|
||||
type HostCatalog struct {
|
||||
Client *api.Client `json:"-"`
|
||||
|
||||
defaultFields []string
|
||||
|
||||
// The ID of the host
|
||||
// Output only.
|
||||
Id string `json:"id,omitempty"`
|
||||
// Scope information for this resource
|
||||
// Output only.
|
||||
Scope info.Scope `json:"scope,omitempty"`
|
||||
// The type of the resource, to help differentiate schemas
|
||||
Type *string `json:"type,omitempty"`
|
||||
// Optional name for identification purposes
|
||||
Name *string `json:"name,omitempty"`
|
||||
// Optional user-set description for identification purposes
|
||||
Description *string `json:"description,omitempty"`
|
||||
// The time this resource was created
|
||||
// Output only.
|
||||
CreatedTime time.Time `json:"created_time,omitempty"`
|
||||
// The time this resource was last updated
|
||||
// Output only.
|
||||
UpdatedTime time.Time `json:"updated_time,omitempty"`
|
||||
// Whether the catalog is disabled
|
||||
Disabled *bool `json:"disabled,omitempty"`
|
||||
// Attributes specific to the catalog type
|
||||
Attributes map[string]interface {
|
||||
} `json:"attributes,omitempty"`
|
||||
Id string `json:"id,omitempty"`
|
||||
Scope *scopes.ScopeInfo `json:"scope,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
CreatedTime time.Time `json:"created_time,omitempty"`
|
||||
UpdatedTime time.Time `json:"updated_time,omitempty"`
|
||||
Disabled bool `json:"disabled,omitempty"`
|
||||
Attributes map[string]interface{} `json:"attributes,omitempty"`
|
||||
}
|
||||
|
||||
func (s *HostCatalog) SetDefault(key string) {
|
||||
lowerKey := strings.ToLower(key)
|
||||
validMap := map[string]string{"attributes": "attributes", "createdtime": "created_time", "description": "description", "disabled": "disabled", "id": "id", "name": "name", "scope": "scope", "type": "type", "updatedtime": "updated_time"}
|
||||
for k, v := range validMap {
|
||||
if k == lowerKey || v == lowerKey {
|
||||
s.defaultFields = strutil.AppendIfMissing(s.defaultFields, v)
|
||||
return
|
||||
}
|
||||
}
|
||||
type hostcatalogClient struct {
|
||||
client *api.Client
|
||||
}
|
||||
|
||||
func (s *HostCatalog) UnsetDefault(key string) {
|
||||
lowerKey := strings.ToLower(key)
|
||||
validMap := map[string]string{"attributes": "attributes", "createdtime": "created_time", "description": "description", "disabled": "disabled", "id": "id", "name": "name", "scope": "scope", "type": "type", "updatedtime": "updated_time"}
|
||||
for k, v := range validMap {
|
||||
if k == lowerKey || v == lowerKey {
|
||||
s.defaultFields = strutil.StrListDelete(s.defaultFields, v)
|
||||
return
|
||||
}
|
||||
}
|
||||
func NewHostCatalogClient(c *api.Client) *hostcatalogClient {
|
||||
return &hostcatalogClient{client: c}
|
||||
}
|
||||
|
||||
func (s HostCatalog) MarshalJSON() ([]byte, error) {
|
||||
m := structs.Map(s)
|
||||
if m == nil {
|
||||
m = make(map[string]interface{})
|
||||
func (c *hostcatalogClient) Create(ctx context.Context, opt ...Option) (*HostCatalog, *api.Error, error) {
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
for _, k := range s.defaultFields {
|
||||
m[k] = nil
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("host-catalogs"), opts.valueMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Create request: %w", err)
|
||||
}
|
||||
return json.Marshal(m)
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during Create call: %w", err)
|
||||
}
|
||||
|
||||
target := new(HostCatalog)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Create response: %w", err)
|
||||
}
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *hostcatalogClient) Read(ctx context.Context, hostCatalogId string, opt ...Option) (*HostCatalog, *api.Error, error) {
|
||||
if hostCatalogId == "" {
|
||||
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into Read request")
|
||||
}
|
||||
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
_, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("host-catalogs/%s", hostCatalogId), nil, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Read request: %w", err)
|
||||
}
|
||||
|
||||
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(HostCatalog)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Read response: %w", err)
|
||||
}
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *hostcatalogClient) Update(ctx context.Context, hostCatalogId string, version uint32, opt ...Option) (*HostCatalog, *api.Error, error) {
|
||||
if hostCatalogId == "" {
|
||||
return nil, nil, fmt.Errorf("empty hostCatalogId value passed into Update request")
|
||||
}
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
opts, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "PATCH", fmt.Sprintf("host-catalogs/%s", hostCatalogId), opts.valueMap, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Update request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during Update call: %w", err)
|
||||
}
|
||||
|
||||
target := new(HostCatalog)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Update response: %w", err)
|
||||
}
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *hostcatalogClient) Delete(ctx context.Context, hostCatalogId string, opt ...Option) (bool, *api.Error, error) {
|
||||
if hostCatalogId == "" {
|
||||
return false, nil, fmt.Errorf("empty hostCatalogId value passed into Delete request")
|
||||
}
|
||||
|
||||
if c.client == nil {
|
||||
return false, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
_, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("host-catalogs/%s", hostCatalogId), nil, apiOpts...)
|
||||
if err != nil {
|
||||
return false, nil, fmt.Errorf("error creating Delete request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return false, nil, fmt.Errorf("error performing client request during Delete call: %w", err)
|
||||
}
|
||||
|
||||
type deleteResponse struct {
|
||||
Existed bool
|
||||
}
|
||||
target := &deleteResponse{}
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return false, nil, fmt.Errorf("error decoding Delete response: %w", err)
|
||||
}
|
||||
|
||||
return target.Existed, apiErr, nil
|
||||
}
|
||||
|
||||
func (c *hostcatalogClient) List(ctx context.Context, opt ...Option) ([]*HostCatalog, *api.Error, error) {
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client")
|
||||
}
|
||||
|
||||
_, apiOpts := getOpts(opt...)
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("host-catalogs"), nil, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating List request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during List call: %w", err)
|
||||
}
|
||||
|
||||
type listResponse struct {
|
||||
Items []*HostCatalog
|
||||
}
|
||||
target := &listResponse{}
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding List response: %w", err)
|
||||
}
|
||||
|
||||
return target.Items, apiErr, nil
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/fatih/structs"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/info"
|
||||
"github.com/hashicorp/watchtower/api/internal/strutil"
|
||||
)
|
||||
|
||||
type HostSet struct {
|
||||
Client *api.Client `json:"-"`
|
||||
|
||||
defaultFields []string
|
||||
|
||||
// The ID of the host
|
||||
// Output only.
|
||||
Id string `json:"id,omitempty"`
|
||||
// Scope information for this resource
|
||||
// Output only.
|
||||
Scope info.Scope `json:"scope,omitempty"`
|
||||
// The type of the resource, to help differentiate schemas
|
||||
Type *string `json:"type,omitempty"`
|
||||
// Optional name for identification purposes
|
||||
Name *string `json:"name,omitempty"`
|
||||
// Optional user-set description for identification purposes
|
||||
Description *string `json:"description,omitempty"`
|
||||
// The time this resource was created
|
||||
// Output only.
|
||||
CreatedTime time.Time `json:"created_time,omitempty"`
|
||||
// The time this resource was last updated
|
||||
// Output only.
|
||||
UpdatedTime time.Time `json:"updated_time,omitempty"`
|
||||
// Whether the host set is disabled
|
||||
Disabled *bool `json:"disabled,omitempty"`
|
||||
// The total count of hosts in this host set
|
||||
// Output only.
|
||||
Size *int64 `json:"size,omitempty"`
|
||||
// A list of hosts in this host set
|
||||
// TODO: Figure out if this should be in the basic HostSet view and what
|
||||
// view to use on the Hosts.
|
||||
// Output only.
|
||||
Hosts []*Host `json:"hosts,omitempty"`
|
||||
}
|
||||
|
||||
func (s *HostSet) SetDefault(key string) {
|
||||
lowerKey := strings.ToLower(key)
|
||||
validMap := map[string]string{"createdtime": "created_time", "description": "description", "disabled": "disabled", "hosts": "hosts", "id": "id", "name": "name", "scope": "scope", "size": "size", "type": "type", "updatedtime": "updated_time"}
|
||||
for k, v := range validMap {
|
||||
if k == lowerKey || v == lowerKey {
|
||||
s.defaultFields = strutil.AppendIfMissing(s.defaultFields, v)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *HostSet) UnsetDefault(key string) {
|
||||
lowerKey := strings.ToLower(key)
|
||||
validMap := map[string]string{"createdtime": "created_time", "description": "description", "disabled": "disabled", "hosts": "hosts", "id": "id", "name": "name", "scope": "scope", "size": "size", "type": "type", "updatedtime": "updated_time"}
|
||||
for k, v := range validMap {
|
||||
if k == lowerKey || v == lowerKey {
|
||||
s.defaultFields = strutil.StrListDelete(s.defaultFields, v)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s HostSet) MarshalJSON() ([]byte, error) {
|
||||
m := structs.Map(s)
|
||||
if m == nil {
|
||||
m = make(map[string]interface{})
|
||||
}
|
||||
for _, k := range s.defaultFields {
|
||||
m[k] = nil
|
||||
}
|
||||
return json.Marshal(m)
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/watchtower/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 {
|
||||
valueMap map[string]interface{}
|
||||
withScopeId string
|
||||
withAutomaticVersioning bool
|
||||
}
|
||||
|
||||
func getDefaultOptions() options {
|
||||
return options{
|
||||
valueMap: make(map[string]interface{}),
|
||||
}
|
||||
}
|
||||
|
||||
func getOpts(opt ...Option) (options, []api.Option) {
|
||||
opts := getDefaultOptions()
|
||||
for _, o := range opt {
|
||||
o(&opts)
|
||||
}
|
||||
var apiOpts []api.Option
|
||||
if opts.withScopeId != "" {
|
||||
apiOpts = append(apiOpts, api.WithScopeId(opts.withScopeId))
|
||||
}
|
||||
return opts, apiOpts
|
||||
}
|
||||
|
||||
func WithScopeId(id string) Option {
|
||||
return func(o *options) {
|
||||
o.withScopeId = id
|
||||
}
|
||||
}
|
||||
|
||||
// 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() Option {
|
||||
return func(o *options) {
|
||||
o.withAutomaticVersioning = true
|
||||
}
|
||||
}
|
||||
|
||||
func WithAddress(inAddress string) Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["address"] = inAddress
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultAddress() Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["address"] = nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithAttributes(inAttributes map[string]interface{}) Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["attributes"] = inAttributes
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultAttributes() Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["attributes"] = nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithDescription(inDescription string) Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["description"] = inDescription
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultDescription() Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["description"] = nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithDisabled(inDisabled bool) Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["disabled"] = inDisabled
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultDisabled() Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["disabled"] = nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithName(inName string) Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["name"] = inName
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultName() Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["name"] = nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithType(inType string) Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["type"] = inType
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultType() Option {
|
||||
return func(o *options) {
|
||||
o.valueMap["type"] = nil
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
)
|
||||
|
||||
func (s HostCatalog) ReadHostCatalog(ctx context.Context, id string) (*HostCatalog, *api.Error, error) {
|
||||
if id == "" {
|
||||
return nil, nil, fmt.Errorf("empty ID value passed into ReadHostCatalog request")
|
||||
}
|
||||
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in ReadHostCatalog request")
|
||||
}
|
||||
|
||||
var opts []api.Option
|
||||
if s.Scope.Id != "" {
|
||||
// If it's explicitly set here, override anything that might be in the
|
||||
// client
|
||||
opts = append(opts, api.WithScopeId(s.Scope.Id))
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "GET", fmt.Sprintf("%s/%s", "host-catalogs", id), nil, opts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating ReadHostCatalog request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during ReadHostCatalog call: %w", err)
|
||||
}
|
||||
|
||||
target := new(HostCatalog)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding ReadHostCatalog repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
type StaticHostCatalog struct {
|
||||
*HostCatalog
|
||||
}
|
||||
|
||||
func (s HostCatalog) AsStaticHostCatalog() (*StaticHostCatalog, error) {
|
||||
out := &StaticHostCatalog{
|
||||
HostCatalog: &s,
|
||||
}
|
||||
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
||||
Result: out,
|
||||
TagName: "json",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating map decoder: %w", err)
|
||||
}
|
||||
|
||||
if err := decoder.Decode(s.Attributes); err != nil {
|
||||
return nil, fmt.Errorf("error decoding attributes map: %w", err)
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
Reference in new issue
Block a user