Skip to content

Commit

Permalink
feat(pkger): add support for env references for buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteenb2 committed Feb 5, 2020
1 parent 8150e1b commit 64c8707
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 54 deletions.
4 changes: 2 additions & 2 deletions cmd/influx/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestCmdPkg(t *testing.T) {
pkg.Objects = append(pkg.Objects, pkger.Object{
APIVersion: pkger.APIVersion,
Type: pkger.KindBucket,
Metadata: pkger.Metadata{Name: "bucket1"},
Metadata: pkger.Resource{"name": "bucket1"},
})
return &pkg, nil
},
Expand Down Expand Up @@ -211,7 +211,7 @@ func TestCmdPkg(t *testing.T) {
pkg.Objects = append(pkg.Objects, pkger.Object{
APIVersion: pkger.APIVersion,
Type: rc.Kind,
Metadata: pkger.Metadata{Name: name},
Metadata: pkger.Resource{"name": name},
})
}

Expand Down
30 changes: 30 additions & 0 deletions cmd/influxd/launcher/pkger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,36 @@ spec:
assert.Equal(t, vars[0].Description, v.Description)
})
})

t.Run("apply a package with env refs", func(t *testing.T) {
pkgStr := fmt.Sprintf(`
apiVersion: %[1]s
kind: Bucket
metadata:
name:
envRef:
key: "bkt-1-name-ref"
spec:
`, pkger.APIVersion)

pkg, err := pkger.Parse(pkger.EncodingYAML, pkger.FromString(pkgStr))
require.NoError(t, err)

sum, _, err := svc.DryRun(timedCtx(time.Second), l.Org.ID, l.User.ID, pkg)
require.NoError(t, err)

require.Len(t, sum.Buckets, 1)
assert.Equal(t, "$bkt-1-name-ref", sum.Buckets[0].Name)
assert.Equal(t, []string{"bkt-1-name-ref"}, sum.MissingEnvs)

sum, err = svc.Apply(timedCtx(time.Second), l.Org.ID, l.User.ID, pkg, pkger.ApplyWithEnvRefs(map[string]string{
"bkt-1-name-ref": "rucket_threeve",
}))
require.NoError(t, err)

assert.Equal(t, "rucket_threeve", sum.Buckets[0].Name)
assert.Empty(t, sum.MissingEnvs)
})
}

func timedCtx(d time.Duration) context.Context {
Expand Down
4 changes: 4 additions & 0 deletions http/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7290,6 +7290,10 @@ components:
type: string
labelID:
type: string
missingEnvRefs:
type: array
items:
type: string
missingSecrets:
type: array
items:
Expand Down
24 changes: 15 additions & 9 deletions pkger/clone_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func bucketToObject(bkt influxdb.Bucket, name string) Object {
k := Object{
APIVersion: APIVersion,
Type: KindBucket,
Metadata: Metadata{Name: name},
Metadata: convertToMetadataResource(name),
Spec: make(Resource),
}
assignNonZeroStrings(k.Spec, map[string]string{fieldDescription: bkt.Description})
Expand All @@ -80,7 +80,7 @@ func checkToObject(ch influxdb.Check, name string) Object {
}
k := Object{
APIVersion: APIVersion,
Metadata: Metadata{Name: name},
Metadata: convertToMetadataResource(name),
Spec: Resource{
fieldStatus: influxdb.TaskStatusActive,
},
Expand Down Expand Up @@ -391,7 +391,7 @@ func DashboardToObject(dash influxdb.Dashboard, name string) Object {
return Object{
APIVersion: APIVersion,
Type: KindDashboard,
Metadata: Metadata{Name: name},
Metadata: convertToMetadataResource(name),
Spec: Resource{
fieldDescription: dash.Description,
fieldDashCharts: charts,
Expand All @@ -406,7 +406,7 @@ func labelToObject(l influxdb.Label, name string) Object {
k := Object{
APIVersion: APIVersion,
Type: KindLabel,
Metadata: Metadata{Name: name},
Metadata: convertToMetadataResource(name),
Spec: make(Resource),
}

Expand All @@ -423,7 +423,7 @@ func endpointKind(e influxdb.NotificationEndpoint, name string) Object {
}
k := Object{
APIVersion: APIVersion,
Metadata: Metadata{Name: name},
Metadata: convertToMetadataResource(name),
Spec: make(Resource),
}
assignNonZeroStrings(k.Spec, map[string]string{
Expand Down Expand Up @@ -466,7 +466,7 @@ func ruleToObject(iRule influxdb.NotificationRule, endpointName, name string) Ob
k := Object{
APIVersion: APIVersion,
Type: KindNotificationRule,
Metadata: Metadata{Name: name},
Metadata: convertToMetadataResource(name),
Spec: Resource{
fieldNotificationRuleEndpointName: endpointName,
},
Expand Down Expand Up @@ -536,7 +536,7 @@ func taskToObject(t influxdb.Task, name string) Object {
k := Object{
APIVersion: APIVersion,
Type: KindTask,
Metadata: Metadata{Name: name},
Metadata: convertToMetadataResource(name),
Spec: Resource{
fieldQuery: strings.TrimSpace(query),
},
Expand All @@ -557,7 +557,7 @@ func telegrafToObject(t influxdb.TelegrafConfig, name string) Object {
k := Object{
APIVersion: APIVersion,
Type: KindTelegraf,
Metadata: Metadata{Name: name},
Metadata: convertToMetadataResource(name),
Spec: Resource{
fieldTelegrafConfig: t.Config,
},
Expand All @@ -577,7 +577,7 @@ func VariableToObject(v influxdb.Variable, name string) Object {
k := Object{
APIVersion: APIVersion,
Type: KindVariable,
Metadata: Metadata{Name: name},
Metadata: convertToMetadataResource(name),
Spec: make(Resource),
}
assignNonZeroStrings(k.Spec, map[string]string{fieldDescription: v.Description})
Expand Down Expand Up @@ -610,6 +610,12 @@ func VariableToObject(v influxdb.Variable, name string) Object {
return k
}

func convertToMetadataResource(name string) Resource {
return Resource{
"name": name,
}
}

func assignNonZeroFluxDurs(r Resource, m map[string]*notification.Duration) {
for field, dur := range m {
if dur == nil {
Expand Down
1 change: 1 addition & 0 deletions pkger/http_remote_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (s *HTTPRemoteService) Apply(ctx context.Context, orgID, userID influxdb.ID

reqBody := ReqApplyPkg{
OrgID: orgID.String(),
EnvRefs: opt.EnvRefs,
Secrets: opt.MissingSecrets,
RawPkg: b,
}
Expand Down
3 changes: 2 additions & 1 deletion pkger/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ type ReqApplyPkg struct {
OrgID string `json:"orgID" yaml:"orgID"`
Remote PkgRemote `json:"remote" yaml:"remote"`
RawPkg json.RawMessage `json:"package" yaml:"package"`
EnvRefs map[string]string `json:"envRefs"`
Secrets map[string]string `json:"secrets"`
}

Expand Down Expand Up @@ -230,7 +231,7 @@ func (s *HTTPServer) applyPkg(w http.ResponseWriter, r *http.Request) {
return
}

sum, err = s.svc.Apply(r.Context(), *orgID, userID, parsedPkg, ApplyWithSecrets(reqBody.Secrets))
sum, err = s.svc.Apply(r.Context(), *orgID, userID, parsedPkg, ApplyWithEnvRefs(reqBody.EnvRefs), ApplyWithSecrets(reqBody.Secrets))
if err != nil && !IsParseErr(err) {
s.api.Err(w, err)
return
Expand Down
43 changes: 25 additions & 18 deletions pkger/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,6 @@ func (s SafeID) String() string {
return influxdb.ID(s).String()
}

// Metadata is the pkg metadata. This data describes the user
// defined identifiers.
type Metadata struct {
Name string `yaml:"name" json:"name"`
}

// Diff is the result of a service DryRun call. The diff outlines
// what is new and or updated from the current state of the platform.
type Diff struct {
Expand Down Expand Up @@ -523,6 +517,7 @@ type Summary struct {
NotificationRules []SummaryNotificationRule `json:"notificationRules"`
Labels []SummaryLabel `json:"labels"`
LabelMappings []SummaryLabelMapping `json:"labelMappings"`
MissingEnvs []string `json:"missingEnvRefs"`
MissingSecrets []string `json:"missingSecrets"`
Tasks []SummaryTask `json:"summaryTask"`
TelegrafConfigs []SummaryTelegraf `json:"telegrafConfigs"`
Expand Down Expand Up @@ -796,7 +791,7 @@ type bucket struct {
id influxdb.ID
OrgID influxdb.ID
Description string
name string
name *references
RetentionRules retentionRules
labels sortedLabels

Expand All @@ -818,7 +813,7 @@ func (b *bucket) Labels() []*label {
}

func (b *bucket) Name() string {
return b.name
return b.name.String()
}

func (b *bucket) ResourceType() influxdb.ResourceType {
Expand Down Expand Up @@ -1355,13 +1350,13 @@ type notificationEndpoint struct {
name string
description string
method string
password references
routingKey references
password *references
routingKey *references
status string
token references
token *references
httpType string
url string
username references
username *references

labels sortedLabels

Expand Down Expand Up @@ -2644,31 +2639,43 @@ func (l legend) influxLegend() influxdb.Legend {
}

const (
fieldReferencesEnv = "envRef"
fieldReferencesSecret = "secretRef"
)

type references struct {
val interface{}
Secret string `json:"secretRef"`
EnvRef string
Secret string
}

func (r *references) hasValue() bool {
return r.EnvRef != "" || r.Secret != "" || r.val != nil
}

func (r references) hasValue() bool {
return r.Secret != "" || r.val != nil
func (r *references) String() string {
if v := r.StringVal(); v != "" {
return v
}
if r.EnvRef != "" {
return "$" + r.EnvRef
}
return ""
}

func (r references) String() string {
func (r *references) StringVal() string {
if r.val != nil {
s, _ := r.val.(string)
return s
}
return ""
}

func (r references) SecretField() influxdb.SecretField {
func (r *references) SecretField() influxdb.SecretField {
if secret := r.Secret; secret != "" {
return influxdb.SecretField{Key: secret}
}
if str := r.String(); str != "" {
if str := r.StringVal(); str != "" {
return influxdb.SecretField{Value: &str}
}
return influxdb.SecretField{}
Expand Down
6 changes: 3 additions & 3 deletions pkger/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func TestPkg(t *testing.T) {
id: influxdb.ID(2),
OrgID: influxdb.ID(100),
Description: "desc2",
name: "name2",
name: &references{val: "name2"},
RetentionRules: retentionRules{newRetentionRule(2 * time.Hour)},
},
"buck_1": {
id: influxdb.ID(1),
OrgID: influxdb.ID(100),
name: "name1",
name: &references{val: "name1"},
Description: "desc1",
RetentionRules: retentionRules{newRetentionRule(time.Hour)},
},
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestPkg(t *testing.T) {
t.Run("label mappings returned in asc order by name", func(t *testing.T) {
bucket1 := &bucket{
id: influxdb.ID(20),
name: "b1",
name: &references{val: "b1"},
}
label1 := &label{
id: influxdb.ID(2),
Expand Down
Loading

0 comments on commit 64c8707

Please sign in to comment.