Skip to content

Commit

Permalink
Merge branch 'master' into feat/conditionally-query-cell
Browse files Browse the repository at this point in the history
  • Loading branch information
asalem1 authored Jun 23, 2020
2 parents 46f1b00 + 38d44c7 commit 1b09f53
Show file tree
Hide file tree
Showing 24 changed files with 287 additions and 109 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
1. [18601](https://github.com/influxdata/influxdb/pull/18601): Provide active config running influx config without args
1. [18606](https://github.com/influxdata/influxdb/pull/18606): Enable influxd binary to look for a config file on startup
1. [18647](https://github.com/influxdata/influxdb/pull/18647): Add support for env ref default values to the template parser
1. [18655](https://github.com/influxdata/influxdb/pull/18655): Add support for platform variable selected field to templates

### Bug Fixes

1. [18602](https://github.com/influxdata/influxdb/pull/18602): Fix uint overflow during setup on 32bit systems
1. [18623](https://github.com/influxdata/influxdb/pull/18623): Drop support for --local flag within influx CLI
1. [18632](https://github.com/influxdata/influxdb/pull/18632): Prevents undefined queries in cells from erroring out in dashboards
1. [18581](https://github.com/influxdata/influxdb/pull/18581): Cache dashboard cell query results to use as a reference for cell configurations
1. [18658](https://github.com/influxdata/influxdb/pull/18658): Add support for 'd' day time identifier in the CLI for bucket and setup commands

## v2.0.0-beta.12 [2020-06-12]

Expand Down
52 changes: 42 additions & 10 deletions authorization/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,24 @@ type TenantService interface {
FindOrganization(ctx context.Context, filter influxdb.OrganizationFilter) (*influxdb.Organization, error)
FindUserByID(ctx context.Context, id influxdb.ID) (*influxdb.User, error)
FindUser(ctx context.Context, filter influxdb.UserFilter) (*influxdb.User, error)
FindBucketByID(ctx context.Context, id influxdb.ID) (*influxdb.Bucket, error)
}

type AuthHandler struct {
chi.Router
api *kithttp.API
log *zap.Logger
authSvc influxdb.AuthorizationService
lookupService influxdb.LookupService
tenantService TenantService
}

// NewHTTPAuthHandler constructs a new http server.
func NewHTTPAuthHandler(log *zap.Logger, authService influxdb.AuthorizationService, tenantService TenantService, lookupService influxdb.LookupService) *AuthHandler {
func NewHTTPAuthHandler(log *zap.Logger, authService influxdb.AuthorizationService, tenantService TenantService) *AuthHandler {
h := &AuthHandler{
api: kithttp.NewAPI(kithttp.WithLog(log)),
log: log,
authSvc: authService,
tenantService: tenantService,
lookupService: lookupService,
}

r := chi.NewRouter()
Expand Down Expand Up @@ -97,7 +96,7 @@ func (h *AuthHandler) handlePostAuthorization(w http.ResponseWriter, r *http.Req
return
}

perms, err := newPermissionsResponse(ctx, auth.Permissions, h.lookupService)
perms, err := h.newPermissionsResponse(ctx, auth.Permissions)
if err != nil {
h.api.Err(w, r, err)
return
Expand Down Expand Up @@ -296,7 +295,7 @@ type resourceResponse struct {
Organization string `json:"org,omitempty"`
}

func newPermissionsResponse(ctx context.Context, ps []influxdb.Permission, svc influxdb.LookupService) ([]permissionResponse, error) {
func (h *AuthHandler) newPermissionsResponse(ctx context.Context, ps []influxdb.Permission) ([]permissionResponse, error) {
res := make([]permissionResponse, len(ps))
for i, p := range ps {
res[i] = permissionResponse{
Expand All @@ -307,7 +306,7 @@ func newPermissionsResponse(ctx context.Context, ps []influxdb.Permission, svc i
}

if p.Resource.ID != nil {
name, err := svc.Name(ctx, p.Resource.Type, *p.Resource.ID)
name, err := h.getNameForResource(ctx, p.Resource.Type, *p.Resource.ID)
if influxdb.ErrorCode(err) == influxdb.ENotFound {
continue
}
Expand All @@ -318,7 +317,7 @@ func newPermissionsResponse(ctx context.Context, ps []influxdb.Permission, svc i
}

if p.Resource.OrgID != nil {
name, err := svc.Name(ctx, influxdb.OrgsResourceType, *p.Resource.OrgID)
name, err := h.getNameForResource(ctx, influxdb.OrgsResourceType, *p.Resource.OrgID)
if influxdb.ErrorCode(err) == influxdb.ENotFound {
continue
}
Expand All @@ -331,6 +330,39 @@ func newPermissionsResponse(ctx context.Context, ps []influxdb.Permission, svc i
return res, nil
}

func (h *AuthHandler) getNameForResource(ctx context.Context, resource influxdb.ResourceType, id influxdb.ID) (string, error) {
if err := resource.Valid(); err != nil {
return "", err
}

if ok := id.Valid(); !ok {
return "", influxdb.ErrInvalidID
}

switch resource {
case influxdb.BucketsResourceType:
r, err := h.tenantService.FindBucketByID(ctx, id)
if err != nil {
return "", err
}
return r.Name, nil
case influxdb.OrgsResourceType:
r, err := h.tenantService.FindOrganizationByID(ctx, id)
if err != nil {
return "", err
}
return r.Name, nil
case influxdb.UsersResourceType:
r, err := h.tenantService.FindUserByID(ctx, id)
if err != nil {
return "", err
}
return r.Name, nil
}

return "", nil
}

func decodePostAuthorizationRequest(ctx context.Context, r *http.Request) (*postAuthorizationRequest, error) {
a := &postAuthorizationRequest{}
if err := json.NewDecoder(r.Body).Decode(a); err != nil {
Expand Down Expand Up @@ -386,7 +418,7 @@ func (h *AuthHandler) handleGetAuthorizations(w http.ResponseWriter, r *http.Req

auths := make([]*authResponse, 0, len(as))
for _, a := range as {
ps, err := newPermissionsResponse(ctx, a.Permissions, h.lookupService)
ps, err := h.newPermissionsResponse(ctx, a.Permissions)
if err != nil {
h.api.Err(w, r, err)
return
Expand Down Expand Up @@ -471,7 +503,7 @@ func (h *AuthHandler) handleGetAuthorization(w http.ResponseWriter, r *http.Requ
return
}

ps, err := newPermissionsResponse(ctx, a.Permissions, h.lookupService)
ps, err := h.newPermissionsResponse(ctx, a.Permissions)
if err != nil {
h.api.Err(w, r, err)
return
Expand Down Expand Up @@ -510,7 +542,7 @@ func (h *AuthHandler) handleUpdateAuthorization(w http.ResponseWriter, r *http.R
return
}

ps, err := newPermissionsResponse(ctx, a.Permissions, h.lookupService)
ps, err := h.newPermissionsResponse(ctx, a.Permissions)
if err != nil {
h.api.Err(w, r, err)
return
Expand Down
40 changes: 14 additions & 26 deletions authorization/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func TestService_handlePostAuthorization(t *testing.T) {
type fields struct {
AuthorizationService influxdb.AuthorizationService
TenantService TenantService
LookupService influxdb.LookupService
}
type args struct {
session *influxdb.Authorization
Expand Down Expand Up @@ -72,16 +71,11 @@ func TestService_handlePostAuthorization(t *testing.T) {
Name: "o1",
}, nil
},
},
LookupService: &mock.LookupService{
NameFn: func(ctx context.Context, resource influxdb.ResourceType, id influxdb.ID) (string, error) {
switch resource {
case influxdb.BucketsResourceType:
return "b1", nil
case influxdb.OrgsResourceType:
return "o1", nil
}
return "", fmt.Errorf("bad resource type %s", resource)
FindBucketByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Bucket, error) {
return &influxdb.Bucket{
ID: id,
Name: "b1",
}, nil
},
},
},
Expand Down Expand Up @@ -168,7 +162,7 @@ func TestService_handlePostAuthorization(t *testing.T) {

svc := NewService(storage, tt.fields.TenantService)

handler := NewHTTPAuthHandler(zaptest.NewLogger(t), svc, tt.fields.TenantService, mock.NewLookupService())
handler := NewHTTPAuthHandler(zaptest.NewLogger(t), svc, tt.fields.TenantService)
router := chi.NewRouter()
router.Mount(handler.Prefix(), handler)

Expand Down Expand Up @@ -223,7 +217,6 @@ func TestService_handleGetAuthorization(t *testing.T) {
type fields struct {
AuthorizationService influxdb.AuthorizationService
TenantService TenantService
LookupService influxdb.LookupService
}
type args struct {
id string
Expand Down Expand Up @@ -283,16 +276,11 @@ func TestService_handleGetAuthorization(t *testing.T) {
Name: "o1",
}, nil
},
},
LookupService: &mock.LookupService{
NameFn: func(ctx context.Context, resource influxdb.ResourceType, id influxdb.ID) (string, error) {
switch resource {
case influxdb.BucketsResourceType:
return "b1", nil
case influxdb.OrgsResourceType:
return "o1", nil
}
return "", fmt.Errorf("bad resource type %s", resource)
FindBucketByIDFn: func(ctx context.Context, id influxdb.ID) (*influxdb.Bucket, error) {
return &influxdb.Bucket{
ID: id,
Name: "b1",
}, nil
},
},
},
Expand Down Expand Up @@ -361,7 +349,7 @@ func TestService_handleGetAuthorization(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Helper()

handler := NewHTTPAuthHandler(zaptest.NewLogger(t), tt.fields.AuthorizationService, tt.fields.TenantService, mock.NewLookupService())
handler := NewHTTPAuthHandler(zaptest.NewLogger(t), tt.fields.AuthorizationService, tt.fields.TenantService)
router := chi.NewRouter()
router.Mount(handler.Prefix(), handler)

Expand Down Expand Up @@ -700,7 +688,7 @@ func TestService_handleGetAuthorizations(t *testing.T) {

svc := NewService(storage, tt.fields.TenantService)

handler := NewHTTPAuthHandler(zaptest.NewLogger(t), svc, tt.fields.TenantService, mock.NewLookupService())
handler := NewHTTPAuthHandler(zaptest.NewLogger(t), svc, tt.fields.TenantService)
router := chi.NewRouter()
router.Mount(handler.Prefix(), handler)

Expand Down Expand Up @@ -806,7 +794,7 @@ func TestService_handleDeleteAuthorization(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Helper()

handler := NewHTTPAuthHandler(zaptest.NewLogger(t), tt.fields.AuthorizationService, tt.fields.TenantService, mock.NewLookupService())
handler := NewHTTPAuthHandler(zaptest.NewLogger(t), tt.fields.AuthorizationService, tt.fields.TenantService)
router := chi.NewRouter()
router.Mount(handler.Prefix(), handler)

Expand Down
5 changes: 5 additions & 0 deletions authorization/mock_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type tenantService struct {
FindUserFn func(context.Context, influxdb.UserFilter) (*influxdb.User, error)
FindOrganizationByIDF func(ctx context.Context, id influxdb.ID) (*influxdb.Organization, error)
FindOrganizationF func(ctx context.Context, filter influxdb.OrganizationFilter) (*influxdb.Organization, error)
FindBucketByIDFn func(context.Context, influxdb.ID) (*influxdb.Bucket, error)
}

// FindUserByID returns a single User by ID.
Expand All @@ -33,3 +34,7 @@ func (s *tenantService) FindOrganizationByID(ctx context.Context, id influxdb.ID
func (s *tenantService) FindOrganization(ctx context.Context, filter influxdb.OrganizationFilter) (*influxdb.Organization, error) {
return s.FindOrganizationF(ctx, filter)
}

func (s *tenantService) FindBucketByID(ctx context.Context, id influxdb.ID) (*influxdb.Bucket, error) {
return s.FindBucketByIDFn(ctx, id)
}
23 changes: 16 additions & 7 deletions cmd/influx/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"time"

"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/http"
Expand All @@ -30,7 +29,7 @@ type cmdBucketBuilder struct {
name string
description string
org organization
retention time.Duration
retention string
}

func newCmdBucketBuilder(svcsFn bucketSVCsFn, opts genericCLIOpts) *cmdBucketBuilder {
Expand Down Expand Up @@ -72,7 +71,7 @@ func (b *cmdBucketBuilder) cmdCreate() *cobra.Command {
opts.mustRegister(cmd)

cmd.Flags().StringVarP(&b.description, "description", "d", "", "Description of bucket that will be created")
cmd.Flags().DurationVarP(&b.retention, "retention", "r", 0, "Duration bucket will retain data. 0 is infinite. Default is 0.")
cmd.Flags().StringVarP(&b.retention, "retention", "r", "", "Duration bucket will retain data. 0 is infinite. Default is 0.")
b.org.register(cmd, false)
b.registerPrintFlags(cmd)

Expand All @@ -89,10 +88,15 @@ func (b *cmdBucketBuilder) cmdCreateRunEFn(*cobra.Command, []string) error {
return err
}

dur, err := rawDurationToTimeDuration(b.retention)
if err != nil {
return err
}

bkt := &influxdb.Bucket{
Name: b.name,
Description: b.description,
RetentionPeriod: b.retention,
RetentionPeriod: dur,
}
bkt.OrgID, err = b.org.getID(orgSVC)
if err != nil {
Expand Down Expand Up @@ -245,7 +249,7 @@ func (b *cmdBucketBuilder) cmdUpdate() *cobra.Command {
cmd.Flags().StringVarP(&b.id, "id", "i", "", "The bucket ID (required)")
cmd.Flags().StringVarP(&b.description, "description", "d", "", "Description of bucket that will be created")
cmd.MarkFlagRequired("id")
cmd.Flags().DurationVarP(&b.retention, "retention", "r", 0, "Duration bucket will retain data. 0 is infinite. Default is 0.")
cmd.Flags().StringVarP(&b.retention, "retention", "r", "", "Duration bucket will retain data. 0 is infinite. Default is 0.")

return cmd
}
Expand All @@ -268,8 +272,13 @@ func (b *cmdBucketBuilder) cmdUpdateRunEFn(cmd *cobra.Command, args []string) er
if b.description != "" {
update.Description = &b.description
}
if b.retention != 0 {
update.RetentionPeriod = &b.retention

dur, err := rawDurationToTimeDuration(b.retention)
if err != nil {
return err
}
if dur != 0 {
update.RetentionPeriod = &dur
}

bkt, err := bktSVC.UpdateBucket(context.Background(), id, update)
Expand Down
2 changes: 1 addition & 1 deletion cmd/influx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (b *cmdConfigBuilder) cmdDelete() *cobra.Command {

b.registerPrintFlags(cmd)
cmd.Flags().StringVarP(&b.name, "name", "n", "", "The config name (required)")
cmd.Flags().MarkHidden("name")
cmd.Flags().MarkDeprecated("name", "provide the name as an arg; example: influx config rm $CFG_NAME")

return cmd
}
Expand Down
Loading

0 comments on commit 1b09f53

Please sign in to comment.