chore: refactor influxid.ID
, cleanup organization checking (#326)
This commit is contained in:
parent
99791bafd3
commit
adc58b8441
@ -21,8 +21,7 @@ type Client struct {
|
||||
}
|
||||
|
||||
type Params struct {
|
||||
OrgId string
|
||||
OrgName string
|
||||
clients.OrgParams
|
||||
|
||||
StackId string
|
||||
Sources []template.Source
|
||||
@ -46,7 +45,7 @@ type ResourceFilter struct {
|
||||
}
|
||||
|
||||
func (c Client) Apply(ctx context.Context, params *Params) error {
|
||||
orgID, err := c.GetOrgId(ctx, params.OrgId, params.OrgName, c.OrganizationsApi)
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -164,7 +163,7 @@ func (c Client) printDiff(diff api.TemplateSummaryDiff, params *Params) error {
|
||||
}
|
||||
for _, l := range labels {
|
||||
var oldRow, newRow []string
|
||||
hexId := influxid.ID(l.Id).String()
|
||||
hexId := influxid.Encode(l.Id)
|
||||
if l.Old != nil {
|
||||
oldRow = buildRow(l.TemplateMetaName, hexId, *l.Old)
|
||||
}
|
||||
@ -196,7 +195,7 @@ func (c Client) printDiff(diff api.TemplateSummaryDiff, params *Params) error {
|
||||
}
|
||||
for _, b := range bkts {
|
||||
var oldRow, newRow []string
|
||||
hexId := influxid.ID(b.Id).String()
|
||||
hexId := influxid.Encode(b.Id)
|
||||
if b.Old != nil {
|
||||
oldRow = buildRow(b.TemplateMetaName, hexId, *b.Old)
|
||||
}
|
||||
@ -220,7 +219,7 @@ func (c Client) printDiff(diff api.TemplateSummaryDiff, params *Params) error {
|
||||
}
|
||||
for _, c := range checks {
|
||||
var oldRow, newRow []string
|
||||
hexId := influxid.ID(c.Id).String()
|
||||
hexId := influxid.Encode(c.Id)
|
||||
if c.Old != nil {
|
||||
oldRow = buildRow(c.TemplateMetaName, hexId, *c.Old)
|
||||
}
|
||||
@ -244,7 +243,7 @@ func (c Client) printDiff(diff api.TemplateSummaryDiff, params *Params) error {
|
||||
}
|
||||
for _, d := range dashboards {
|
||||
var oldRow, newRow []string
|
||||
hexId := influxid.ID(d.Id).String()
|
||||
hexId := influxid.Encode(d.Id)
|
||||
if d.Old != nil {
|
||||
oldRow = buildRow(d.TemplateMetaName, hexId, *d.Old)
|
||||
}
|
||||
@ -264,7 +263,7 @@ func (c Client) printDiff(diff api.TemplateSummaryDiff, params *Params) error {
|
||||
}
|
||||
for _, e := range endpoints {
|
||||
var oldRow, newRow []string
|
||||
hexId := influxid.ID(e.Id).String()
|
||||
hexId := influxid.Encode(e.Id)
|
||||
if e.Old != nil {
|
||||
oldRow = buildRow(e.TemplateMetaName, hexId, *e.Old)
|
||||
}
|
||||
@ -284,12 +283,12 @@ func (c Client) printDiff(diff api.TemplateSummaryDiff, params *Params) error {
|
||||
if nrf.Description != nil {
|
||||
desc = *nrf.Description
|
||||
}
|
||||
eid := influxid.ID(nrf.EndpointID).String()
|
||||
eid := influxid.Encode(nrf.EndpointID)
|
||||
return []string{metaName, id, nrf.Name, desc, nrf.Every, nrf.Offset, nrf.EndpointName, eid, nrf.EndpointType}
|
||||
}
|
||||
for _, r := range rules {
|
||||
var oldRow, newRow []string
|
||||
hexId := influxid.ID(r.Id).String()
|
||||
hexId := influxid.Encode(r.Id)
|
||||
if r.Old != nil {
|
||||
oldRow = buildRow(r.TemplateMetaName, hexId, *r.Old)
|
||||
}
|
||||
@ -313,7 +312,7 @@ func (c Client) printDiff(diff api.TemplateSummaryDiff, params *Params) error {
|
||||
}
|
||||
for _, t := range teles {
|
||||
var oldRow, newRow []string
|
||||
hexId := influxid.ID(t.Id).String()
|
||||
hexId := influxid.Encode(t.Id)
|
||||
if t.Old != nil {
|
||||
oldRow = buildRow(t.TemplateMetaName, hexId, *t.Old)
|
||||
}
|
||||
@ -348,7 +347,7 @@ func (c Client) printDiff(diff api.TemplateSummaryDiff, params *Params) error {
|
||||
}
|
||||
for _, t := range tasks {
|
||||
var oldRow, newRow []string
|
||||
hexId := influxid.ID(t.Id).String()
|
||||
hexId := influxid.Encode(t.Id)
|
||||
if t.Old != nil {
|
||||
oldRow = buildRow(t.TemplateMetaName, hexId, *t.Old)
|
||||
}
|
||||
@ -375,7 +374,7 @@ func (c Client) printDiff(diff api.TemplateSummaryDiff, params *Params) error {
|
||||
}
|
||||
for _, v := range vars {
|
||||
var oldRow, newRow []string
|
||||
hexId := influxid.ID(v.Id).String()
|
||||
hexId := influxid.Encode(v.Id)
|
||||
if v.Old != nil {
|
||||
oldRow = buildRow(v.TemplateMetaName, hexId, *v.Old)
|
||||
}
|
||||
@ -394,8 +393,8 @@ func (c Client) printDiff(diff api.TemplateSummaryDiff, params *Params) error {
|
||||
SetHeaders("Resource Type", "Resource Meta Name", "Resource Name", "Resource ID", "Label Package Name", "Label Name", "Label ID")
|
||||
|
||||
for _, m := range mappings {
|
||||
resId := influxid.ID(m.ResourceID).String()
|
||||
labelId := influxid.ID(m.LabelID).String()
|
||||
resId := influxid.Encode(m.ResourceID)
|
||||
labelId := influxid.Encode(m.LabelID)
|
||||
row := []string{m.ResourceType, m.ResourceName, resId, m.LabelTemplateMetaName, m.LabelName, labelId}
|
||||
switch m.StateStatus {
|
||||
case "new":
|
||||
|
@ -107,8 +107,7 @@ func BuildResourcePermissions() []*ResourcePermission {
|
||||
}
|
||||
|
||||
func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
|
||||
orgID, err := c.getOrgID(ctx, params.OrgParams)
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -131,7 +130,7 @@ func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
for _, bp := range bucketPerms {
|
||||
for _, p := range bp.perms {
|
||||
// verify the input ID
|
||||
if _, err := influxid.IDFromString(p); err != nil {
|
||||
if err := influxid.Validate(p); err != nil {
|
||||
return fmt.Errorf("invalid bucket ID '%s': %w (did you pass a bucket name instead of an ID?)", p, err)
|
||||
}
|
||||
|
||||
@ -307,8 +306,8 @@ func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
if params.OrgName != "" {
|
||||
req.Org(params.OrgName)
|
||||
}
|
||||
if params.OrgID.Valid() {
|
||||
req.OrgID(params.OrgID.String())
|
||||
if params.OrgID != "" {
|
||||
req.OrgID(params.OrgID)
|
||||
}
|
||||
|
||||
auths, err := req.Execute()
|
||||
@ -454,7 +453,3 @@ func makePermResource(permType string, id string, orgId string) api.PermissionRe
|
||||
}
|
||||
return pr
|
||||
}
|
||||
|
||||
func (c Client) getOrgID(ctx context.Context, params clients.OrgParams) (string, error) {
|
||||
return c.GetOrgIdI(ctx, params.OrgID, params.OrgName, c.OrganizationsApi)
|
||||
}
|
||||
|
@ -32,13 +32,11 @@ type Client struct {
|
||||
type Params struct {
|
||||
// Organization containing TSM data to back up.
|
||||
// If not set, all orgs will be included.
|
||||
OrgID string
|
||||
Org string
|
||||
clients.OrgParams
|
||||
|
||||
// Bucket containing TSM data to back up.
|
||||
// If not set, all buckets within the org filter will be included.
|
||||
BucketID string
|
||||
Bucket string
|
||||
clients.BucketParams
|
||||
|
||||
// Path to the directory where backup files should be written.
|
||||
Path string
|
||||
@ -51,13 +49,13 @@ func (p *Params) matches(bkt api.BucketMetadataManifest) bool {
|
||||
if p.OrgID != "" && bkt.OrganizationID != p.OrgID {
|
||||
return false
|
||||
}
|
||||
if p.Org != "" && bkt.OrganizationName != p.Org {
|
||||
if p.OrgName != "" && bkt.OrganizationName != p.OrgName {
|
||||
return false
|
||||
}
|
||||
if p.BucketID != "" && bkt.BucketID != p.BucketID {
|
||||
return false
|
||||
}
|
||||
if p.Bucket != "" && bkt.BucketName != p.Bucket {
|
||||
if p.BucketName != "" && bkt.BucketName != p.BucketName {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
@ -11,8 +11,7 @@ import (
|
||||
)
|
||||
|
||||
type BucketsCreateParams struct {
|
||||
OrgID string
|
||||
OrgName string
|
||||
clients.OrgParams
|
||||
Name string
|
||||
Description string
|
||||
Retention string
|
||||
@ -21,8 +20,9 @@ type BucketsCreateParams struct {
|
||||
}
|
||||
|
||||
func (c Client) Create(ctx context.Context, params *BucketsCreateParams) error {
|
||||
if params.OrgID == "" && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return clients.ErrMustSpecifyOrg
|
||||
orgId, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rp, err := duration.RawDurationToTimeDuration(params.Retention)
|
||||
@ -35,7 +35,7 @@ func (c Client) Create(ctx context.Context, params *BucketsCreateParams) error {
|
||||
}
|
||||
|
||||
reqBody := api.PostBucketRequest{
|
||||
OrgID: params.OrgID,
|
||||
OrgID: orgId,
|
||||
Name: params.Name,
|
||||
RetentionRules: []api.RetentionRule{},
|
||||
SchemaType: ¶ms.SchemaType,
|
||||
@ -59,21 +59,6 @@ func (c Client) Create(ctx context.Context, params *BucketsCreateParams) error {
|
||||
}
|
||||
reqBody.RetentionRules = append(reqBody.RetentionRules, *rule)
|
||||
}
|
||||
if reqBody.OrgID == "" {
|
||||
name := params.OrgName
|
||||
if name == "" {
|
||||
name = c.ActiveConfig.Org
|
||||
}
|
||||
resp, err := c.GetOrgs(ctx).Org(name).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to lookup ID of org %q: %w", name, err)
|
||||
}
|
||||
orgs := resp.GetOrgs()
|
||||
if len(orgs) == 0 {
|
||||
return fmt.Errorf("no organization found with name %q", name)
|
||||
}
|
||||
reqBody.OrgID = orgs[0].GetId()
|
||||
}
|
||||
|
||||
bucket, err := c.PostBuckets(ctx).PostBucketRequest(reqBody).Execute()
|
||||
if err != nil {
|
||||
|
@ -33,8 +33,8 @@ func TestBucketsCreate(t *testing.T) {
|
||||
{
|
||||
name: "minimal",
|
||||
params: bucket.BucketsCreateParams{
|
||||
OrgID: "123",
|
||||
Name: "my-bucket",
|
||||
OrgParams: clients.OrgParams{OrgID: "123"},
|
||||
Name: "my-bucket",
|
||||
},
|
||||
registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) {
|
||||
bucketsApi.EXPECT().PostBuckets(gomock.Any()).Return(api.ApiPostBucketsRequest{ApiService: bucketsApi})
|
||||
@ -59,7 +59,7 @@ func TestBucketsCreate(t *testing.T) {
|
||||
{
|
||||
name: "fully specified",
|
||||
params: bucket.BucketsCreateParams{
|
||||
OrgID: "123",
|
||||
OrgParams: clients.OrgParams{OrgID: "123"},
|
||||
Name: "my-bucket",
|
||||
Description: "my cool bucket",
|
||||
Retention: "24h",
|
||||
@ -92,7 +92,7 @@ func TestBucketsCreate(t *testing.T) {
|
||||
{
|
||||
name: "retention but not shard-group duration",
|
||||
params: bucket.BucketsCreateParams{
|
||||
OrgID: "123",
|
||||
OrgParams: clients.OrgParams{OrgID: "123"},
|
||||
Name: "my-bucket",
|
||||
Retention: "24h",
|
||||
},
|
||||
@ -120,7 +120,7 @@ func TestBucketsCreate(t *testing.T) {
|
||||
{
|
||||
name: "create bucket with explicit schema",
|
||||
params: bucket.BucketsCreateParams{
|
||||
OrgID: "123",
|
||||
OrgParams: clients.OrgParams{OrgID: "123"},
|
||||
Name: "my-bucket",
|
||||
SchemaType: api.SCHEMATYPE_EXPLICIT,
|
||||
},
|
||||
@ -148,7 +148,7 @@ func TestBucketsCreate(t *testing.T) {
|
||||
{
|
||||
name: "look up org by name",
|
||||
params: bucket.BucketsCreateParams{
|
||||
OrgName: "my-org",
|
||||
OrgParams: clients.OrgParams{OrgName: "my-org"},
|
||||
Name: "my-bucket",
|
||||
Description: "my cool bucket",
|
||||
Retention: "24h",
|
||||
@ -242,8 +242,8 @@ func TestBucketsCreate(t *testing.T) {
|
||||
{
|
||||
name: "no such org",
|
||||
params: bucket.BucketsCreateParams{
|
||||
OrgParams: clients.OrgParams{OrgName: "fake-org"},
|
||||
Name: "my-bucket",
|
||||
OrgName: "fake-org",
|
||||
Description: "my cool bucket",
|
||||
Retention: "24h",
|
||||
ShardGroupDuration: "1h",
|
||||
@ -252,7 +252,7 @@ func TestBucketsCreate(t *testing.T) {
|
||||
orgApi.EXPECT().GetOrgs(gomock.Any()).Return(api.ApiGetOrgsRequest{ApiService: orgApi})
|
||||
orgApi.EXPECT().GetOrgsExecute(gomock.Any()).Return(api.Organizations{}, nil)
|
||||
},
|
||||
expectedInErr: "no organization found",
|
||||
expectedInErr: "no organization with name \"fake-org\"",
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -9,27 +9,24 @@ import (
|
||||
)
|
||||
|
||||
type BucketsDeleteParams struct {
|
||||
ID string
|
||||
Name string
|
||||
OrgID string
|
||||
OrgName string
|
||||
clients.OrgBucketParams
|
||||
}
|
||||
|
||||
func (c Client) Delete(ctx context.Context, params *BucketsDeleteParams) error {
|
||||
if params.ID == "" && params.Name == "" {
|
||||
if params.BucketID == "" && params.BucketName == "" {
|
||||
return clients.ErrMustSpecifyBucket
|
||||
}
|
||||
|
||||
var bucket api.Bucket
|
||||
var getReq api.ApiGetBucketsRequest
|
||||
if params.ID != "" {
|
||||
getReq = c.GetBuckets(ctx).Id(params.ID)
|
||||
if params.BucketID != "" {
|
||||
getReq = c.GetBuckets(ctx).Id(params.BucketID)
|
||||
} else {
|
||||
if params.OrgID == "" && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return ErrMustSpecifyOrgDeleteByName
|
||||
}
|
||||
getReq = c.GetBuckets(ctx)
|
||||
getReq = getReq.Name(params.Name)
|
||||
getReq = getReq.Name(params.BucketName)
|
||||
if params.OrgID != "" {
|
||||
getReq = getReq.OrgID(params.OrgID)
|
||||
}
|
||||
@ -41,9 +38,9 @@ func (c Client) Delete(ctx context.Context, params *BucketsDeleteParams) error {
|
||||
}
|
||||
}
|
||||
|
||||
displayId := params.ID
|
||||
displayId := params.BucketID
|
||||
if displayId == "" {
|
||||
displayId = params.Name
|
||||
displayId = params.BucketName
|
||||
}
|
||||
|
||||
resp, err := getReq.Execute()
|
||||
|
@ -33,7 +33,9 @@ func TestBucketsDelete(t *testing.T) {
|
||||
name: "by ID",
|
||||
configOrgName: "my-default-org",
|
||||
params: bucket.BucketsDeleteParams{
|
||||
ID: "123",
|
||||
OrgBucketParams: clients.OrgBucketParams{
|
||||
BucketParams: clients.BucketParams{BucketID: "123"},
|
||||
},
|
||||
},
|
||||
registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) {
|
||||
bucketsApi.EXPECT().GetBuckets(gomock.Any()).Return(api.ApiGetBucketsRequest{ApiService: bucketsApi})
|
||||
@ -67,8 +69,10 @@ func TestBucketsDelete(t *testing.T) {
|
||||
name: "by name and org ID",
|
||||
configOrgName: "my-default-org",
|
||||
params: bucket.BucketsDeleteParams{
|
||||
Name: "my-bucket",
|
||||
OrgID: "456",
|
||||
OrgBucketParams: clients.OrgBucketParams{
|
||||
BucketParams: clients.BucketParams{BucketName: "my-bucket"},
|
||||
OrgParams: clients.OrgParams{OrgID: "456"},
|
||||
},
|
||||
},
|
||||
registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) {
|
||||
bucketsApi.EXPECT().GetBuckets(gomock.Any()).Return(api.ApiGetBucketsRequest{ApiService: bucketsApi})
|
||||
@ -102,8 +106,10 @@ func TestBucketsDelete(t *testing.T) {
|
||||
name: "by name and org name",
|
||||
configOrgName: "my-default-org",
|
||||
params: bucket.BucketsDeleteParams{
|
||||
Name: "my-bucket",
|
||||
OrgName: "my-org",
|
||||
OrgBucketParams: clients.OrgBucketParams{
|
||||
BucketParams: clients.BucketParams{BucketName: "my-bucket"},
|
||||
OrgParams: clients.OrgParams{OrgName: "my-org"},
|
||||
},
|
||||
},
|
||||
registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) {
|
||||
bucketsApi.EXPECT().GetBuckets(gomock.Any()).Return(api.ApiGetBucketsRequest{ApiService: bucketsApi})
|
||||
@ -137,7 +143,9 @@ func TestBucketsDelete(t *testing.T) {
|
||||
name: "by name in default org",
|
||||
configOrgName: "my-default-org",
|
||||
params: bucket.BucketsDeleteParams{
|
||||
Name: "my-bucket",
|
||||
OrgBucketParams: clients.OrgBucketParams{
|
||||
BucketParams: clients.BucketParams{BucketName: "my-bucket"},
|
||||
},
|
||||
},
|
||||
registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) {
|
||||
bucketsApi.EXPECT().GetBuckets(gomock.Any()).Return(api.ApiGetBucketsRequest{ApiService: bucketsApi})
|
||||
@ -170,14 +178,18 @@ func TestBucketsDelete(t *testing.T) {
|
||||
{
|
||||
name: "by name without org",
|
||||
params: bucket.BucketsDeleteParams{
|
||||
Name: "my-bucket",
|
||||
OrgBucketParams: clients.OrgBucketParams{
|
||||
BucketParams: clients.BucketParams{BucketName: "my-bucket"},
|
||||
},
|
||||
},
|
||||
expectedInErr: "must specify org ID or org name",
|
||||
},
|
||||
{
|
||||
name: "no such bucket",
|
||||
params: bucket.BucketsDeleteParams{
|
||||
ID: "123",
|
||||
OrgBucketParams: clients.OrgBucketParams{
|
||||
BucketParams: clients.BucketParams{BucketID: "123"},
|
||||
},
|
||||
},
|
||||
registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) {
|
||||
bucketsApi.EXPECT().GetBuckets(gomock.Any()).Return(api.ApiGetBucketsRequest{ApiService: bucketsApi})
|
||||
|
@ -9,10 +9,7 @@ import (
|
||||
)
|
||||
|
||||
type BucketsListParams struct {
|
||||
OrgID string
|
||||
OrgName string
|
||||
Name string
|
||||
ID string
|
||||
clients.OrgBucketParams
|
||||
Limit int
|
||||
Offset int
|
||||
PageSize int
|
||||
@ -33,7 +30,7 @@ func (c Client) List(ctx context.Context, params *BucketsListParams) error {
|
||||
if params.OrgID == "" && params.OrgName == "" {
|
||||
req = req.Org(c.ActiveConfig.Org)
|
||||
}
|
||||
if params.Name != "" || params.ID != "" {
|
||||
if params.BucketName != "" || params.BucketID != "" {
|
||||
return c.findOneBucket(params, req)
|
||||
}
|
||||
|
||||
@ -111,11 +108,11 @@ func (c Client) List(ctx context.Context, params *BucketsListParams) error {
|
||||
// Used to look up buckets by ID or name.
|
||||
func (c Client) findOneBucket(params *BucketsListParams, req api.ApiGetBucketsRequest) error {
|
||||
var description string
|
||||
if params.ID != "" {
|
||||
req = req.Id(params.ID)
|
||||
if params.BucketID != "" {
|
||||
req = req.Id(params.BucketID)
|
||||
description = " by ID"
|
||||
} else if params.Name != "" {
|
||||
req = req.Name(params.Name)
|
||||
} else if params.BucketName != "" {
|
||||
req = req.Name(params.BucketName)
|
||||
description = " by name"
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,9 @@ func TestBucketsList(t *testing.T) {
|
||||
{
|
||||
name: "by ID",
|
||||
params: bucket.BucketsListParams{
|
||||
ID: "123",
|
||||
OrgBucketParams: clients.OrgBucketParams{
|
||||
BucketParams: clients.BucketParams{BucketID: "123"},
|
||||
},
|
||||
},
|
||||
configOrgName: "my-default-org",
|
||||
registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) {
|
||||
@ -62,7 +64,9 @@ func TestBucketsList(t *testing.T) {
|
||||
{
|
||||
name: "by name",
|
||||
params: bucket.BucketsListParams{
|
||||
Name: "my-bucket",
|
||||
OrgBucketParams: clients.OrgBucketParams{
|
||||
BucketParams: clients.BucketParams{BucketName: "my-bucket"},
|
||||
},
|
||||
},
|
||||
configOrgName: "my-default-org",
|
||||
registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) {
|
||||
@ -148,7 +152,9 @@ func TestBucketsList(t *testing.T) {
|
||||
{
|
||||
name: "override org by ID",
|
||||
params: bucket.BucketsListParams{
|
||||
OrgID: "456",
|
||||
OrgBucketParams: clients.OrgBucketParams{
|
||||
OrgParams: clients.OrgParams{OrgID: "456"},
|
||||
},
|
||||
},
|
||||
configOrgName: "my-default-org",
|
||||
registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) {
|
||||
@ -164,8 +170,10 @@ func TestBucketsList(t *testing.T) {
|
||||
{
|
||||
name: "override org by name",
|
||||
params: bucket.BucketsListParams{
|
||||
OrgName: "my-org",
|
||||
Limit: 2,
|
||||
OrgBucketParams: clients.OrgBucketParams{
|
||||
OrgParams: clients.OrgParams{OrgName: "my-org"},
|
||||
},
|
||||
Limit: 2,
|
||||
},
|
||||
configOrgName: "my-default-org",
|
||||
registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) {
|
||||
@ -204,8 +212,10 @@ func TestBucketsList(t *testing.T) {
|
||||
{
|
||||
name: "list multiple bucket schema types",
|
||||
params: bucket.BucketsListParams{
|
||||
OrgName: "my-org",
|
||||
Limit: 3,
|
||||
OrgBucketParams: clients.OrgBucketParams{
|
||||
OrgParams: clients.OrgParams{OrgName: "my-org"},
|
||||
},
|
||||
Limit: 3,
|
||||
},
|
||||
configOrgName: "my-default-org",
|
||||
registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) {
|
||||
|
@ -6,12 +6,12 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/influx-cli/v2/api"
|
||||
"github.com/influxdata/influx-cli/v2/clients"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/duration"
|
||||
)
|
||||
|
||||
type BucketsUpdateParams struct {
|
||||
ID string
|
||||
Name string
|
||||
clients.BucketParams
|
||||
Description string
|
||||
Retention string
|
||||
ShardGroupDuration string
|
||||
@ -19,8 +19,8 @@ type BucketsUpdateParams struct {
|
||||
|
||||
func (c Client) Update(ctx context.Context, params *BucketsUpdateParams) error {
|
||||
reqBody := api.PatchBucketRequest{}
|
||||
if params.Name != "" {
|
||||
reqBody.SetName(params.Name)
|
||||
if params.BucketName != "" {
|
||||
reqBody.SetName(params.BucketName)
|
||||
}
|
||||
if params.Description != "" {
|
||||
reqBody.SetDescription(params.Description)
|
||||
@ -44,9 +44,9 @@ func (c Client) Update(ctx context.Context, params *BucketsUpdateParams) error {
|
||||
reqBody.SetRetentionRules([]api.PatchRetentionRule{*patchRule})
|
||||
}
|
||||
|
||||
bucket, err := c.PatchBucketsID(ctx, params.ID).PatchBucketRequest(reqBody).Execute()
|
||||
bucket, err := c.PatchBucketsID(ctx, params.BucketID).PatchBucketRequest(reqBody).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update bucket %q: %w", params.ID, err)
|
||||
return fmt.Errorf("failed to update bucket %q: %w", params.BucketID, err)
|
||||
}
|
||||
|
||||
return c.printBuckets(bucketPrintOptions{bucket: &bucket})
|
||||
|
@ -29,8 +29,10 @@ func TestBucketsUpdate(t *testing.T) {
|
||||
{
|
||||
name: "name",
|
||||
params: bucket.BucketsUpdateParams{
|
||||
ID: "123",
|
||||
Name: "cold-storage",
|
||||
BucketParams: clients.BucketParams{
|
||||
BucketID: "123",
|
||||
BucketName: "cold-storage",
|
||||
},
|
||||
},
|
||||
registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) {
|
||||
bucketsApi.EXPECT().PatchBucketsID(gomock.Any(), gomock.Eq("123")).
|
||||
@ -53,8 +55,8 @@ func TestBucketsUpdate(t *testing.T) {
|
||||
{
|
||||
name: "description",
|
||||
params: bucket.BucketsUpdateParams{
|
||||
ID: "123",
|
||||
Description: "a very useful description",
|
||||
BucketParams: clients.BucketParams{BucketID: "123"},
|
||||
Description: "a very useful description",
|
||||
},
|
||||
registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) {
|
||||
bucketsApi.EXPECT().PatchBucketsID(gomock.Any(), gomock.Eq("123")).
|
||||
@ -78,8 +80,8 @@ func TestBucketsUpdate(t *testing.T) {
|
||||
{
|
||||
name: "retention",
|
||||
params: bucket.BucketsUpdateParams{
|
||||
ID: "123",
|
||||
Retention: "3w",
|
||||
BucketParams: clients.BucketParams{BucketID: "123"},
|
||||
Retention: "3w",
|
||||
},
|
||||
registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) {
|
||||
bucketsApi.EXPECT().PatchBucketsID(gomock.Any(), gomock.Eq("123")).
|
||||
@ -107,7 +109,7 @@ func TestBucketsUpdate(t *testing.T) {
|
||||
{
|
||||
name: "shard-group duration",
|
||||
params: bucket.BucketsUpdateParams{
|
||||
ID: "123",
|
||||
BucketParams: clients.BucketParams{BucketID: "123"},
|
||||
ShardGroupDuration: "10h30m",
|
||||
},
|
||||
registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) {
|
||||
|
@ -39,26 +39,27 @@ func (c Client) resolveMeasurement(ctx context.Context, ids orgBucketID, name st
|
||||
return res.MeasurementSchemas[0].Id, nil
|
||||
}
|
||||
|
||||
// todo move to params.go
|
||||
func (c Client) resolveOrgBucketIds(ctx context.Context, params clients.OrgBucketParams) (*orgBucketID, error) {
|
||||
if params.BucketName == "" && !params.BucketID.Valid() {
|
||||
if params.BucketName == "" && params.BucketID == "" {
|
||||
return nil, errors.New("bucket missing: specify bucket ID or bucket name")
|
||||
}
|
||||
|
||||
if !params.OrgID.Valid() && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
if params.OrgID == "" && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return nil, errors.New("org missing: specify org ID or org name")
|
||||
}
|
||||
|
||||
req := c.GetBuckets(ctx)
|
||||
var nameID string
|
||||
if params.BucketID.Valid() {
|
||||
req = req.Id(params.BucketID.String())
|
||||
nameID = params.BucketID.String()
|
||||
if params.BucketID != "" {
|
||||
req = req.Id(params.BucketID)
|
||||
nameID = params.BucketID
|
||||
} else {
|
||||
req = req.Name(params.BucketName)
|
||||
nameID = params.BucketName
|
||||
}
|
||||
if params.OrgID.Valid() {
|
||||
req = req.OrgID(params.OrgID.String())
|
||||
if params.OrgID != "" {
|
||||
req = req.OrgID(params.OrgID)
|
||||
} else if params.OrgName != "" {
|
||||
req = req.Org(params.OrgName)
|
||||
} else {
|
||||
|
@ -16,7 +16,6 @@ import (
|
||||
"github.com/influxdata/influx-cli/v2/clients/bucket_schema"
|
||||
"github.com/influxdata/influx-cli/v2/internal/mock"
|
||||
"github.com/influxdata/influx-cli/v2/internal/testutils"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
tmock "github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -26,9 +25,9 @@ func TestClient_Create(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var (
|
||||
orgID = influxid.MustIDFromString("deadf00dbaadf00d")
|
||||
bucketID = influxid.MustIDFromString("f00ddeadf00dbaad")
|
||||
measurementID = influxid.MustIDFromString("1010f00ddeedfeed")
|
||||
orgID = "deadf00dbaadf00d"
|
||||
bucketID = "f00ddeadf00dbaad"
|
||||
measurementID = "1010f00ddeedfeed"
|
||||
createdAt = time.Date(2004, 4, 9, 2, 15, 0, 0, time.UTC)
|
||||
)
|
||||
|
||||
@ -45,9 +44,9 @@ func TestClient_Create(t *testing.T) {
|
||||
|
||||
type args struct {
|
||||
OrgName string
|
||||
OrgID influxid.ID
|
||||
OrgID string
|
||||
BucketName string
|
||||
BucketID influxid.ID
|
||||
BucketID string
|
||||
Name string
|
||||
ColumnsFile string
|
||||
ExtendedOutput bool
|
||||
@ -80,8 +79,8 @@ func TestClient_Create(t *testing.T) {
|
||||
var buckets []api.Bucket
|
||||
if len(n) == 1 {
|
||||
bucket := api.NewBucket(n[0], nil)
|
||||
bucket.SetOrgID(orgID.String())
|
||||
bucket.SetId(bucketID.String())
|
||||
bucket.SetOrgID(orgID)
|
||||
bucket.SetId(bucketID)
|
||||
bucket.SetName(n[0])
|
||||
buckets = []api.Bucket{*bucket}
|
||||
}
|
||||
@ -95,9 +94,9 @@ func TestClient_Create(t *testing.T) {
|
||||
a.buckets.EXPECT().
|
||||
GetBucketsExecute(tmock.MatchedBy(func(in api.ApiGetBucketsRequest) bool {
|
||||
matchOrg := (in.GetOrg() != nil && *in.GetOrg() == a.params.OrgName) ||
|
||||
(in.GetOrgID() != nil && a.params.OrgID.Valid() && *in.GetOrgID() == a.params.OrgID.String())
|
||||
(in.GetOrgID() != nil && *in.GetOrgID() == a.params.OrgID)
|
||||
matchBucket := (in.GetName() != nil && *in.GetName() == a.params.BucketName) ||
|
||||
(in.GetId() != nil && a.params.BucketID.Valid() && *in.GetId() == a.params.BucketID.String())
|
||||
(in.GetId() != nil && *in.GetId() == a.params.BucketID)
|
||||
return matchOrg && matchBucket
|
||||
})).
|
||||
Return(api.Buckets{Buckets: &buckets}, nil)
|
||||
@ -122,19 +121,19 @@ func TestClient_Create(t *testing.T) {
|
||||
return func(t *testing.T, a *setupArgs) {
|
||||
t.Helper()
|
||||
|
||||
req := api.ApiCreateMeasurementSchemaRequest{ApiService: a.schemas}.BucketID(bucketID.String())
|
||||
req := api.ApiCreateMeasurementSchemaRequest{ApiService: a.schemas}.BucketID(bucketID)
|
||||
|
||||
a.schemas.EXPECT().
|
||||
CreateMeasurementSchema(gomock.Any(), bucketID.String()).
|
||||
CreateMeasurementSchema(gomock.Any(), bucketID).
|
||||
Return(req)
|
||||
|
||||
a.schemas.EXPECT().
|
||||
CreateMeasurementSchemaExecute(tmock.MatchedBy(func(in api.ApiCreateMeasurementSchemaRequest) bool {
|
||||
orgIDPtr := orgID.String()
|
||||
return assert.Equal(t, in.GetOrgID(), &orgIDPtr) && assert.Equal(t, in.GetBucketID(), bucketID.String())
|
||||
orgIDPtr := orgID
|
||||
return assert.Equal(t, in.GetOrgID(), &orgIDPtr) && assert.Equal(t, in.GetBucketID(), bucketID)
|
||||
})).
|
||||
Return(api.MeasurementSchema{
|
||||
Id: measurementID.String(),
|
||||
Id: measurementID,
|
||||
Name: a.params.Name,
|
||||
Columns: a.cols,
|
||||
CreatedAt: createdAt,
|
||||
|
@ -16,7 +16,6 @@ import (
|
||||
"github.com/influxdata/influx-cli/v2/clients/bucket_schema"
|
||||
"github.com/influxdata/influx-cli/v2/internal/mock"
|
||||
"github.com/influxdata/influx-cli/v2/internal/testutils"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
tmock "github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -26,9 +25,9 @@ func TestClient_List(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var (
|
||||
orgID = influxid.MustIDFromString("deadf00dbaadf00d")
|
||||
bucketID = influxid.MustIDFromString("f00ddeadf00dbaad")
|
||||
measurementID = influxid.MustIDFromString("1010f00ddeedfeed")
|
||||
orgID = "deadf00dbaadf00d"
|
||||
bucketID = "f00ddeadf00dbaad"
|
||||
measurementID = "1010f00ddeedfeed"
|
||||
createdAt = time.Date(2004, 4, 9, 2, 15, 0, 0, time.UTC)
|
||||
updatedAt = time.Date(2009, 9, 1, 2, 15, 0, 0, time.UTC)
|
||||
)
|
||||
@ -46,9 +45,9 @@ func TestClient_List(t *testing.T) {
|
||||
|
||||
type args struct {
|
||||
OrgName string
|
||||
OrgID influxid.ID
|
||||
OrgID string
|
||||
BucketName string
|
||||
BucketID influxid.ID
|
||||
BucketID string
|
||||
Name string
|
||||
ExtendedOutput bool
|
||||
}
|
||||
@ -74,8 +73,8 @@ func TestClient_List(t *testing.T) {
|
||||
var buckets []api.Bucket
|
||||
if len(n) == 1 {
|
||||
bucket := api.NewBucket(n[0], nil)
|
||||
bucket.SetOrgID(orgID.String())
|
||||
bucket.SetId(bucketID.String())
|
||||
bucket.SetOrgID(orgID)
|
||||
bucket.SetId(bucketID)
|
||||
bucket.SetName(n[0])
|
||||
buckets = []api.Bucket{*bucket}
|
||||
}
|
||||
@ -89,9 +88,9 @@ func TestClient_List(t *testing.T) {
|
||||
a.buckets.EXPECT().
|
||||
GetBucketsExecute(tmock.MatchedBy(func(in api.ApiGetBucketsRequest) bool {
|
||||
matchOrg := (in.GetOrg() != nil && *in.GetOrg() == a.params.OrgName) ||
|
||||
(in.GetOrgID() != nil && a.params.OrgID.Valid() && *in.GetOrgID() == a.params.OrgID.String())
|
||||
(in.GetOrgID() != nil && *in.GetOrgID() == a.params.OrgID)
|
||||
matchBucket := (in.GetName() != nil && *in.GetName() == a.params.BucketName) ||
|
||||
(in.GetId() != nil && a.params.BucketID.Valid() && *in.GetId() == a.params.BucketID.String())
|
||||
(in.GetId() != nil && *in.GetId() == a.params.BucketID)
|
||||
return matchOrg && matchBucket
|
||||
})).
|
||||
Return(api.Buckets{Buckets: &buckets}, nil)
|
||||
@ -116,22 +115,22 @@ func TestClient_List(t *testing.T) {
|
||||
return func(t *testing.T, a *setupArgs) {
|
||||
t.Helper()
|
||||
|
||||
req := api.ApiGetMeasurementSchemasRequest{ApiService: a.schemas}.BucketID(bucketID.String())
|
||||
req := api.ApiGetMeasurementSchemasRequest{ApiService: a.schemas}.BucketID(bucketID)
|
||||
|
||||
a.schemas.EXPECT().
|
||||
GetMeasurementSchemas(gomock.Any(), bucketID.String()).
|
||||
GetMeasurementSchemas(gomock.Any(), bucketID).
|
||||
Return(req)
|
||||
|
||||
a.schemas.EXPECT().
|
||||
GetMeasurementSchemasExecute(tmock.MatchedBy(func(in api.ApiGetMeasurementSchemasRequest) bool {
|
||||
return (in.GetOrgID() != nil && *in.GetOrgID() == orgID.String()) &&
|
||||
in.GetBucketID() == bucketID.String() &&
|
||||
return (in.GetOrgID() != nil && *in.GetOrgID() == orgID) &&
|
||||
in.GetBucketID() == bucketID &&
|
||||
(in.GetName() != nil && *in.GetName() == a.params.Name)
|
||||
})).
|
||||
Return(api.MeasurementSchemaList{
|
||||
MeasurementSchemas: []api.MeasurementSchema{
|
||||
{
|
||||
Id: measurementID.String(),
|
||||
Id: measurementID,
|
||||
Name: a.params.Name,
|
||||
Columns: a.cols,
|
||||
CreatedAt: createdAt,
|
||||
@ -177,7 +176,7 @@ func TestClient_List(t *testing.T) {
|
||||
{
|
||||
name: "bucket not found by id",
|
||||
opts: opts(
|
||||
withArgs(args{OrgName: "my-org", BucketID: influxid.MustIDFromString("baadf00d7777deed")}),
|
||||
withArgs(args{OrgName: "my-org", BucketID: "baadf00d7777deed"}),
|
||||
expGetBuckets(),
|
||||
),
|
||||
expErr: `bucket "baadf00d7777deed" not found`,
|
||||
|
@ -16,7 +16,6 @@ import (
|
||||
"github.com/influxdata/influx-cli/v2/clients/bucket_schema"
|
||||
"github.com/influxdata/influx-cli/v2/internal/mock"
|
||||
"github.com/influxdata/influx-cli/v2/internal/testutils"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
tmock "github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -26,9 +25,9 @@ func TestClient_Update(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var (
|
||||
orgID = influxid.MustIDFromString("deadf00dbaadf00d")
|
||||
bucketID = influxid.MustIDFromString("f00ddeadf00dbaad")
|
||||
measurementID = influxid.MustIDFromString("1010f00ddeedfeed")
|
||||
orgID = "deadf00dbaadf00d"
|
||||
bucketID = "f00ddeadf00dbaad"
|
||||
measurementID = "1010f00ddeedfeed"
|
||||
createdAt = time.Date(2004, 4, 9, 2, 15, 0, 0, time.UTC)
|
||||
updatedAt = time.Date(2009, 9, 1, 2, 15, 0, 0, time.UTC)
|
||||
)
|
||||
@ -46,9 +45,9 @@ func TestClient_Update(t *testing.T) {
|
||||
|
||||
type args struct {
|
||||
OrgName string
|
||||
OrgID influxid.ID
|
||||
OrgID string
|
||||
BucketName string
|
||||
BucketID influxid.ID
|
||||
BucketID string
|
||||
Name string
|
||||
ColumnsFile string
|
||||
ExtendedOutput bool
|
||||
@ -81,8 +80,8 @@ func TestClient_Update(t *testing.T) {
|
||||
var buckets []api.Bucket
|
||||
if len(n) == 1 {
|
||||
bucket := api.NewBucket(n[0], nil)
|
||||
bucket.SetOrgID(orgID.String())
|
||||
bucket.SetId(bucketID.String())
|
||||
bucket.SetOrgID(orgID)
|
||||
bucket.SetId(bucketID)
|
||||
bucket.SetName(n[0])
|
||||
buckets = []api.Bucket{*bucket}
|
||||
}
|
||||
@ -96,9 +95,9 @@ func TestClient_Update(t *testing.T) {
|
||||
a.buckets.EXPECT().
|
||||
GetBucketsExecute(tmock.MatchedBy(func(in api.ApiGetBucketsRequest) bool {
|
||||
matchOrg := (in.GetOrg() != nil && *in.GetOrg() == a.params.OrgName) ||
|
||||
(in.GetOrgID() != nil && a.params.OrgID.Valid() && *in.GetOrgID() == a.params.OrgID.String())
|
||||
(in.GetOrgID() != nil && *in.GetOrgID() == a.params.OrgID)
|
||||
matchBucket := (in.GetName() != nil && *in.GetName() == a.params.BucketName) ||
|
||||
(in.GetId() != nil && a.params.BucketID.Valid() && *in.GetId() == a.params.BucketID.String())
|
||||
(in.GetId() != nil && *in.GetId() == a.params.BucketID)
|
||||
return matchOrg && matchBucket
|
||||
})).
|
||||
Return(api.Buckets{Buckets: &buckets}, nil)
|
||||
@ -123,22 +122,22 @@ func TestClient_Update(t *testing.T) {
|
||||
return func(t *testing.T, a *setupArgs) {
|
||||
t.Helper()
|
||||
|
||||
req := api.ApiGetMeasurementSchemasRequest{ApiService: a.schemas}.BucketID(bucketID.String())
|
||||
req := api.ApiGetMeasurementSchemasRequest{ApiService: a.schemas}.BucketID(bucketID)
|
||||
|
||||
a.schemas.EXPECT().
|
||||
GetMeasurementSchemas(gomock.Any(), bucketID.String()).
|
||||
GetMeasurementSchemas(gomock.Any(), bucketID).
|
||||
Return(req)
|
||||
|
||||
a.schemas.EXPECT().
|
||||
GetMeasurementSchemasExecute(tmock.MatchedBy(func(in api.ApiGetMeasurementSchemasRequest) bool {
|
||||
return (in.GetOrgID() != nil && *in.GetOrgID() == orgID.String()) &&
|
||||
in.GetBucketID() == bucketID.String() &&
|
||||
return (in.GetOrgID() != nil && *in.GetOrgID() == orgID) &&
|
||||
in.GetBucketID() == bucketID &&
|
||||
(in.GetName() != nil && *in.GetName() == a.params.Name)
|
||||
})).
|
||||
Return(api.MeasurementSchemaList{
|
||||
MeasurementSchemas: []api.MeasurementSchema{
|
||||
{
|
||||
Id: measurementID.String(),
|
||||
Id: measurementID,
|
||||
Name: a.params.Name,
|
||||
Columns: a.cols,
|
||||
CreatedAt: createdAt,
|
||||
@ -153,22 +152,22 @@ func TestClient_Update(t *testing.T) {
|
||||
return func(t *testing.T, a *setupArgs) {
|
||||
t.Helper()
|
||||
|
||||
req := api.ApiUpdateMeasurementSchemaRequest{ApiService: a.schemas}.BucketID(bucketID.String()).MeasurementID(measurementID.String())
|
||||
req := api.ApiUpdateMeasurementSchemaRequest{ApiService: a.schemas}.BucketID(bucketID).MeasurementID(measurementID)
|
||||
|
||||
a.schemas.EXPECT().
|
||||
UpdateMeasurementSchema(gomock.Any(), bucketID.String(), measurementID.String()).
|
||||
UpdateMeasurementSchema(gomock.Any(), bucketID, measurementID).
|
||||
Return(req)
|
||||
|
||||
a.schemas.EXPECT().
|
||||
UpdateMeasurementSchemaExecute(tmock.MatchedBy(func(in api.ApiUpdateMeasurementSchemaRequest) bool {
|
||||
orgIDPtr := orgID.String()
|
||||
orgIDPtr := orgID
|
||||
return assert.Equal(t, in.GetOrgID(), &orgIDPtr) &&
|
||||
assert.Equal(t, in.GetBucketID(), bucketID.String()) &&
|
||||
assert.Equal(t, in.GetMeasurementID(), measurementID.String()) &&
|
||||
assert.Equal(t, in.GetBucketID(), bucketID) &&
|
||||
assert.Equal(t, in.GetMeasurementID(), measurementID) &&
|
||||
assert.Equal(t, in.GetMeasurementSchemaUpdateRequest().Columns, a.cols)
|
||||
})).
|
||||
Return(api.MeasurementSchema{
|
||||
Id: measurementID.String(),
|
||||
Id: measurementID,
|
||||
Name: a.params.Name,
|
||||
Columns: a.cols,
|
||||
CreatedAt: createdAt,
|
||||
|
@ -1,13 +1,9 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/influxdata/influx-cli/v2/api"
|
||||
"github.com/influxdata/influx-cli/v2/config"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/stdio"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/tabwriter"
|
||||
)
|
||||
@ -42,32 +38,3 @@ func (c *CLI) PrintTable(headers []string, rows ...map[string]interface{}) error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *CLI) GetOrgId(ctx context.Context, paramOrgId, paramOrgName string, orgApi api.OrganizationsApi) (string, error) {
|
||||
if paramOrgId != "" {
|
||||
return paramOrgId, nil
|
||||
}
|
||||
orgName := paramOrgName
|
||||
if orgName == "" {
|
||||
orgName = c.ActiveConfig.Org
|
||||
}
|
||||
if orgName == "" {
|
||||
return "", ErrMustSpecifyOrg
|
||||
}
|
||||
res, err := orgApi.GetOrgs(ctx).Org(orgName).Execute()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to lookup org with name %q: %w", orgName, err)
|
||||
}
|
||||
if len(res.GetOrgs()) == 0 {
|
||||
return "", fmt.Errorf("no organization with name %q", orgName)
|
||||
}
|
||||
return res.GetOrgs()[0].GetId(), nil
|
||||
}
|
||||
|
||||
func (c *CLI) GetOrgIdI(ctx context.Context, paramOrgId influxid.ID, paramOrgName string, orgApi api.OrganizationsApi) (string, error) {
|
||||
orgId := ""
|
||||
if paramOrgId.Valid() {
|
||||
orgId = paramOrgId.String()
|
||||
}
|
||||
return c.GetOrgId(ctx, orgId, paramOrgName, orgApi)
|
||||
}
|
||||
|
@ -19,20 +19,20 @@ type Params struct {
|
||||
}
|
||||
|
||||
func (c Client) List(ctx context.Context, params *Params) error {
|
||||
if !params.OrgID.Valid() && params.OrgName == "" && c.ActiveConfig.Org == "" && len(params.Ids) == 0 {
|
||||
if params.OrgID == "" && params.OrgName == "" && c.ActiveConfig.Org == "" && len(params.Ids) == 0 {
|
||||
return fmt.Errorf("at least one of org, org-id, or id must be provided")
|
||||
}
|
||||
|
||||
const limit = 100
|
||||
req := c.GetDashboards(ctx)
|
||||
req = req.Limit(limit)
|
||||
if params.OrgID.Valid() {
|
||||
req = req.OrgID(params.OrgID.String())
|
||||
if params.OrgID != "" {
|
||||
req = req.OrgID(params.OrgID)
|
||||
}
|
||||
if params.OrgName != "" {
|
||||
req = req.Org(params.OrgName)
|
||||
}
|
||||
if !params.OrgID.Valid() && params.OrgName == "" {
|
||||
if params.OrgID == "" && params.OrgName == "" {
|
||||
req = req.Org(c.ActiveConfig.Org)
|
||||
}
|
||||
dashboards, err := req.Id(params.Ids).Execute()
|
||||
|
@ -2,7 +2,6 @@ package delete
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@ -22,14 +21,12 @@ type Params struct {
|
||||
Stop string
|
||||
}
|
||||
|
||||
var ErrMustSpecifyBucket = errors.New("must specify bucket ID or bucket name")
|
||||
|
||||
func (c Client) Delete(ctx context.Context, params *Params) error {
|
||||
if !params.OrgID.Valid() && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
if params.OrgID == "" && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return clients.ErrMustSpecifyOrg
|
||||
}
|
||||
if !params.BucketID.Valid() && params.BucketName == "" {
|
||||
return ErrMustSpecifyBucket
|
||||
if params.BucketID == "" && params.BucketName == "" {
|
||||
return clients.ErrMustSpecifyBucket
|
||||
}
|
||||
start, err := time.Parse(time.RFC3339Nano, params.Start)
|
||||
if err != nil {
|
||||
@ -46,15 +43,15 @@ func (c Client) Delete(ctx context.Context, params *Params) error {
|
||||
}
|
||||
|
||||
req := c.PostDelete(ctx).DeletePredicateRequest(*reqBody)
|
||||
if params.OrgID.Valid() {
|
||||
req = req.OrgID(params.OrgID.String())
|
||||
if params.OrgID != "" {
|
||||
req = req.OrgID(params.OrgID)
|
||||
} else if params.OrgName != "" {
|
||||
req = req.Org(params.OrgName)
|
||||
} else {
|
||||
req = req.Org(c.ActiveConfig.Org)
|
||||
}
|
||||
if params.BucketID.Valid() {
|
||||
req = req.BucketID(params.BucketID.String())
|
||||
if params.BucketID != "" {
|
||||
req = req.BucketID(params.BucketID)
|
||||
} else {
|
||||
req = req.Bucket(params.BucketName)
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ import (
|
||||
"github.com/influxdata/influx-cli/v2/clients/delete"
|
||||
"github.com/influxdata/influx-cli/v2/config"
|
||||
"github.com/influxdata/influx-cli/v2/internal/mock"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
tmock "github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -20,8 +19,8 @@ import (
|
||||
func TestClient_Delete(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
id1, _ := influxid.IDFromString("1111111111111111")
|
||||
id2, _ := influxid.IDFromString("2222222222222222")
|
||||
id1 := "1111111111111111"
|
||||
id2 := "2222222222222222"
|
||||
|
||||
start, _ := time.Parse(time.RFC3339Nano, "2020-01-01T00:00:00Z")
|
||||
stop, _ := time.Parse(time.RFC3339Nano, "2021-01-01T00:00:00Z")
|
||||
@ -53,9 +52,9 @@ func TestClient_Delete(t *testing.T) {
|
||||
delApi.EXPECT().PostDeleteExecute(tmock.MatchedBy(func(in api.ApiPostDeleteRequest) bool {
|
||||
body := in.GetDeletePredicateRequest()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id1.String(), *in.GetOrgID()) &&
|
||||
assert.Equal(t, id1, *in.GetOrgID()) &&
|
||||
assert.Nil(t, in.GetOrg()) &&
|
||||
assert.Equal(t, id2.String(), *in.GetBucketID()) &&
|
||||
assert.Equal(t, id2, *in.GetBucketID()) &&
|
||||
assert.Nil(t, in.GetBucket()) &&
|
||||
assert.Equal(t, start, body.GetStart()) &&
|
||||
assert.Equal(t, stop, body.GetStop()) &&
|
||||
@ -83,7 +82,7 @@ func TestClient_Delete(t *testing.T) {
|
||||
delApi.EXPECT().PostDeleteExecute(tmock.MatchedBy(func(in api.ApiPostDeleteRequest) bool {
|
||||
body := in.GetDeletePredicateRequest()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id1.String(), *in.GetOrgID()) &&
|
||||
assert.Equal(t, id1, *in.GetOrgID()) &&
|
||||
assert.Nil(t, in.GetOrg()) &&
|
||||
assert.Equal(t, "my-bucket", *in.GetBucket()) &&
|
||||
assert.Nil(t, in.GetBucketID()) &&
|
||||
@ -116,7 +115,7 @@ func TestClient_Delete(t *testing.T) {
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, "my-org", *in.GetOrg()) &&
|
||||
assert.Nil(t, in.GetOrgID()) &&
|
||||
assert.Equal(t, id2.String(), *in.GetBucketID()) &&
|
||||
assert.Equal(t, id2, *in.GetBucketID()) &&
|
||||
assert.Nil(t, in.GetBucket()) &&
|
||||
assert.Equal(t, start, body.GetStart()) &&
|
||||
assert.Equal(t, stop, body.GetStop()) &&
|
||||
@ -160,7 +159,7 @@ func TestClient_Delete(t *testing.T) {
|
||||
{
|
||||
name: "no bucket",
|
||||
defaultOrgName: "my-default-org",
|
||||
expectedErr: delete.ErrMustSpecifyBucket.Error(),
|
||||
expectedErr: clients.ErrMustSpecifyBucket.Error(),
|
||||
},
|
||||
{
|
||||
name: "bad start",
|
||||
|
@ -62,32 +62,16 @@ func (c Client) Export(ctx context.Context, params *Params) error {
|
||||
type AllParams struct {
|
||||
template.OutParams
|
||||
|
||||
OrgId string
|
||||
OrgName string
|
||||
clients.OrgParams
|
||||
|
||||
LabelFilters []string
|
||||
KindFilters []string
|
||||
}
|
||||
|
||||
func (c Client) ExportAll(ctx context.Context, params *AllParams) error {
|
||||
if params.OrgId == "" && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return clients.ErrMustSpecifyOrg
|
||||
}
|
||||
|
||||
orgId := params.OrgId
|
||||
if orgId == "" {
|
||||
orgName := params.OrgName
|
||||
if orgName == "" {
|
||||
orgName = c.ActiveConfig.Org
|
||||
}
|
||||
res, err := c.GetOrgs(ctx).Org(orgName).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to look up ID for org %q: %w", orgName, err)
|
||||
}
|
||||
if len(res.GetOrgs()) == 0 {
|
||||
return fmt.Errorf("no org found with name %q", orgName)
|
||||
}
|
||||
orgId = res.GetOrgs()[0].GetId()
|
||||
orgId, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
orgExport := api.TemplateExportOrgIDs{OrgID: &orgId}
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/influxdata/influx-cli/v2/api"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/influxdata/influx-cli/v2/clients"
|
||||
)
|
||||
|
||||
type CreateParams struct {
|
||||
@ -25,30 +25,29 @@ func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
return c.printOrgs(printOrgOpts{org: &res})
|
||||
}
|
||||
|
||||
func (c Client) Delete(ctx context.Context, id influxid.ID) error {
|
||||
org, err := c.GetOrgsID(ctx, id.String()).Execute()
|
||||
func (c Client) Delete(ctx context.Context, id string) error {
|
||||
org, err := c.GetOrgsID(ctx, id).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("org %q not found: %w", id, err)
|
||||
|
||||
}
|
||||
if err := c.DeleteOrgsID(ctx, id.String()).Execute(); err != nil {
|
||||
if err := c.DeleteOrgsID(ctx, id).Execute(); err != nil {
|
||||
return fmt.Errorf("failed to delete org %q: %w", id, err)
|
||||
}
|
||||
return c.printOrgs(printOrgOpts{org: &org, deleted: true})
|
||||
}
|
||||
|
||||
type ListParams struct {
|
||||
Name string
|
||||
ID influxid.ID
|
||||
clients.OrgParams
|
||||
}
|
||||
|
||||
func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
req := c.GetOrgs(ctx)
|
||||
if params.Name != "" {
|
||||
req = req.Org(params.Name)
|
||||
if params.OrgName != "" {
|
||||
req = req.Org(params.OrgName)
|
||||
}
|
||||
if params.ID.Valid() {
|
||||
req = req.OrgID(params.ID.String())
|
||||
if params.OrgID != "" {
|
||||
req = req.OrgID(params.OrgID)
|
||||
}
|
||||
orgs, err := req.Execute()
|
||||
if err != nil {
|
||||
@ -62,23 +61,22 @@ func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
}
|
||||
|
||||
type UpdateParams struct {
|
||||
ID influxid.ID
|
||||
Name string
|
||||
clients.OrgParams
|
||||
Description string
|
||||
}
|
||||
|
||||
func (c Client) Update(ctx context.Context, params *UpdateParams) error {
|
||||
body := api.PatchOrganizationRequest{}
|
||||
if params.Name != "" {
|
||||
body.Name = ¶ms.Name
|
||||
if params.OrgName != "" {
|
||||
body.Name = ¶ms.OrgName
|
||||
}
|
||||
if params.Description != "" {
|
||||
body.Description = ¶ms.Description
|
||||
}
|
||||
|
||||
res, err := c.PatchOrgsID(ctx, params.ID.String()).PatchOrganizationRequest(body).Execute()
|
||||
res, err := c.PatchOrgsID(ctx, params.OrgID).PatchOrganizationRequest(body).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update org %q: %w", params.ID, err)
|
||||
return fmt.Errorf("failed to update org %q: %w", params.OrgID, err)
|
||||
}
|
||||
return c.printOrgs(printOrgOpts{org: &res})
|
||||
}
|
||||
|
@ -6,32 +6,24 @@ import (
|
||||
|
||||
"github.com/influxdata/influx-cli/v2/api"
|
||||
"github.com/influxdata/influx-cli/v2/clients"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
)
|
||||
|
||||
type AddMemberParams struct {
|
||||
MemberId influxid.ID
|
||||
OrgName string
|
||||
OrgID influxid.ID
|
||||
clients.OrgParams
|
||||
MemberId string
|
||||
}
|
||||
|
||||
func (c Client) AddMember(ctx context.Context, params *AddMemberParams) (err error) {
|
||||
if !params.OrgID.Valid() && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return clients.ErrMustSpecifyOrg
|
||||
}
|
||||
|
||||
orgID := params.OrgID.String()
|
||||
if !params.OrgID.Valid() {
|
||||
if orgID, err = c.getOrgId(ctx, params.OrgName); err != nil {
|
||||
return err
|
||||
}
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
member, err := c.PostOrgsIDMembers(ctx, orgID).
|
||||
AddResourceMemberRequestBody(*api.NewAddResourceMemberRequestBody(params.MemberId.String())).
|
||||
AddResourceMemberRequestBody(*api.NewAddResourceMemberRequestBody(params.MemberId)).
|
||||
Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to add user %q to org %q: %w", params.MemberId.String(), orgID, err)
|
||||
return fmt.Errorf("failed to add user %q to org %q: %w", params.MemberId, orgID, err)
|
||||
}
|
||||
|
||||
_, err = c.StdIO.Write([]byte(fmt.Sprintf("user %q has been added as a member of org %q\n", *member.Id, orgID)))
|
||||
@ -39,22 +31,15 @@ func (c Client) AddMember(ctx context.Context, params *AddMemberParams) (err err
|
||||
}
|
||||
|
||||
type ListMemberParams struct {
|
||||
OrgName string
|
||||
OrgID influxid.ID
|
||||
clients.OrgParams
|
||||
}
|
||||
|
||||
const maxConcurrentGetUserRequests = 10
|
||||
|
||||
func (c Client) ListMembers(ctx context.Context, params *ListMemberParams) (err error) {
|
||||
if !params.OrgID.Valid() && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return clients.ErrMustSpecifyOrg
|
||||
}
|
||||
|
||||
orgID := params.OrgID.String()
|
||||
if !params.OrgID.Valid() {
|
||||
if orgID, err = c.getOrgId(ctx, params.OrgName); err != nil {
|
||||
return err
|
||||
}
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
members, err := c.GetOrgsIDMembers(ctx, orgID).Execute()
|
||||
@ -119,44 +104,20 @@ func (c Client) ListMembers(ctx context.Context, params *ListMemberParams) (err
|
||||
}
|
||||
|
||||
type RemoveMemberParams struct {
|
||||
MemberId influxid.ID
|
||||
OrgName string
|
||||
OrgID influxid.ID
|
||||
clients.OrgParams
|
||||
MemberId string
|
||||
}
|
||||
|
||||
func (c Client) RemoveMember(ctx context.Context, params *RemoveMemberParams) (err error) {
|
||||
if !params.OrgID.Valid() && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return clients.ErrMustSpecifyOrg
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
orgID := params.OrgID.String()
|
||||
if !params.OrgID.Valid() {
|
||||
if orgID, err = c.getOrgId(ctx, params.OrgName); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err = c.DeleteOrgsIDMembersID(ctx, params.MemberId.String(), orgID).Execute(); err != nil {
|
||||
if err = c.DeleteOrgsIDMembersID(ctx, params.MemberId, orgID).Execute(); err != nil {
|
||||
return fmt.Errorf("failed to remove member %q from org %q", params.MemberId, orgID)
|
||||
}
|
||||
|
||||
_, err = c.StdIO.Write([]byte(fmt.Sprintf("user %q has been removed from org %q\n", params.MemberId, orgID)))
|
||||
return err
|
||||
}
|
||||
|
||||
func (c Client) getOrgId(ctx context.Context, orgName string) (string, error) {
|
||||
req := c.GetOrgs(ctx)
|
||||
if orgName != "" {
|
||||
req = req.Org(orgName)
|
||||
} else {
|
||||
req = req.Org(c.ActiveConfig.Org)
|
||||
}
|
||||
orgs, err := req.Execute()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to find org %q: %w", orgName, err)
|
||||
}
|
||||
if orgs.Orgs == nil || len(*orgs.Orgs) == 0 {
|
||||
return "", fmt.Errorf("no org found with name %q", orgName)
|
||||
}
|
||||
return *(*orgs.Orgs)[0].Id, nil
|
||||
}
|
||||
|
@ -15,14 +15,13 @@ import (
|
||||
"github.com/influxdata/influx-cli/v2/config"
|
||||
"github.com/influxdata/influx-cli/v2/internal/mock"
|
||||
"github.com/influxdata/influx-cli/v2/internal/testutils"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
tmock "github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var id1, _ = influxid.IDFromString("1111111111111111")
|
||||
var id2, _ = influxid.IDFromString("2222222222222222")
|
||||
var id1 = "1111111111111111"
|
||||
var id2 = "2222222222222222"
|
||||
|
||||
func TestClient_AddMember(t *testing.T) {
|
||||
t.Parallel()
|
||||
@ -38,27 +37,27 @@ func TestClient_AddMember(t *testing.T) {
|
||||
{
|
||||
name: "org by ID",
|
||||
params: org.AddMemberParams{
|
||||
OrgID: id1,
|
||||
MemberId: id2,
|
||||
OrgParams: clients.OrgParams{OrgID: id1},
|
||||
MemberId: id2,
|
||||
},
|
||||
defaultOrgName: "my-org",
|
||||
registerExpectations: func(t *testing.T, orgApi *mock.MockOrganizationsApi) {
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1.String())).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1.String()))
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1)).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1))
|
||||
orgApi.EXPECT().PostOrgsIDMembersExecute(tmock.MatchedBy(func(in api.ApiPostOrgsIDMembersRequest) bool {
|
||||
body := in.GetAddResourceMemberRequestBody()
|
||||
return assert.Equal(t, id1.String(), in.GetOrgID()) &&
|
||||
return assert.Equal(t, id1, in.GetOrgID()) &&
|
||||
assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id2.String(), body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2.String())}, nil)
|
||||
assert.Equal(t, id2, body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2)}, nil)
|
||||
},
|
||||
expectedOut: "user \"2222222222222222\" has been added as a member of org \"1111111111111111\"",
|
||||
},
|
||||
{
|
||||
name: "org by name",
|
||||
params: org.AddMemberParams{
|
||||
OrgName: "org",
|
||||
MemberId: id2,
|
||||
OrgParams: clients.OrgParams{OrgName: "org"},
|
||||
MemberId: id2,
|
||||
},
|
||||
defaultOrgName: "my-org",
|
||||
registerExpectations: func(t *testing.T, orgApi *mock.MockOrganizationsApi) {
|
||||
@ -66,17 +65,17 @@ func TestClient_AddMember(t *testing.T) {
|
||||
orgApi.EXPECT().GetOrgsExecute(tmock.MatchedBy(func(in api.ApiGetOrgsRequest) bool {
|
||||
return assert.Equal(t, "org", *in.GetOrg())
|
||||
})).Return(api.Organizations{
|
||||
Orgs: &[]api.Organization{{Id: api.PtrString(id1.String())}},
|
||||
Orgs: &[]api.Organization{{Id: api.PtrString(id1)}},
|
||||
}, nil)
|
||||
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1.String())).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1.String()))
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1)).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1))
|
||||
orgApi.EXPECT().PostOrgsIDMembersExecute(tmock.MatchedBy(func(in api.ApiPostOrgsIDMembersRequest) bool {
|
||||
body := in.GetAddResourceMemberRequestBody()
|
||||
return assert.Equal(t, id1.String(), in.GetOrgID()) &&
|
||||
return assert.Equal(t, id1, in.GetOrgID()) &&
|
||||
assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id2.String(), body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2.String())}, nil)
|
||||
assert.Equal(t, id2, body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2)}, nil)
|
||||
},
|
||||
expectedOut: "user \"2222222222222222\" has been added as a member of org \"1111111111111111\"",
|
||||
},
|
||||
@ -91,17 +90,17 @@ func TestClient_AddMember(t *testing.T) {
|
||||
orgApi.EXPECT().GetOrgsExecute(tmock.MatchedBy(func(in api.ApiGetOrgsRequest) bool {
|
||||
return assert.Equal(t, "my-org", *in.GetOrg())
|
||||
})).Return(api.Organizations{
|
||||
Orgs: &[]api.Organization{{Id: api.PtrString(id1.String())}},
|
||||
Orgs: &[]api.Organization{{Id: api.PtrString(id1)}},
|
||||
}, nil)
|
||||
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1.String())).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1.String()))
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1)).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1))
|
||||
orgApi.EXPECT().PostOrgsIDMembersExecute(tmock.MatchedBy(func(in api.ApiPostOrgsIDMembersRequest) bool {
|
||||
body := in.GetAddResourceMemberRequestBody()
|
||||
return assert.Equal(t, id1.String(), in.GetOrgID()) &&
|
||||
return assert.Equal(t, id1, in.GetOrgID()) &&
|
||||
assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id2.String(), body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2.String())}, nil)
|
||||
assert.Equal(t, id2, body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2)}, nil)
|
||||
},
|
||||
expectedOut: "user \"2222222222222222\" has been added as a member of org \"1111111111111111\"",
|
||||
},
|
||||
@ -176,18 +175,18 @@ func TestClient_ListMembers(t *testing.T) {
|
||||
orgApi.EXPECT().GetOrgsExecute(tmock.MatchedBy(func(in api.ApiGetOrgsRequest) bool {
|
||||
return assert.Equal(t, "my-org", *in.GetOrg())
|
||||
})).Return(api.Organizations{
|
||||
Orgs: &[]api.Organization{{Id: api.PtrString(id1.String())}},
|
||||
Orgs: &[]api.Organization{{Id: api.PtrString(id1)}},
|
||||
}, nil)
|
||||
|
||||
req := api.ApiGetOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1.String())
|
||||
orgApi.EXPECT().GetOrgsIDMembers(gomock.Any(), gomock.Eq(id1.String())).Return(req)
|
||||
req := api.ApiGetOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1)
|
||||
orgApi.EXPECT().GetOrgsIDMembers(gomock.Any(), gomock.Eq(id1)).Return(req)
|
||||
orgApi.EXPECT().GetOrgsIDMembersExecute(gomock.Eq(req)).Return(api.ResourceMembers{}, nil)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "one member",
|
||||
params: org.ListMemberParams{
|
||||
OrgName: "org",
|
||||
OrgParams: clients.OrgParams{OrgName: "org"},
|
||||
},
|
||||
defaultOrgName: "my-org",
|
||||
registerOrgExpectations: func(t *testing.T, orgApi *mock.MockOrganizationsApi) {
|
||||
@ -195,19 +194,19 @@ func TestClient_ListMembers(t *testing.T) {
|
||||
orgApi.EXPECT().GetOrgsExecute(tmock.MatchedBy(func(in api.ApiGetOrgsRequest) bool {
|
||||
return assert.Equal(t, "org", *in.GetOrg())
|
||||
})).Return(api.Organizations{
|
||||
Orgs: &[]api.Organization{{Id: api.PtrString(id1.String())}},
|
||||
Orgs: &[]api.Organization{{Id: api.PtrString(id1)}},
|
||||
}, nil)
|
||||
|
||||
req := api.ApiGetOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1.String())
|
||||
orgApi.EXPECT().GetOrgsIDMembers(gomock.Any(), gomock.Eq(id1.String())).Return(req)
|
||||
req := api.ApiGetOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1)
|
||||
orgApi.EXPECT().GetOrgsIDMembers(gomock.Any(), gomock.Eq(id1)).Return(req)
|
||||
orgApi.EXPECT().GetOrgsIDMembersExecute(gomock.Eq(req)).
|
||||
Return(api.ResourceMembers{Users: &[]api.ResourceMember{{Id: api.PtrString(id2.String())}}}, nil)
|
||||
Return(api.ResourceMembers{Users: &[]api.ResourceMember{{Id: api.PtrString(id2)}}}, nil)
|
||||
},
|
||||
registerUserExpectations: func(t *testing.T, userApi *mock.MockUsersApi) {
|
||||
req := api.ApiGetUsersIDRequest{ApiService: userApi}.UserID(id2.String())
|
||||
userApi.EXPECT().GetUsersID(gomock.Any(), gomock.Eq(id2.String())).Return(req)
|
||||
req := api.ApiGetUsersIDRequest{ApiService: userApi}.UserID(id2)
|
||||
userApi.EXPECT().GetUsersID(gomock.Any(), gomock.Eq(id2)).Return(req)
|
||||
userApi.EXPECT().GetUsersIDExecute(gomock.Eq(req)).Return(api.UserResponse{
|
||||
Id: api.PtrString(id2.String()),
|
||||
Id: api.PtrString(id2),
|
||||
Name: "user1",
|
||||
Status: api.PtrString("active"),
|
||||
}, nil)
|
||||
@ -217,12 +216,12 @@ func TestClient_ListMembers(t *testing.T) {
|
||||
{
|
||||
name: "many members",
|
||||
params: org.ListMemberParams{
|
||||
OrgID: id1,
|
||||
OrgParams: clients.OrgParams{OrgID: id1},
|
||||
},
|
||||
// NOTE: We previously saw a deadlock when # members was > 10, so test that here.
|
||||
registerOrgExpectations: func(t *testing.T, orgApi *mock.MockOrganizationsApi) {
|
||||
req := api.ApiGetOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1.String())
|
||||
orgApi.EXPECT().GetOrgsIDMembers(gomock.Any(), gomock.Eq(id1.String())).Return(req)
|
||||
req := api.ApiGetOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1)
|
||||
orgApi.EXPECT().GetOrgsIDMembers(gomock.Any(), gomock.Eq(id1)).Return(req)
|
||||
members := make([]api.ResourceMember, 11)
|
||||
for i := 0; i < 11; i++ {
|
||||
members[i] = api.ResourceMember{Id: api.PtrString(fmt.Sprintf("%016d", i))}
|
||||
@ -273,17 +272,17 @@ func TestClient_ListMembers(t *testing.T) {
|
||||
{
|
||||
name: "no such user",
|
||||
params: org.ListMemberParams{
|
||||
OrgID: id1,
|
||||
OrgParams: clients.OrgParams{OrgID: id1},
|
||||
},
|
||||
registerOrgExpectations: func(t *testing.T, orgApi *mock.MockOrganizationsApi) {
|
||||
req := api.ApiGetOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1.String())
|
||||
orgApi.EXPECT().GetOrgsIDMembers(gomock.Any(), gomock.Eq(id1.String())).Return(req)
|
||||
req := api.ApiGetOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1)
|
||||
orgApi.EXPECT().GetOrgsIDMembers(gomock.Any(), gomock.Eq(id1)).Return(req)
|
||||
orgApi.EXPECT().GetOrgsIDMembersExecute(gomock.Eq(req)).
|
||||
Return(api.ResourceMembers{Users: &[]api.ResourceMember{{Id: api.PtrString(id2.String())}}}, nil)
|
||||
Return(api.ResourceMembers{Users: &[]api.ResourceMember{{Id: api.PtrString(id2)}}}, nil)
|
||||
},
|
||||
registerUserExpectations: func(t *testing.T, userApi *mock.MockUsersApi) {
|
||||
req := api.ApiGetUsersIDRequest{ApiService: userApi}.UserID(id2.String())
|
||||
userApi.EXPECT().GetUsersID(gomock.Any(), gomock.Eq(id2.String())).Return(req)
|
||||
req := api.ApiGetUsersIDRequest{ApiService: userApi}.UserID(id2)
|
||||
userApi.EXPECT().GetUsersID(gomock.Any(), gomock.Eq(id2)).Return(req)
|
||||
userApi.EXPECT().GetUsersIDExecute(gomock.Eq(req)).Return(api.UserResponse{}, errors.New("not found"))
|
||||
},
|
||||
expectedErr: "user \"2222222222222222\": not found",
|
||||
@ -345,14 +344,14 @@ func TestClient_RemoveMembers(t *testing.T) {
|
||||
{
|
||||
name: "org by ID",
|
||||
params: org.RemoveMemberParams{
|
||||
OrgID: id1,
|
||||
MemberId: id2,
|
||||
OrgParams: clients.OrgParams{OrgID: id1},
|
||||
MemberId: id2,
|
||||
},
|
||||
defaultOrgName: "my-org",
|
||||
registerExpectations: func(t *testing.T, orgApi *mock.MockOrganizationsApi) {
|
||||
req := api.ApiDeleteOrgsIDMembersIDRequest{ApiService: orgApi}.OrgID(id1.String()).UserID(id2.String())
|
||||
req := api.ApiDeleteOrgsIDMembersIDRequest{ApiService: orgApi}.OrgID(id1).UserID(id2)
|
||||
orgApi.EXPECT().
|
||||
DeleteOrgsIDMembersID(gomock.Any(), gomock.Eq(id2.String()), gomock.Eq(id1.String())).Return(req)
|
||||
DeleteOrgsIDMembersID(gomock.Any(), gomock.Eq(id2), gomock.Eq(id1)).Return(req)
|
||||
orgApi.EXPECT().DeleteOrgsIDMembersIDExecute(gomock.Eq(req)).Return(nil)
|
||||
},
|
||||
expectedOut: "user \"2222222222222222\" has been removed from org \"1111111111111111\"",
|
||||
@ -360,8 +359,8 @@ func TestClient_RemoveMembers(t *testing.T) {
|
||||
{
|
||||
name: "org by name",
|
||||
params: org.RemoveMemberParams{
|
||||
OrgName: "org",
|
||||
MemberId: id2,
|
||||
OrgParams: clients.OrgParams{OrgName: "org"},
|
||||
MemberId: id2,
|
||||
},
|
||||
defaultOrgName: "my-org",
|
||||
registerExpectations: func(t *testing.T, orgApi *mock.MockOrganizationsApi) {
|
||||
@ -369,12 +368,12 @@ func TestClient_RemoveMembers(t *testing.T) {
|
||||
orgApi.EXPECT().GetOrgsExecute(tmock.MatchedBy(func(in api.ApiGetOrgsRequest) bool {
|
||||
return assert.Equal(t, "org", *in.GetOrg())
|
||||
})).Return(api.Organizations{
|
||||
Orgs: &[]api.Organization{{Id: api.PtrString(id1.String())}},
|
||||
Orgs: &[]api.Organization{{Id: api.PtrString(id1)}},
|
||||
}, nil)
|
||||
|
||||
req := api.ApiDeleteOrgsIDMembersIDRequest{ApiService: orgApi}.OrgID(id1.String()).UserID(id2.String())
|
||||
req := api.ApiDeleteOrgsIDMembersIDRequest{ApiService: orgApi}.OrgID(id1).UserID(id2)
|
||||
orgApi.EXPECT().
|
||||
DeleteOrgsIDMembersID(gomock.Any(), gomock.Eq(id2.String()), gomock.Eq(id1.String())).Return(req)
|
||||
DeleteOrgsIDMembersID(gomock.Any(), gomock.Eq(id2), gomock.Eq(id1)).Return(req)
|
||||
orgApi.EXPECT().DeleteOrgsIDMembersIDExecute(gomock.Eq(req)).Return(nil)
|
||||
},
|
||||
expectedOut: "user \"2222222222222222\" has been removed from org \"1111111111111111\"",
|
||||
@ -390,12 +389,12 @@ func TestClient_RemoveMembers(t *testing.T) {
|
||||
orgApi.EXPECT().GetOrgsExecute(tmock.MatchedBy(func(in api.ApiGetOrgsRequest) bool {
|
||||
return assert.Equal(t, "my-org", *in.GetOrg())
|
||||
})).Return(api.Organizations{
|
||||
Orgs: &[]api.Organization{{Id: api.PtrString(id1.String())}},
|
||||
Orgs: &[]api.Organization{{Id: api.PtrString(id1)}},
|
||||
}, nil)
|
||||
|
||||
req := api.ApiDeleteOrgsIDMembersIDRequest{ApiService: orgApi}.OrgID(id1.String()).UserID(id2.String())
|
||||
req := api.ApiDeleteOrgsIDMembersIDRequest{ApiService: orgApi}.OrgID(id1).UserID(id2)
|
||||
orgApi.EXPECT().
|
||||
DeleteOrgsIDMembersID(gomock.Any(), gomock.Eq(id2.String()), gomock.Eq(id1.String())).Return(req)
|
||||
DeleteOrgsIDMembersID(gomock.Any(), gomock.Eq(id2), gomock.Eq(id1)).Return(req)
|
||||
orgApi.EXPECT().DeleteOrgsIDMembersIDExecute(gomock.Eq(req)).Return(nil)
|
||||
},
|
||||
expectedOut: "user \"2222222222222222\" has been removed from org \"1111111111111111\"",
|
||||
|
@ -13,13 +13,12 @@ import (
|
||||
"github.com/influxdata/influx-cli/v2/clients/org"
|
||||
"github.com/influxdata/influx-cli/v2/internal/mock"
|
||||
"github.com/influxdata/influx-cli/v2/internal/testutils"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
tmock "github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var id, _ = influxid.IDFromString("1111111111111111")
|
||||
var id = "1111111111111111"
|
||||
|
||||
func TestClient_Create(t *testing.T) {
|
||||
t.Parallel()
|
||||
@ -121,14 +120,14 @@ func TestClient_Delete(t *testing.T) {
|
||||
|
||||
cli := org.Client{CLI: clients.CLI{StdIO: stdio}, OrganizationsApi: orgApi}
|
||||
|
||||
getReq := api.ApiGetOrgsIDRequest{ApiService: orgApi}.OrgID(id.String())
|
||||
orgApi.EXPECT().GetOrgsID(gomock.Any(), gomock.Eq(id.String())).Return(getReq)
|
||||
getReq := api.ApiGetOrgsIDRequest{ApiService: orgApi}.OrgID(id)
|
||||
orgApi.EXPECT().GetOrgsID(gomock.Any(), gomock.Eq(id)).Return(getReq)
|
||||
orgApi.EXPECT().GetOrgsIDExecute(gomock.Eq(getReq)).
|
||||
DoAndReturn(func(request api.ApiGetOrgsIDRequest) (api.Organization, error) {
|
||||
if tc.notFound {
|
||||
return api.Organization{}, &api.Error{Code: api.ERRORCODE_NOT_FOUND}
|
||||
}
|
||||
return api.Organization{Id: api.PtrString(id.String()), Name: "my-org"}, nil
|
||||
return api.Organization{Id: api.PtrString(id), Name: "my-org"}, nil
|
||||
})
|
||||
|
||||
if tc.notFound {
|
||||
@ -137,8 +136,8 @@ func TestClient_Delete(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
delReq := api.ApiDeleteOrgsIDRequest{ApiService: orgApi}.OrgID(id.String())
|
||||
orgApi.EXPECT().DeleteOrgsID(gomock.Any(), gomock.Eq(id.String())).Return(delReq)
|
||||
delReq := api.ApiDeleteOrgsIDRequest{ApiService: orgApi}.OrgID(id)
|
||||
orgApi.EXPECT().DeleteOrgsID(gomock.Any(), gomock.Eq(id)).Return(delReq)
|
||||
orgApi.EXPECT().DeleteOrgsIDExecute(gomock.Eq(delReq)).Return(nil)
|
||||
|
||||
require.NoError(t, cli.Delete(context.Background(), id))
|
||||
@ -180,8 +179,10 @@ func TestClient_List(t *testing.T) {
|
||||
outLines: []string{`123\s+org1`, `456\s+org2`},
|
||||
},
|
||||
{
|
||||
name: "by name",
|
||||
params: org.ListParams{Name: "org1"},
|
||||
name: "by name",
|
||||
params: org.ListParams{
|
||||
OrgParams: clients.OrgParams{OrgName: "org1"},
|
||||
},
|
||||
registerExpectations: func(t *testing.T, orgApi *mock.MockOrganizationsApi) {
|
||||
orgApi.EXPECT().GetOrgs(gomock.Any()).Return(api.ApiGetOrgsRequest{ApiService: orgApi})
|
||||
orgApi.EXPECT().GetOrgsExecute(tmock.MatchedBy(func(in api.ApiGetOrgsRequest) bool {
|
||||
@ -195,15 +196,17 @@ func TestClient_List(t *testing.T) {
|
||||
outLines: []string{`123\s+org1`},
|
||||
},
|
||||
{
|
||||
name: "by ID",
|
||||
params: org.ListParams{ID: id},
|
||||
name: "by ID",
|
||||
params: org.ListParams{
|
||||
OrgParams: clients.OrgParams{OrgID: id},
|
||||
},
|
||||
registerExpectations: func(t *testing.T, orgApi *mock.MockOrganizationsApi) {
|
||||
orgApi.EXPECT().GetOrgs(gomock.Any()).Return(api.ApiGetOrgsRequest{ApiService: orgApi})
|
||||
orgApi.EXPECT().GetOrgsExecute(tmock.MatchedBy(func(in api.ApiGetOrgsRequest) bool {
|
||||
return assert.Nil(t, in.GetOrg()) && assert.Equal(t, id.String(), *in.GetOrgID())
|
||||
return assert.Nil(t, in.GetOrg()) && assert.Equal(t, id, *in.GetOrgID())
|
||||
})).Return(api.Organizations{
|
||||
Orgs: &[]api.Organization{
|
||||
{Id: api.PtrString(id.String()), Name: "org3"},
|
||||
{Id: api.PtrString(id), Name: "org3"},
|
||||
},
|
||||
}, nil)
|
||||
},
|
||||
@ -242,36 +245,44 @@ func TestClient_Update(t *testing.T) {
|
||||
outLine string
|
||||
}{
|
||||
{
|
||||
name: "name",
|
||||
params: org.UpdateParams{ID: id, Name: "my-org"},
|
||||
name: "name",
|
||||
params: org.UpdateParams{
|
||||
OrgParams: clients.OrgParams{
|
||||
OrgID: id,
|
||||
OrgName: "my-org",
|
||||
},
|
||||
},
|
||||
registerExpectations: func(t *testing.T, orgApi *mock.MockOrganizationsApi) {
|
||||
orgApi.EXPECT().PatchOrgsID(gomock.Any(), gomock.Eq(id.String())).
|
||||
Return(api.ApiPatchOrgsIDRequest{ApiService: orgApi}.OrgID(id.String()))
|
||||
orgApi.EXPECT().PatchOrgsID(gomock.Any(), gomock.Eq(id)).
|
||||
Return(api.ApiPatchOrgsIDRequest{ApiService: orgApi}.OrgID(id))
|
||||
orgApi.EXPECT().PatchOrgsIDExecute(tmock.MatchedBy(func(in api.ApiPatchOrgsIDRequest) bool {
|
||||
body := in.GetPatchOrganizationRequest()
|
||||
return assert.Equal(t, id.String(), in.GetOrgID()) &&
|
||||
return assert.Equal(t, id, in.GetOrgID()) &&
|
||||
assert.NotNil(t, body) &&
|
||||
assert.Equal(t, "my-org", body.GetName()) &&
|
||||
assert.Nil(t, body.Description)
|
||||
})).Return(api.Organization{Id: api.PtrString(id.String()), Name: "my-org"}, nil)
|
||||
})).Return(api.Organization{Id: api.PtrString(id), Name: "my-org"}, nil)
|
||||
},
|
||||
outLine: fmt.Sprintf(`%s\s+my-org`, id.String()),
|
||||
outLine: fmt.Sprintf(`%s\s+my-org`, id),
|
||||
},
|
||||
{
|
||||
name: "description",
|
||||
params: org.UpdateParams{ID: id, Description: "my cool org"},
|
||||
name: "description",
|
||||
params: org.UpdateParams{
|
||||
OrgParams: clients.OrgParams{OrgID: id},
|
||||
Description: "my cool org",
|
||||
},
|
||||
registerExpectations: func(t *testing.T, orgApi *mock.MockOrganizationsApi) {
|
||||
orgApi.EXPECT().PatchOrgsID(gomock.Any(), gomock.Eq(id.String())).
|
||||
Return(api.ApiPatchOrgsIDRequest{ApiService: orgApi}.OrgID(id.String()))
|
||||
orgApi.EXPECT().PatchOrgsID(gomock.Any(), gomock.Eq(id)).
|
||||
Return(api.ApiPatchOrgsIDRequest{ApiService: orgApi}.OrgID(id))
|
||||
orgApi.EXPECT().PatchOrgsIDExecute(tmock.MatchedBy(func(in api.ApiPatchOrgsIDRequest) bool {
|
||||
body := in.GetPatchOrganizationRequest()
|
||||
return assert.Equal(t, id.String(), in.GetOrgID()) &&
|
||||
return assert.Equal(t, id, in.GetOrgID()) &&
|
||||
assert.NotNil(t, body) &&
|
||||
assert.Nil(t, body.Name) &&
|
||||
assert.Equal(t, "my cool org", body.GetDescription())
|
||||
})).Return(api.Organization{Id: api.PtrString(id.String()), Name: "my-org"}, nil)
|
||||
})).Return(api.Organization{Id: api.PtrString(id), Name: "my-org"}, nil)
|
||||
},
|
||||
outLine: fmt.Sprintf(`%s\s+my-org`, id.String()),
|
||||
outLine: fmt.Sprintf(`%s\s+my-org`, id),
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -1,16 +1,41 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/influxdata/influx-cli/v2/api"
|
||||
"github.com/influxdata/influx-cli/v2/config"
|
||||
)
|
||||
|
||||
type OrgParams struct {
|
||||
OrgID influxid.ID
|
||||
OrgID string
|
||||
OrgName string
|
||||
}
|
||||
|
||||
func (p OrgParams) GetOrgID(ctx context.Context, activeConfig config.Config, orgApi api.OrganizationsApi) (string, error) {
|
||||
if p.OrgID != "" {
|
||||
return p.OrgID, nil
|
||||
}
|
||||
orgName := p.OrgName
|
||||
if orgName == "" {
|
||||
orgName = activeConfig.Org
|
||||
}
|
||||
if orgName == "" {
|
||||
return "", ErrMustSpecifyOrg
|
||||
}
|
||||
res, err := orgApi.GetOrgs(ctx).Org(orgName).Execute()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to lookup org with name %q: %w", orgName, err)
|
||||
}
|
||||
if len(res.GetOrgs()) == 0 {
|
||||
return "", fmt.Errorf("no organization with name %q", orgName)
|
||||
}
|
||||
return res.GetOrgs()[0].GetId(), nil
|
||||
}
|
||||
|
||||
type BucketParams struct {
|
||||
BucketID influxid.ID
|
||||
BucketID string
|
||||
BucketName string
|
||||
}
|
||||
|
||||
|
@ -120,7 +120,7 @@ func BuildExternAST(profilers []string) *api.Extern {
|
||||
}
|
||||
|
||||
func (c Client) Query(ctx context.Context, params *Params) error {
|
||||
if !params.OrgID.Valid() && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
if params.OrgID == "" && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return clients.ErrMustSpecifyOrg
|
||||
}
|
||||
|
||||
@ -130,8 +130,8 @@ func (c Client) Query(ctx context.Context, params *Params) error {
|
||||
}
|
||||
|
||||
req := c.PostQuery(ctx).Query(query).AcceptEncoding("gzip")
|
||||
if params.OrgID.Valid() {
|
||||
req = req.OrgID(params.OrgID.String())
|
||||
if params.OrgID != "" {
|
||||
req = req.OrgID(params.OrgID)
|
||||
} else if params.OrgName != "" {
|
||||
req = req.Org(params.OrgName)
|
||||
} else {
|
||||
|
@ -15,7 +15,6 @@ import (
|
||||
"github.com/influxdata/influx-cli/v2/clients/query"
|
||||
"github.com/influxdata/influx-cli/v2/config"
|
||||
"github.com/influxdata/influx-cli/v2/internal/mock"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
tmock "github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -92,8 +91,7 @@ func TestQuery(t *testing.T) {
|
||||
fakeQuery := query.BuildDefaultAST("I'm a query!")
|
||||
fakeResults := "data data data"
|
||||
|
||||
orgID, err := influxid.IDFromString("1111111111111111")
|
||||
require.NoError(t, err)
|
||||
orgID := "1111111111111111"
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
@ -117,7 +115,7 @@ func TestQuery(t *testing.T) {
|
||||
body := in.GetQuery()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, fakeQuery, *body) &&
|
||||
assert.Equal(t, orgID.String(), *in.GetOrgID()) &&
|
||||
assert.Equal(t, orgID, *in.GetOrgID()) &&
|
||||
assert.Nil(t, in.GetOrg())
|
||||
})).Return(&http.Response{Body: io.NopCloser(strings.NewReader(fakeResults))}, nil)
|
||||
},
|
||||
@ -206,7 +204,7 @@ func TestQuery(t *testing.T) {
|
||||
body := in.GetQuery()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, fakeQuery, *body) &&
|
||||
assert.Equal(t, orgID.String(), *in.GetOrgID()) &&
|
||||
assert.Equal(t, orgID, *in.GetOrgID()) &&
|
||||
assert.Nil(t, in.GetOrg())
|
||||
})).DoAndReturn(func(api.ApiPostQueryRequest) (*http.Response, error) {
|
||||
pr, pw := io.Pipe()
|
||||
|
@ -16,10 +16,9 @@ type Client struct {
|
||||
}
|
||||
|
||||
type CreateParams struct {
|
||||
clients.OrgParams
|
||||
Name string
|
||||
Description string
|
||||
OrgID string
|
||||
OrgName string
|
||||
RemoteURL string
|
||||
RemoteAPIToken string
|
||||
RemoteOrgID string
|
||||
@ -28,7 +27,7 @@ type CreateParams struct {
|
||||
|
||||
func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
|
||||
orgID, err := c.GetOrgId(ctx, params.OrgID, params.OrgName, c.OrganizationsApi)
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -106,15 +105,14 @@ func (c Client) Update(ctx context.Context, params *UpdateParams) error {
|
||||
}
|
||||
|
||||
type ListParams struct {
|
||||
clients.OrgParams
|
||||
Name string
|
||||
OrgID string
|
||||
OrgName string
|
||||
RemoteURL string
|
||||
}
|
||||
|
||||
func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
|
||||
orgID, err := c.GetOrgId(ctx, params.OrgID, params.OrgName, c.OrganizationsApi)
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -16,10 +16,9 @@ type Client struct {
|
||||
}
|
||||
|
||||
type CreateParams struct {
|
||||
clients.OrgParams
|
||||
Name string
|
||||
Description string
|
||||
OrgID string
|
||||
OrgName string
|
||||
RemoteID string
|
||||
LocalBucketID string
|
||||
RemoteBucketID string
|
||||
@ -27,7 +26,7 @@ type CreateParams struct {
|
||||
}
|
||||
|
||||
func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
orgID, err := c.GetOrgId(ctx, params.OrgID, params.OrgName, c.OrganizationsApi)
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -58,16 +57,14 @@ func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
}
|
||||
|
||||
type ListParams struct {
|
||||
clients.OrgParams
|
||||
Name string
|
||||
OrgID string
|
||||
OrgName string
|
||||
RemoteID string
|
||||
LocalBucketID string
|
||||
}
|
||||
|
||||
func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
|
||||
orgID, err := c.GetOrgId(ctx, params.OrgID, params.OrgName, c.OrganizationsApi)
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -40,8 +40,7 @@ type Params struct {
|
||||
|
||||
// Original ID/name of the organization to restore.
|
||||
// If not set, all orgs will be restored.
|
||||
OrgID string
|
||||
Org string
|
||||
clients.OrgParams
|
||||
|
||||
// New name to use for the restored organization.
|
||||
// If not set, the org will be restored using its backed-up name.
|
||||
@ -49,8 +48,7 @@ type Params struct {
|
||||
|
||||
// Original ID/name of the bucket to restore.
|
||||
// If not set, all buckets within the org filter will be restored.
|
||||
BucketID string
|
||||
Bucket string
|
||||
clients.BucketParams
|
||||
|
||||
// New name to use for the restored bucket.
|
||||
// If not set, the bucket will be restored using its backed-up name.
|
||||
@ -65,13 +63,13 @@ func (p *Params) matches(bkt br.ManifestBucketEntry) bool {
|
||||
if p.OrgID != "" && bkt.OrganizationID != p.OrgID {
|
||||
return false
|
||||
}
|
||||
if p.Org != "" && bkt.OrganizationName != p.Org {
|
||||
if p.OrgName != "" && bkt.OrganizationName != p.OrgName {
|
||||
return false
|
||||
}
|
||||
if p.BucketID != "" && bkt.BucketID != p.BucketID {
|
||||
return false
|
||||
}
|
||||
if p.Bucket != "" && bkt.BucketName != p.Bucket {
|
||||
if p.BucketName != "" && bkt.BucketName != p.BucketName {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
@ -31,7 +31,7 @@ type DeleteParams struct {
|
||||
}
|
||||
|
||||
func (c Client) Delete(ctx context.Context, params *DeleteParams) error {
|
||||
orgID, err := c.getOrgID(ctx, params.OrgParams)
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -60,8 +60,7 @@ type ListParams struct {
|
||||
}
|
||||
|
||||
func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
|
||||
orgID, err := c.getOrgID(ctx, params.OrgParams)
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -89,7 +88,7 @@ type UpdateParams struct {
|
||||
}
|
||||
|
||||
func (c Client) Update(ctx context.Context, params *UpdateParams) error {
|
||||
orgID, err := c.getOrgID(ctx, params.OrgParams)
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -149,7 +148,3 @@ func (c Client) printSecrets(opts secretPrintOpt) error {
|
||||
}
|
||||
return c.PrintTable(headers, rows...)
|
||||
}
|
||||
|
||||
func (c Client) getOrgID(ctx context.Context, params clients.OrgParams) (string, error) {
|
||||
return c.GetOrgIdI(ctx, params.OrgID, params.OrgName, c.OrganizationsApi)
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ import (
|
||||
"github.com/influxdata/influx-cli/v2/clients/secret"
|
||||
"github.com/influxdata/influx-cli/v2/config"
|
||||
"github.com/influxdata/influx-cli/v2/internal/mock"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@ -25,8 +24,7 @@ func TestSecret_List(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
printHeader := "Key\t\tOrganization ID\n"
|
||||
id, err := influxid.IDFromString("1111111111111111")
|
||||
require.NoError(t, err)
|
||||
id := "1111111111111111"
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
@ -45,12 +43,12 @@ func TestSecret_List(t *testing.T) {
|
||||
},
|
||||
defaultOrgName: defaultOrgName,
|
||||
registerExpectations: func(t *testing.T, secretApi *mock.MockSecretsApi, orgApi *mock.MockOrganizationsApi) {
|
||||
req := api.ApiGetOrgsIDSecretsRequest{ApiService: secretApi}.OrgID(id.String())
|
||||
secretApi.EXPECT().GetOrgsIDSecrets(gomock.Any(), gomock.Eq(id.String())).Return(req)
|
||||
req := api.ApiGetOrgsIDSecretsRequest{ApiService: secretApi}.OrgID(id)
|
||||
secretApi.EXPECT().GetOrgsIDSecrets(gomock.Any(), gomock.Eq(id)).Return(req)
|
||||
secretApi.EXPECT().GetOrgsIDSecretsExecute(gomock.Eq(req)).
|
||||
Return(api.SecretKeysResponse{Secrets: &[]string{fakeResults}}, nil)
|
||||
},
|
||||
expectMatcher: printHeader + fakeResults + "\t" + id.String() + "\n",
|
||||
expectMatcher: printHeader + fakeResults + "\t" + id + "\n",
|
||||
},
|
||||
{
|
||||
name: "default org",
|
||||
@ -122,8 +120,7 @@ func TestSecret_Delete(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
printHeader := "Key\tOrganization ID\t\tDeleted\n"
|
||||
id, err := influxid.IDFromString("1111111111111111")
|
||||
require.NoError(t, err)
|
||||
id := "1111111111111111"
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
@ -144,12 +141,12 @@ func TestSecret_Delete(t *testing.T) {
|
||||
defaultOrgName: defaultOrgName,
|
||||
registerExpectations: func(t *testing.T, secretApi *mock.MockSecretsApi) {
|
||||
req := api.ApiPostOrgsIDSecretsRequest{ApiService: secretApi}.
|
||||
OrgID(id.String()).
|
||||
OrgID(id).
|
||||
SecretKeys(api.SecretKeys{Secrets: &[]string{"key1"}})
|
||||
secretApi.EXPECT().PostOrgsIDSecrets(gomock.Any(), gomock.Eq(id.String())).Return(req)
|
||||
secretApi.EXPECT().PostOrgsIDSecrets(gomock.Any(), gomock.Eq(id)).Return(req)
|
||||
secretApi.EXPECT().PostOrgsIDSecretsExecute(gomock.Eq(req)).Return(nil)
|
||||
},
|
||||
expectMatcher: printHeader + fakeKey + "\t" + id.String() + "\ttrue\n",
|
||||
expectMatcher: printHeader + fakeKey + "\t" + id + "\ttrue\n",
|
||||
},
|
||||
{
|
||||
// This situation cannot happen since the CLI will stop it.
|
||||
@ -163,12 +160,12 @@ func TestSecret_Delete(t *testing.T) {
|
||||
defaultOrgName: defaultOrgName,
|
||||
registerExpectations: func(t *testing.T, secretApi *mock.MockSecretsApi) {
|
||||
req := api.ApiPostOrgsIDSecretsRequest{ApiService: secretApi}.
|
||||
OrgID(id.String()).
|
||||
OrgID(id).
|
||||
SecretKeys(api.SecretKeys{Secrets: &[]string{""}})
|
||||
secretApi.EXPECT().PostOrgsIDSecrets(gomock.Any(), gomock.Eq(id.String())).Return(req)
|
||||
secretApi.EXPECT().PostOrgsIDSecrets(gomock.Any(), gomock.Eq(id)).Return(req)
|
||||
secretApi.EXPECT().PostOrgsIDSecretsExecute(gomock.Eq(req)).Return(nil)
|
||||
},
|
||||
expectMatcher: printHeader + "\t" + id.String() + "\ttrue\n",
|
||||
expectMatcher: printHeader + "\t" + id + "\ttrue\n",
|
||||
},
|
||||
{
|
||||
name: "delete no org",
|
||||
@ -220,8 +217,7 @@ func TestSecret_Update(t *testing.T) {
|
||||
|
||||
printHeader := "Key\tOrganization ID\n"
|
||||
fakeValue := "someValue"
|
||||
id, err := influxid.IDFromString("1111111111111111")
|
||||
require.NoError(t, err)
|
||||
id := "1111111111111111"
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
@ -243,12 +239,12 @@ func TestSecret_Update(t *testing.T) {
|
||||
defaultOrgName: defaultOrgName,
|
||||
registerExpectations: func(t *testing.T, secretApi *mock.MockSecretsApi, stdio *mock.MockStdIO) {
|
||||
req := api.ApiPatchOrgsIDSecretsRequest{ApiService: secretApi}.
|
||||
OrgID(id.String()).
|
||||
OrgID(id).
|
||||
RequestBody(map[string]string{fakeKey: fakeValue})
|
||||
secretApi.EXPECT().PatchOrgsIDSecrets(gomock.Any(), gomock.Eq(id.String())).Return(req)
|
||||
secretApi.EXPECT().PatchOrgsIDSecrets(gomock.Any(), gomock.Eq(id)).Return(req)
|
||||
secretApi.EXPECT().PatchOrgsIDSecretsExecute(gomock.Eq(req)).Return(nil)
|
||||
},
|
||||
expectMatcher: printHeader + fakeKey + "\t" + id.String() + "\n",
|
||||
expectMatcher: printHeader + fakeKey + "\t" + id + "\n",
|
||||
},
|
||||
{
|
||||
name: "update no key",
|
||||
@ -261,12 +257,12 @@ func TestSecret_Update(t *testing.T) {
|
||||
defaultOrgName: defaultOrgName,
|
||||
registerExpectations: func(t *testing.T, secretApi *mock.MockSecretsApi, stdio *mock.MockStdIO) {
|
||||
req := api.ApiPatchOrgsIDSecretsRequest{ApiService: secretApi}.
|
||||
OrgID(id.String()).
|
||||
OrgID(id).
|
||||
RequestBody(map[string]string{"": fakeValue})
|
||||
secretApi.EXPECT().PatchOrgsIDSecrets(gomock.Any(), gomock.Eq(id.String())).Return(req)
|
||||
secretApi.EXPECT().PatchOrgsIDSecrets(gomock.Any(), gomock.Eq(id)).Return(req)
|
||||
secretApi.EXPECT().PatchOrgsIDSecretsExecute(gomock.Eq(req)).Return(nil)
|
||||
},
|
||||
expectMatcher: printHeader + "\t" + id.String() + "\n",
|
||||
expectMatcher: printHeader + "\t" + id + "\n",
|
||||
},
|
||||
{
|
||||
name: "update no value",
|
||||
@ -281,12 +277,12 @@ func TestSecret_Update(t *testing.T) {
|
||||
stdio.EXPECT().GetSecret(gomock.Eq("Please type your secret"), gomock.Eq(0)).Return(fakeValue, nil)
|
||||
|
||||
req := api.ApiPatchOrgsIDSecretsRequest{ApiService: secretApi}.
|
||||
OrgID(id.String()).
|
||||
OrgID(id).
|
||||
RequestBody(map[string]string{fakeKey: fakeValue})
|
||||
secretApi.EXPECT().PatchOrgsIDSecrets(gomock.Any(), gomock.Eq(id.String())).Return(req)
|
||||
secretApi.EXPECT().PatchOrgsIDSecrets(gomock.Any(), gomock.Eq(id)).Return(req)
|
||||
secretApi.EXPECT().PatchOrgsIDSecretsExecute(gomock.Eq(req)).Return(nil)
|
||||
},
|
||||
expectMatcher: printHeader + fakeKey + "\t" + id.String() + "\n",
|
||||
expectMatcher: printHeader + fakeKey + "\t" + id + "\n",
|
||||
},
|
||||
{
|
||||
name: "update no org",
|
||||
|
@ -18,19 +18,14 @@ type Client struct {
|
||||
}
|
||||
|
||||
type ListParams struct {
|
||||
OrgId string
|
||||
OrgName string
|
||||
clients.OrgParams
|
||||
|
||||
StackIds []string
|
||||
StackNames []string
|
||||
}
|
||||
|
||||
func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
if params.OrgId == "" && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return clients.ErrMustSpecifyOrg
|
||||
}
|
||||
|
||||
orgId, err := c.GetOrgId(ctx, params.OrgId, params.OrgName, c.OrganizationsApi)
|
||||
orgId, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -44,8 +39,7 @@ func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
}
|
||||
|
||||
type InitParams struct {
|
||||
OrgId string
|
||||
OrgName string
|
||||
clients.OrgParams
|
||||
|
||||
Name string
|
||||
Description string
|
||||
@ -53,11 +47,7 @@ type InitParams struct {
|
||||
}
|
||||
|
||||
func (c Client) Init(ctx context.Context, params *InitParams) error {
|
||||
if params.OrgId == "" && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return clients.ErrMustSpecifyOrg
|
||||
}
|
||||
|
||||
orgId, err := c.GetOrgId(ctx, params.OrgId, params.OrgName, c.OrganizationsApi)
|
||||
orgId, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -80,19 +70,14 @@ func (c Client) Init(ctx context.Context, params *InitParams) error {
|
||||
}
|
||||
|
||||
type RemoveParams struct {
|
||||
OrgId string
|
||||
OrgName string
|
||||
clients.OrgParams
|
||||
|
||||
Ids []string
|
||||
Force bool
|
||||
}
|
||||
|
||||
func (c Client) Remove(ctx context.Context, params *RemoveParams) error {
|
||||
if params.OrgId == "" && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return clients.ErrMustSpecifyOrg
|
||||
}
|
||||
|
||||
orgId, err := c.GetOrgId(ctx, params.OrgId, params.OrgName, c.OrganizationsApi)
|
||||
orgId, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -52,8 +52,8 @@ func addOrg(n NameOrID, g api.ApiGetTasksRequest) api.ApiGetTasksRequest {
|
||||
}
|
||||
|
||||
func (c Client) getOrg(params *clients.OrgParams) (NameOrID, error) {
|
||||
if params.OrgID.Valid() {
|
||||
return NameOrID{ID: params.OrgID.String()}, nil
|
||||
if params.OrgID != "" {
|
||||
return NameOrID{ID: params.OrgID}, nil
|
||||
}
|
||||
if params.OrgName != "" {
|
||||
return NameOrID{Name: params.OrgName}, nil
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
|
||||
"github.com/influxdata/influx-cli/v2/api"
|
||||
"github.com/influxdata/influx-cli/v2/clients"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
@ -27,7 +26,7 @@ type ListParams struct {
|
||||
}
|
||||
|
||||
func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
if params.Id == "" && !params.OrgID.Valid() && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
if params.Id == "" && params.OrgID == "" && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return errors.New("at least one of org, org-id, or id must be provided")
|
||||
}
|
||||
|
||||
@ -40,7 +39,7 @@ func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
return c.printTelegrafs(telegrafPrintOpts{graf: &telegraf})
|
||||
}
|
||||
|
||||
orgID, err := c.getOrgID(ctx, params.OrgID, params.OrgName)
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -61,7 +60,7 @@ type CreateParams struct {
|
||||
}
|
||||
|
||||
func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
orgID, err := c.getOrgID(ctx, params.OrgID, params.OrgName)
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -154,7 +153,3 @@ func (c Client) printTelegrafs(opts telegrafPrintOpts) error {
|
||||
|
||||
return c.PrintTable(headers, rows...)
|
||||
}
|
||||
|
||||
func (c Client) getOrgID(ctx context.Context, orgID influxid.ID, orgName string) (string, error) {
|
||||
return c.GetOrgIdI(ctx, orgID, orgName, c.OrganizationsApi)
|
||||
}
|
||||
|
@ -16,8 +16,7 @@ type Client struct {
|
||||
}
|
||||
|
||||
type SummarizeParams struct {
|
||||
OrgId string
|
||||
OrgName string
|
||||
clients.OrgParams
|
||||
|
||||
Sources []template.Source
|
||||
|
||||
@ -26,7 +25,7 @@ type SummarizeParams struct {
|
||||
}
|
||||
|
||||
func (c Client) Summarize(ctx context.Context, params *SummarizeParams) error {
|
||||
orgID, err := c.GetOrgId(ctx, params.OrgId, params.OrgName, c.OrganizationsApi)
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -54,14 +53,13 @@ func (c Client) Summarize(ctx context.Context, params *SummarizeParams) error {
|
||||
}
|
||||
|
||||
type ValidateParams struct {
|
||||
OrgId string
|
||||
OrgName string
|
||||
clients.OrgParams
|
||||
|
||||
Sources []template.Source
|
||||
}
|
||||
|
||||
func (c Client) Validate(ctx context.Context, params *ValidateParams) error {
|
||||
orgID, err := c.GetOrgId(ctx, params.OrgId, params.OrgName, c.OrganizationsApi)
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
|
||||
"github.com/influxdata/influx-cli/v2/api"
|
||||
"github.com/influxdata/influx-cli/v2/clients"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/stdio"
|
||||
)
|
||||
|
||||
@ -30,7 +29,7 @@ func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
return clients.ErrPasswordIsTooShort
|
||||
}
|
||||
|
||||
orgID, err := c.GetOrgIdI(ctx, params.OrgID, params.OrgName, c.OrganizationsApi)
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -61,26 +60,26 @@ func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Client) Delete(ctx context.Context, id influxid.ID) error {
|
||||
user, err := c.GetUsersID(ctx, id.String()).Execute()
|
||||
func (c Client) Delete(ctx context.Context, id string) error {
|
||||
user, err := c.GetUsersID(ctx, id).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("user %q not found: %w", id, err)
|
||||
}
|
||||
if err := c.DeleteUsersID(ctx, id.String()).Execute(); err != nil {
|
||||
if err := c.DeleteUsersID(ctx, id).Execute(); err != nil {
|
||||
return fmt.Errorf("failed to delete user %q: %w", id, err)
|
||||
}
|
||||
return c.printUsers(printUserOpts{user: &user, deleted: true})
|
||||
}
|
||||
|
||||
type ListParams struct {
|
||||
Id influxid.ID
|
||||
Id string
|
||||
Name string
|
||||
}
|
||||
|
||||
func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
req := c.GetUsers(ctx)
|
||||
if params.Id.Valid() {
|
||||
req = req.Id(params.Id.String())
|
||||
if params.Id != "" {
|
||||
req = req.Id(params.Id)
|
||||
}
|
||||
if params.Name != "" {
|
||||
req = req.Name(params.Name)
|
||||
@ -96,17 +95,17 @@ func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
return c.printUsers(printOpts)
|
||||
}
|
||||
|
||||
type UpdateParmas struct {
|
||||
Id influxid.ID
|
||||
type UpdateParams struct {
|
||||
Id string
|
||||
Name string
|
||||
}
|
||||
|
||||
func (c Client) Update(ctx context.Context, params *UpdateParmas) error {
|
||||
func (c Client) Update(ctx context.Context, params *UpdateParams) error {
|
||||
update := api.User{}
|
||||
if params.Name != "" {
|
||||
update.SetName(params.Name)
|
||||
}
|
||||
user, err := c.PatchUsersID(ctx, params.Id.String()).User(update).Execute()
|
||||
user, err := c.PatchUsersID(ctx, params.Id).User(update).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update user %q: %w", params.Id, err)
|
||||
}
|
||||
@ -114,18 +113,18 @@ func (c Client) Update(ctx context.Context, params *UpdateParmas) error {
|
||||
}
|
||||
|
||||
type SetPasswordParams struct {
|
||||
Id influxid.ID
|
||||
Id string
|
||||
Name string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (c Client) SetPassword(ctx context.Context, params *SetPasswordParams) (err error) {
|
||||
if !params.Id.Valid() && params.Name == "" {
|
||||
if params.Id == "" && params.Name == "" {
|
||||
return ErrMustSpecifyUser
|
||||
}
|
||||
id := params.Id.String()
|
||||
id := params.Id
|
||||
displayName := id
|
||||
if !params.Id.Valid() {
|
||||
if params.Id == "" {
|
||||
displayName = params.Name
|
||||
users, err := c.GetUsers(ctx).Name(params.Name).Execute()
|
||||
if err != nil {
|
||||
@ -147,7 +146,7 @@ func (c Client) SetPassword(ctx context.Context, params *SetPasswordParams) (err
|
||||
|
||||
body := api.PasswordResetBody{Password: password}
|
||||
if err := c.PostUsersIDPassword(ctx, id).PasswordResetBody(body).Execute(); err != nil {
|
||||
return fmt.Errorf("failed to set password for user %q: %w", params.Id.String(), err)
|
||||
return fmt.Errorf("failed to set password for user %q: %w", params.Id, err)
|
||||
}
|
||||
_, err = c.StdIO.Write([]byte(fmt.Sprintf("Successfully updated password for user %q\n", displayName)))
|
||||
return err
|
||||
|
@ -14,14 +14,13 @@ import (
|
||||
"github.com/influxdata/influx-cli/v2/config"
|
||||
"github.com/influxdata/influx-cli/v2/internal/mock"
|
||||
"github.com/influxdata/influx-cli/v2/internal/testutils"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
tmock "github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var id1, _ = influxid.IDFromString("1111111111111111")
|
||||
var id2, _ = influxid.IDFromString("2222222222222222")
|
||||
var id1 = "1111111111111111"
|
||||
var id2 = "2222222222222222"
|
||||
|
||||
func TestClient_Create(t *testing.T) {
|
||||
t.Parallel()
|
||||
@ -52,27 +51,27 @@ func TestClient_Create(t *testing.T) {
|
||||
userApi.EXPECT().PostUsersExecute(tmock.MatchedBy(func(in api.ApiPostUsersRequest) bool {
|
||||
body := in.GetUser()
|
||||
return assert.NotNil(t, body) && assert.Equal(t, "my-user", body.GetName())
|
||||
})).Return(api.UserResponse{Id: api.PtrString(id2.String()), Name: "my-user"}, nil)
|
||||
})).Return(api.UserResponse{Id: api.PtrString(id2), Name: "my-user"}, nil)
|
||||
|
||||
userApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2.String())).
|
||||
Return(api.ApiPostUsersIDPasswordRequest{ApiService: userApi}.UserID(id2.String()))
|
||||
userApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2)).
|
||||
Return(api.ApiPostUsersIDPasswordRequest{ApiService: userApi}.UserID(id2))
|
||||
userApi.EXPECT().
|
||||
PostUsersIDPasswordExecute(tmock.MatchedBy(func(in api.ApiPostUsersIDPasswordRequest) bool {
|
||||
body := in.GetPasswordResetBody()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id2.String(), in.GetUserID()) &&
|
||||
assert.Equal(t, id2, in.GetUserID()) &&
|
||||
assert.Equal(t, "my-password", body.GetPassword())
|
||||
})).Return(nil)
|
||||
},
|
||||
registerOrgExpectations: func(t *testing.T, orgApi *mock.MockOrganizationsApi) {
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1.String())).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1.String()))
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1)).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1))
|
||||
orgApi.EXPECT().PostOrgsIDMembersExecute(tmock.MatchedBy(func(in api.ApiPostOrgsIDMembersRequest) bool {
|
||||
body := in.GetAddResourceMemberRequestBody()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id1.String(), in.GetOrgID()) &&
|
||||
assert.Equal(t, id2.String(), body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2.String())}, nil)
|
||||
assert.Equal(t, id1, in.GetOrgID()) &&
|
||||
assert.Equal(t, id2, body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2)}, nil)
|
||||
},
|
||||
expectedOut: `2222222222222222\s+my-user`,
|
||||
},
|
||||
@ -91,15 +90,15 @@ func TestClient_Create(t *testing.T) {
|
||||
userApi.EXPECT().PostUsersExecute(tmock.MatchedBy(func(in api.ApiPostUsersRequest) bool {
|
||||
body := in.GetUser()
|
||||
return assert.NotNil(t, body) && assert.Equal(t, "my-user", body.GetName())
|
||||
})).Return(api.UserResponse{Id: api.PtrString(id2.String()), Name: "my-user"}, nil)
|
||||
})).Return(api.UserResponse{Id: api.PtrString(id2), Name: "my-user"}, nil)
|
||||
|
||||
userApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2.String())).
|
||||
Return(api.ApiPostUsersIDPasswordRequest{ApiService: userApi}.UserID(id2.String()))
|
||||
userApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2)).
|
||||
Return(api.ApiPostUsersIDPasswordRequest{ApiService: userApi}.UserID(id2))
|
||||
userApi.EXPECT().
|
||||
PostUsersIDPasswordExecute(tmock.MatchedBy(func(in api.ApiPostUsersIDPasswordRequest) bool {
|
||||
body := in.GetPasswordResetBody()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id2.String(), in.GetUserID()) &&
|
||||
assert.Equal(t, id2, in.GetUserID()) &&
|
||||
assert.Equal(t, "my-password", body.GetPassword())
|
||||
})).Return(nil)
|
||||
},
|
||||
@ -107,16 +106,16 @@ func TestClient_Create(t *testing.T) {
|
||||
orgApi.EXPECT().GetOrgs(gomock.Any()).Return(api.ApiGetOrgsRequest{ApiService: orgApi})
|
||||
orgApi.EXPECT().GetOrgsExecute(tmock.MatchedBy(func(in api.ApiGetOrgsRequest) bool {
|
||||
return assert.Equal(t, "my-org", *in.GetOrg())
|
||||
})).Return(api.Organizations{Orgs: &[]api.Organization{{Id: api.PtrString(id1.String())}}}, nil)
|
||||
})).Return(api.Organizations{Orgs: &[]api.Organization{{Id: api.PtrString(id1)}}}, nil)
|
||||
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1.String())).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1.String()))
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1)).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1))
|
||||
orgApi.EXPECT().PostOrgsIDMembersExecute(tmock.MatchedBy(func(in api.ApiPostOrgsIDMembersRequest) bool {
|
||||
body := in.GetAddResourceMemberRequestBody()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id1.String(), in.GetOrgID()) &&
|
||||
assert.Equal(t, id2.String(), body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2.String())}, nil)
|
||||
assert.Equal(t, id1, in.GetOrgID()) &&
|
||||
assert.Equal(t, id2, body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2)}, nil)
|
||||
},
|
||||
expectedOut: `2222222222222222\s+my-user`,
|
||||
},
|
||||
@ -132,15 +131,15 @@ func TestClient_Create(t *testing.T) {
|
||||
userApi.EXPECT().PostUsersExecute(tmock.MatchedBy(func(in api.ApiPostUsersRequest) bool {
|
||||
body := in.GetUser()
|
||||
return assert.NotNil(t, body) && assert.Equal(t, "my-user", body.GetName())
|
||||
})).Return(api.UserResponse{Id: api.PtrString(id2.String()), Name: "my-user"}, nil)
|
||||
})).Return(api.UserResponse{Id: api.PtrString(id2), Name: "my-user"}, nil)
|
||||
|
||||
userApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2.String())).
|
||||
Return(api.ApiPostUsersIDPasswordRequest{ApiService: userApi}.UserID(id2.String()))
|
||||
userApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2)).
|
||||
Return(api.ApiPostUsersIDPasswordRequest{ApiService: userApi}.UserID(id2))
|
||||
userApi.EXPECT().
|
||||
PostUsersIDPasswordExecute(tmock.MatchedBy(func(in api.ApiPostUsersIDPasswordRequest) bool {
|
||||
body := in.GetPasswordResetBody()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id2.String(), in.GetUserID()) &&
|
||||
assert.Equal(t, id2, in.GetUserID()) &&
|
||||
assert.Equal(t, "my-password", body.GetPassword())
|
||||
})).Return(nil)
|
||||
},
|
||||
@ -148,16 +147,16 @@ func TestClient_Create(t *testing.T) {
|
||||
orgApi.EXPECT().GetOrgs(gomock.Any()).Return(api.ApiGetOrgsRequest{ApiService: orgApi})
|
||||
orgApi.EXPECT().GetOrgsExecute(tmock.MatchedBy(func(in api.ApiGetOrgsRequest) bool {
|
||||
return assert.Equal(t, "my-default-org", *in.GetOrg())
|
||||
})).Return(api.Organizations{Orgs: &[]api.Organization{{Id: api.PtrString(id1.String())}}}, nil)
|
||||
})).Return(api.Organizations{Orgs: &[]api.Organization{{Id: api.PtrString(id1)}}}, nil)
|
||||
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1.String())).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1.String()))
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1)).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1))
|
||||
orgApi.EXPECT().PostOrgsIDMembersExecute(tmock.MatchedBy(func(in api.ApiPostOrgsIDMembersRequest) bool {
|
||||
body := in.GetAddResourceMemberRequestBody()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id1.String(), in.GetOrgID()) &&
|
||||
assert.Equal(t, id2.String(), body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2.String())}, nil)
|
||||
assert.Equal(t, id1, in.GetOrgID()) &&
|
||||
assert.Equal(t, id2, body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2)}, nil)
|
||||
},
|
||||
expectedOut: `2222222222222222\s+my-user`,
|
||||
},
|
||||
@ -175,17 +174,17 @@ func TestClient_Create(t *testing.T) {
|
||||
userApi.EXPECT().PostUsersExecute(tmock.MatchedBy(func(in api.ApiPostUsersRequest) bool {
|
||||
body := in.GetUser()
|
||||
return assert.NotNil(t, body) && assert.Equal(t, "my-user", body.GetName())
|
||||
})).Return(api.UserResponse{Id: api.PtrString(id2.String()), Name: "my-user"}, nil)
|
||||
})).Return(api.UserResponse{Id: api.PtrString(id2), Name: "my-user"}, nil)
|
||||
},
|
||||
registerOrgExpectations: func(t *testing.T, orgApi *mock.MockOrganizationsApi) {
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1.String())).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1.String()))
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1)).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1))
|
||||
orgApi.EXPECT().PostOrgsIDMembersExecute(tmock.MatchedBy(func(in api.ApiPostOrgsIDMembersRequest) bool {
|
||||
body := in.GetAddResourceMemberRequestBody()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id1.String(), in.GetOrgID()) &&
|
||||
assert.Equal(t, id2.String(), body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2.String())}, nil)
|
||||
assert.Equal(t, id1, in.GetOrgID()) &&
|
||||
assert.Equal(t, id2, body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2)}, nil)
|
||||
},
|
||||
expectedOut: `2222222222222222\s+my-user`,
|
||||
expectedStderr: `initial password not set`,
|
||||
@ -228,16 +227,16 @@ func TestClient_Create(t *testing.T) {
|
||||
userApi.EXPECT().PostUsersExecute(tmock.MatchedBy(func(in api.ApiPostUsersRequest) bool {
|
||||
body := in.GetUser()
|
||||
return assert.NotNil(t, body) && assert.Equal(t, "my-user", body.GetName())
|
||||
})).Return(api.UserResponse{Id: api.PtrString(id2.String()), Name: "my-user"}, nil)
|
||||
})).Return(api.UserResponse{Id: api.PtrString(id2), Name: "my-user"}, nil)
|
||||
},
|
||||
registerOrgExpectations: func(t *testing.T, orgApi *mock.MockOrganizationsApi) {
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1.String())).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1.String()))
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1)).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1))
|
||||
orgApi.EXPECT().PostOrgsIDMembersExecute(tmock.MatchedBy(func(in api.ApiPostOrgsIDMembersRequest) bool {
|
||||
body := in.GetAddResourceMemberRequestBody()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id1.String(), in.GetOrgID()) &&
|
||||
assert.Equal(t, id2.String(), body.GetId())
|
||||
assert.Equal(t, id1, in.GetOrgID()) &&
|
||||
assert.Equal(t, id2, body.GetId())
|
||||
})).Return(api.ResourceMember{}, errors.New("I broke"))
|
||||
},
|
||||
expectedOut: `2222222222222222\s+my-user`,
|
||||
@ -259,27 +258,27 @@ func TestClient_Create(t *testing.T) {
|
||||
userApi.EXPECT().PostUsersExecute(tmock.MatchedBy(func(in api.ApiPostUsersRequest) bool {
|
||||
body := in.GetUser()
|
||||
return assert.NotNil(t, body) && assert.Equal(t, "my-user", body.GetName())
|
||||
})).Return(api.UserResponse{Id: api.PtrString(id2.String()), Name: "my-user"}, nil)
|
||||
})).Return(api.UserResponse{Id: api.PtrString(id2), Name: "my-user"}, nil)
|
||||
|
||||
userApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2.String())).
|
||||
Return(api.ApiPostUsersIDPasswordRequest{ApiService: userApi}.UserID(id2.String()))
|
||||
userApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2)).
|
||||
Return(api.ApiPostUsersIDPasswordRequest{ApiService: userApi}.UserID(id2))
|
||||
userApi.EXPECT().
|
||||
PostUsersIDPasswordExecute(tmock.MatchedBy(func(in api.ApiPostUsersIDPasswordRequest) bool {
|
||||
body := in.GetPasswordResetBody()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id2.String(), in.GetUserID()) &&
|
||||
assert.Equal(t, id2, in.GetUserID()) &&
|
||||
assert.Equal(t, "my-password", body.GetPassword())
|
||||
})).Return(errors.New("I broke"))
|
||||
},
|
||||
registerOrgExpectations: func(t *testing.T, orgApi *mock.MockOrganizationsApi) {
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1.String())).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1.String()))
|
||||
orgApi.EXPECT().PostOrgsIDMembers(gomock.Any(), gomock.Eq(id1)).
|
||||
Return(api.ApiPostOrgsIDMembersRequest{ApiService: orgApi}.OrgID(id1))
|
||||
orgApi.EXPECT().PostOrgsIDMembersExecute(tmock.MatchedBy(func(in api.ApiPostOrgsIDMembersRequest) bool {
|
||||
body := in.GetAddResourceMemberRequestBody()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id1.String(), in.GetOrgID()) &&
|
||||
assert.Equal(t, id2.String(), body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2.String())}, nil)
|
||||
assert.Equal(t, id1, in.GetOrgID()) &&
|
||||
assert.Equal(t, id2, body.GetId())
|
||||
})).Return(api.ResourceMember{Id: api.PtrString(id2)}, nil)
|
||||
},
|
||||
expectedOut: `2222222222222222\s+my-user`,
|
||||
expectedErr: "I broke",
|
||||
@ -358,14 +357,14 @@ func TestClient_Delete(t *testing.T) {
|
||||
|
||||
cli := user.Client{CLI: clients.CLI{StdIO: stdio}, UsersApi: userApi}
|
||||
|
||||
getReq := api.ApiGetUsersIDRequest{ApiService: userApi}.UserID(id2.String())
|
||||
userApi.EXPECT().GetUsersID(gomock.Any(), gomock.Eq(id2.String())).Return(getReq)
|
||||
getReq := api.ApiGetUsersIDRequest{ApiService: userApi}.UserID(id2)
|
||||
userApi.EXPECT().GetUsersID(gomock.Any(), gomock.Eq(id2)).Return(getReq)
|
||||
userApi.EXPECT().GetUsersIDExecute(gomock.Eq(getReq)).
|
||||
DoAndReturn(func(api.ApiGetUsersIDRequest) (api.UserResponse, error) {
|
||||
if tc.notFound {
|
||||
return api.UserResponse{}, &api.Error{Code: api.ERRORCODE_NOT_FOUND}
|
||||
}
|
||||
return api.UserResponse{Id: api.PtrString(id2.String()), Name: "my-user"}, nil
|
||||
return api.UserResponse{Id: api.PtrString(id2), Name: "my-user"}, nil
|
||||
})
|
||||
|
||||
if tc.notFound {
|
||||
@ -374,8 +373,8 @@ func TestClient_Delete(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
delReq := api.ApiDeleteUsersIDRequest{ApiService: userApi}.UserID(id2.String())
|
||||
userApi.EXPECT().DeleteUsersID(gomock.Any(), gomock.Eq(id2.String())).Return(delReq)
|
||||
delReq := api.ApiDeleteUsersIDRequest{ApiService: userApi}.UserID(id2)
|
||||
userApi.EXPECT().DeleteUsersID(gomock.Any(), gomock.Eq(id2)).Return(delReq)
|
||||
userApi.EXPECT().DeleteUsersIDExecute(delReq).Return(nil)
|
||||
|
||||
err := cli.Delete(context.Background(), id2)
|
||||
@ -438,10 +437,10 @@ func TestClient_List(t *testing.T) {
|
||||
registerExpectations: func(t *testing.T, usersApi *mock.MockUsersApi) {
|
||||
usersApi.EXPECT().GetUsers(gomock.Any()).Return(api.ApiGetUsersRequest{ApiService: usersApi})
|
||||
usersApi.EXPECT().GetUsersExecute(tmock.MatchedBy(func(in api.ApiGetUsersRequest) bool {
|
||||
return assert.Equal(t, id2.String(), *in.GetId()) && assert.Nil(t, in.GetName())
|
||||
return assert.Equal(t, id2, *in.GetId()) && assert.Nil(t, in.GetName())
|
||||
})).Return(api.Users{
|
||||
Users: &[]api.UserResponse{
|
||||
{Id: api.PtrString(id2.String()), Name: "user11"},
|
||||
{Id: api.PtrString(id2), Name: "user11"},
|
||||
},
|
||||
}, nil)
|
||||
},
|
||||
@ -477,21 +476,21 @@ func TestClient_Update(t *testing.T) {
|
||||
|
||||
ctrl := gomock.NewController(t)
|
||||
userApi := mock.NewMockUsersApi(ctrl)
|
||||
userApi.EXPECT().PatchUsersID(gomock.Any(), gomock.Eq(id2.String())).
|
||||
Return(api.ApiPatchUsersIDRequest{ApiService: userApi}.UserID(id2.String()))
|
||||
userApi.EXPECT().PatchUsersID(gomock.Any(), gomock.Eq(id2)).
|
||||
Return(api.ApiPatchUsersIDRequest{ApiService: userApi}.UserID(id2))
|
||||
userApi.EXPECT().PatchUsersIDExecute(tmock.MatchedBy(func(in api.ApiPatchUsersIDRequest) bool {
|
||||
body := in.GetUser()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id2.String(), in.GetUserID()) &&
|
||||
assert.Equal(t, id2, in.GetUserID()) &&
|
||||
assert.Equal(t, newName, body.GetName())
|
||||
})).Return(api.UserResponse{Id: api.PtrString(id2.String()), Name: newName}, nil)
|
||||
})).Return(api.UserResponse{Id: api.PtrString(id2), Name: newName}, nil)
|
||||
|
||||
stdout := bytes.Buffer{}
|
||||
stdio := mock.NewMockStdIO(ctrl)
|
||||
stdio.EXPECT().Write(gomock.Any()).DoAndReturn(stdout.Write).AnyTimes()
|
||||
|
||||
cli := user.Client{CLI: clients.CLI{StdIO: stdio}, UsersApi: userApi}
|
||||
require.NoError(t, cli.Update(context.Background(), &user.UpdateParmas{Id: id2, Name: newName}))
|
||||
require.NoError(t, cli.Update(context.Background(), &user.UpdateParams{Id: id2, Name: newName}))
|
||||
testutils.MatchLines(t, []string{`ID\s+Name`, `2222222222222222\s+my-new-name`}, strings.Split(stdout.String(), "\n"))
|
||||
}
|
||||
|
||||
@ -511,12 +510,12 @@ func TestClient_SetPassword(t *testing.T) {
|
||||
Id: id2,
|
||||
},
|
||||
registerExpectations: func(t *testing.T, usersApi *mock.MockUsersApi) {
|
||||
usersApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2.String())).
|
||||
Return(api.ApiPostUsersIDPasswordRequest{ApiService: usersApi}.UserID(id2.String()))
|
||||
usersApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2)).
|
||||
Return(api.ApiPostUsersIDPasswordRequest{ApiService: usersApi}.UserID(id2))
|
||||
usersApi.EXPECT().PostUsersIDPasswordExecute(tmock.MatchedBy(func(in api.ApiPostUsersIDPasswordRequest) bool {
|
||||
body := in.GetPasswordResetBody()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id2.String(), in.GetUserID()) &&
|
||||
assert.Equal(t, id2, in.GetUserID()) &&
|
||||
assert.Equal(t, "mypassword", body.GetPassword())
|
||||
})).Return(nil)
|
||||
},
|
||||
@ -530,14 +529,14 @@ func TestClient_SetPassword(t *testing.T) {
|
||||
usersApi.EXPECT().GetUsers(gomock.Any()).Return(api.ApiGetUsersRequest{ApiService: usersApi})
|
||||
usersApi.EXPECT().GetUsersExecute(tmock.MatchedBy(func(in api.ApiGetUsersRequest) bool {
|
||||
return assert.Equal(t, "my-user", *in.GetName())
|
||||
})).Return(api.Users{Users: &[]api.UserResponse{{Id: api.PtrString(id2.String())}}}, nil)
|
||||
})).Return(api.Users{Users: &[]api.UserResponse{{Id: api.PtrString(id2)}}}, nil)
|
||||
|
||||
usersApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2.String())).
|
||||
Return(api.ApiPostUsersIDPasswordRequest{ApiService: usersApi}.UserID(id2.String()))
|
||||
usersApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2)).
|
||||
Return(api.ApiPostUsersIDPasswordRequest{ApiService: usersApi}.UserID(id2))
|
||||
usersApi.EXPECT().PostUsersIDPasswordExecute(tmock.MatchedBy(func(in api.ApiPostUsersIDPasswordRequest) bool {
|
||||
body := in.GetPasswordResetBody()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id2.String(), in.GetUserID()) &&
|
||||
assert.Equal(t, id2, in.GetUserID()) &&
|
||||
assert.Equal(t, "mypassword", body.GetPassword())
|
||||
})).Return(nil)
|
||||
},
|
||||
@ -550,12 +549,12 @@ func TestClient_SetPassword(t *testing.T) {
|
||||
},
|
||||
noExpectAsk: true,
|
||||
registerExpectations: func(t *testing.T, usersApi *mock.MockUsersApi) {
|
||||
usersApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2.String())).
|
||||
Return(api.ApiPostUsersIDPasswordRequest{ApiService: usersApi}.UserID(id2.String()))
|
||||
usersApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2)).
|
||||
Return(api.ApiPostUsersIDPasswordRequest{ApiService: usersApi}.UserID(id2))
|
||||
usersApi.EXPECT().PostUsersIDPasswordExecute(tmock.MatchedBy(func(in api.ApiPostUsersIDPasswordRequest) bool {
|
||||
body := in.GetPasswordResetBody()
|
||||
return assert.NotNil(t, body) &&
|
||||
assert.Equal(t, id2.String(), in.GetUserID()) &&
|
||||
assert.Equal(t, id2, in.GetUserID()) &&
|
||||
assert.Equal(t, "mypassword", body.GetPassword())
|
||||
})).Return(nil)
|
||||
},
|
||||
|
@ -17,21 +17,21 @@ type Client struct {
|
||||
}
|
||||
|
||||
type AuthLookupParams struct {
|
||||
ID influxid.ID
|
||||
ID string
|
||||
Username string
|
||||
}
|
||||
|
||||
func (p AuthLookupParams) Validate() (err error) {
|
||||
if p.Username == "" && !p.ID.Valid() {
|
||||
if p.Username == "" && p.ID == "" {
|
||||
err = fmt.Errorf("id or username required")
|
||||
} else if p.Username != "" && p.ID.Valid() {
|
||||
} else if p.Username != "" && p.ID != "" {
|
||||
err = fmt.Errorf("specify id or username, not both")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (p AuthLookupParams) IsSet() bool {
|
||||
return p.ID.Valid() || p.Username != ""
|
||||
return p.ID != "" || p.Username != ""
|
||||
}
|
||||
|
||||
type v1PrintOpts struct {
|
||||
@ -65,7 +65,7 @@ func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
return fmt.Errorf("only one of --password and --no-password may be specified")
|
||||
}
|
||||
|
||||
orgID, err := c.getOrgID(ctx, params.OrgParams)
|
||||
orgID, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -101,7 +101,7 @@ func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
for _, bp := range bucketPerms {
|
||||
for _, p := range bp.perms {
|
||||
// verify the input ID
|
||||
if _, err := influxid.IDFromString(p); err != nil {
|
||||
if err := influxid.Validate(p); err != nil {
|
||||
return fmt.Errorf("invalid bucket ID '%s': %w (did you pass a bucket name instead of an ID?)", p, err)
|
||||
}
|
||||
|
||||
@ -220,8 +220,8 @@ func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
if params.UserID != "" {
|
||||
req = req.UserID(params.UserID)
|
||||
}
|
||||
if params.OrgID.Valid() {
|
||||
req = req.OrgID(params.OrgID.String())
|
||||
if params.OrgID != "" {
|
||||
req = req.OrgID(params.OrgID)
|
||||
}
|
||||
if params.OrgName != "" {
|
||||
req = req.Org(params.OrgName)
|
||||
@ -229,8 +229,8 @@ func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
if params.Username != "" {
|
||||
req = req.Token(params.Username)
|
||||
}
|
||||
if params.ID.Valid() {
|
||||
req = req.AuthID(params.ID.String())
|
||||
if params.ID != "" {
|
||||
req = req.AuthID(params.ID)
|
||||
}
|
||||
|
||||
auths, err := req.Execute()
|
||||
@ -388,8 +388,8 @@ func (c Client) printV1Tokens(params *v1PrintOpts) error {
|
||||
}
|
||||
|
||||
func (c Client) getAuthReqID(ctx context.Context, params *AuthLookupParams) (id string, err error) {
|
||||
if params.ID.Valid() {
|
||||
id = params.ID.String()
|
||||
if params.ID != "" {
|
||||
id = params.ID
|
||||
} else {
|
||||
var auths api.Authorizations
|
||||
auths, err = c.LegacyAuthorizationsApi.GetLegacyAuthorizations(ctx).Token(params.Username).Execute()
|
||||
@ -401,7 +401,3 @@ func (c Client) getAuthReqID(ctx context.Context, params *AuthLookupParams) (id
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c Client) getOrgID(ctx context.Context, params clients.OrgParams) (string, error) {
|
||||
return c.GetOrgIdI(ctx, params.OrgID, params.OrgName, c.OrganizationsApi)
|
||||
}
|
||||
|
@ -29,18 +29,18 @@ type ListParams struct {
|
||||
}
|
||||
|
||||
func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
if !params.OrgID.Valid() && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
if params.OrgID == "" && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return clients.ErrMustSpecifyOrg
|
||||
}
|
||||
|
||||
req := c.GetDBRPs(ctx)
|
||||
if params.OrgID.Valid() {
|
||||
req = req.OrgID(params.OrgID.String())
|
||||
if params.OrgID != "" {
|
||||
req = req.OrgID(params.OrgID)
|
||||
}
|
||||
if params.OrgName != "" {
|
||||
req = req.Org(params.OrgName)
|
||||
}
|
||||
if !params.OrgID.Valid() && params.OrgName == "" {
|
||||
if params.OrgID == "" && params.OrgName == "" {
|
||||
req = req.Org(c.ActiveConfig.Org)
|
||||
}
|
||||
|
||||
@ -83,36 +83,18 @@ type CreateParams struct {
|
||||
}
|
||||
|
||||
func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
if !params.OrgID.Valid() && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return clients.ErrMustSpecifyOrg
|
||||
orgId, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
reqBody := api.DBRPCreate{
|
||||
OrgID: &orgId,
|
||||
BucketID: params.BucketID,
|
||||
Database: params.DB,
|
||||
RetentionPolicy: params.RP,
|
||||
}
|
||||
|
||||
// For compatibility with the cloud API for creating a DBRP, an org ID must be
|
||||
// provided. The ID will be obtained based on the org name if an
|
||||
// org name is provided but no ID is.
|
||||
if params.OrgID.Valid() {
|
||||
reqBody.OrgID = api.PtrString(params.OrgID.String())
|
||||
} else {
|
||||
orgName := params.OrgName
|
||||
if orgName == "" {
|
||||
orgName = c.ActiveConfig.Org
|
||||
}
|
||||
res, err := c.GetOrgs(ctx).Org(orgName).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to look up ID for org %q: %w", orgName, err)
|
||||
}
|
||||
if len(res.GetOrgs()) == 0 {
|
||||
return fmt.Errorf("no org found with name %q", orgName)
|
||||
}
|
||||
reqBody.OrgID = api.PtrString(res.GetOrgs()[0].GetId())
|
||||
}
|
||||
|
||||
dbrp, err := c.PostDBRP(ctx).DBRPCreate(reqBody).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create dbrp for bucket %q: %w", params.BucketID, err)
|
||||
@ -131,7 +113,7 @@ type UpdateParams struct {
|
||||
}
|
||||
|
||||
func (c Client) Update(ctx context.Context, params *UpdateParams) error {
|
||||
if !params.OrgID.Valid() && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
if params.OrgID == "" && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return clients.ErrMustSpecifyOrg
|
||||
}
|
||||
|
||||
@ -144,13 +126,13 @@ func (c Client) Update(ctx context.Context, params *UpdateParams) error {
|
||||
}
|
||||
|
||||
req := c.PatchDBRPID(ctx, params.ID)
|
||||
if params.OrgID.Valid() {
|
||||
req = req.OrgID(params.OrgID.String())
|
||||
if params.OrgID != "" {
|
||||
req = req.OrgID(params.OrgID)
|
||||
}
|
||||
if params.OrgName != "" {
|
||||
req = req.Org(params.OrgName)
|
||||
}
|
||||
if !params.OrgID.Valid() && params.OrgName == "" {
|
||||
if params.OrgID == "" && params.OrgName == "" {
|
||||
req = req.Org(c.ActiveConfig.Org)
|
||||
}
|
||||
|
||||
@ -171,7 +153,7 @@ type DeleteParams struct {
|
||||
}
|
||||
|
||||
func (c Client) Delete(ctx context.Context, params *DeleteParams) error {
|
||||
if !params.OrgID.Valid() && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
if params.OrgID == "" && params.OrgName == "" && c.ActiveConfig.Org == "" {
|
||||
return clients.ErrMustSpecifyOrg
|
||||
}
|
||||
|
||||
@ -182,15 +164,15 @@ func (c Client) Delete(ctx context.Context, params *DeleteParams) error {
|
||||
|
||||
// The org name or ID must be set on requests for OSS because of how the OSS
|
||||
// authorization mechanism currently works.
|
||||
if params.OrgID.Valid() {
|
||||
getReq = getReq.OrgID(params.OrgID.String())
|
||||
deleteReq = deleteReq.OrgID(params.OrgID.String())
|
||||
if params.OrgID != "" {
|
||||
getReq = getReq.OrgID(params.OrgID)
|
||||
deleteReq = deleteReq.OrgID(params.OrgID)
|
||||
}
|
||||
if params.OrgName != "" {
|
||||
getReq = getReq.Org(params.OrgName)
|
||||
deleteReq = deleteReq.Org(params.OrgName)
|
||||
}
|
||||
if !params.OrgID.Valid() && params.OrgName == "" {
|
||||
if params.OrgID == "" && params.OrgName == "" {
|
||||
getReq = getReq.Org(c.ActiveConfig.Org)
|
||||
deleteReq = deleteReq.Org(c.ActiveConfig.Org)
|
||||
}
|
||||
|
@ -14,12 +14,11 @@ import (
|
||||
v1dbrps "github.com/influxdata/influx-cli/v2/clients/v1_dbrps"
|
||||
"github.com/influxdata/influx-cli/v2/internal/mock"
|
||||
"github.com/influxdata/influx-cli/v2/internal/testutils"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var (
|
||||
id1, _ = influxid.IDFromString("1111111111111111")
|
||||
id1 = "1111111111111111"
|
||||
errApiTest = errors.New("api error for testing")
|
||||
)
|
||||
|
||||
|
@ -33,11 +33,8 @@ type Client struct {
|
||||
}
|
||||
|
||||
type Params struct {
|
||||
BucketID string
|
||||
BucketName string
|
||||
OrgID string
|
||||
OrgName string
|
||||
Precision api.WritePrecision
|
||||
clients.OrgBucketParams
|
||||
Precision api.WritePrecision
|
||||
}
|
||||
|
||||
var ErrWriteCanceled = errors.New("write canceled")
|
||||
|
@ -66,8 +66,10 @@ func TestWriteByIDs(t *testing.T) {
|
||||
mockBatcher := lineBatcher{}
|
||||
|
||||
params := write.Params{
|
||||
OrgID: "12345",
|
||||
BucketID: "98765",
|
||||
OrgBucketParams: clients.OrgBucketParams{
|
||||
OrgParams: clients.OrgParams{OrgID: "12345"},
|
||||
BucketParams: clients.BucketParams{BucketID: "98765"},
|
||||
},
|
||||
Precision: api.WRITEPRECISION_S,
|
||||
}
|
||||
|
||||
@ -118,9 +120,11 @@ func TestWriteByNames(t *testing.T) {
|
||||
mockBatcher := lineBatcher{}
|
||||
|
||||
params := write.Params{
|
||||
OrgName: "my-org",
|
||||
BucketName: "my-bucket",
|
||||
Precision: api.WRITEPRECISION_US,
|
||||
OrgBucketParams: clients.OrgBucketParams{
|
||||
OrgParams: clients.OrgParams{OrgName: "my-org"},
|
||||
BucketParams: clients.BucketParams{BucketName: "my-bucket"},
|
||||
},
|
||||
Precision: api.WRITEPRECISION_US,
|
||||
}
|
||||
|
||||
ctrl := gomock.NewController(t)
|
||||
@ -170,8 +174,10 @@ func TestWriteOrgFromConfig(t *testing.T) {
|
||||
mockBatcher := lineBatcher{}
|
||||
|
||||
params := write.Params{
|
||||
BucketName: "my-bucket",
|
||||
Precision: api.WRITEPRECISION_US,
|
||||
OrgBucketParams: clients.OrgBucketParams{
|
||||
BucketParams: clients.BucketParams{BucketName: "my-bucket"},
|
||||
},
|
||||
Precision: api.WRITEPRECISION_US,
|
||||
}
|
||||
|
||||
defaultOrg := "my-default-org"
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/influxdata/influx-cli/v2/clients"
|
||||
"github.com/influxdata/influx-cli/v2/clients/apply"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/template"
|
||||
@ -16,8 +17,7 @@ import (
|
||||
|
||||
func newApplyCmd() cli.Command {
|
||||
var params struct {
|
||||
orgId string
|
||||
orgName string
|
||||
orgParams clients.OrgParams
|
||||
stackId string
|
||||
inPaths cli.StringSlice
|
||||
inUrls cli.StringSlice
|
||||
@ -89,19 +89,7 @@ For more templates created by the community, see
|
||||
https://github.com/influxdata/community-templates.
|
||||
`,
|
||||
Flags: append(
|
||||
commonFlags(),
|
||||
&cli.StringFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.orgId,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
Usage: "The name of the organization",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.orgName,
|
||||
},
|
||||
append(commonFlags(), getOrgFlags(¶ms.orgParams)...),
|
||||
&cli.StringFlag{
|
||||
Name: "stack-id",
|
||||
Usage: "Stack ID to associate with template application",
|
||||
@ -167,8 +155,7 @@ https://github.com/influxdata/community-templates.
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
parsedParams := apply.Params{
|
||||
OrgId: params.orgId,
|
||||
OrgName: params.orgName,
|
||||
OrgParams: params.orgParams,
|
||||
StackId: params.stackId,
|
||||
Recursive: params.recursive,
|
||||
Quiet: params.quiet,
|
||||
|
@ -26,19 +26,7 @@ Examples:
|
||||
ArgsUsage: "path",
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
|
||||
Flags: append(
|
||||
commonFlagsNoPrint(),
|
||||
&cli.StringFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.OrgID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
Usage: "The name of the organization",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.Org,
|
||||
},
|
||||
append(commonFlagsNoPrint(), getOrgFlags(¶ms.OrgParams)...),
|
||||
&cli.StringFlag{
|
||||
Name: "bucket-id",
|
||||
Usage: "The ID of the bucket to backup",
|
||||
@ -47,7 +35,7 @@ Examples:
|
||||
&cli.StringFlag{
|
||||
Name: "bucket, b",
|
||||
Usage: "The name of the bucket to backup",
|
||||
Destination: ¶ms.Bucket,
|
||||
Destination: ¶ms.BucketName,
|
||||
},
|
||||
&cli.GenericFlag{
|
||||
Name: "compression",
|
||||
|
@ -30,7 +30,7 @@ func newBucketCreateCmd() cli.Command {
|
||||
Usage: "Create bucket",
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
|
||||
Flags: append(
|
||||
commonFlags(),
|
||||
append(commonFlags(), getOrgFlags(¶ms.OrgParams)...),
|
||||
&cli.StringFlag{
|
||||
Name: "name, n",
|
||||
Usage: "New bucket name",
|
||||
@ -53,18 +53,6 @@ func newBucketCreateCmd() cli.Command {
|
||||
Usage: "Shard group duration used internally by the storage engine",
|
||||
Destination: ¶ms.ShardGroupDuration,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.OrgID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
Usage: "The name of the organization",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.OrgName,
|
||||
},
|
||||
&cli.GenericFlag{
|
||||
Name: "schema-type",
|
||||
Usage: "The schema type (implicit, explicit)",
|
||||
@ -90,28 +78,16 @@ func newBucketDeleteCmd() cli.Command {
|
||||
Usage: "Delete bucket",
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
|
||||
Flags: append(
|
||||
commonFlags(),
|
||||
append(commonFlags(), getOrgFlags(¶ms.OrgParams)...),
|
||||
&cli.StringFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The bucket ID, required if name isn't provided",
|
||||
Destination: ¶ms.ID,
|
||||
Destination: ¶ms.BucketID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "name, n",
|
||||
Usage: "The bucket name, org or org-id will be required by choosing this",
|
||||
Destination: ¶ms.Name,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.OrgID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
Usage: "The name of the organization",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.OrgName,
|
||||
Destination: ¶ms.BucketName,
|
||||
},
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
@ -134,28 +110,16 @@ func newBucketListCmd() cli.Command {
|
||||
Aliases: []string{"find", "ls"},
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
|
||||
Flags: append(
|
||||
commonFlags(),
|
||||
append(commonFlags(), getOrgFlags(¶ms.OrgParams)...),
|
||||
&cli.StringFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The bucket ID, required if name isn't provided",
|
||||
Destination: ¶ms.ID,
|
||||
Destination: ¶ms.BucketID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "name, n",
|
||||
Usage: "The bucket name, org or org-id will be required by choosing this",
|
||||
Destination: ¶ms.Name,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.OrgID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
Usage: "The name of the organization",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.OrgName,
|
||||
Destination: ¶ms.BucketName,
|
||||
},
|
||||
&cli.IntFlag{
|
||||
Name: "limit",
|
||||
@ -199,13 +163,13 @@ func newBucketUpdateCmd() cli.Command {
|
||||
Name: "name, n",
|
||||
Usage: "New name to set on the bucket",
|
||||
EnvVar: "INFLUX_BUCKET_NAME",
|
||||
Destination: ¶ms.Name,
|
||||
Destination: ¶ms.BucketName,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The bucket ID",
|
||||
Required: true,
|
||||
Destination: ¶ms.ID,
|
||||
Destination: ¶ms.BucketID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "description, d",
|
||||
|
@ -6,7 +6,6 @@ import (
|
||||
"github.com/influxdata/influx-cli/v2/clients"
|
||||
"github.com/influxdata/influx-cli/v2/clients/bucket_schema"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -102,7 +101,7 @@ func newBucketSchemaCreateCmd() cli.Command {
|
||||
func newBucketSchemaUpdateCmd() cli.Command {
|
||||
var params struct {
|
||||
clients.OrgBucketParams
|
||||
ID influxid.ID
|
||||
ID string
|
||||
Name string
|
||||
ColumnsFile string
|
||||
ColumnsFormat bucket_schema.ColumnsFormat
|
||||
@ -116,10 +115,10 @@ func newBucketSchemaUpdateCmd() cli.Command {
|
||||
commonFlags(),
|
||||
append(
|
||||
getOrgBucketFlags(¶ms.OrgBucketParams),
|
||||
&cli.GenericFlag{
|
||||
Name: "id",
|
||||
Usage: "ID of the measurement",
|
||||
Value: ¶ms.ID,
|
||||
&cli.StringFlag{
|
||||
Name: "id",
|
||||
Usage: "ID of the measurement",
|
||||
Destination: ¶ms.ID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "name",
|
||||
@ -147,7 +146,7 @@ func newBucketSchemaUpdateCmd() cli.Command {
|
||||
return getBucketSchemaClient(ctx).
|
||||
Update(getContext(ctx), bucket_schema.UpdateParams{
|
||||
OrgBucketParams: params.OrgBucketParams,
|
||||
ID: params.ID.String(),
|
||||
ID: params.ID,
|
||||
Name: params.Name,
|
||||
Stdin: os.Stdin,
|
||||
ColumnsFile: params.ColumnsFile,
|
||||
|
@ -14,11 +14,11 @@ func newDeleteCmd() cli.Command {
|
||||
Description: "Delete points from InfluxDB, by specify start, end time and a sql like predicate string",
|
||||
Flags: append(
|
||||
commonFlagsNoPrint(),
|
||||
&cli.GenericFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization that owns the bucket",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Value: ¶ms.OrgID,
|
||||
&cli.StringFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization that owns the bucket",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.OrgID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
@ -26,11 +26,11 @@ func newDeleteCmd() cli.Command {
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.OrgName,
|
||||
},
|
||||
&cli.GenericFlag{
|
||||
Name: "bucket-id",
|
||||
Usage: "The ID of the bucket to delete from",
|
||||
EnvVar: "INFLUX_BUCKET_ID",
|
||||
Value: ¶ms.BucketID,
|
||||
&cli.StringFlag{
|
||||
Name: "bucket-id",
|
||||
Usage: "The ID of the bucket to delete from",
|
||||
EnvVar: "INFLUX_BUCKET_ID",
|
||||
Destination: ¶ms.BucketID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "bucket, b",
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/influxdata/influx-cli/v2/clients"
|
||||
"github.com/influxdata/influx-cli/v2/clients/export"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/template"
|
||||
@ -252,10 +253,9 @@ https://docs.influxdata.com/influxdb/latest/reference/cli/influx/export/`,
|
||||
|
||||
func newExportAllCmd() cli.Command {
|
||||
var params struct {
|
||||
out string
|
||||
orgId string
|
||||
orgName string
|
||||
filters cli.StringSlice
|
||||
orgParams clients.OrgParams
|
||||
out string
|
||||
filters cli.StringSlice
|
||||
}
|
||||
return cli.Command{
|
||||
Name: "all",
|
||||
@ -293,19 +293,7 @@ and
|
||||
https://docs.influxdata.com/influxdb/latest/reference/cli/influx/export/all/
|
||||
`,
|
||||
Flags: append(
|
||||
commonFlagsNoPrint(),
|
||||
&cli.StringFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.orgId,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
Usage: "The name of the organization",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.orgName,
|
||||
},
|
||||
append(commonFlagsNoPrint(), getOrgFlags(¶ms.orgParams)...),
|
||||
&cli.StringFlag{
|
||||
Name: "file, f",
|
||||
Usage: "Output file for created template; defaults to std out if no file provided; the extension of provided file (.yml/.json) will dictate encoding",
|
||||
@ -320,8 +308,7 @@ https://docs.influxdata.com/influxdb/latest/reference/cli/influx/export/all/
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
parsedParams := export.AllParams{
|
||||
OrgId: params.orgId,
|
||||
OrgName: params.orgName,
|
||||
OrgParams: params.orgParams,
|
||||
}
|
||||
|
||||
for _, filter := range params.filters.Value() {
|
||||
|
@ -302,11 +302,11 @@ func commonFlags() []cli.Flag {
|
||||
// the flags to the given params container.
|
||||
func getOrgFlags(params *clients.OrgParams) []cli.Flag {
|
||||
return []cli.Flag{
|
||||
&cli.GenericFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Value: ¶ms.OrgID,
|
||||
&cli.StringFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.OrgID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
@ -321,10 +321,10 @@ func getOrgFlags(params *clients.OrgParams) []cli.Flag {
|
||||
// the flags to the given params container.
|
||||
func getBucketFlags(params *clients.BucketParams) []cli.Flag {
|
||||
return []cli.Flag{
|
||||
&cli.GenericFlag{
|
||||
Name: "bucket-id, i",
|
||||
Usage: "The bucket ID, required if name isn't provided",
|
||||
Value: ¶ms.BucketID,
|
||||
&cli.StringFlag{
|
||||
Name: "bucket-id, i",
|
||||
Usage: "The bucket ID, required if name isn't provided",
|
||||
Destination: ¶ms.BucketID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "bucket, n",
|
||||
|
@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"github.com/influxdata/influx-cli/v2/clients/org"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -54,18 +53,18 @@ func newOrgCreateCmd() cli.Command {
|
||||
}
|
||||
|
||||
func newOrgDeleteCmd() cli.Command {
|
||||
var id influxid.ID
|
||||
var id string
|
||||
return cli.Command{
|
||||
Name: "delete",
|
||||
Usage: "Delete organization",
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
|
||||
Flags: append(
|
||||
commonFlags(),
|
||||
&cli.GenericFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The organization ID",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Value: &id,
|
||||
&cli.StringFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The organization ID",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: &id,
|
||||
},
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
@ -91,13 +90,13 @@ func newOrgListCmd() cli.Command {
|
||||
Name: "name, n",
|
||||
Usage: "The organization name",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.Name,
|
||||
Destination: ¶ms.OrgName,
|
||||
},
|
||||
&cli.GenericFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The organization ID",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Value: ¶ms.ID,
|
||||
&cli.StringFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The organization ID",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.OrgID,
|
||||
},
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
@ -118,18 +117,18 @@ func newOrgUpdateCmd() cli.Command {
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
|
||||
Flags: append(
|
||||
commonFlags(),
|
||||
&cli.GenericFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The organization ID",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Required: true,
|
||||
Value: ¶ms.ID,
|
||||
&cli.StringFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The organization ID",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Required: true,
|
||||
Destination: ¶ms.OrgID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "name, n",
|
||||
Usage: "New name to set on the organization",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.Name,
|
||||
Destination: ¶ms.OrgName,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "description, d",
|
||||
|
@ -27,11 +27,11 @@ func newOrgMembersAddCmd() cli.Command {
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
|
||||
Flags: append(
|
||||
commonFlagsNoPrint(),
|
||||
&cli.GenericFlag{
|
||||
Name: "member, m",
|
||||
Usage: "The member ID",
|
||||
Required: true,
|
||||
Value: ¶ms.MemberId,
|
||||
&cli.StringFlag{
|
||||
Name: "member, m",
|
||||
Usage: "The member ID",
|
||||
Required: true,
|
||||
Destination: ¶ms.MemberId,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "name, n",
|
||||
@ -39,11 +39,11 @@ func newOrgMembersAddCmd() cli.Command {
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.OrgName,
|
||||
},
|
||||
&cli.GenericFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The organization ID",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Value: ¶ms.OrgID,
|
||||
&cli.StringFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The organization ID",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.OrgID,
|
||||
},
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
@ -71,11 +71,11 @@ func newOrgMembersListCmd() cli.Command {
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.OrgName,
|
||||
},
|
||||
&cli.GenericFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The organization ID",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Value: ¶ms.OrgID,
|
||||
&cli.StringFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The organization ID",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.OrgID,
|
||||
},
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
@ -97,11 +97,11 @@ func newOrgMembersRemoveCmd() cli.Command {
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
|
||||
Flags: append(
|
||||
commonFlagsNoPrint(),
|
||||
&cli.GenericFlag{
|
||||
Name: "member, m",
|
||||
Usage: "The member ID",
|
||||
Required: true,
|
||||
Value: ¶ms.MemberId,
|
||||
&cli.StringFlag{
|
||||
Name: "member, m",
|
||||
Usage: "The member ID",
|
||||
Required: true,
|
||||
Destination: ¶ms.MemberId,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "name, n",
|
||||
@ -109,11 +109,11 @@ func newOrgMembersRemoveCmd() cli.Command {
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.OrgName,
|
||||
},
|
||||
&cli.GenericFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The organization ID",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Value: ¶ms.OrgID,
|
||||
&cli.StringFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The organization ID",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.OrgID,
|
||||
},
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
|
@ -19,19 +19,7 @@ func newQueryCmd() cli.Command {
|
||||
ArgsUsage: "[query literal or '-' for stdin]",
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
|
||||
Flags: append(
|
||||
commonFlagsNoPrint(),
|
||||
&cli.GenericFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Value: &orgParams.OrgID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
Usage: "The name of the organization",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: &orgParams.OrgName,
|
||||
},
|
||||
append(commonFlagsNoPrint(), getOrgFlags(&orgParams)...),
|
||||
&cli.StringFlag{
|
||||
Name: "file, f",
|
||||
Usage: "Path to Flux query file",
|
||||
|
@ -28,7 +28,7 @@ func newRemoteCreateCmd() cli.Command {
|
||||
Usage: "Create a new remote connection",
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
|
||||
Flags: append(
|
||||
commonFlags(),
|
||||
append(commonFlags(), getOrgFlags(¶ms.OrgParams)...),
|
||||
&cli.StringFlag{
|
||||
Name: "name, n",
|
||||
Usage: "Name for the new remote connection",
|
||||
@ -40,18 +40,6 @@ func newRemoteCreateCmd() cli.Command {
|
||||
Usage: "Description for the new remote connection",
|
||||
Destination: ¶ms.Description,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the local organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.OrgID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
Usage: "The name of the organization",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.OrgName,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "remote-url",
|
||||
Usage: "The url for the remote database",
|
||||
|
@ -39,7 +39,7 @@ Examples:
|
||||
Name: "org, o",
|
||||
Usage: "The original name of the organization to restore",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.Org,
|
||||
Destination: ¶ms.OrgName,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "bucket-id",
|
||||
@ -49,7 +49,7 @@ Examples:
|
||||
&cli.StringFlag{
|
||||
Name: "bucket, b",
|
||||
Usage: "The original name of the bucket to restore",
|
||||
Destination: ¶ms.Bucket,
|
||||
Destination: ¶ms.BucketName,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "new-bucket",
|
||||
@ -68,19 +68,19 @@ Examples:
|
||||
}
|
||||
params.Path = ctx.Args().Get(0)
|
||||
|
||||
if params.Full && (params.Org != "" ||
|
||||
if params.Full && (params.OrgName != "" ||
|
||||
params.OrgID != "" ||
|
||||
params.Bucket != "" ||
|
||||
params.BucketName != "" ||
|
||||
params.BucketID != "" ||
|
||||
params.NewOrgName != "" ||
|
||||
params.NewBucketName != "") {
|
||||
return errors.New("--full restore cannot be limited to a single org or bucket")
|
||||
}
|
||||
|
||||
if params.NewOrgName != "" && params.OrgID == "" && params.Org == "" {
|
||||
if params.NewOrgName != "" && params.OrgID == "" && params.OrgName == "" {
|
||||
return errors.New("--org-id or --org must be set to use --new-org")
|
||||
}
|
||||
if params.NewBucketName != "" && params.BucketID == "" && params.Bucket == "" {
|
||||
if params.NewBucketName != "" && params.BucketID == "" && params.BucketName == "" {
|
||||
return errors.New("--bucket-id or --bucket must be set to use --new-bucket")
|
||||
}
|
||||
|
||||
|
@ -38,19 +38,7 @@ For information about Stacks and how they integrate with InfluxDB templates, see
|
||||
https://docs.influxdata.com/influxdb/latest/reference/cli/influx/stacks/`,
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
|
||||
Flags: append(
|
||||
commonFlags(),
|
||||
&cli.StringFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.OrgId,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
Usage: "The name of the organization",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.OrgName,
|
||||
},
|
||||
append(commonFlags(), getOrgFlags(¶ms.OrgParams)...),
|
||||
&cli.StringSliceFlag{
|
||||
Name: "stack-id",
|
||||
Usage: "Stack ID to filter by",
|
||||
@ -103,19 +91,7 @@ and
|
||||
https://docs.influxdata.com/influxdb/latest/reference/cli/influx/stacks/init/`,
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
|
||||
Flags: append(
|
||||
commonFlags(),
|
||||
&cli.StringFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.OrgId,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
Usage: "The name of the organization",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.OrgName,
|
||||
},
|
||||
append(commonFlags(), getOrgFlags(¶ms.OrgParams)...),
|
||||
&cli.StringFlag{
|
||||
Name: "stack-name, n",
|
||||
Usage: "Name given to created stack",
|
||||
@ -152,19 +128,7 @@ func newStacksRemoveCmd() cli.Command {
|
||||
Usage: "Remove a stack(s) and all associated resources",
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
|
||||
Flags: append(
|
||||
commonFlags(),
|
||||
&cli.StringFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.OrgId,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
Usage: "The name of the organization",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.OrgName,
|
||||
},
|
||||
append(commonFlags(), getOrgFlags(¶ms.OrgParams)...),
|
||||
&cli.StringSliceFlag{
|
||||
Name: "stack-id",
|
||||
Usage: "Stack IDs to be removed",
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/influxdata/influx-cli/v2/clients"
|
||||
"github.com/influxdata/influx-cli/v2/clients/template"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
|
||||
pkgtmpl "github.com/influxdata/influx-cli/v2/pkg/template"
|
||||
@ -13,28 +14,15 @@ import (
|
||||
)
|
||||
|
||||
type templateParams struct {
|
||||
orgId string
|
||||
orgName string
|
||||
files cli.StringSlice
|
||||
urls cli.StringSlice
|
||||
recurse bool
|
||||
encoding pkgtmpl.Encoding
|
||||
orgParams clients.OrgParams
|
||||
files cli.StringSlice
|
||||
urls cli.StringSlice
|
||||
recurse bool
|
||||
encoding pkgtmpl.Encoding
|
||||
}
|
||||
|
||||
func templateFlags(params *templateParams) []cli.Flag {
|
||||
return []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: ¶ms.orgId,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
Usage: "The name of the organization",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.orgName,
|
||||
},
|
||||
return append(getOrgFlags(¶ms.orgParams), []cli.Flag{
|
||||
&cli.StringSliceFlag{
|
||||
Name: "file, f",
|
||||
Usage: "Path to template file; Supports file paths or (deprecated) HTTP(S) URLs",
|
||||
@ -56,7 +44,7 @@ func templateFlags(params *templateParams) []cli.Flag {
|
||||
Usage: "Encoding for the input stream. If a file is provided will gather encoding type from file extension. If extension provided will override.",
|
||||
Value: ¶ms.encoding,
|
||||
},
|
||||
}
|
||||
}...)
|
||||
}
|
||||
|
||||
func (params templateParams) parseSources() ([]pkgtmpl.Source, error) {
|
||||
@ -121,8 +109,7 @@ func newTemplateCmd() cli.Command {
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
parsedParams := template.SummarizeParams{
|
||||
OrgId: params.orgId,
|
||||
OrgName: params.orgName,
|
||||
OrgParams: params.orgParams,
|
||||
RenderTableColors: !params.noColor,
|
||||
RenderTableBorders: !params.noTableBorders,
|
||||
}
|
||||
@ -152,8 +139,7 @@ func newTemplateValidateCmd() cli.Command {
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
parsedParams := template.ValidateParams{
|
||||
OrgId: params.orgId,
|
||||
OrgName: params.orgName,
|
||||
OrgParams: params.orgParams,
|
||||
}
|
||||
sources, err := params.parseSources()
|
||||
if err != nil {
|
||||
|
@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"github.com/influxdata/influx-cli/v2/clients/user"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@ -28,19 +27,7 @@ func newUserCreateCmd() cli.Command {
|
||||
Name: "create",
|
||||
Usage: "Create user",
|
||||
Flags: append(
|
||||
commonFlags(),
|
||||
&cli.GenericFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Value: ¶ms.OrgID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
Usage: "The name of the organization",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: ¶ms.OrgName,
|
||||
},
|
||||
append(commonFlags(), getOrgFlags(¶ms.OrgParams)...),
|
||||
&cli.StringFlag{
|
||||
Name: "name, n",
|
||||
Usage: "The user name",
|
||||
@ -68,17 +55,17 @@ func newUserCreateCmd() cli.Command {
|
||||
}
|
||||
|
||||
func newUserDeleteCmd() cli.Command {
|
||||
var id influxid.ID
|
||||
var id string
|
||||
return cli.Command{
|
||||
Name: "delete",
|
||||
Usage: "Delete user",
|
||||
Flags: append(
|
||||
commonFlags(),
|
||||
&cli.GenericFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The user ID",
|
||||
Required: true,
|
||||
Value: &id,
|
||||
&cli.StringFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The user ID",
|
||||
Required: true,
|
||||
Destination: &id,
|
||||
},
|
||||
),
|
||||
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
|
||||
@ -97,10 +84,10 @@ func newUserListCmd() cli.Command {
|
||||
Usage: "List users",
|
||||
Flags: append(
|
||||
commonFlags(),
|
||||
&cli.GenericFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The user ID",
|
||||
Value: ¶ms.Id,
|
||||
&cli.StringFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The user ID",
|
||||
Destination: ¶ms.Id,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "name, n",
|
||||
@ -117,16 +104,16 @@ func newUserListCmd() cli.Command {
|
||||
}
|
||||
|
||||
func newUserUpdateCmd() cli.Command {
|
||||
var params user.UpdateParmas
|
||||
var params user.UpdateParams
|
||||
return cli.Command{
|
||||
Name: "update",
|
||||
Flags: append(
|
||||
commonFlags(),
|
||||
&cli.GenericFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The user ID",
|
||||
Required: true,
|
||||
Value: ¶ms.Id,
|
||||
&cli.StringFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The user ID",
|
||||
Required: true,
|
||||
Destination: ¶ms.Id,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "name, n",
|
||||
@ -148,10 +135,10 @@ func newUserSetPasswordCmd() cli.Command {
|
||||
Name: "password",
|
||||
Flags: append(
|
||||
commonFlagsNoPrint(),
|
||||
&cli.GenericFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The user ID",
|
||||
Value: ¶ms.Id,
|
||||
&cli.StringFlag{
|
||||
Name: "id, i",
|
||||
Usage: "The user ID",
|
||||
Destination: ¶ms.Id,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "name, n",
|
||||
|
@ -10,10 +10,10 @@ import (
|
||||
// and can specify to require one but not both.
|
||||
func getAuthLookupFlags(params *v1_auth.AuthLookupParams) []cli.Flag {
|
||||
return []cli.Flag{
|
||||
&cli.GenericFlag{
|
||||
Name: "id",
|
||||
Usage: "The ID of the authorization",
|
||||
Value: ¶ms.ID,
|
||||
&cli.StringFlag{
|
||||
Name: "id",
|
||||
Usage: "The ID of the authorization",
|
||||
Destination: ¶ms.ID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "username",
|
||||
|
@ -64,7 +64,7 @@ func (p *writeParams) makeErrorFile() (*os.File, error) {
|
||||
}
|
||||
|
||||
func (p *writeParams) Flags() []cli.Flag {
|
||||
return []cli.Flag{
|
||||
return append(getOrgFlags(&p.OrgParams), []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "bucket-id",
|
||||
Usage: "The ID of destination bucket",
|
||||
@ -77,18 +77,6 @@ func (p *writeParams) Flags() []cli.Flag {
|
||||
EnvVar: "INFLUX_BUCKET_NAME",
|
||||
Destination: &p.BucketName,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVar: "INFLUX_ORG_ID",
|
||||
Destination: &p.OrgID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org, o",
|
||||
Usage: "The name of the organization",
|
||||
EnvVar: "INFLUX_ORG",
|
||||
Destination: &p.OrgName,
|
||||
},
|
||||
&cli.GenericFlag{
|
||||
Name: "precision, p",
|
||||
Usage: "Precision of the timestamps of the lines",
|
||||
@ -167,7 +155,7 @@ func (p *writeParams) Flags() []cli.Flag {
|
||||
Usage: "Input compression, either 'none' or 'gzip'",
|
||||
Value: &p.Compression,
|
||||
},
|
||||
}
|
||||
}...)
|
||||
}
|
||||
|
||||
func newWriteCmd() cli.Command {
|
||||
|
@ -18,115 +18,34 @@ var (
|
||||
ErrInvalidIDLength = errors.New("id must have a length of 16 bytes")
|
||||
)
|
||||
|
||||
// ID is a unique identifier.
|
||||
//
|
||||
// Its zero value is not a valid ID.
|
||||
type ID uint64
|
||||
|
||||
// IDFromString creates an ID from a given string.
|
||||
//
|
||||
// It errors if the input string does not match a valid ID.
|
||||
func IDFromString(str string) (ID, error) {
|
||||
var id ID
|
||||
err := id.DecodeFromString(str)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
// Validate ensures that a passed string has a valid ID syntax.
|
||||
// Checks that the string is of length 16, and is a valid hex-encoded uint.
|
||||
func Validate(id string) error {
|
||||
_, err := Decode(id)
|
||||
return err
|
||||
}
|
||||
|
||||
// MustIDFromString is like IDFromString but panics if
|
||||
// s is not a valid base-16 identifier.
|
||||
func MustIDFromString(s string) ID {
|
||||
id, err := IDFromString(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// Decode parses b as a hex-encoded byte-slice-string.
|
||||
//
|
||||
// It errors if the input byte slice does not have the correct length
|
||||
// or if it contains all zeros.
|
||||
func (i *ID) Decode(b []byte) error {
|
||||
if len(b) != IDLength {
|
||||
return ErrInvalidIDLength
|
||||
}
|
||||
|
||||
res, err := strconv.ParseUint(string(b), 16, 64)
|
||||
if err != nil {
|
||||
return ErrInvalidID
|
||||
}
|
||||
|
||||
if *i = ID(res); !i.Valid() {
|
||||
return ErrInvalidID
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecodeFromString parses s as a hex-encoded string.
|
||||
func (i *ID) DecodeFromString(s string) error {
|
||||
return i.Decode([]byte(s))
|
||||
}
|
||||
|
||||
// Encode converts ID to a hex-encoded byte-slice-string.
|
||||
//
|
||||
// It errors if the receiving ID holds its zero value.
|
||||
func (i ID) Encode() ([]byte, error) {
|
||||
if !i.Valid() {
|
||||
return nil, ErrInvalidID
|
||||
}
|
||||
|
||||
// Encode converts a uint64 to a hex-encoded byte-slice-string.
|
||||
func Encode(id uint64) string {
|
||||
b := make([]byte, hex.DecodedLen(IDLength))
|
||||
binary.BigEndian.PutUint64(b, uint64(i))
|
||||
binary.BigEndian.PutUint64(b, id)
|
||||
|
||||
dst := make([]byte, hex.EncodedLen(len(b)))
|
||||
hex.Encode(dst, b)
|
||||
return dst, nil
|
||||
return string(dst)
|
||||
}
|
||||
|
||||
// Valid checks whether the receiving ID is a valid one or not.
|
||||
func (i ID) Valid() bool {
|
||||
return i != 0
|
||||
}
|
||||
|
||||
// String returns the ID as a hex encoded string.
|
||||
// Decode parses id as a hex-encoded byte-slice-string.
|
||||
//
|
||||
// Returns an empty string in the case the ID is invalid.
|
||||
func (i ID) String() string {
|
||||
enc, _ := i.Encode()
|
||||
return string(enc)
|
||||
}
|
||||
|
||||
// GoString formats the ID the same as the String method.
|
||||
// Without this, when using the %#v verb, an ID would be printed as a uint64,
|
||||
// so you would see e.g. 0x2def021097c6000 instead of 02def021097c6000
|
||||
// (note the leading 0x, which means the former doesn't show up in searches for the latter).
|
||||
func (i ID) GoString() string {
|
||||
return `"` + i.String() + `"`
|
||||
}
|
||||
|
||||
// MarshalText encodes i as text.
|
||||
// Providing this method is a fallback for json.Marshal,
|
||||
// with the added benefit that IDs encoded as map keys will be the expected string encoding,
|
||||
// rather than the effective fmt.Sprintf("%d", i) that json.Marshal uses by default for integer types.
|
||||
func (i ID) MarshalText() ([]byte, error) {
|
||||
return i.Encode()
|
||||
}
|
||||
|
||||
// UnmarshalText decodes i from a byte slice.
|
||||
// Providing this method is also a fallback for json.Unmarshal,
|
||||
// also relevant when IDs are used as map keys.
|
||||
func (i *ID) UnmarshalText(b []byte) error {
|
||||
return i.Decode(b)
|
||||
}
|
||||
|
||||
func (i *ID) Set(s string) error {
|
||||
id, err := IDFromString(s)
|
||||
if err != nil {
|
||||
return err
|
||||
// It errors if the input byte slice does not have the correct length
|
||||
// or if it contains all zeros.
|
||||
func Decode(id string) (uint64, error) {
|
||||
if len([]byte(id)) != 16 {
|
||||
return 0, ErrInvalidIDLength
|
||||
}
|
||||
*i = id
|
||||
return nil
|
||||
res, err := strconv.ParseUint(id, 16, 64)
|
||||
if err != nil || res == 0 {
|
||||
return 0, ErrInvalidID
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
@ -2,222 +2,46 @@ package influxid_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
)
|
||||
|
||||
func TestIDFromString(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
id string
|
||||
want influxid.ID
|
||||
wantErr bool
|
||||
err string
|
||||
}{
|
||||
{
|
||||
name: "Should be able to decode an all zeros ID",
|
||||
id: "0000000000000000",
|
||||
wantErr: true,
|
||||
err: influxid.ErrInvalidID.Error(),
|
||||
},
|
||||
{
|
||||
name: "Should be able to decode an all f ID",
|
||||
id: "ffffffffffffffff",
|
||||
want: influxid.MustIDFromString("ffffffffffffffff"),
|
||||
},
|
||||
{
|
||||
name: "Should be able to decode an ID",
|
||||
id: "020f755c3c082000",
|
||||
want: influxid.MustIDFromString("020f755c3c082000"),
|
||||
},
|
||||
{
|
||||
name: "Should not be able to decode a non hex ID",
|
||||
id: "gggggggggggggggg",
|
||||
wantErr: true,
|
||||
err: influxid.ErrInvalidID.Error(),
|
||||
},
|
||||
{
|
||||
name: "Should not be able to decode inputs with length less than 16 bytes",
|
||||
id: "abc",
|
||||
wantErr: true,
|
||||
err: influxid.ErrInvalidIDLength.Error(),
|
||||
},
|
||||
{
|
||||
name: "Should not be able to decode inputs with length greater than 16 bytes",
|
||||
id: "abcdabcdabcdabcd0",
|
||||
wantErr: true,
|
||||
err: influxid.ErrInvalidIDLength.Error(),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := influxid.IDFromString(tt.id)
|
||||
|
||||
// Check negative test cases
|
||||
if (err != nil) && tt.wantErr {
|
||||
if tt.err != err.Error() {
|
||||
t.Errorf("IDFromString() errors out \"%s\", want \"%s\"", err, tt.err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Check positive test cases
|
||||
if !reflect.DeepEqual(got, tt.want) && !tt.wantErr {
|
||||
t.Errorf("IDFromString() outputs %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeFromString(t *testing.T) {
|
||||
var id influxid.ID
|
||||
err := id.DecodeFromString("020f755c3c082000")
|
||||
if err != nil {
|
||||
func TestDecode(t *testing.T) {
|
||||
if _, err := influxid.Decode("020f755c3c082000"); err != nil {
|
||||
t.Errorf(err.Error())
|
||||
}
|
||||
want := []byte{48, 50, 48, 102, 55, 53, 53, 99, 51, 99, 48, 56, 50, 48, 48, 48}
|
||||
got, _ := id.Encode()
|
||||
if !bytes.Equal(want, got) {
|
||||
t.Errorf("got %s not equal to wanted %s", string(got), string(want))
|
||||
}
|
||||
if id.String() != "020f755c3c082000" {
|
||||
t.Errorf("expecting string representation to contain the right value")
|
||||
}
|
||||
if !id.Valid() {
|
||||
t.Errorf("expecting ID to be a valid one")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncode(t *testing.T) {
|
||||
var id influxid.ID
|
||||
if _, err := id.Encode(); err == nil {
|
||||
t.Errorf("encoding an invalid ID should not be possible")
|
||||
}
|
||||
|
||||
id.DecodeFromString("5ca1ab1eba5eba11")
|
||||
res, _ := influxid.Decode("5ca1ab1eba5eba11")
|
||||
want := []byte{53, 99, 97, 49, 97, 98, 49, 101, 98, 97, 53, 101, 98, 97, 49, 49}
|
||||
got, _ := id.Encode()
|
||||
got := []byte(influxid.Encode(res))
|
||||
if !bytes.Equal(want, got) {
|
||||
t.Errorf("encoding error")
|
||||
}
|
||||
if id.String() != "5ca1ab1eba5eba11" {
|
||||
t.Errorf("expecting string representation to contain the right value")
|
||||
}
|
||||
if !id.Valid() {
|
||||
t.Errorf("expecting ID to be a valid one")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeFromAllZeros(t *testing.T) {
|
||||
var id influxid.ID
|
||||
err := id.Decode(make([]byte, influxid.IDLength))
|
||||
if err == nil {
|
||||
if _, err := influxid.Decode(string(make([]byte, influxid.IDLength))); err == nil {
|
||||
t.Errorf("expecting all zeros ID to not be a valid ID")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeFromShorterString(t *testing.T) {
|
||||
var id influxid.ID
|
||||
err := id.DecodeFromString("020f75")
|
||||
if err == nil {
|
||||
if _, err := influxid.Decode("020f75"); err == nil {
|
||||
t.Errorf("expecting shorter inputs to error")
|
||||
}
|
||||
if id.String() != "" {
|
||||
t.Errorf("expecting invalid ID to be serialized into empty string")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeFromLongerString(t *testing.T) {
|
||||
var id influxid.ID
|
||||
err := id.DecodeFromString("020f755c3c082000aaa")
|
||||
if err == nil {
|
||||
if _, err := influxid.Decode("020f755c3c082000aaa"); err == nil {
|
||||
t.Errorf("expecting shorter inputs to error")
|
||||
}
|
||||
if id.String() != "" {
|
||||
t.Errorf("expecting invalid ID to be serialized into empty string")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeFromEmptyString(t *testing.T) {
|
||||
var id influxid.ID
|
||||
err := id.DecodeFromString("")
|
||||
if err == nil {
|
||||
if _, err := influxid.Decode(""); err == nil {
|
||||
t.Errorf("expecting empty inputs to error")
|
||||
}
|
||||
if id.String() != "" {
|
||||
t.Errorf("expecting invalid ID to be serialized into empty string")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalling(t *testing.T) {
|
||||
var id0 influxid.ID
|
||||
_, err := json.Marshal(id0)
|
||||
if err == nil {
|
||||
t.Errorf("expecting empty ID to not be a valid one")
|
||||
}
|
||||
|
||||
init := "ca55e77eca55e77e"
|
||||
id1, err := influxid.IDFromString(init)
|
||||
if err != nil {
|
||||
t.Errorf(err.Error())
|
||||
}
|
||||
|
||||
serialized, err := json.Marshal(id1)
|
||||
if err != nil {
|
||||
t.Errorf(err.Error())
|
||||
}
|
||||
|
||||
var id2 influxid.ID
|
||||
json.Unmarshal(serialized, &id2)
|
||||
|
||||
bytes1, _ := id1.Encode()
|
||||
bytes2, _ := id2.Encode()
|
||||
|
||||
if !bytes.Equal(bytes1, bytes2) {
|
||||
t.Errorf("error marshalling/unmarshalling ID")
|
||||
}
|
||||
|
||||
// When used as a map key, IDs must use their string encoding.
|
||||
// If you only implement json.Marshaller, they will be encoded with Go's default integer encoding.
|
||||
b, err := json.Marshal(map[influxid.ID]int{0x1234: 5678})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
const exp = `{"0000000000001234":5678}`
|
||||
if string(b) != exp {
|
||||
t.Errorf("expected map to json.Marshal as %s; got %s", exp, string(b))
|
||||
}
|
||||
|
||||
var idMap map[influxid.ID]int
|
||||
if err := json.Unmarshal(b, &idMap); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(idMap) != 1 {
|
||||
t.Errorf("expected length 1, got %d", len(idMap))
|
||||
}
|
||||
if idMap[0x1234] != 5678 {
|
||||
t.Errorf("unmarshalled incorrectly; exp 0x1234:5678, got %v", idMap)
|
||||
}
|
||||
}
|
||||
|
||||
func TestID_GoString(t *testing.T) {
|
||||
type idGoStringTester struct {
|
||||
ID influxid.ID
|
||||
}
|
||||
var x idGoStringTester
|
||||
|
||||
const idString = "02def021097c6000"
|
||||
if err := x.ID.DecodeFromString(idString); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sharpV := fmt.Sprintf("%#v", x)
|
||||
want := `influxid_test.idGoStringTester{ID:"` + idString + `"}`
|
||||
if sharpV != want {
|
||||
t.Fatalf("bad GoString: got %q, want %q", sharpV, want)
|
||||
}
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ func PrintSummary(summary api.TemplateSummaryResources, out io.Writer, useColor
|
||||
if labels := summary.Labels; len(labels) > 0 {
|
||||
printer := newPrinter("LABELS", []string{"Description", "Color"})
|
||||
for _, l := range labels {
|
||||
id := influxid.ID(l.Id).String()
|
||||
id := influxid.Encode(l.Id)
|
||||
var desc string
|
||||
if l.Properties.Description != nil {
|
||||
desc = *l.Properties.Description
|
||||
@ -39,7 +39,7 @@ func PrintSummary(summary api.TemplateSummaryResources, out io.Writer, useColor
|
||||
if buckets := summary.Buckets; len(buckets) > 0 {
|
||||
printer := newPrinter("BUCKETS", []string{"Retention", "Description", "Schema Type"})
|
||||
for _, b := range buckets {
|
||||
id := influxid.ID(b.Id).String()
|
||||
id := influxid.Encode(b.Id)
|
||||
var desc string
|
||||
if b.Description != nil {
|
||||
desc = *b.Description
|
||||
@ -61,7 +61,7 @@ func PrintSummary(summary api.TemplateSummaryResources, out io.Writer, useColor
|
||||
if checks := summary.Checks; len(checks) > 0 {
|
||||
printer := newPrinter("CHECKS", []string{"Description"})
|
||||
for _, c := range checks {
|
||||
id := influxid.ID(c.Id).String()
|
||||
id := influxid.Encode(c.Id)
|
||||
var desc string
|
||||
if c.Description != nil {
|
||||
desc = *c.Description
|
||||
@ -75,7 +75,7 @@ func PrintSummary(summary api.TemplateSummaryResources, out io.Writer, useColor
|
||||
if dashboards := summary.Dashboards; len(dashboards) > 0 {
|
||||
printer := newPrinter("DASHBOARDS", []string{"Description"})
|
||||
for _, d := range dashboards {
|
||||
id := influxid.ID(d.Id).String()
|
||||
id := influxid.Encode(d.Id)
|
||||
var desc string
|
||||
if d.Description != nil {
|
||||
desc = *d.Description
|
||||
@ -89,7 +89,7 @@ func PrintSummary(summary api.TemplateSummaryResources, out io.Writer, useColor
|
||||
if endpoints := summary.NotificationEndpoints; len(endpoints) > 0 {
|
||||
printer := newPrinter("NOTIFICATION ENDPOINTS", []string{"Description", "Status"})
|
||||
for _, e := range endpoints {
|
||||
id := influxid.ID(e.Id).String()
|
||||
id := influxid.Encode(e.Id)
|
||||
var desc string
|
||||
if e.Description != nil {
|
||||
desc = *e.Description
|
||||
@ -103,8 +103,8 @@ func PrintSummary(summary api.TemplateSummaryResources, out io.Writer, useColor
|
||||
if rules := summary.NotificationRules; len(rules) > 0 {
|
||||
printer := newPrinter("NOTIFICATION RULES", []string{"Description", "Every", "Offset", "Endpoint Name", "Endpoint ID", "Endpoint Type"})
|
||||
for _, r := range rules {
|
||||
id := influxid.ID(r.Id).String()
|
||||
eid := influxid.ID(r.EndpointID).String()
|
||||
id := influxid.Encode(r.Id)
|
||||
eid := influxid.Encode(r.EndpointID)
|
||||
var desc string
|
||||
if r.Description != nil {
|
||||
desc = *r.Description
|
||||
@ -118,7 +118,7 @@ func PrintSummary(summary api.TemplateSummaryResources, out io.Writer, useColor
|
||||
if tasks := summary.Tasks; len(tasks) > 0 {
|
||||
printer := newPrinter("TASKS", []string{"Description", "Cycle"})
|
||||
for _, t := range tasks {
|
||||
id := influxid.ID(t.Id).String()
|
||||
id := influxid.Encode(t.Id)
|
||||
var desc string
|
||||
if t.Description != nil {
|
||||
desc = *t.Description
|
||||
@ -156,7 +156,7 @@ func PrintSummary(summary api.TemplateSummaryResources, out io.Writer, useColor
|
||||
if vars := summary.Variables; len(vars) > 0 {
|
||||
printer := newPrinter("VARIABLES", []string{"Description", "Arg Type", "Arg Values"})
|
||||
for _, v := range vars {
|
||||
id := influxid.ID(v.Id).String()
|
||||
id := influxid.Encode(v.Id)
|
||||
var desc string
|
||||
if v.Description != nil {
|
||||
desc = *v.Description
|
||||
@ -176,8 +176,8 @@ func PrintSummary(summary api.TemplateSummaryResources, out io.Writer, useColor
|
||||
Title("LABEL ASSOCIATIONS").
|
||||
SetHeaders("Resource Type", "Resource Name", "Resource ID", "Label Name", "Label ID")
|
||||
for _, m := range mappings {
|
||||
rid := influxid.ID(m.ResourceID).String()
|
||||
lid := influxid.ID(m.LabelID).String()
|
||||
rid := influxid.Encode(m.ResourceID)
|
||||
lid := influxid.Encode(m.LabelID)
|
||||
printer.Append([]string{m.ResourceType, m.ResourceName, rid, m.LabelName, lid})
|
||||
}
|
||||
printer.Render()
|
||||
|
Loading…
x
Reference in New Issue
Block a user