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

Commit

Permalink
#27: Add resource for custom events with entity verification rule
Browse files Browse the repository at this point in the history
  • Loading branch information
gessnerfl committed Dec 17, 2019
1 parent 4b8e9e5 commit 0a3f72f
Show file tree
Hide file tree
Showing 9 changed files with 653 additions and 30 deletions.
12 changes: 8 additions & 4 deletions instana/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const ResourceInstanaCustomEventSpecificationSystemRule = "instana_custom_event_
//ResourceInstanaCustomEventSpecificationThresholdRule the name of the terraform-provider-instana resource to manage custom event specifications with threshold rule
const ResourceInstanaCustomEventSpecificationThresholdRule = "instana_custom_event_spec_threshold_rule"

//ResourceInstanaCustomEventSpecificationEntityVerificationRule the name of the terraform-provider-instana resource to manage custom event specifications with entity verification rule
const ResourceInstanaCustomEventSpecificationEntityVerificationRule = "instana_custom_event_spec_entity_verification_rule"

//ProviderMeta data structure for the meta data which is configured and provided to the resources by this provider
type ProviderMeta struct {
InstanaAPI restapi.InstanaAPI
Expand Down Expand Up @@ -80,10 +83,11 @@ func providerSchema() map[string]*schema.Schema {

func providerResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
ResourceInstanaUserRole: CreateResourceUserRole(),
ResourceInstanaApplicationConfig: CreateResourceApplicationConfig(),
ResourceInstanaCustomEventSpecificationSystemRule: CreateResourceCustomEventSpecificationWithSystemRule(),
ResourceInstanaCustomEventSpecificationThresholdRule: CreateResourceCustomEventSpecificationWithThresholdRule(),
ResourceInstanaUserRole: CreateResourceUserRole(),
ResourceInstanaApplicationConfig: CreateResourceApplicationConfig(),
ResourceInstanaCustomEventSpecificationSystemRule: CreateResourceCustomEventSpecificationWithSystemRule(),
ResourceInstanaCustomEventSpecificationThresholdRule: CreateResourceCustomEventSpecificationWithThresholdRule(),
ResourceInstanaCustomEventSpecificationEntityVerificationRule: CreateResourceCustomEventSpecificationWithEntityVerificationRule(),
}
}

Expand Down
7 changes: 5 additions & 2 deletions instana/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func validateSchema(schemaMap map[string]*schema.Schema, t *testing.T) {
}

func validateResourcesMap(resourceMap map[string]*schema.Resource, t *testing.T) {
if len(resourceMap) != 4 {
t.Fatal("Expected 4 resources to be configured")
if len(resourceMap) != 5 {
t.Fatal("Expected 5 resources to be configured")
}

if resourceMap[ResourceInstanaUserRole] == nil {
Expand All @@ -60,6 +60,9 @@ func validateResourcesMap(resourceMap map[string]*schema.Resource, t *testing.T)
if resourceMap[ResourceInstanaCustomEventSpecificationThresholdRule] == nil {
t.Fatal("Expected a resources to be configured for instana custom event specification threshold rule")
}
if resourceMap[ResourceInstanaCustomEventSpecificationEntityVerificationRule] == nil {
t.Fatal("Expected a resources to be configured for instana custom event specification entity verification rule")
}
}

func validateConfigureFunc(schemaMap map[string]*schema.Schema, configureFunc func(*schema.ResourceData) (interface{}, error), t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package instana

import (
"github.com/gessnerfl/terraform-provider-instana/instana/restapi"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/hashicorp/terraform/terraform"
)

const (
//EntityVerificationRuleFieldMatchingEntityLabel constant value for the schema field matching_entity_label
EntityVerificationRuleFieldMatchingEntityLabel = ruleFieldPrefix + "matching_entity_label"
//EntityVerificationRuleFieldMatchingEntityType constant value for the schema field matching_entity_type
EntityVerificationRuleFieldMatchingEntityType = ruleFieldPrefix + "matching_entity_type"
//EntityVerificationRuleFieldMatchingOperator constant value for the schema field matching_operator
EntityVerificationRuleFieldMatchingOperator = ruleFieldPrefix + "matching_operator"
)

var entityVerificationRuleSchemaFields = map[string]*schema.Schema{
EntityVerificationRuleFieldMatchingEntityLabel: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The label of the matching entity",
},
EntityVerificationRuleFieldMatchingEntityType: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The type of the matching entity",
},
EntityVerificationRuleFieldMatchingOperator: &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(restapi.SupportedMatchingOperatorTypes.ToStringSlice(), false),
Description: "The operator which should be applied for matching the label for the given entity (e.g. IS, CONTAINS, STARTS_WITH, ENDS_WITH, NONE)",
},
}

//CreateResourceCustomEventSpecificationWithEntityVerificationRule creates the resource definition for the instana api endpoint for Custom Event Specifications for Threshold rules
func CreateResourceCustomEventSpecificationWithEntityVerificationRule() *schema.Resource {
return &schema.Resource{
Read: createReadCustomEventSpecificationWithEntityVerificationRule(),
Create: createCreateCustomEventSpecificationWithEntityVerificationRule(),
Update: createUpdateCustomEventSpecificationWithEntityVerificationRule(),
Delete: createDeleteCustomEventSpecificationWithEntityVerificationRule(),

Schema: createCustomEventSpecificationSchema(entityVerificationRuleSchemaFields),
SchemaVersion: 1,
MigrateState: CreateMigrateCustomEventConfigStateFunction(make(map[int](func(inst *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error)))),
}
}

func createReadCustomEventSpecificationWithEntityVerificationRule() func(*schema.ResourceData, interface{}) error {
return createCustomEventSpecificationReadFunc(mapEntityVerificationRuleToTerraformState)
}

func createCreateCustomEventSpecificationWithEntityVerificationRule() func(*schema.ResourceData, interface{}) error {
return createCustomEventSpecificationCreateFunc(mapEntityVerificationRuleToInstanaAPIModel, mapEntityVerificationRuleToTerraformState)
}

func createUpdateCustomEventSpecificationWithEntityVerificationRule() func(*schema.ResourceData, interface{}) error {
return createCustomEventSpecificationUpdateFunc(mapEntityVerificationRuleToInstanaAPIModel, mapEntityVerificationRuleToTerraformState)
}

func createDeleteCustomEventSpecificationWithEntityVerificationRule() func(*schema.ResourceData, interface{}) error {
return createCustomEventSpecificationDeleteFunc(mapEntityVerificationRuleToInstanaAPIModel)
}

func mapEntityVerificationRuleToInstanaAPIModel(d *schema.ResourceData) (restapi.RuleSpecification, error) {
severity, err := ConvertSeverityFromTerraformToInstanaAPIRepresentation(d.Get(CustomEventSpecificationRuleSeverity).(string))
if err != nil {
return restapi.RuleSpecification{}, err
}
entityLabel := d.Get(EntityVerificationRuleFieldMatchingEntityLabel).(string)
entityType := d.Get(EntityVerificationRuleFieldMatchingEntityType).(string)
operator := restapi.MatchingOperatorType(d.Get(EntityVerificationRuleFieldMatchingOperator).(string))

return restapi.NewEntityVerificationRuleSpecification(entityLabel, entityType, operator, severity), nil
}

func mapEntityVerificationRuleToTerraformState(d *schema.ResourceData, spec restapi.CustomEventSpecification) error {
ruleSpec := spec.Rules[0]
severity, err := ConvertSeverityFromInstanaAPIToTerraformRepresentation(ruleSpec.Severity)
if err != nil {
return err
}

d.Set(CustomEventSpecificationRuleSeverity, severity)
d.Set(EntityVerificationRuleFieldMatchingEntityLabel, ruleSpec.MatchingEntityLabel)
d.Set(EntityVerificationRuleFieldMatchingEntityType, ruleSpec.MatchingEntityType)
d.Set(EntityVerificationRuleFieldMatchingOperator, ruleSpec.MatchingOperator)
return nil
}
Loading

0 comments on commit 0a3f72f

Please sign in to comment.