[#102] Add function pass forwarding for annotating

* Expose the GetAnnotations method of the document.Document interface.
* Add the Annotate method for adding a single annotation to a
  document.Document
* Clean up the implementation of Label and GetLabels

Change-Id: I94af2380f50848bfcd64af9d5c2c991305d9746f
Relates-To: #102
This commit is contained in:
Ian Howell 2020-03-17 12:14:44 -05:00
parent 3a6332f4de
commit cdc6ba66f1
3 changed files with 42 additions and 30 deletions

View File

@ -81,10 +81,7 @@ func (infra *Infra) Deploy() error {
// also if prune is set to true, this fulfills requirement for all labeled document to be labeled.
// Pruning by annotation is not available, therefore we need to use label.
for _, doc := range docs {
err := doc.Label(document.DeployedByLabel, document.InitinfraIdentifier)
if err != nil {
return err
}
doc.Label(document.DeployedByLabel, document.InitinfraIdentifier)
}
return kctl.Apply(docs, ao)

View File

@ -11,41 +11,46 @@ type Factory struct {
// Document interface
type Document interface {
Label(key string, value string) error
GetLabels() map[string]string
Annotate(key, value string)
AsYAML() ([]byte, error)
MarshalJSON() ([]byte, error)
GetName() string
GetKind() string
GetNamespace() string
GetString(path string) (string, error)
GetStringSlice(path string) ([]string, error)
GetAnnotations() map[string]string
GetBool(path string) (bool, error)
GetFloat64(path string) (float64, error)
GetInt64(path string) (int64, error)
GetSlice(path string) ([]interface{}, error)
GetStringMap(path string) (map[string]string, error)
GetKind() string
GetLabels() map[string]string
GetMap(path string) (map[string]interface{}, error)
GetName() string
GetNamespace() string
GetSlice(path string) ([]interface{}, error)
GetString(path string) (string, error)
GetStringMap(path string) (map[string]string, error)
GetStringSlice(path string) ([]string, error)
Label(key, value string)
MarshalJSON() ([]byte, error)
}
// Factory implements Document
var _ Document = &Factory{}
// Annotate document by applying annotation to it
func (d *Factory) Annotate(key string, value string) {
annotations := d.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}
annotations[key] = value
d.SetAnnotations(annotations)
}
// Label document by applying label on it
func (d *Factory) Label(key string, value string) error {
r := d.GetKustomizeResource()
labels := r.GetLabels()
func (d *Factory) Label(key string, value string) {
labels := d.GetKustomizeResource().GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[key] = value
r.SetLabels(labels)
err := d.SetKustomizeResource(&r)
return err
}
// GetLabels returns applied labels for the document
func (d *Factory) GetLabels() map[string]string {
r := d.GetKustomizeResource()
labels := r.GetLabels()
return labels
d.GetKustomizeResource().SetLabels(labels)
}
// GetNamespace returns the namespace the resource thinks it's in.

View File

@ -102,10 +102,20 @@ func TestDocument(t *testing.T) {
require.NoError(err, "Unexpected error trying to GetAllDocuments")
for _, doc := range docs {
err := doc.Label("test", "testval")
require.NoError(err, "Unexpected error trying to Label")
doc.Label("test", "testval")
labelList := doc.GetLabels()
assert.Contains(labelList, "test", "Could not find expected label")
assert.Contains(labelList, "test")
}
})
t.Run("Annotate", func(t *testing.T) {
docs, err := bundle.GetAllDocuments()
require.NoError(err, "Unexpected error trying to GetAllDocuments")
for _, doc := range docs {
doc.Annotate("test", "testval")
annotationList := doc.GetAnnotations()
assert.Contains(annotationList, "test")
}
})
}