2020-02-03 19:22:47 +00:00
|
|
|
package pkger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2"
|
|
|
|
"github.com/influxdata/influxdb/v2/pkg/httpc"
|
2020-02-03 19:22:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HTTPRemoteService provides an http client that is fluent in all things pkger.
|
|
|
|
type HTTPRemoteService struct {
|
|
|
|
Client *httpc.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ SVC = (*HTTPRemoteService)(nil)
|
|
|
|
|
2020-03-20 23:20:53 +00:00
|
|
|
func (s *HTTPRemoteService) InitStack(ctx context.Context, userID influxdb.ID, stack Stack) (Stack, error) {
|
2020-03-23 23:51:43 +00:00
|
|
|
reqBody := ReqCreateStack{
|
|
|
|
OrgID: stack.OrgID.String(),
|
|
|
|
Name: stack.Name,
|
2020-03-26 20:23:14 +00:00
|
|
|
Description: stack.Description,
|
|
|
|
URLs: stack.URLs,
|
2020-03-23 23:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var respBody RespCreateStack
|
|
|
|
err := s.Client.
|
|
|
|
PostJSON(reqBody, RoutePrefix, "/stacks").
|
|
|
|
DecodeJSON(&respBody).
|
|
|
|
Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return Stack{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newStack := Stack{
|
2020-03-26 20:23:14 +00:00
|
|
|
Name: respBody.Name,
|
|
|
|
Description: respBody.Description,
|
|
|
|
URLs: respBody.URLs,
|
|
|
|
Resources: make([]StackResource, 0),
|
|
|
|
CRUDLog: respBody.CRUDLog,
|
2020-03-23 23:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id, err := influxdb.IDFromString(respBody.ID)
|
|
|
|
if err != nil {
|
|
|
|
return Stack{}, err
|
|
|
|
}
|
|
|
|
newStack.ID = *id
|
|
|
|
|
|
|
|
orgID, err := influxdb.IDFromString(respBody.OrgID)
|
|
|
|
if err != nil {
|
|
|
|
return Stack{}, err
|
|
|
|
}
|
|
|
|
newStack.OrgID = *orgID
|
|
|
|
|
|
|
|
return newStack, nil
|
2020-03-20 02:08:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 19:22:47 +00:00
|
|
|
// CreatePkg will produce a pkg from the parameters provided.
|
|
|
|
func (s *HTTPRemoteService) CreatePkg(ctx context.Context, setters ...CreatePkgSetFn) (*Pkg, error) {
|
|
|
|
var opt CreateOpt
|
|
|
|
for _, setter := range setters {
|
|
|
|
if err := setter(&opt); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-03-06 18:52:18 +00:00
|
|
|
|
|
|
|
var orgIDs []ReqCreateOrgIDOpt
|
|
|
|
for _, org := range opt.OrgIDs {
|
|
|
|
orgIDs = append(orgIDs, ReqCreateOrgIDOpt{
|
|
|
|
OrgID: org.OrgID.String(),
|
|
|
|
Filters: struct {
|
|
|
|
ByLabel []string `json:"byLabel"`
|
|
|
|
ByResourceKind []Kind `json:"byResourceKind"`
|
|
|
|
}{
|
|
|
|
ByLabel: org.LabelNames,
|
|
|
|
ByResourceKind: org.ResourceKinds,
|
|
|
|
},
|
|
|
|
})
|
2020-02-03 19:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reqBody := ReqCreatePkg{
|
|
|
|
OrgIDs: orgIDs,
|
|
|
|
Resources: opt.Resources,
|
|
|
|
}
|
|
|
|
|
|
|
|
var newPkg *Pkg
|
|
|
|
err := s.Client.
|
|
|
|
PostJSON(reqBody, RoutePrefix).
|
|
|
|
Decode(func(resp *http.Response) error {
|
|
|
|
pkg, err := Parse(EncodingJSON, FromReader(resp.Body))
|
|
|
|
newPkg = pkg
|
|
|
|
return err
|
|
|
|
}).
|
|
|
|
Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := newPkg.Validate(ValidWithoutResources()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newPkg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DryRun provides a dry run of the pkg application. The pkg will be marked verified
|
|
|
|
// for later calls to Apply. This func will be run on an Apply if it has not been run
|
|
|
|
// already.
|
2020-02-06 05:42:01 +00:00
|
|
|
func (s *HTTPRemoteService) DryRun(ctx context.Context, orgID, userID influxdb.ID, pkg *Pkg, opts ...ApplyOptFn) (Summary, Diff, error) {
|
|
|
|
return s.apply(ctx, orgID, pkg, true, opts...)
|
2020-02-03 19:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply will apply all the resources identified in the provided pkg. The entire pkg will be applied
|
|
|
|
// in its entirety. If a failure happens midway then the entire pkg will be rolled back to the state
|
|
|
|
// from before the pkg was applied.
|
2020-04-11 04:51:13 +00:00
|
|
|
func (s *HTTPRemoteService) Apply(ctx context.Context, orgID, userID influxdb.ID, pkg *Pkg, opts ...ApplyOptFn) (Summary, Diff, error) {
|
|
|
|
return s.apply(ctx, orgID, pkg, false, opts...)
|
2020-02-06 05:42:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPRemoteService) apply(ctx context.Context, orgID influxdb.ID, pkg *Pkg, dryRun bool, opts ...ApplyOptFn) (Summary, Diff, error) {
|
2020-04-01 00:01:45 +00:00
|
|
|
opt := applyOptFromOptFns(opts...)
|
2020-02-03 19:22:47 +00:00
|
|
|
|
|
|
|
b, err := pkg.Encode(EncodingJSON)
|
|
|
|
if err != nil {
|
2020-02-06 05:42:01 +00:00
|
|
|
return Summary{}, Diff{}, err
|
2020-02-03 19:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reqBody := ReqApplyPkg{
|
|
|
|
OrgID: orgID.String(),
|
2020-02-06 05:42:01 +00:00
|
|
|
DryRun: dryRun,
|
2020-02-05 00:15:20 +00:00
|
|
|
EnvRefs: opt.EnvRefs,
|
2020-02-03 19:22:47 +00:00
|
|
|
Secrets: opt.MissingSecrets,
|
|
|
|
RawPkg: b,
|
|
|
|
}
|
2020-04-01 00:01:45 +00:00
|
|
|
if opt.StackID != 0 {
|
|
|
|
stackID := opt.StackID.String()
|
|
|
|
reqBody.StackID = &stackID
|
|
|
|
}
|
2020-02-03 19:22:47 +00:00
|
|
|
|
|
|
|
var resp RespApplyPkg
|
2020-02-06 05:42:01 +00:00
|
|
|
err = s.Client.
|
2020-02-03 19:22:47 +00:00
|
|
|
PostJSON(reqBody, RoutePrefix, "/apply").
|
|
|
|
DecodeJSON(&resp).
|
|
|
|
Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return Summary{}, Diff{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Summary, resp.Diff, NewParseError(resp.Errors...)
|
|
|
|
}
|