terraform/builtin/providers/circonus/resource_circonus_contact.go

1279 lines
38 KiB
Go
Raw Normal View History

Circonus Provider (#12338) * Begin stubbing out the Circonus provider. * Remove all references to `reverse:secret_key`. This value is dynamically set by the service and unused by Terraform. * Update the `circonus_check` resource. Still a WIP. * Add docs for the `circonus_check` resource. Commit miss, this should have been included in the last commit. * "Fix" serializing check tags I still need to figure out how I can make them order agnostic w/o using a TypeSet. I'm worried that's what I'm going to have to do. * Spike a quick circonus_broker data source. * Convert tags to a Set so the order does not matter. * Add a `circonus_account` data source. * Correctly spell account. Pointed out by: @postwait * Add the `circonus_contact_group` resource. * Push descriptions into their own file in order to reduce the busyness of the schema when reviewing code. * Rename `circonus_broker` and `broker` to `circonus_collector` and `collector`, respectively. Change made with concent by Circonus to reduce confusion (@postwait, @maier, and several others). * Use upstream contsants where available. * Import the latest circonus-gometrics. * Move to using a Set of collectors vs a list attached to a single attribute. * Rename "cid" to "id" in the circonus_account data source and elsewhere where possible. * Inject a tag automatically. Update gometrics. * Checkpoint `circonus_metric` resource. * Enable provider-level auto-tagging. This is disabled by default. * Rearrange metric. This is an experimental "style" of a provider. We'll see. That moment. When you think you've gone off the rails on a mad scientist experiment but like the outcome and think you may be onto something but haven't proven it to yourself or anyone else yet? That. That exact feeling of semi-confidence while being alone in the wilderness. Please let this not be the Terraform provider equivalent of DJB's C style of coding. We'll know in another resource or two if this was a horrible mistake or not. * Begin moving `resource_circonus_check` over to the new world order/structure: Much of this is WIP and incomplete, but here is the new supported structure: ``` variable "used_metric_name" { default = "_usage`0`_used" } resource "circonus_check" "usage" { # collectors = ["${var.collectors}"] collector { id = "${var.collectors[0]}" } name = "${var.check_name}" notes = "${var.notes}" json { url = "https://${var.target}/account/current" http_headers = { "Accept" = "application/json" "X-Circonus-App-Name" = "TerraformCheck" "X-Circonus-Auth-Token" = "${var.api_token}" } } stream { name = "${circonus_metric.used.name}" tags = "${circonus_metric.used.tags}" type = "${circonus_metric.used.type}" } tags = { source = "circonus" } } resource "circonus_metric" "used" { name = "${var.used_metric_name}" tags = { source = "circonus" } type = "numeric" } ``` * Document the `circonus_metric` resource. * Updated `circonus_check` docs. * If a port was present, automatically set it in the Config. * Alpha sort the check parameters now that they've been renamed. * Fix a handful of panics as a result of the schema changing. * Move back to a `TypeSet` for tags. After a stint with `TypeMap`, move back to `TypeSet`. A set of strings seems to match the API the best. The `map` type was convenient because it reduced the amount of boilerplate, but you loose out on other things. For instance, tags come in the form of `category:value`, so naturally it seems like you could use a map, but you can't without severe loss of functionality because assigning two values to the same category is common. And you can't normalize map input or suppress the output correctly (this was eventually what broke the camel's back). I tried an experiment of normalizing the input to be `category:value` as the key in the map and a value of `""`, but... seee diff suppress. In this case, simple is good. While here bring some cleanups to _Metric since that was my initial testing target. * Rename `providerConfig` to `_ProviderConfig` * Checkpoint the `json` check type. * Fix a few residual issues re: missing descriptions. * Rename `validateRegexp` to `_ValidateRegexp` * Use tags as real sets, not just a slice of strings. * Move the DiffSuppressFunc for tags down to the Elem. * Fix up unit tests to chase the updated, default hasher function being used. * Remove `Computed` attribute from `TypeSet` objects. This fixes a pile of issues re: update that I was having. * Rename functions. `GetStringOk` -> `GetStringOK` `GetSetAsListOk` -> `GetSetAsListOK` `GetIntOk` -> `GetIntOK` * Various small cleanups and comments rolled into a single commit. * Add a `postgresql` check type for the `circonus_check` resource. * Rename various validator functions to be _CapitalCase vs capitalCase. * Err... finish the validator renames. * Add `GetFloat64()` support. * Add `icmp_ping` check type support. * Catch up to the _API*Attr renames. Deliberately left out of the previous commit in order to create a clean example of what is required to add a new check type to the `circonus_check` resource. * Clarify when the `target` attribute is required for the `postgresql` check type. * Correctly pull the metric ID attribute from the right location. * Add a circonus_stream_group resource (a.k.a. a Circonus "metric cluster") * Add support for the [`caql`](https://login.circonus.com/user/docs/caql_reference) check type. * Add support for the `http` check type. * `s/SSL/TLS/g` * Add support for `tcp` check types. * Enumerate the available metrics that are supported for each check type. * Add [`cloudwatch`](https://login.circonus.com/user/docs/Data/CheckTypes/CloudWatch) check type support. * Add a `circonus_trigger` resource (a.k.a Circonus Ruleset). * Rename a handful of functions to make it clear in the function name the direction of flow for information moving through the provider. TL;DR: Replace `parse` and `read` with "foo to bar"-like names. * Fix the attribute name used in a validator. Absent != After. * Set the minimum `absent` predicate to 70s per testing. * Fix the regression tests for circonus_trigger now that absent has a 70s min * Fix up the `tcp` check to require a `host` attribute. Fix tests. It's clear I didn't run these before committing/pushing the `tcp` check last time. * Fix `circonus_check` for `cloudwatch` checks. * Rename `parsePerCheckTypeConfig()` to `_CheckConfigToAPI` to be consistent with other function names. grep(1)ability of code++ * Slack buttons as an integer are string encoded. * Fix updates for `circonus_contact`. * Fix the out parameters for contact groups. * Move to using `_CastSchemaToTF()` where appropriate. * Fix circonus_contact_group. Updates work as expected now. * Use `_StateSet()` in place of `d.Set()` everywhere. * Make a quick pass over the collector datasource to modernize its style * Quick pass for items identified by `golint`. * Fix up collectors * Fix the `json` check type. Reconcile possible sources of drift. Update now works as expected. * Normalize trigger durations to seconds. * Improve the robustness of the state handling for the `circonus_contact_group` resource. * I'm torn on this, but sort the contact groups in the notify list. This does mean that if the first contact group in the list has a higher lexical sort order the plan won't converge until the offending resource is tainted and recreated. But there's also some sorting happening elsewhere, so.... sort and taint for now and this will need to be revisited in the future. * Add support for the `httptrap` check type. * Remove empty units from the state file. * Metric clusters can return a 404. Detect this accordingly in its respective Exists handler. * Add a `circonus_graph` resource. * Fix a handful of bugs in the graph provider. * Re-enable the necessary `ConflictsWith` definitions and normalize attribute names. * Objects that have been deleted via the UI return a 404. Handle in Exists(). * Teach `circonus_graph`'s Stack set to accept nil values. * Set `ForceNew: true` for a graph's name. * Chase various API fixes required to make `circonus_graph` work as expected. * Fix up the handling of sub-1 zoom resolutions for graphs. * Add the `check_by_collector` out parameter to the `circonus_check` resource. * Improve validation of line vs area graphs. Fix graph_style. * Fix up the `logarithmic` graph axis option. * Resolve various trivial `go vet` issues. * Add a stream_group out parameter. * Remove incorrectly applied `Optional` attributes to the `circonus_account` resource. * Remove various `Optional` attributes from the `circonus_collector` data source. * Centralize the common need to suppress leading and trailing whitespace into `suppressWhitespace`. * Sync up with upstream vendor fixes for circonus_graph. * Update the checksum value for the http check. * Chase `circonus_graph`'s underlying `line_style` API object change from `string` to `*string`. * Clean up tests to use a generic terraform regression testing account. * Add support for the MySQL to the `circonus_check` resource. * Begin stubbing out the Circonus provider. * Remove all references to `reverse:secret_key`. This value is dynamically set by the service and unused by Terraform. * Update the `circonus_check` resource. Still a WIP. * Add docs for the `circonus_check` resource. Commit miss, this should have been included in the last commit. * "Fix" serializing check tags I still need to figure out how I can make them order agnostic w/o using a TypeSet. I'm worried that's what I'm going to have to do. * Spike a quick circonus_broker data source. * Convert tags to a Set so the order does not matter. * Add a `circonus_account` data source. * Correctly spell account. Pointed out by: @postwait * Add the `circonus_contact_group` resource. * Push descriptions into their own file in order to reduce the busyness of the schema when reviewing code. * Rename `circonus_broker` and `broker` to `circonus_collector` and `collector`, respectively. Change made with concent by Circonus to reduce confusion (@postwait, @maier, and several others). * Use upstream contsants where available. * Import the latest circonus-gometrics. * Move to using a Set of collectors vs a list attached to a single attribute. * Rename "cid" to "id" in the circonus_account data source and elsewhere where possible. * Inject a tag automatically. Update gometrics. * Checkpoint `circonus_metric` resource. * Enable provider-level auto-tagging. This is disabled by default. * Rearrange metric. This is an experimental "style" of a provider. We'll see. That moment. When you think you've gone off the rails on a mad scientist experiment but like the outcome and think you may be onto something but haven't proven it to yourself or anyone else yet? That. That exact feeling of semi-confidence while being alone in the wilderness. Please let this not be the Terraform provider equivalent of DJB's C style of coding. We'll know in another resource or two if this was a horrible mistake or not. * Begin moving `resource_circonus_check` over to the new world order/structure: Much of this is WIP and incomplete, but here is the new supported structure: ``` variable "used_metric_name" { default = "_usage`0`_used" } resource "circonus_check" "usage" { # collectors = ["${var.collectors}"] collector { id = "${var.collectors[0]}" } name = "${var.check_name}" notes = "${var.notes}" json { url = "https://${var.target}/account/current" http_headers = { "Accept" = "application/json" "X-Circonus-App-Name" = "TerraformCheck" "X-Circonus-Auth-Token" = "${var.api_token}" } } stream { name = "${circonus_metric.used.name}" tags = "${circonus_metric.used.tags}" type = "${circonus_metric.used.type}" } tags = { source = "circonus" } } resource "circonus_metric" "used" { name = "${var.used_metric_name}" tags = { source = "circonus" } type = "numeric" } ``` * Document the `circonus_metric` resource. * Updated `circonus_check` docs. * If a port was present, automatically set it in the Config. * Alpha sort the check parameters now that they've been renamed. * Fix a handful of panics as a result of the schema changing. * Move back to a `TypeSet` for tags. After a stint with `TypeMap`, move back to `TypeSet`. A set of strings seems to match the API the best. The `map` type was convenient because it reduced the amount of boilerplate, but you loose out on other things. For instance, tags come in the form of `category:value`, so naturally it seems like you could use a map, but you can't without severe loss of functionality because assigning two values to the same category is common. And you can't normalize map input or suppress the output correctly (this was eventually what broke the camel's back). I tried an experiment of normalizing the input to be `category:value` as the key in the map and a value of `""`, but... seee diff suppress. In this case, simple is good. While here bring some cleanups to _Metric since that was my initial testing target. * Rename `providerConfig` to `_ProviderConfig` * Checkpoint the `json` check type. * Fix a few residual issues re: missing descriptions. * Rename `validateRegexp` to `_ValidateRegexp` * Use tags as real sets, not just a slice of strings. * Move the DiffSuppressFunc for tags down to the Elem. * Fix up unit tests to chase the updated, default hasher function being used. * Remove `Computed` attribute from `TypeSet` objects. This fixes a pile of issues re: update that I was having. * Rename functions. `GetStringOk` -> `GetStringOK` `GetSetAsListOk` -> `GetSetAsListOK` `GetIntOk` -> `GetIntOK` * Various small cleanups and comments rolled into a single commit. * Add a `postgresql` check type for the `circonus_check` resource. * Rename various validator functions to be _CapitalCase vs capitalCase. * Err... finish the validator renames. * Add `GetFloat64()` support. * Add `icmp_ping` check type support. * Catch up to the _API*Attr renames. Deliberately left out of the previous commit in order to create a clean example of what is required to add a new check type to the `circonus_check` resource. * Clarify when the `target` attribute is required for the `postgresql` check type. * Correctly pull the metric ID attribute from the right location. * Add a circonus_stream_group resource (a.k.a. a Circonus "metric cluster") * Add support for the [`caql`](https://login.circonus.com/user/docs/caql_reference) check type. * Add support for the `http` check type. * `s/SSL/TLS/g` * Add support for `tcp` check types. * Enumerate the available metrics that are supported for each check type. * Add [`cloudwatch`](https://login.circonus.com/user/docs/Data/CheckTypes/CloudWatch) check type support. * Add a `circonus_trigger` resource (a.k.a Circonus Ruleset). * Rename a handful of functions to make it clear in the function name the direction of flow for information moving through the provider. TL;DR: Replace `parse` and `read` with "foo to bar"-like names. * Fix the attribute name used in a validator. Absent != After. * Set the minimum `absent` predicate to 70s per testing. * Fix the regression tests for circonus_trigger now that absent has a 70s min * Fix up the `tcp` check to require a `host` attribute. Fix tests. It's clear I didn't run these before committing/pushing the `tcp` check last time. * Fix `circonus_check` for `cloudwatch` checks. * Rename `parsePerCheckTypeConfig()` to `_CheckConfigToAPI` to be consistent with other function names. grep(1)ability of code++ * Slack buttons as an integer are string encoded. * Fix updates for `circonus_contact`. * Fix the out parameters for contact groups. * Move to using `_CastSchemaToTF()` where appropriate. * Fix circonus_contact_group. Updates work as expected now. * Use `_StateSet()` in place of `d.Set()` everywhere. * Make a quick pass over the collector datasource to modernize its style * Quick pass for items identified by `golint`. * Fix up collectors * Fix the `json` check type. Reconcile possible sources of drift. Update now works as expected. * Normalize trigger durations to seconds. * Improve the robustness of the state handling for the `circonus_contact_group` resource. * I'm torn on this, but sort the contact groups in the notify list. This does mean that if the first contact group in the list has a higher lexical sort order the plan won't converge until the offending resource is tainted and recreated. But there's also some sorting happening elsewhere, so.... sort and taint for now and this will need to be revisited in the future. * Add support for the `httptrap` check type. * Remove empty units from the state file. * Metric clusters can return a 404. Detect this accordingly in its respective Exists handler. * Add a `circonus_graph` resource. * Fix a handful of bugs in the graph provider. * Re-enable the necessary `ConflictsWith` definitions and normalize attribute names. * Objects that have been deleted via the UI return a 404. Handle in Exists(). * Teach `circonus_graph`'s Stack set to accept nil values. * Set `ForceNew: true` for a graph's name. * Chase various API fixes required to make `circonus_graph` work as expected. * Fix up the handling of sub-1 zoom resolutions for graphs. * Add the `check_by_collector` out parameter to the `circonus_check` resource. * Improve validation of line vs area graphs. Fix graph_style. * Fix up the `logarithmic` graph axis option. * Resolve various trivial `go vet` issues. * Add a stream_group out parameter. * Remove incorrectly applied `Optional` attributes to the `circonus_account` resource. * Remove various `Optional` attributes from the `circonus_collector` data source. * Centralize the common need to suppress leading and trailing whitespace into `suppressWhitespace`. * Sync up with upstream vendor fixes for circonus_graph. * Update the checksum value for the http check. * Chase `circonus_graph`'s underlying `line_style` API object change from `string` to `*string`. * Clean up tests to use a generic terraform regression testing account. * Rename all identifiers that began with a `_` and replace with a corresponding lowercase glyph. * Remove stale comment in types. * Move the calls to `ResourceData`'s `SetId()` calls to be first in the list so that no resources are lost in the event of a `panic()`. * Remove `stateSet` from the `circonus_trigger` resource. * Remove `stateSet` from the `circonus_stream_group` resource. * Remove `schemaSet` from the `circonus_graph` resource. * Remove `stateSet` from the `circonus_contact` resource. * Remove `stateSet` from the `circonus_metric` resource. * Remove `stateSet` from the `circonus_account` data source. * Remove `stateSet` from the `circonus_collector` data source. * Remove stray `stateSet` call from the `circonus_contact` resource. This is an odd artifact to find... I'm completely unsure as to why it was there to begin with but am mostly certain it's a bug and needs to be removed. * Remove `stateSet` from the `circonus_check` resource. * Remove the `stateSet` helper function. All call sites have been converted to return errors vs `panic()`'ing at runtime. * Remove a pile of unused functions and type definitions. * Remove the last of the `attrReader` interface. * Remove an unused `Sprintf` call. * Update `circonus-gometrics` and remove unused files. * Document what `convertToHelperSchema()` does. Rename `castSchemaToTF` to `convertToHelperSchema`. Change the function parameter ordering so the `map` of attribute descriptions: this is much easier to maintain when the description map is first when creating schema inline. * Move descriptions into their respective source files. * Remove all instances of `panic()`. In the case of software bugs, log an error. Never `panic()` and always return a value. * Rename `stream_group` to `metric_cluster`. * Rename triggers to rule sets * Rename `stream` to `metric`. * Chase the `stream` -> `metric` change into the docs. * Remove some unused test functions. * Add the now required `color` attribute for graphing a `metric_cluster`. * Add a missing description to silence a warning. * Add `id` as a selector for the account data source. * Futureproof testing: Randomize all asset names to prevent any possible resource conflicts. This isn't a necessary change for our current build and regression testing, but *just in case* we have a radical change to our testing framework in the future, make all resource names fully random. * Rename various values to match the Circonus docs. * s/alarm/alert/g * Ensure ruleset criteria can not be empty.
2017-03-10 21:19:17 +01:00
package circonus
import (
"bytes"
"encoding/json"
"fmt"
"log"
"strings"
"time"
"github.com/circonus-labs/circonus-gometrics/api"
"github.com/circonus-labs/circonus-gometrics/api/config"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)
const (
// circonus_contact attributes
contactAggregationWindowAttr = "aggregation_window"
contactAlertOptionAttr = "alert_option"
contactEmailAttr = "email"
contactHTTPAttr = "http"
contactIRCAttr = "irc"
contactLongMessageAttr = "long_message"
contactLongSubjectAttr = "long_subject"
contactLongSummaryAttr = "long_summary"
contactNameAttr = "name"
contactPagerDutyAttr = "pager_duty"
contactSMSAttr = "sms"
contactShortMessageAttr = "short_message"
contactShortSummaryAttr = "short_summary"
contactSlackAttr = "slack"
contactTagsAttr = "tags"
contactVictorOpsAttr = "victorops"
contactXMPPAttr = "xmpp"
// circonus_contact.alert_option attributes
contactEscalateAfterAttr = "escalate_after"
contactEscalateToAttr = "escalate_to"
contactReminderAttr = "reminder"
contactSeverityAttr = "severity"
// circonus_contact.email attributes
contactEmailAddressAttr = "address"
//contactUserCIDAttr
// circonus_contact.http attributes
contactHTTPAddressAttr schemaAttr = "address"
contactHTTPFormatAttr = "format"
contactHTTPMethodAttr = "method"
// circonus_contact.irc attributes
//contactUserCIDAttr
// circonus_contact.pager_duty attributes
//contactContactGroupFallbackAttr
contactPagerDutyIntegrationKeyAttr schemaAttr = "integration_key"
contactPagerDutyWebhookURLAttr schemaAttr = "webook_url"
// circonus_contact.slack attributes
//contactContactGroupFallbackAttr
contactSlackButtonsAttr = "buttons"
contactSlackChannelAttr = "channel"
contactSlackTeamAttr = "team"
contactSlackUsernameAttr = "username"
// circonus_contact.sms attributes
contactSMSAddressAttr = "address"
//contactUserCIDAttr
// circonus_contact.victorops attributes
//contactContactGroupFallbackAttr
contactVictorOpsAPIKeyAttr = "api_key"
contactVictorOpsCriticalAttr = "critical"
contactVictorOpsInfoAttr = "info"
contactVictorOpsTeamAttr = "team"
contactVictorOpsWarningAttr = "warning"
// circonus_contact.victorops attributes
//contactUserCIDAttr
contactXMPPAddressAttr = "address"
// circonus_contact read-only attributes
contactLastModifiedAttr = "last_modified"
contactLastModifiedByAttr = "last_modified_by"
// circonus_contact.* shared attributes
contactContactGroupFallbackAttr = "contact_group_fallback"
contactUserCIDAttr = "user"
)
const (
// Contact methods from Circonus
circonusMethodEmail = "email"
circonusMethodHTTP = "http"
circonusMethodIRC = "irc"
circonusMethodPagerDuty = "pagerduty"
circonusMethodSlack = "slack"
circonusMethodSMS = "sms"
circonusMethodVictorOps = "victorops"
circonusMethodXMPP = "xmpp"
)
type contactHTTPInfo struct {
Address string `json:"url"`
Format string `json:"params"`
Method string `json:"method"`
}
type contactPagerDutyInfo struct {
FallbackGroupCID int `json:"failover_group,string"`
IntegrationKey string `json:"service_key"`
WebookURL string `json:"webook_url"`
}
type contactSlackInfo struct {
Buttons int `json:"buttons,string"`
Channel string `json:"channel"`
FallbackGroupCID int `json:"failover_group,string"`
Team string `json:"team"`
Username string `json:"username"`
}
type contactVictorOpsInfo struct {
APIKey string `json:"api_key"`
Critical int `json:"critical,string"`
FallbackGroupCID int `json:"failover_group,string"`
Info int `json:"info,string"`
Team string `json:"team"`
Warning int `json:"warning,string"`
}
var contactGroupDescriptions = attrDescrs{
contactAggregationWindowAttr: "",
contactAlertOptionAttr: "",
contactContactGroupFallbackAttr: "",
contactEmailAttr: "",
contactHTTPAttr: "",
contactIRCAttr: "",
contactLastModifiedAttr: "",
contactLastModifiedByAttr: "",
contactLongMessageAttr: "",
contactLongSubjectAttr: "",
contactLongSummaryAttr: "",
contactNameAttr: "",
contactPagerDutyAttr: "",
contactSMSAttr: "",
contactShortMessageAttr: "",
contactShortSummaryAttr: "",
contactSlackAttr: "",
contactTagsAttr: "",
contactVictorOpsAttr: "",
contactXMPPAttr: "",
}
var contactAlertDescriptions = attrDescrs{
contactEscalateAfterAttr: "",
contactEscalateToAttr: "",
contactReminderAttr: "",
contactSeverityAttr: "",
}
var contactEmailDescriptions = attrDescrs{
contactEmailAddressAttr: "",
contactUserCIDAttr: "",
}
var contactHTTPDescriptions = attrDescrs{
contactHTTPAddressAttr: "",
contactHTTPFormatAttr: "",
contactHTTPMethodAttr: "",
}
var contactPagerDutyDescriptions = attrDescrs{
contactContactGroupFallbackAttr: "",
contactPagerDutyIntegrationKeyAttr: "",
contactPagerDutyWebhookURLAttr: "",
}
var contactSlackDescriptions = attrDescrs{
contactContactGroupFallbackAttr: "",
contactSlackButtonsAttr: "",
contactSlackChannelAttr: "",
contactSlackTeamAttr: "",
contactSlackUsernameAttr: "Username Slackbot uses in Slack to deliver a notification",
}
var contactSMSDescriptions = attrDescrs{
contactSMSAddressAttr: "",
contactUserCIDAttr: "",
}
var contactVictorOpsDescriptions = attrDescrs{
contactContactGroupFallbackAttr: "",
contactVictorOpsAPIKeyAttr: "",
contactVictorOpsCriticalAttr: "",
contactVictorOpsInfoAttr: "",
contactVictorOpsTeamAttr: "",
contactVictorOpsWarningAttr: "",
}
var contactXMPPDescriptions = attrDescrs{
contactUserCIDAttr: "",
contactXMPPAddressAttr: "",
}
func resourceContactGroup() *schema.Resource {
return &schema.Resource{
Create: contactGroupCreate,
Read: contactGroupRead,
Update: contactGroupUpdate,
Delete: contactGroupDelete,
Exists: contactGroupExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: convertToHelperSchema(contactGroupDescriptions, map[schemaAttr]*schema.Schema{
contactAggregationWindowAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: defaultCirconusAggregationWindow,
DiffSuppressFunc: suppressEquivalentTimeDurations,
StateFunc: normalizeTimeDurationStringToSeconds,
ValidateFunc: validateFuncs(
validateDurationMin(contactAggregationWindowAttr, "0s"),
),
},
contactAlertOptionAttr: &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Set: contactGroupAlertOptionsChecksum,
Elem: &schema.Resource{
Schema: convertToHelperSchema(contactAlertDescriptions, map[schemaAttr]*schema.Schema{
contactEscalateAfterAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: suppressEquivalentTimeDurations,
StateFunc: normalizeTimeDurationStringToSeconds,
ValidateFunc: validateFuncs(
validateDurationMin(contactEscalateAfterAttr, defaultCirconusAlertMinEscalateAfter),
),
},
contactEscalateToAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateContactGroupCID(contactEscalateToAttr),
},
contactReminderAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: suppressEquivalentTimeDurations,
StateFunc: normalizeTimeDurationStringToSeconds,
ValidateFunc: validateFuncs(
validateDurationMin(contactReminderAttr, "0s"),
),
},
contactSeverityAttr: &schema.Schema{
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateFuncs(
validateIntMin(contactSeverityAttr, minSeverity),
validateIntMax(contactSeverityAttr, maxSeverity),
),
},
}),
},
},
contactEmailAttr: &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: convertToHelperSchema(contactEmailDescriptions, map[schemaAttr]*schema.Schema{
contactEmailAddressAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{contactEmailAttr + "." + contactUserCIDAttr},
},
contactUserCIDAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateUserCID(contactUserCIDAttr),
ConflictsWith: []string{contactEmailAttr + "." + contactEmailAddressAttr},
},
}),
},
},
contactHTTPAttr: &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: convertToHelperSchema(contactHTTPDescriptions, map[schemaAttr]*schema.Schema{
contactHTTPAddressAttr: &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validateHTTPURL(contactHTTPAddressAttr, urlBasicCheck),
},
contactHTTPFormatAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: defaultCirconusHTTPFormat,
ValidateFunc: validateStringIn(contactHTTPFormatAttr, validContactHTTPFormats),
},
contactHTTPMethodAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: defaultCirconusHTTPMethod,
ValidateFunc: validateStringIn(contactHTTPMethodAttr, validContactHTTPMethods),
},
}),
},
},
contactIRCAttr: &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
contactUserCIDAttr: &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validateUserCID(contactUserCIDAttr),
},
},
},
},
contactLongMessageAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
StateFunc: suppressWhitespace,
},
contactLongSubjectAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
StateFunc: suppressWhitespace,
},
contactLongSummaryAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
StateFunc: suppressWhitespace,
},
contactNameAttr: &schema.Schema{
Type: schema.TypeString,
Required: true,
},
contactPagerDutyAttr: &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: convertToHelperSchema(contactPagerDutyDescriptions, map[schemaAttr]*schema.Schema{
contactContactGroupFallbackAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateContactGroupCID(contactContactGroupFallbackAttr),
},
contactPagerDutyIntegrationKeyAttr: &schema.Schema{
Type: schema.TypeString,
Required: true,
Sensitive: true,
ValidateFunc: validateHTTPURL(contactPagerDutyIntegrationKeyAttr, urlIsAbs),
},
contactPagerDutyWebhookURLAttr: &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validateHTTPURL(contactPagerDutyWebhookURLAttr, urlIsAbs),
},
}),
},
},
contactShortMessageAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
StateFunc: suppressWhitespace,
},
contactShortSummaryAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
StateFunc: suppressWhitespace,
},
contactSlackAttr: &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: convertToHelperSchema(contactSlackDescriptions, map[schemaAttr]*schema.Schema{
contactContactGroupFallbackAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateContactGroupCID(contactContactGroupFallbackAttr),
},
contactSlackButtonsAttr: &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
},
contactSlackChannelAttr: &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validateFuncs(
validateRegexp(contactSlackChannelAttr, `^#[\S]+$`),
),
},
contactSlackTeamAttr: &schema.Schema{
Type: schema.TypeString,
Required: true,
},
contactSlackUsernameAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: defaultCirconusSlackUsername,
ValidateFunc: validateFuncs(
validateRegexp(contactSlackChannelAttr, `^[\S]+$`),
),
},
}),
},
},
contactSMSAttr: &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: convertToHelperSchema(contactSMSDescriptions, map[schemaAttr]*schema.Schema{
contactSMSAddressAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{contactSMSAttr + "." + contactUserCIDAttr},
},
contactUserCIDAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateUserCID(contactUserCIDAttr),
ConflictsWith: []string{contactSMSAttr + "." + contactSMSAddressAttr},
},
}),
},
},
contactTagsAttr: tagMakeConfigSchema(contactTagsAttr),
contactVictorOpsAttr: &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: convertToHelperSchema(contactVictorOpsDescriptions, map[schemaAttr]*schema.Schema{
contactContactGroupFallbackAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateContactGroupCID(contactContactGroupFallbackAttr),
},
contactVictorOpsAPIKeyAttr: &schema.Schema{
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
contactVictorOpsCriticalAttr: &schema.Schema{
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateFuncs(
validateIntMin(contactVictorOpsCriticalAttr, 1),
validateIntMax(contactVictorOpsCriticalAttr, 5),
),
},
contactVictorOpsInfoAttr: &schema.Schema{
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateFuncs(
validateIntMin(contactVictorOpsInfoAttr, 1),
validateIntMax(contactVictorOpsInfoAttr, 5),
),
},
contactVictorOpsTeamAttr: &schema.Schema{
Type: schema.TypeString,
Required: true,
},
contactVictorOpsWarningAttr: &schema.Schema{
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateFuncs(
validateIntMin(contactVictorOpsWarningAttr, 1),
validateIntMax(contactVictorOpsWarningAttr, 5),
),
},
}),
},
},
contactXMPPAttr: &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: convertToHelperSchema(contactXMPPDescriptions, map[schemaAttr]*schema.Schema{
contactXMPPAddressAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{contactXMPPAttr + "." + contactUserCIDAttr},
},
contactUserCIDAttr: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateUserCID(contactUserCIDAttr),
ConflictsWith: []string{contactXMPPAttr + "." + contactXMPPAddressAttr},
},
}),
},
},
// OUT parameters
contactLastModifiedAttr: &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
contactLastModifiedByAttr: &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
}),
}
}
func contactGroupCreate(d *schema.ResourceData, meta interface{}) error {
ctxt := meta.(*providerContext)
in, err := getContactGroupInput(d)
if err != nil {
return err
}
cg, err := ctxt.client.CreateContactGroup(in)
if err != nil {
return err
}
d.SetId(cg.CID)
return contactGroupRead(d, meta)
}
func contactGroupExists(d *schema.ResourceData, meta interface{}) (bool, error) {
c := meta.(*providerContext)
cid := d.Id()
cg, err := c.client.FetchContactGroup(api.CIDType(&cid))
if err != nil {
if strings.Contains(err.Error(), defaultCirconus404ErrorString) {
return false, nil
}
return false, err
}
if cg.CID == "" {
return false, nil
}
return true, nil
}
func contactGroupRead(d *schema.ResourceData, meta interface{}) error {
c := meta.(*providerContext)
cid := d.Id()
cg, err := c.client.FetchContactGroup(api.CIDType(&cid))
if err != nil {
return err
}
if cg.CID == "" {
return nil
}
d.SetId(cg.CID)
httpState, err := contactGroupHTTPToState(cg)
if err != nil {
return err
}
pagerDutyState, err := contactGroupPagerDutyToState(cg)
if err != nil {
return err
}
slackState, err := contactGroupSlackToState(cg)
if err != nil {
return err
}
smsState, err := contactGroupSMSToState(cg)
if err != nil {
return err
}
victorOpsState, err := contactGroupVictorOpsToState(cg)
if err != nil {
return err
}
xmppState, err := contactGroupXMPPToState(cg)
if err != nil {
return err
}
d.Set(contactAggregationWindowAttr, fmt.Sprintf("%ds", cg.AggregationWindow))
if err := d.Set(contactAlertOptionAttr, contactGroupAlertOptionsToState(cg)); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Unable to store contact %q attribute: {{err}}", contactAlertOptionAttr), err)
}
if err := d.Set(contactEmailAttr, contactGroupEmailToState(cg)); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Unable to store contact %q attribute: {{err}}", contactEmailAttr), err)
}
if err := d.Set(contactHTTPAttr, httpState); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Unable to store contact %q attribute: {{err}}", contactHTTPAttr), err)
}
if err := d.Set(contactIRCAttr, contactGroupIRCToState(cg)); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Unable to store contact %q attribute: {{err}}", contactIRCAttr), err)
}
d.Set(contactLongMessageAttr, cg.AlertFormats.LongMessage)
d.Set(contactLongSubjectAttr, cg.AlertFormats.LongSubject)
d.Set(contactLongSummaryAttr, cg.AlertFormats.LongSummary)
d.Set(contactNameAttr, cg.Name)
if err := d.Set(contactPagerDutyAttr, pagerDutyState); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Unable to store contact %q attribute: {{err}}", contactPagerDutyAttr), err)
}
d.Set(contactShortMessageAttr, cg.AlertFormats.ShortMessage)
d.Set(contactShortSummaryAttr, cg.AlertFormats.ShortSummary)
if err := d.Set(contactSlackAttr, slackState); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Unable to store contact %q attribute: {{err}}", contactSlackAttr), err)
}
if err := d.Set(contactSMSAttr, smsState); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Unable to store contact %q attribute: {{err}}", contactSMSAttr), err)
}
if err := d.Set(contactTagsAttr, cg.Tags); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Unable to store contact %q attribute: {{err}}", contactTagsAttr), err)
}
if err := d.Set(contactVictorOpsAttr, victorOpsState); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Unable to store contact %q attribute: {{err}}", contactVictorOpsAttr), err)
}
if err := d.Set(contactXMPPAttr, xmppState); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Unable to store contact %q attribute: {{err}}", contactXMPPAttr), err)
}
// Out parameters
d.Set(contactLastModifiedAttr, cg.LastModified)
d.Set(contactLastModifiedByAttr, cg.LastModifiedBy)
return nil
}
func contactGroupUpdate(d *schema.ResourceData, meta interface{}) error {
c := meta.(*providerContext)
in, err := getContactGroupInput(d)
if err != nil {
return err
}
in.CID = d.Id()
if _, err := c.client.UpdateContactGroup(in); err != nil {
return errwrap.Wrapf(fmt.Sprintf("unable to update contact group %q: {{err}}", d.Id()), err)
}
return contactGroupRead(d, meta)
}
func contactGroupDelete(d *schema.ResourceData, meta interface{}) error {
c := meta.(*providerContext)
cid := d.Id()
if _, err := c.client.DeleteContactGroupByCID(api.CIDType(&cid)); err != nil {
return errwrap.Wrapf(fmt.Sprintf("unable to delete contact group %q: {{err}}", d.Id()), err)
}
d.SetId("")
return nil
}
func contactGroupAlertOptionsToState(cg *api.ContactGroup) []interface{} {
if config.NumSeverityLevels != len(cg.Reminders) {
log.Printf("[FATAL] PROVIDER BUG: Need to update constants in contactGroupAlertOptionsToState re: reminders")
return nil
}
if config.NumSeverityLevels != len(cg.Escalations) {
log.Printf("[FATAL] PROVIDER BUG: Need to update constants in contactGroupAlertOptionsToState re: escalations")
return nil
}
// Populate all alert options for every severity level. We'll prune empty
// values at the end of this function.
const defaultNumAlertOptions = 4
alertOptions := make([]*map[string]interface{}, config.NumSeverityLevels)
for severityIndex := 0; severityIndex < config.NumSeverityLevels; severityIndex++ {
sevAction := make(map[string]interface{}, defaultNumAlertOptions)
sevAction[string(contactSeverityAttr)] = severityIndex + 1
alertOptions[severityIndex] = &sevAction
}
for severityIndex, reminder := range cg.Reminders {
if reminder != 0 {
(*alertOptions[severityIndex])[string(contactReminderAttr)] = fmt.Sprintf("%ds", reminder)
}
}
for severityIndex, escalate := range cg.Escalations {
if escalate == nil {
continue
}
(*alertOptions[severityIndex])[string(contactEscalateAfterAttr)] = fmt.Sprintf("%ds", escalate.After)
(*alertOptions[severityIndex])[string(contactEscalateToAttr)] = escalate.ContactGroupCID
}
alertOptionsList := make([]interface{}, 0, config.NumSeverityLevels)
for i := 0; i < config.NumSeverityLevels; i++ {
// NOTE: the 1 is from the always-populated contactSeverityAttr which is
// always set.
if len(*alertOptions[i]) > 1 {
alertOptionsList = append(alertOptionsList, *alertOptions[i])
}
}
return alertOptionsList
}
func contactGroupEmailToState(cg *api.ContactGroup) []interface{} {
emailContacts := make([]interface{}, 0, len(cg.Contacts.Users)+len(cg.Contacts.External))
for _, ext := range cg.Contacts.External {
switch ext.Method {
case circonusMethodEmail:
emailContacts = append(emailContacts, map[string]interface{}{
contactEmailAddressAttr: ext.Info,
})
}
}
for _, user := range cg.Contacts.Users {
switch user.Method {
case circonusMethodEmail:
emailContacts = append(emailContacts, map[string]interface{}{
contactUserCIDAttr: user.UserCID,
})
}
}
return emailContacts
}
func contactGroupHTTPToState(cg *api.ContactGroup) ([]interface{}, error) {
httpContacts := make([]interface{}, 0, len(cg.Contacts.External))
for _, ext := range cg.Contacts.External {
switch ext.Method {
case contactHTTPAttr:
url := contactHTTPInfo{}
if err := json.Unmarshal([]byte(ext.Info), &url); err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("unable to decode external %s JSON (%q): {{err}}", contactHTTPAttr, ext.Info), err)
}
httpContacts = append(httpContacts, map[string]interface{}{
string(contactHTTPAddressAttr): url.Address,
string(contactHTTPFormatAttr): url.Format,
string(contactHTTPMethodAttr): url.Method,
})
}
}
return httpContacts, nil
}
func getContactGroupInput(d *schema.ResourceData) (*api.ContactGroup, error) {
cg := api.NewContactGroup()
if v, ok := d.GetOk(contactAggregationWindowAttr); ok {
aggWindow, _ := time.ParseDuration(v.(string))
cg.AggregationWindow = uint(aggWindow.Seconds())
}
if v, ok := d.GetOk(contactAlertOptionAttr); ok {
alertOptionsRaw := v.(*schema.Set).List()
ensureEscalationSeverity := func(severity int) {
if cg.Escalations[severity] == nil {
cg.Escalations[severity] = &api.ContactGroupEscalation{}
}
}
for _, alertOptionRaw := range alertOptionsRaw {
alertOptionsMap := alertOptionRaw.(map[string]interface{})
severityIndex := -1
if optRaw, ok := alertOptionsMap[contactSeverityAttr]; ok {
severityIndex = optRaw.(int) - 1
}
if optRaw, ok := alertOptionsMap[contactEscalateAfterAttr]; ok {
if optRaw.(string) != "" {
d, _ := time.ParseDuration(optRaw.(string))
if d != 0 {
ensureEscalationSeverity(severityIndex)
cg.Escalations[severityIndex].After = uint(d.Seconds())
}
}
}
if optRaw, ok := alertOptionsMap[contactEscalateToAttr]; ok && optRaw.(string) != "" {
ensureEscalationSeverity(severityIndex)
cg.Escalations[severityIndex].ContactGroupCID = optRaw.(string)
}
if optRaw, ok := alertOptionsMap[contactReminderAttr]; ok {
if optRaw.(string) == "" {
optRaw = "0s"
}
d, _ := time.ParseDuration(optRaw.(string))
cg.Reminders[severityIndex] = uint(d.Seconds())
}
}
}
if v, ok := d.GetOk(contactNameAttr); ok {
cg.Name = v.(string)
}
if v, ok := d.GetOk(contactEmailAttr); ok {
emailListRaw := v.(*schema.Set).List()
for _, emailMapRaw := range emailListRaw {
emailMap := emailMapRaw.(map[string]interface{})
var requiredAttrFound bool
if v, ok := emailMap[contactEmailAddressAttr]; ok && v.(string) != "" {
requiredAttrFound = true
cg.Contacts.External = append(cg.Contacts.External, api.ContactGroupContactsExternal{
Info: v.(string),
Method: circonusMethodEmail,
})
}
if v, ok := emailMap[contactUserCIDAttr]; ok && v.(string) != "" {
requiredAttrFound = true
cg.Contacts.Users = append(cg.Contacts.Users, api.ContactGroupContactsUser{
Method: circonusMethodEmail,
UserCID: v.(string),
})
}
// Can't mark two attributes that are conflicting as required so we do our
// own validation check here.
if !requiredAttrFound {
return nil, fmt.Errorf("In type %s, either %s or %s must be specified", contactEmailAttr, contactEmailAddressAttr, contactUserCIDAttr)
}
}
}
if v, ok := d.GetOk(contactHTTPAttr); ok {
httpListRaw := v.(*schema.Set).List()
for _, httpMapRaw := range httpListRaw {
httpMap := httpMapRaw.(map[string]interface{})
httpInfo := contactHTTPInfo{}
if v, ok := httpMap[string(contactHTTPAddressAttr)]; ok {
httpInfo.Address = v.(string)
}
if v, ok := httpMap[string(contactHTTPFormatAttr)]; ok {
httpInfo.Format = v.(string)
}
if v, ok := httpMap[string(contactHTTPMethodAttr)]; ok {
httpInfo.Method = v.(string)
}
js, err := json.Marshal(httpInfo)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("error marshalling %s JSON config string: {{err}}", contactHTTPAttr), err)
}
cg.Contacts.External = append(cg.Contacts.External, api.ContactGroupContactsExternal{
Info: string(js),
Method: circonusMethodHTTP,
})
}
}
if v, ok := d.GetOk(contactIRCAttr); ok {
ircListRaw := v.(*schema.Set).List()
for _, ircMapRaw := range ircListRaw {
ircMap := ircMapRaw.(map[string]interface{})
if v, ok := ircMap[contactUserCIDAttr]; ok && v.(string) != "" {
cg.Contacts.Users = append(cg.Contacts.Users, api.ContactGroupContactsUser{
Method: circonusMethodIRC,
UserCID: v.(string),
})
}
}
}
if v, ok := d.GetOk(contactPagerDutyAttr); ok {
pagerDutyListRaw := v.(*schema.Set).List()
for _, pagerDutyMapRaw := range pagerDutyListRaw {
pagerDutyMap := pagerDutyMapRaw.(map[string]interface{})
pagerDutyInfo := contactPagerDutyInfo{}
if v, ok := pagerDutyMap[contactContactGroupFallbackAttr]; ok && v.(string) != "" {
cid := v.(string)
contactGroupID, err := failoverGroupCIDToID(api.CIDType(&cid))
if err != nil {
return nil, errwrap.Wrapf("error reading contact group CID: {{err}}", err)
}
pagerDutyInfo.FallbackGroupCID = contactGroupID
}
if v, ok := pagerDutyMap[string(contactPagerDutyIntegrationKeyAttr)]; ok {
pagerDutyInfo.IntegrationKey = v.(string)
}
if v, ok := pagerDutyMap[string(contactPagerDutyWebhookURLAttr)]; ok {
pagerDutyInfo.WebookURL = v.(string)
}
js, err := json.Marshal(pagerDutyInfo)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("error marshalling %s JSON config string: {{err}}", contactPagerDutyAttr), err)
}
cg.Contacts.External = append(cg.Contacts.External, api.ContactGroupContactsExternal{
Info: string(js),
Method: circonusMethodPagerDuty,
})
}
}
if v, ok := d.GetOk(contactSlackAttr); ok {
slackListRaw := v.(*schema.Set).List()
for _, slackMapRaw := range slackListRaw {
slackMap := slackMapRaw.(map[string]interface{})
slackInfo := contactSlackInfo{}
var buttons int
if v, ok := slackMap[contactSlackButtonsAttr]; ok {
if v.(bool) {
buttons = 1
}
slackInfo.Buttons = buttons
}
if v, ok := slackMap[contactSlackChannelAttr]; ok {
slackInfo.Channel = v.(string)
}
if v, ok := slackMap[contactContactGroupFallbackAttr]; ok && v.(string) != "" {
cid := v.(string)
contactGroupID, err := failoverGroupCIDToID(api.CIDType(&cid))
if err != nil {
return nil, errwrap.Wrapf("error reading contact group CID: {{err}}", err)
}
slackInfo.FallbackGroupCID = contactGroupID
}
if v, ok := slackMap[contactSlackTeamAttr]; ok {
slackInfo.Team = v.(string)
}
if v, ok := slackMap[contactSlackUsernameAttr]; ok {
slackInfo.Username = v.(string)
}
js, err := json.Marshal(slackInfo)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("error marshalling %s JSON config string: {{err}}", contactSlackAttr), err)
}
cg.Contacts.External = append(cg.Contacts.External, api.ContactGroupContactsExternal{
Info: string(js),
Method: circonusMethodSlack,
})
}
}
if v, ok := d.GetOk(contactSMSAttr); ok {
smsListRaw := v.(*schema.Set).List()
for _, smsMapRaw := range smsListRaw {
smsMap := smsMapRaw.(map[string]interface{})
var requiredAttrFound bool
if v, ok := smsMap[contactSMSAddressAttr]; ok && v.(string) != "" {
requiredAttrFound = true
cg.Contacts.External = append(cg.Contacts.External, api.ContactGroupContactsExternal{
Info: v.(string),
Method: circonusMethodSMS,
})
}
if v, ok := smsMap[contactUserCIDAttr]; ok && v.(string) != "" {
requiredAttrFound = true
cg.Contacts.Users = append(cg.Contacts.Users, api.ContactGroupContactsUser{
Method: circonusMethodSMS,
UserCID: v.(string),
})
}
// Can't mark two attributes that are conflicting as required so we do our
// own validation check here.
if !requiredAttrFound {
return nil, fmt.Errorf("In type %s, either %s or %s must be specified", contactEmailAttr, contactEmailAddressAttr, contactUserCIDAttr)
}
}
}
if v, ok := d.GetOk(contactVictorOpsAttr); ok {
victorOpsListRaw := v.(*schema.Set).List()
for _, victorOpsMapRaw := range victorOpsListRaw {
victorOpsMap := victorOpsMapRaw.(map[string]interface{})
victorOpsInfo := contactVictorOpsInfo{}
if v, ok := victorOpsMap[contactContactGroupFallbackAttr]; ok && v.(string) != "" {
cid := v.(string)
contactGroupID, err := failoverGroupCIDToID(api.CIDType(&cid))
if err != nil {
return nil, errwrap.Wrapf("error reading contact group CID: {{err}}", err)
}
victorOpsInfo.FallbackGroupCID = contactGroupID
}
if v, ok := victorOpsMap[contactVictorOpsAPIKeyAttr]; ok {
victorOpsInfo.APIKey = v.(string)
}
if v, ok := victorOpsMap[contactVictorOpsCriticalAttr]; ok {
victorOpsInfo.Critical = v.(int)
}
if v, ok := victorOpsMap[contactVictorOpsInfoAttr]; ok {
victorOpsInfo.Info = v.(int)
}
if v, ok := victorOpsMap[contactVictorOpsTeamAttr]; ok {
victorOpsInfo.Team = v.(string)
}
if v, ok := victorOpsMap[contactVictorOpsWarningAttr]; ok {
victorOpsInfo.Warning = v.(int)
}
js, err := json.Marshal(victorOpsInfo)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("error marshalling %s JSON config string: {{err}}", contactVictorOpsAttr), err)
}
cg.Contacts.External = append(cg.Contacts.External, api.ContactGroupContactsExternal{
Info: string(js),
Method: circonusMethodVictorOps,
})
}
}
if v, ok := d.GetOk(contactXMPPAttr); ok {
xmppListRaw := v.(*schema.Set).List()
for _, xmppMapRaw := range xmppListRaw {
xmppMap := xmppMapRaw.(map[string]interface{})
if v, ok := xmppMap[contactXMPPAddressAttr]; ok && v.(string) != "" {
cg.Contacts.External = append(cg.Contacts.External, api.ContactGroupContactsExternal{
Info: v.(string),
Method: circonusMethodXMPP,
})
}
if v, ok := xmppMap[contactUserCIDAttr]; ok && v.(string) != "" {
cg.Contacts.Users = append(cg.Contacts.Users, api.ContactGroupContactsUser{
Method: circonusMethodXMPP,
UserCID: v.(string),
})
}
}
}
if v, ok := d.GetOk(contactLongMessageAttr); ok {
msg := v.(string)
cg.AlertFormats.LongMessage = &msg
}
if v, ok := d.GetOk(contactLongSubjectAttr); ok {
msg := v.(string)
cg.AlertFormats.LongSubject = &msg
}
if v, ok := d.GetOk(contactLongSummaryAttr); ok {
msg := v.(string)
cg.AlertFormats.LongSummary = &msg
}
if v, ok := d.GetOk(contactShortMessageAttr); ok {
msg := v.(string)
cg.AlertFormats.ShortMessage = &msg
}
if v, ok := d.GetOk(contactShortSummaryAttr); ok {
msg := v.(string)
cg.AlertFormats.ShortSummary = &msg
}
if v, ok := d.GetOk(contactShortMessageAttr); ok {
msg := v.(string)
cg.AlertFormats.ShortMessage = &msg
}
if v, found := d.GetOk(checkTagsAttr); found {
cg.Tags = derefStringList(flattenSet(v.(*schema.Set)))
}
if err := validateContactGroup(cg); err != nil {
return nil, err
}
return cg, nil
}
func contactGroupIRCToState(cg *api.ContactGroup) []interface{} {
ircContacts := make([]interface{}, 0, len(cg.Contacts.Users))
for _, user := range cg.Contacts.Users {
switch user.Method {
case contactIRCAttr:
ircContacts = append(ircContacts, map[string]interface{}{
contactUserCIDAttr: user.UserCID,
})
}
}
return ircContacts
}
func contactGroupPagerDutyToState(cg *api.ContactGroup) ([]interface{}, error) {
pdContacts := make([]interface{}, 0, len(cg.Contacts.External))
for _, ext := range cg.Contacts.External {
switch ext.Method {
case contactPagerDutyAttr:
pdInfo := contactPagerDutyInfo{}
if err := json.Unmarshal([]byte(ext.Info), &pdInfo); err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("unable to decode external %s JSON (%q): {{err}}", contactPagerDutyAttr, ext.Info), err)
}
pdContacts = append(pdContacts, map[string]interface{}{
string(contactContactGroupFallbackAttr): failoverGroupIDToCID(pdInfo.FallbackGroupCID),
string(contactPagerDutyIntegrationKeyAttr): pdInfo.IntegrationKey,
string(contactPagerDutyWebhookURLAttr): pdInfo.WebookURL,
})
}
}
return pdContacts, nil
}
func contactGroupSlackToState(cg *api.ContactGroup) ([]interface{}, error) {
slackContacts := make([]interface{}, 0, len(cg.Contacts.External))
for _, ext := range cg.Contacts.External {
switch ext.Method {
case contactSlackAttr:
slackInfo := contactSlackInfo{}
if err := json.Unmarshal([]byte(ext.Info), &slackInfo); err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("unable to decode external %s JSON (%q): {{err}}", contactSlackAttr, ext.Info), err)
}
slackContacts = append(slackContacts, map[string]interface{}{
contactContactGroupFallbackAttr: failoverGroupIDToCID(slackInfo.FallbackGroupCID),
contactSlackButtonsAttr: int(slackInfo.Buttons) == int(1),
contactSlackChannelAttr: slackInfo.Channel,
contactSlackTeamAttr: slackInfo.Team,
contactSlackUsernameAttr: slackInfo.Username,
})
}
}
return slackContacts, nil
}
func contactGroupSMSToState(cg *api.ContactGroup) ([]interface{}, error) {
smsContacts := make([]interface{}, 0, len(cg.Contacts.Users)+len(cg.Contacts.External))
for _, ext := range cg.Contacts.External {
switch ext.Method {
case contactSMSAttr:
smsContacts = append(smsContacts, map[string]interface{}{
contactSMSAddressAttr: ext.Info,
})
}
}
for _, user := range cg.Contacts.Users {
switch user.Method {
case contactSMSAttr:
smsContacts = append(smsContacts, map[string]interface{}{
contactUserCIDAttr: user.UserCID,
})
}
}
return smsContacts, nil
}
func contactGroupVictorOpsToState(cg *api.ContactGroup) ([]interface{}, error) {
victorOpsContacts := make([]interface{}, 0, len(cg.Contacts.External))
for _, ext := range cg.Contacts.External {
switch ext.Method {
case contactVictorOpsAttr:
victorOpsInfo := contactVictorOpsInfo{}
if err := json.Unmarshal([]byte(ext.Info), &victorOpsInfo); err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("unable to decode external %s JSON (%q): {{err}}", contactVictorOpsInfoAttr, ext.Info), err)
}
victorOpsContacts = append(victorOpsContacts, map[string]interface{}{
contactContactGroupFallbackAttr: failoverGroupIDToCID(victorOpsInfo.FallbackGroupCID),
contactVictorOpsAPIKeyAttr: victorOpsInfo.APIKey,
contactVictorOpsCriticalAttr: victorOpsInfo.Critical,
contactVictorOpsInfoAttr: victorOpsInfo.Info,
contactVictorOpsTeamAttr: victorOpsInfo.Team,
contactVictorOpsWarningAttr: victorOpsInfo.Warning,
})
}
}
return victorOpsContacts, nil
}
func contactGroupXMPPToState(cg *api.ContactGroup) ([]interface{}, error) {
xmppContacts := make([]interface{}, 0, len(cg.Contacts.Users)+len(cg.Contacts.External))
for _, ext := range cg.Contacts.External {
switch ext.Method {
case contactXMPPAttr:
xmppContacts = append(xmppContacts, map[string]interface{}{
contactXMPPAddressAttr: ext.Info,
})
}
}
for _, user := range cg.Contacts.Users {
switch user.Method {
case contactXMPPAttr:
xmppContacts = append(xmppContacts, map[string]interface{}{
contactUserCIDAttr: user.UserCID,
})
}
}
return xmppContacts, nil
}
// contactGroupAlertOptionsChecksum creates a stable hash of the normalized values
func contactGroupAlertOptionsChecksum(v interface{}) int {
m := v.(map[string]interface{})
b := &bytes.Buffer{}
b.Grow(defaultHashBufSize)
fmt.Fprintf(b, "%x", m[contactSeverityAttr].(int))
fmt.Fprint(b, normalizeTimeDurationStringToSeconds(m[contactEscalateAfterAttr]))
fmt.Fprint(b, m[contactEscalateToAttr])
fmt.Fprint(b, normalizeTimeDurationStringToSeconds(m[contactReminderAttr]))
return hashcode.String(b.String())
}