diff --git a/pkger/http_remote_service.go b/pkger/http_remote_service.go index 47bb0c191f..12f61842fa 100644 --- a/pkger/http_remote_service.go +++ b/pkger/http_remote_service.go @@ -3,6 +3,7 @@ package pkger import ( "context" "net/http" + "net/url" "github.com/influxdata/influxdb" "github.com/influxdata/influxdb/pkg/httpc" @@ -15,6 +16,10 @@ type HTTPRemoteService struct { var _ SVC = (*HTTPRemoteService)(nil) +func (s *HTTPRemoteService) InitStack(ctx context.Context, orgID, userID influxdb.ID, urls ...url.URL) (Stack, error) { + panic("not implemented yet") +} + // CreatePkg will produce a pkg from the parameters provided. func (s *HTTPRemoteService) CreatePkg(ctx context.Context, setters ...CreatePkgSetFn) (*Pkg, error) { var opt CreateOpt diff --git a/pkger/http_server_test.go b/pkger/http_server_test.go index 8efc3f9aa8..b40bc567f1 100644 --- a/pkger/http_server_test.go +++ b/pkger/http_server_test.go @@ -475,6 +475,10 @@ type fakeSVC struct { ApplyFn func(ctx context.Context, orgID, userID influxdb.ID, pkg *pkger.Pkg, opts ...pkger.ApplyOptFn) (pkger.Summary, error) } +func (f *fakeSVC) InitStack(ctx context.Context, orgID, userID influxdb.ID, urls ...url.URL) (pkger.Stack, error) { + panic("not implemented") +} + func (f *fakeSVC) CreatePkg(ctx context.Context, setters ...pkger.CreatePkgSetFn) (*pkger.Pkg, error) { panic("not implemented") } diff --git a/pkger/service.go b/pkger/service.go index 233a104ff0..e36c18178c 100644 --- a/pkger/service.go +++ b/pkger/service.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "net/url" "sort" "strings" "sync" @@ -17,8 +18,26 @@ import ( // APIVersion marks the current APIVersion for influx packages. const APIVersion = "influxdata.com/v2alpha1" +type ( + Stack struct { + ID influxdb.ID + CreatedAt time.Time + UpdatedAt time.Time + URLS []url.URL + Resources []StackResource + } + + StackResource struct { + APIVersion string + ID influxdb.ID + Kind Kind + Name string + } +) + // SVC is the packages service interface. type SVC interface { + InitStack(ctx context.Context, orgID, userID influxdb.ID, urls ...url.URL) (Stack, error) CreatePkg(ctx context.Context, setters ...CreatePkgSetFn) (*Pkg, error) DryRun(ctx context.Context, orgID, userID influxdb.ID, pkg *Pkg, opts ...ApplyOptFn) (Summary, Diff, error) Apply(ctx context.Context, orgID, userID influxdb.ID, pkg *Pkg, opts ...ApplyOptFn) (Summary, error) @@ -124,11 +143,24 @@ func WithVariableSVC(varSVC influxdb.VariableService) ServiceSetterFn { } } +type Store interface { + CreateStack(ctx context.Context, orgID influxdb.ID, stack Stack) error + ReadStackByID(ctx context.Context, id influxdb.ID) (Stack, error) + UpdateStack(ctx context.Context, orgID influxdb.ID, stack Stack) error + DeleteStack(ctx context.Context, id influxdb.ID) error +} + // Service provides the pkger business logic including all the dependencies to make // this resource sausage. type Service struct { log *zap.Logger + // internal dependencies + idGen influxdb.IDGenerator + timeGen influxdb.TimeGenerator + store Store + + // external service dependencies bucketSVC influxdb.BucketService checkSVC influxdb.CheckService dashSVC influxdb.DashboardService @@ -171,6 +203,12 @@ func NewService(opts ...ServiceSetterFn) *Service { } } +func (s *Service) InitStack(ctx context.Context, orgID, userID influxdb.ID, urls ...url.URL) (Stack, error) { + stack := Stack{ + } + return stack, nil +} + type ( // CreatePkgSetFn is a functional input for setting the pkg fields. CreatePkgSetFn func(opt *CreateOpt) error diff --git a/pkger/service_logging.go b/pkger/service_logging.go index e2e88d6a23..f6625067f0 100644 --- a/pkger/service_logging.go +++ b/pkger/service_logging.go @@ -2,6 +2,7 @@ package pkger import ( "context" + "net/url" "time" "github.com/influxdata/influxdb" @@ -25,6 +26,27 @@ func MWLogging(log *zap.Logger) SVCMiddleware { var _ SVC = (*loggingMW)(nil) +func (s *loggingMW) InitStack(ctx context.Context, orgID, userID influxdb.ID, urls ...url.URL) (stack Stack, err error) { + defer func(start time.Time) { + if err != nil { + urlStrs := make([]string, 0, len(urls)) + for _, u := range urls { + urlStrs = append(urlStrs, u.String()) + } + s.logger.Error( + "failed to init stack", + zap.Error(err), + zap.Duration("took", time.Since(start)), + zap.Stringer("orgID", orgID), + zap.Stringer("userID", userID), + zap.Strings("urls", urlStrs), + ) + return + } + }(time.Now()) + return s.next.InitStack(ctx, orgID, userID, urls...) +} + func (s *loggingMW) CreatePkg(ctx context.Context, setters ...CreatePkgSetFn) (pkg *Pkg, err error) { defer func(start time.Time) { dur := zap.Duration("took", time.Since(start)) diff --git a/pkger/service_metrics.go b/pkger/service_metrics.go index c4b13f123d..4c91bf1f05 100644 --- a/pkger/service_metrics.go +++ b/pkger/service_metrics.go @@ -2,6 +2,7 @@ package pkger import ( "context" + "net/url" "github.com/influxdata/influxdb" "github.com/influxdata/influxdb/kit/metric" @@ -27,6 +28,12 @@ func MWMetrics(reg *prom.Registry) SVCMiddleware { } } +func (s *mwMetrics) InitStack(ctx context.Context, orgID, userID influxdb.ID, urls ...url.URL) (Stack, error) { + rec := s.rec.Record("init_stack") + stack, err := s.next.InitStack(ctx, orgID, userID, urls...) + return stack, rec(err) +} + func (s *mwMetrics) CreatePkg(ctx context.Context, setters ...CreatePkgSetFn) (*Pkg, error) { rec := s.rec.Record("create_pkg") pkg, err := s.next.CreatePkg(ctx, setters...) diff --git a/pkger/service_tracing.go b/pkger/service_tracing.go index ddce7cb89a..106031c9d5 100644 --- a/pkger/service_tracing.go +++ b/pkger/service_tracing.go @@ -2,6 +2,7 @@ package pkger import ( "context" + "net/url" "github.com/influxdata/influxdb" "github.com/influxdata/influxdb/kit/tracing" @@ -20,6 +21,12 @@ func MWTracing() SVCMiddleware { var _ SVC = (*traceMW)(nil) +func (s *traceMW) InitStack(ctx context.Context, orgID, userID influxdb.ID, urls ...url.URL) (Stack, error) { + span, ctx := tracing.StartSpanFromContextWithOperationName(ctx, "InitStack") + defer span.Finish() + return s.next.InitStack(ctx, orgID, userID, urls...) +} + func (s *traceMW) CreatePkg(ctx context.Context, setters ...CreatePkgSetFn) (pkg *Pkg, err error) { span, ctx := tracing.StartSpanFromContextWithOperationName(ctx, "CreatePkg") defer span.Finish()