Skip to content

Commit

Permalink
PC-7097 Allow multiple attachments in SLO (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Piwowarczyk authored Dec 1, 2022
1 parent bfa95be commit 42da027
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 13 deletions.
20 changes: 19 additions & 1 deletion docs/resources/agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,25 @@ description: |-

[Agent configuration documentation](https://docs.nobl9.com/nobl9_agent)


## Example Usage

```terraform
resource "nobl9_project" "this" {
display_name = "Test Terraform"
name = "test-terraform"
description = "An example terraform project"
}
resource "nobl9_agent" "this" {
name = "${nobl9_project.this.name}-prom-agent"
project = nobl9_project.this.name
source_of = ["Metrics", "Services"]
agent_type = "prometheus"
prometheus_config {
url = "http://web.net"
}
}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand Down
29 changes: 26 additions & 3 deletions docs/resources/slo.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ resource "nobl9_slo" "this" {
values = ["red"]
}
attachment {
utl = "https://www.nobl9.com/"
display_name = "SLO provider"
}
attachment {
utl = "https://duckduckgo.com/"
display_name = "Nice search engine"
}
alert_policies = [
"foo-front-page-latency"
]
Expand Down Expand Up @@ -91,7 +101,8 @@ resource "nobl9_slo" "this" {
### Optional

- `alert_policies` (List of String) Alert Policies attached to SLO
- `attachments` (Block List, Max: 1) (see [below for nested schema](#nestedblock--attachments))
- `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))
- `composite` (Block Set, Max: 1) [Composite SLO documentation](https://docs.nobl9.com/yaml-guide/#slo) (see [below for nested schema](#nestedblock--composite))
- `description` (String) Optional description of the resource.
- `display_name` (String) Display name of the resource.
Expand Down Expand Up @@ -1068,16 +1079,28 @@ Required:



<a id="nestedblock--attachment"></a>
### Nested Schema for `attachment`

Required:

- `url` (String) URL to the attachment

Optional:

- `display_name` (String) Name displayed for the attachment. Max. length: 63 characters.


<a id="nestedblock--attachments"></a>
### Nested Schema for `attachments`

Required:

- `url` (String) Url to the attachment
- `url` (String) URL to the attachment

Optional:

- `display_name` (String) Name which is displayed for the attachment
- `display_name` (String) Name displayed for the attachment. Max. length: 63 characters.


<a id="nestedblock--composite"></a>
Expand Down
10 changes: 10 additions & 0 deletions examples/resources/nobl9_slo/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ resource "nobl9_slo" "this" {
values = ["red"]
}

attachment {
utl = "https://www.nobl9.com/"
display_name = "SLO provider"
}

attachment {
utl = "https://duckduckgo.com/"
display_name = "Nice search engine"
}

alert_policies = [
"foo-front-page-latency"
]
Expand Down
67 changes: 60 additions & 7 deletions nobl9/resource_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,21 +269,45 @@ func schemaSLO() map[string]*schema.Schema {
DiffSuppressFunc: diffSuppressListStringOrder("alert_policies"),
},
"attachments": {
Type: schema.TypeList,
Optional: true,
Description: "",
MaxItems: 20,
Deprecated: "\"attachments\" argument is deprecated use \"attachment\" instead",
ConflictsWith: []string{"attachment"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"display_name": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validateMaxLength("display_name", 63),
Description: "Name displayed for the attachment. Max. length: 63 characters.",
},
"url": {
Type: schema.TypeString,
Required: true,
Description: "URL to the attachment",
},
},
},
},
"attachment": {
Type: schema.TypeList,
Optional: true,
Description: "",
MaxItems: 1,
MaxItems: 20,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"display_name": {
Type: schema.TypeString,
Optional: true,
Description: "Name which is displayed for the attachment",
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validateMaxLength("display_name", 63),
Description: "Name displayed for the attachment. Max. length: 63 characters.",
},
"url": {
Type: schema.TypeString,
Required: true,
Description: "Url to the attachment",
Description: "URL to the attachment",
},
},
},
Expand Down Expand Up @@ -411,6 +435,10 @@ func marshalSLO(d *schema.ResourceData) (*n9api.SLO, diag.Diagnostics) {
if diags.HasError() {
return nil, diags
}
attachments, ok := d.GetOk("attachment")
if !ok {
attachments = d.Get("attachments")
}

return &n9api.SLO{
ObjectHeader: n9api.ObjectHeader{
Expand All @@ -427,7 +455,7 @@ func marshalSLO(d *schema.ResourceData) (*n9api.SLO, diag.Diagnostics) {
Thresholds: marshalThresholds(d),
TimeWindows: marshalTimeWindows(d),
AlertPolicies: toStringSlice(d.Get("alert_policies").([]interface{})),
Attachments: marshalAttachments(d.Get("attachments").([]interface{})),
Attachments: marshalAttachments(attachments.([]interface{})),
},
}, diags
}
Expand Down Expand Up @@ -670,6 +698,8 @@ func unmarshalAttachments(d *schema.ResourceData, spec map[string]interface{}) e
return nil
}

declaredAttachmentTag := getDeclaredAttachmentTag(d)

attachments := spec["attachments"].([]interface{})
res := make([]interface{}, len(attachments))
for i, v := range attachments {
Expand All @@ -680,8 +710,16 @@ func unmarshalAttachments(d *schema.ResourceData, spec map[string]interface{}) e
}
res[i] = attachment
}
return d.Set(declaredAttachmentTag, res)
}

return d.Set("attachments", res)
// getDeclaredAttachmentTag return name of attachments object declared in .tf file
func getDeclaredAttachmentTag(d *schema.ResourceData) string {
_, newAttachments := d.GetChange("attachments")
if len(newAttachments.([]interface{})) > 0 {
return "attachments"
}
return "attachment"
}

func unmarshalIndicator(d *schema.ResourceData, spec map[string]interface{}) error {
Expand Down Expand Up @@ -2274,3 +2312,18 @@ func unmarshalThousandeyesMetric(metric map[string]interface{}) map[string]inter

return res
}

func validateMaxLength(fieldName string, maxLength int) func(interface{}, cty.Path) diag.Diagnostics {
return func(v any, _ cty.Path) diag.Diagnostics {
var diags diag.Diagnostics
if len(v.(string)) > 63 {
diagnostic := diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("%s is too long", fieldName),
Detail: fmt.Sprintf("%s cannot be longer than %d characters", fieldName, maxLength),
}
diags = append(diags, diagnostic)
}
return diags
}
}
Loading

0 comments on commit 42da027

Please sign in to comment.