Skip to content

Commit

Permalink
feat: PC-12282 Introduce metadata annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
skrolikiewicz committed Apr 22, 2024
1 parent 4fdb5d4 commit 39dc456
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ define _print_check_step
printf -- '------\n%s...\n' "${1}"
endef

.PHONY: install
.PHONY: install/provider
## Install provider locally.
install: build
install/provider: build
mkdir -p ~/.terraform.d/plugins/$(HOSTNAME)/$(NAMESPACE)/$(NAME)/$(VERSION)/$(OS_ARCH)
mv $(BINARY) ~/.terraform.d/plugins/$(HOSTNAME)/$(NAMESPACE)/$(NAME)/$(VERSION)/$(OS_ARCH)

Expand Down
1 change: 1 addition & 0 deletions docs/resources/alert_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ resource "nobl9_alert_policy" "this" {
### Optional

- `alert_method` (Block List) (see [below for nested schema](#nestedblock--alert_method))
- `annotations` (Map of String) [Metadata annotations](https://docs.nobl9.com/Features/Labels/#metadata-annotations) attached to the resource.
- `cooldown` (String) An interval measured from the last time stamp when all alert policy conditions were satisfied before alert is marked as resolved
- `description` (String) Optional description of the resource. Here, you can add details about who is responsible for the integration (team/owner) or the purpose of creating it.
- `display_name` (String) User-friendly display name of the resource.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ resource "nobl9_project" "this" {

### Optional

- `annotations` (Map of String) [Metadata annotations](https://docs.nobl9.com/Features/Labels/#metadata-annotations) attached to the resource.
- `description` (String) Optional description of the resource. Here, you can add details about who is responsible for the integration (team/owner) or the purpose of creating it.
- `display_name` (String) User-friendly display name of the resource.
- `label` (Block List) [Labels](https://docs.nobl9.com/Features/labels/) containing a single key and a list of values. (see [below for nested schema](#nestedblock--label))
Expand Down
1 change: 1 addition & 0 deletions docs/resources/service.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ resource "nobl9_service" "this" {

### Optional

- `annotations` (Map of String) [Metadata annotations](https://docs.nobl9.com/Features/Labels/#metadata-annotations) attached to the resource.
- `description` (String) Optional description of the resource. Here, you can add details about who is responsible for the integration (team/owner) or the purpose of creating it.
- `display_name` (String) User-friendly display name of the resource.
- `label` (Block List) [Labels](https://docs.nobl9.com/Features/labels/) containing a single key and a list of values. (see [below for nested schema](#nestedblock--label))
Expand Down
1 change: 1 addition & 0 deletions docs/resources/slo.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ resource "nobl9_slo" "this" {
### Optional

- `alert_policies` (List of String) Alert Policies attached to SLO.
- `annotations` (Map of String) [Metadata annotations](https://docs.nobl9.com/Features/Labels/#metadata-annotations) attached to the resource.
- `anomaly_config` (Block Set, Max: 1) Configuration for Anomalies. Currently supported Anomaly Type is NoData (see [below for nested schema](#nestedblock--anomaly_config))
- `attachment` (Block List, Max: 20) (see [below for nested schema](#nestedblock--attachment))
- `attachments` (Block List, Max: 20, Deprecated) (see [below for nested schema](#nestedblock--attachments))
Expand Down
23 changes: 23 additions & 0 deletions nobl9/metadata_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ func schemaLabels() *schema.Schema {
}
}

func schemaAnnotations() *schema.Schema {
return &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Description: "[Metadata annotations](https://docs.nobl9.com/Features/Labels/#metadata-annotations) attached to the resource.",
Elem: &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
}
}

func validateNotEmptyString(variableName string) func(interface{}, string) ([]string, []error) {
return func(valueRaw interface{}, _ string) ([]string, []error) {
if valueRaw.(string) == "" {
Expand Down Expand Up @@ -182,6 +194,17 @@ func getMarshaledLabels(d *schema.ResourceData) (v1alpha.Labels, diag.Diagnostic
return marshalLabels(labels)
}

func getMarshaledAnnotations(d *schema.ResourceData) v1alpha.MetadataAnnotations {
rawAnnotations := d.Get("annotations").(map[string]interface{})
annotations := make(map[string]string, len(rawAnnotations))

for k, v := range rawAnnotations {
annotations[k] = v.(string)
}

return annotations
}

func marshalLabels(labels []interface{}) (v1alpha.Labels, diag.Diagnostics) {
var diags diag.Diagnostics
labelsResult := make(v1alpha.Labels, len(labels))
Expand Down
9 changes: 9 additions & 0 deletions nobl9/resource_alert_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func resourceAlertPolicy() *schema.Resource {
"display_name": schemaDisplayName(),
"project": schemaProject(),
"description": schemaDescription(),
"annotations": schemaAnnotations(),
"severity": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -144,12 +145,15 @@ func marshalAlertPolicy(d *schema.ResourceData) (*v1alphaAlertPolicy.AlertPolicy
return nil, diags
}

annotationsMarshaled := getMarshaledAnnotations(d)

alertPolicy := v1alphaAlertPolicy.New(
v1alphaAlertPolicy.Metadata{
Name: d.Get("name").(string),
DisplayName: displayName,
Project: d.Get("project").(string),
Labels: labelsMarshaled,
Annotations: annotationsMarshaled,
},
v1alphaAlertPolicy.Spec{
Description: d.Get("description").(string),
Expand Down Expand Up @@ -236,6 +240,11 @@ func unmarshalAlertPolicy(d *schema.ResourceData, objects []v1alphaAlertPolicy.A
diags = appendError(diags, err)
}

if len(metadata.Annotations) > 0 {
err = d.Set("annotations", metadata.Annotations)
diags = appendError(diags, err)
}

spec := object.Spec
err = d.Set("description", spec.Description)
diags = appendError(diags, err)
Expand Down
5 changes: 5 additions & 0 deletions nobl9/resource_alert_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ resource "nobl9_alert_policy" "%s" {
project = "%s"
severity = "Medium"
annotations = {
env = "development"
name = "example annotation"
}
condition {
measurement = "burnedBudget"
value = 0.9
Expand Down
9 changes: 9 additions & 0 deletions nobl9/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func resourceProject() *schema.Resource {
"display_name": schemaDisplayName(),
"description": schemaDescription(),
"label": schemaLabels(),
"annotations": schemaAnnotations(),
},
CreateContext: resourceProjectApply,
UpdateContext: resourceProjectApply,
Expand All @@ -36,11 +37,14 @@ func marshalProject(d *schema.ResourceData) (*v1alphaProject.Project, diag.Diagn
return nil, diags
}

annotationsMarshaled := getMarshaledAnnotations(d)

project := v1alphaProject.New(
v1alphaProject.Metadata{
Name: d.Get("name").(string),
DisplayName: d.Get("display_name").(string),
Labels: labelsMarshaled,
Annotations: annotationsMarshaled,
},
v1alphaProject.Spec{
Description: d.Get("description").(string),
Expand Down Expand Up @@ -68,6 +72,11 @@ func unmarshalProject(d *schema.ResourceData, objects []v1alphaProject.Project)
diags = appendError(diags, err)
}

if len(metadata.Annotations) > 0 {
err = d.Set("annotations", metadata.Annotations)
diags = appendError(diags, err)
}

spec := object.Spec
err = d.Set("description", spec.Description)
diags = appendError(diags, err)
Expand Down
5 changes: 5 additions & 0 deletions nobl9/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ resource "nobl9_project" "%s" {
key = "env"
values = ["dev", "staging", "prod"]
}
annotations = {
env = "development"
name = "example annotation"
}
}
`, name, name, name)
}
Expand Down
9 changes: 9 additions & 0 deletions nobl9/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func resourceService() *schema.Resource {
"project": schemaProject(),
"description": schemaDescription(),
"label": schemaLabels(),
"annotations": schemaAnnotations(),
"status": {
Type: schema.TypeMap,
Computed: true,
Expand Down Expand Up @@ -50,12 +51,15 @@ func marshalService(d *schema.ResourceData) (*v1alphaService.Service, diag.Diagn
return nil, diags
}

annotationsMarshaled := getMarshaledAnnotations(d)

service := v1alphaService.New(
v1alphaService.Metadata{
Name: d.Get("name").(string),
DisplayName: displayName,
Project: d.Get("project").(string),
Labels: labelsMarshaled,
Annotations: annotationsMarshaled,
},
v1alphaService.Spec{
Description: d.Get("description").(string),
Expand Down Expand Up @@ -84,6 +88,11 @@ func unmarshalService(d *schema.ResourceData, objects []v1alphaService.Service)
diags = appendError(diags, err)
}

if len(metadata.Annotations) > 0 {
err = d.Set("annotations", metadata.Annotations)
diags = appendError(diags, err)
}

status := map[string]int{"sloCount": object.Status.SloCount}
err = d.Set("status", status)
diags = appendError(diags, err)
Expand Down
5 changes: 5 additions & 0 deletions nobl9/resource_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ resource "nobl9_service" "%s" {
key = "dev"
values = ["dev", "staging", "prod"]
}
annotations = {
env = "development"
name = "example annotation"
}
}
`, name, name, name, testProject)
}
8 changes: 8 additions & 0 deletions nobl9/resource_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ func schemaSLO() map[string]*schema.Schema {
"project": schemaProject(),
"description": schemaDescription(),
"label": schemaLabels(),
"annotations": schemaAnnotations(),
"composite": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -573,13 +574,15 @@ func marshalSLO(d *schema.ResourceData) (*v1alphaSLO.SLO, diag.Diagnostics) {
if diags.HasError() {
return nil, diags
}
annotationsMarshaled := getMarshaledAnnotations(d)

slo := v1alphaSLO.New(
v1alphaSLO.Metadata{
Name: d.Get("name").(string),
DisplayName: displayName,
Project: d.Get("project").(string),
Labels: labelsMarshaled,
Annotations: annotationsMarshaled,
},
v1alphaSLO.Spec{
Description: d.Get("description").(string),
Expand Down Expand Up @@ -858,6 +861,11 @@ func unmarshalSLO(d *schema.ResourceData, objects []v1alphaSLO.SLO) diag.Diagnos
diags = appendError(diags, err)
}

if len(metadata.Annotations) > 0 {
err = d.Set("annotations", metadata.Annotations)
diags = appendError(diags, err)
}

spec := object.Spec

err = d.Set("alert_policies", spec.AlertPolicies)
Expand Down
57 changes: 57 additions & 0 deletions nobl9/resource_slo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestAcc_Nobl9SLO(t *testing.T) {
{"test-anomaly-config-different-project", testAnomalyConfigNoDataDifferentProject},
{"test-max-one-primary-objective", testMaxOnePrimaryObjective},
{"test-no-primary-objective", testNoPrimaryObjective},
{"test-metadata-annotations", testMetadataAnnotations},
}

for _, tc := range cases {
Expand Down Expand Up @@ -3387,3 +3388,59 @@ resource "nobl9_slo" "%s" {
}
`, name, name, serviceName, testProject, agentName, testProject)
}

func testMetadataAnnotations(name string) string {
var serviceName = name + "-tf-service"
var agentName = name + "-tf-agent"

config := testService(serviceName) +
testThousandEyesAgent(agentName) + `
resource "nobl9_slo" ":name" {
name = ":name"
display_name = ":name"
project = ":project"
service = nobl9_service.:serviceName.name
annotations = {
env = "development"
name = "example annotation"
}
budgeting_method = "Occurrences"
objective {
display_name = "obj1"
name = "tf-objective-1"
target = 0.7
value = 1
op = "lt"
raw_metric {
query {
thousandeyes {
test_id = 11
}
}
}
}
time_window {
count = 10
is_rolling = true
unit = "Minute"
}
indicator {
name = nobl9_agent.:agentName.name
project = ":project"
kind = "Agent"
}
}
`
config = strings.ReplaceAll(config, ":name", name)
config = strings.ReplaceAll(config, ":serviceName", serviceName)
config = strings.ReplaceAll(config, ":agentName", agentName)
config = strings.ReplaceAll(config, ":project", testProject)

return config
}

0 comments on commit 39dc456

Please sign in to comment.