Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Bugfix/43 order of event types #46

Merged
merged 10 commits into from
Mar 2, 2020
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,6 @@ resource "instana_custom_event_spec_system_rule" "example" {

rule_severity = "warning"
rule_system_rule_id = "system-rule-id"

downstream_integration_ids = [ "integration-id-1", "integration-id-2" ] #Optional
downstream_broadcast_to_all_alerting_configs = true #Optional, default = true
}
```

Expand All @@ -173,9 +170,6 @@ resource "instana_custom_event_spec_entity_verification_rule" "example" {
rule_matching_operator = "is"
rule_matching_entity_label = "entity-label"
rule_offline_duration = 60000

downstream_integration_ids = [ "integration-id-1", "integration-id-2" ] #Optional
downstream_broadcast_to_all_alerting_configs = true #Optional, default = true
}
```

Expand Down Expand Up @@ -206,9 +200,6 @@ resource "instana_custom_event_spec_threshold_rule" "example" {
rule_aggregation = "sum" #Optional depending on metric type
rule_condition_operator = "=="
rule_condition_value = 1.2

downstream_integration_ids = [ "integration-id-1", "integration-id-2" ] #Optional
downstream_broadcast_to_all_alerting_configs = true #Optional, default = true
}
```

Expand Down
51 changes: 41 additions & 10 deletions instana/resource-alerting-channel-email.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,34 @@ const (
ResourceInstanaAlertingChannelEmail = "instana_alerting_channel_email"
)

//AlertingChannelEmailEmailsSchemaField schema definition for instana alerting channel emails field
var AlertingChannelEmailEmailsSchemaField = &schema.Schema{
Type: schema.TypeSet,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Required: true,
Description: "The list of emails of the Email alerting channel",
}

//NewAlertingChannelEmailResourceHandle creates the resource handle for Alerting Channels of type Email
func NewAlertingChannelEmailResourceHandle() *ResourceHandle {
return &ResourceHandle{
ResourceName: ResourceInstanaAlertingChannelEmail,
Schema: map[string]*schema.Schema{
AlertingChannelFieldName: alertingChannelNameSchemaField,
AlertingChannelFieldFullName: alertingChannelFullNameSchemaField,
AlertingChannelEmailFieldEmails: &schema.Schema{
Type: schema.TypeList,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
AlertingChannelFieldName: alertingChannelNameSchemaField,
AlertingChannelFieldFullName: alertingChannelFullNameSchemaField,
AlertingChannelEmailFieldEmails: AlertingChannelEmailEmailsSchemaField,
},
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: alertingChannelEmailSchemaV0().CoreConfigSchema().ImpliedType(),
Upgrade: func(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
return rawState, nil
},
Required: true,
Description: "The list of emails of the Email alerting channel",
Version: 0,
},
},
RestResourceFactory: func(api restapi.InstanaAPI) restapi.RestResource { return api.AlertingChannels() },
Expand All @@ -51,6 +64,24 @@ func mapStateToDataObjectForAlertingChannelEmail(d *schema.ResourceData, formatt
ID: d.Id(),
Name: name,
Kind: restapi.EmailChannelType,
Emails: ReadStringArrayParameterFromResource(d, AlertingChannelEmailFieldEmails),
Emails: ReadStringSetParameterFromResource(d, AlertingChannelEmailFieldEmails),
}, nil
}

func alertingChannelEmailSchemaV0() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
AlertingChannelFieldName: alertingChannelNameSchemaField,
AlertingChannelFieldFullName: alertingChannelFullNameSchemaField,
AlertingChannelEmailFieldEmails: &schema.Schema{
Type: schema.TypeList,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Required: true,
Description: "The list of emails of the Email alerting channel",
},
},
}
}
72 changes: 51 additions & 21 deletions instana/resource-alerting-channel-email_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package instana_test

import (
"fmt"
"net/http"
"strconv"
"strings"
"testing"

"github.com/gorilla/mux"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"

. "github.com/gessnerfl/terraform-provider-instana/instana"
Expand Down Expand Up @@ -67,6 +69,9 @@ func TestCRUDOfAlertingChannelEmailResourceWithMockServer(t *testing.T) {
resourceDefinitionWithoutName0 := strings.ReplaceAll(resourceDefinitionWithoutName, iteratorPlaceholder, "0")
resourceDefinitionWithoutName1 := strings.ReplaceAll(resourceDefinitionWithoutName, iteratorPlaceholder, "1")

hashFunctionEmails := schema.HashSchema(AlertingChannelEmailEmailsSchemaField.Elem.(*schema.Schema))
emailAddress1 := "EMAIL1"
emailAddress2 := "EMAIL2"
resource.UnitTest(t, resource.TestCase{
Providers: testAlertingChannelEmailProviders,
Steps: []resource.TestStep{
Expand All @@ -76,8 +81,8 @@ func TestCRUDOfAlertingChannelEmailResourceWithMockServer(t *testing.T) {
resource.TestCheckResourceAttrSet(testAlertingChannelEmailDefinition, "id"),
resource.TestCheckResourceAttr(testAlertingChannelEmailDefinition, AlertingChannelFieldName, "name 0"),
resource.TestCheckResourceAttr(testAlertingChannelEmailDefinition, AlertingChannelFieldFullName, "prefix name 0 suffix"),
resource.TestCheckResourceAttr(testAlertingChannelEmailDefinition, AlertingChannelEmailFieldEmails+".0", "EMAIL1"),
resource.TestCheckResourceAttr(testAlertingChannelEmailDefinition, AlertingChannelEmailFieldEmails+".1", "EMAIL2"),
resource.TestCheckResourceAttr(testAlertingChannelEmailDefinition, fmt.Sprintf("%s.%d", AlertingChannelEmailFieldEmails, hashFunctionEmails(emailAddress1)), emailAddress1),
resource.TestCheckResourceAttr(testAlertingChannelEmailDefinition, fmt.Sprintf("%s.%d", AlertingChannelEmailFieldEmails, hashFunctionEmails(emailAddress2)), emailAddress2),
),
},
resource.TestStep{
Expand All @@ -86,8 +91,8 @@ func TestCRUDOfAlertingChannelEmailResourceWithMockServer(t *testing.T) {
resource.TestCheckResourceAttrSet(testAlertingChannelEmailDefinition, "id"),
resource.TestCheckResourceAttr(testAlertingChannelEmailDefinition, AlertingChannelFieldName, "name 1"),
resource.TestCheckResourceAttr(testAlertingChannelEmailDefinition, AlertingChannelFieldFullName, "prefix name 1 suffix"),
resource.TestCheckResourceAttr(testAlertingChannelEmailDefinition, AlertingChannelEmailFieldEmails+".0", "EMAIL1"),
resource.TestCheckResourceAttr(testAlertingChannelEmailDefinition, AlertingChannelEmailFieldEmails+".1", "EMAIL2"),
resource.TestCheckResourceAttr(testAlertingChannelEmailDefinition, fmt.Sprintf("%s.%d", AlertingChannelEmailFieldEmails, hashFunctionEmails(emailAddress1)), emailAddress1),
resource.TestCheckResourceAttr(testAlertingChannelEmailDefinition, fmt.Sprintf("%s.%d", AlertingChannelEmailFieldEmails, hashFunctionEmails(emailAddress2)), emailAddress2),
),
},
},
Expand All @@ -102,7 +107,40 @@ func TestResourceAlertingChannelEmailDefinition(t *testing.T) {
schemaAssert := testutils.NewTerraformSchemaAssert(schemaMap, t)
schemaAssert.AssertSchemaIsRequiredAndOfTypeString(AlertingChannelFieldName)
schemaAssert.AssertSchemaIsComputedAndOfTypeString(AlertingChannelFieldFullName)
schemaAssert.AssertSchemaIsRequiredAndOfTypeListOfStrings(AlertingChannelEmailFieldEmails)
schemaAssert.AssertSchemaIsRequiredAndOfTypeSetOfStrings(AlertingChannelEmailFieldEmails)
}

func TestShouldReturnCorrectResourceNameForAlertingChannelEmail(t *testing.T) {
name := NewAlertingChannelEmailResourceHandle().ResourceName

assert.Equal(t, "instana_alerting_channel_email", name, "Expected resource name to be instana_alerting_channel_email")
}

func TestAlertingChannelEmailResourceShouldHaveSchemaVersionOne(t *testing.T) {
assert.Equal(t, 1, NewAlertingChannelEmailResourceHandle().SchemaVersion)
}

func TestAlertingChannelEmailShouldHaveOneStateUpgraderForVersionZero(t *testing.T) {
resourceHandler := NewAlertingChannelEmailResourceHandle()

assert.Equal(t, 1, len(resourceHandler.StateUpgraders))
assert.Equal(t, 0, resourceHandler.StateUpgraders[0].Version)
}

func TestShouldReturnStateOfAlertingChannelEmailUnchangedWhenMigratingFromVersion0ToVersion1(t *testing.T) {
emails := []interface{}{"email1", "email2"}
name := "name"
fullname := "fullname"
rawData := make(map[string]interface{})
rawData[AlertingChannelFieldName] = name
rawData[AlertingChannelFieldFullName] = fullname
rawData[AlertingChannelEmailFieldEmails] = emails
meta := "dummy"

result, err := NewAlertingChannelEmailResourceHandle().StateUpgraders[0].Upgrade(rawData, meta)

assert.Nil(t, err)
assert.Equal(t, rawData, result)
}

func TestShouldUpdateResourceStateForAlertingChannelEmail(t *testing.T) {
Expand All @@ -120,7 +158,11 @@ func TestShouldUpdateResourceStateForAlertingChannelEmail(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "id", resourceData.Id(), "id should be equal")
assert.Equal(t, "name", resourceData.Get(AlertingChannelFieldFullName), "name should be equal to full name")
assert.Equal(t, []interface{}{"email1", "email2"}, resourceData.Get(AlertingChannelEmailFieldEmails), "list of emails should be equal")

emails := resourceData.Get(AlertingChannelEmailFieldEmails).(*schema.Set)
assert.Equal(t, 2, emails.Len())
assert.Contains(t, emails.List(), "email1")
assert.Contains(t, emails.List(), "email2")
}

func TestShouldConvertStateOfAlertingChannelEmailToDataModel(t *testing.T) {
Expand All @@ -139,19 +181,7 @@ func TestShouldConvertStateOfAlertingChannelEmailToDataModel(t *testing.T) {
assert.IsType(t, restapi.AlertingChannel{}, model, "Model should be an alerting channel")
assert.Equal(t, "id", model.GetID())
assert.Equal(t, "prefix name suffix", model.(restapi.AlertingChannel).Name, "name should be equal to full name")
assert.Equal(t, emails, model.(restapi.AlertingChannel).Emails, "list of emails should be equal")
}

func TestAlertingChannelEmailShouldHaveSchemaVersionZero(t *testing.T) {
assert.Equal(t, 0, NewAlertingChannelEmailResourceHandle().SchemaVersion)
}

func TestAlertingChannelEmailShouldHaveNoStateUpgrader(t *testing.T) {
assert.Equal(t, 0, len(NewAlertingChannelEmailResourceHandle().StateUpgraders))
}

func TestShouldReturnCorrectResourceNameForAlertingChannelEmail(t *testing.T) {
name := NewAlertingChannelEmailResourceHandle().ResourceName

assert.Equal(t, "instana_alerting_channel_email", name, "Expected resource name to be instana_alerting_channel_email")
assert.Len(t, model.(restapi.AlertingChannel).Emails, 2)
assert.Contains(t, model.(restapi.AlertingChannel).Emails, "email1")
assert.Contains(t, model.(restapi.AlertingChannel).Emails, "email2")
}
71 changes: 53 additions & 18 deletions instana/resource-alerting-channel-webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,45 @@ const (
ResourceInstanaAlertingChannelWebhook = "instana_alerting_channel_webhook"
)

//AlertingChannelWebhookWebhookURLsSchemaField schema field definition of instana_alerting_channel_webhook field webhook_urls
var AlertingChannelWebhookWebhookURLsSchemaField = &schema.Schema{
Type: schema.TypeSet,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Required: true,
Description: "The list of webhook urls of the Webhook alerting channel",
}

//AlertingChannelWebhookHTTPHeadersSchemaField schema field definition of instana_alerting_channel_webhook field http_headers
var AlertingChannelWebhookHTTPHeadersSchemaField = &schema.Schema{
Type: schema.TypeMap,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "The optional map of HTTP headers of the Webhook alerting channel",
}

//NewAlertingChannelWebhookResourceHandle creates the resource handle for Alerting Channels of type Webhook
func NewAlertingChannelWebhookResourceHandle() *ResourceHandle {
return &ResourceHandle{
ResourceName: ResourceInstanaAlertingChannelWebhook,
Schema: map[string]*schema.Schema{
AlertingChannelFieldName: alertingChannelNameSchemaField,
AlertingChannelFieldFullName: alertingChannelFullNameSchemaField,
AlertingChannelWebhookFieldWebhookURLs: &schema.Schema{
Type: schema.TypeList,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Required: true,
Description: "The list of webhook urls of the Webhook alerting channel",
},
AlertingChannelWebhookFieldHTTPHeaders: &schema.Schema{
Type: schema.TypeMap,
Elem: &schema.Schema{
Type: schema.TypeString,
AlertingChannelFieldName: alertingChannelNameSchemaField,
AlertingChannelFieldFullName: alertingChannelFullNameSchemaField,
AlertingChannelWebhookFieldWebhookURLs: AlertingChannelWebhookWebhookURLsSchemaField,
AlertingChannelWebhookFieldHTTPHeaders: AlertingChannelWebhookHTTPHeadersSchemaField,
},
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: alertingChannelWebhookSchemaV0().CoreConfigSchema().ImpliedType(),
Upgrade: func(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
return rawState, nil
},
Optional: true,
Description: "The optional map of HTTP headers of the Webhook alerting channel",
Version: 0,
},
},
RestResourceFactory: func(api restapi.InstanaAPI) restapi.RestResource { return api.AlertingChannels() },
Expand Down Expand Up @@ -80,7 +96,7 @@ func mapStateToDataObjectForAlertingChannelWebhook(d *schema.ResourceData, forma
ID: d.Id(),
Name: name,
Kind: restapi.WebhookChannelType,
WebhookURLs: ReadStringArrayParameterFromResource(d, AlertingChannelWebhookFieldWebhookURLs),
WebhookURLs: ReadStringSetParameterFromResource(d, AlertingChannelWebhookFieldWebhookURLs),
Headers: headers,
}, nil
}
Expand All @@ -100,3 +116,22 @@ func createHTTPHeaderListFromMap(d *schema.ResourceData) []string {
}
return []string{}
}

func alertingChannelWebhookSchemaV0() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
AlertingChannelFieldName: alertingChannelNameSchemaField,
AlertingChannelFieldFullName: alertingChannelFullNameSchemaField,
AlertingChannelWebhookFieldWebhookURLs: &schema.Schema{
Type: schema.TypeList,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Required: true,
Description: "The list of webhook urls of the Webhook alerting channel",
},
AlertingChannelWebhookFieldHTTPHeaders: AlertingChannelWebhookHTTPHeadersSchemaField,
},
}
}
Loading