Skip to content

Commit

Permalink
updated resources, data source and documentation for Slack Direct des…
Browse files Browse the repository at this point in the history
…tination support
  • Loading branch information
Divya-Singh1693 authored and Srikant Sahu committed Sep 24, 2024
1 parent bdc87d9 commit 44face1
Show file tree
Hide file tree
Showing 16 changed files with 273 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/IBM/code-engine-go-sdk v0.0.0-20240126185534-a6e054aa01ed
github.com/IBM/container-registry-go-sdk v1.1.0
github.com/IBM/continuous-delivery-go-sdk v1.6.0
github.com/IBM/event-notifications-go-admin-sdk v0.8.0
github.com/IBM/event-notifications-go-admin-sdk v0.9.0
github.com/IBM/eventstreams-go-sdk v1.4.0
github.com/IBM/go-sdk-core v1.1.0
github.com/IBM/go-sdk-core/v3 v3.2.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ github.com/IBM/continuous-delivery-go-sdk v1.6.0 h1:eAL/jIWHrDFlWDF+Qd9Y5UN99Pr5
github.com/IBM/continuous-delivery-go-sdk v1.6.0/go.mod h1:nZdKUnubXNLo+zo28R4Rd+TGDqiJ/xoE8WO/A3kLw1E=
github.com/IBM/event-notifications-go-admin-sdk v0.8.0 h1:xk2CYTayQtKi6LSgGGFRxFJfWUxyM5SY8Rs64ducAhw=
github.com/IBM/event-notifications-go-admin-sdk v0.8.0/go.mod h1:OByvqfrNVxs7G6ggv8pwQCEVw10/TBJCLh7NM3z707w=
github.com/IBM/event-notifications-go-admin-sdk v0.9.0 h1:eaCd+GkxhNyot+8rA9WkAQdlVYrRD20LYiXjEytFO6M=
github.com/IBM/event-notifications-go-admin-sdk v0.9.0/go.mod h1:OByvqfrNVxs7G6ggv8pwQCEVw10/TBJCLh7NM3z707w=
github.com/IBM/eventstreams-go-sdk v1.4.0 h1:yS/Ns29sBOe8W2tynQmz9HTKqQZ0ckse4Py5Oy/F2rM=
github.com/IBM/eventstreams-go-sdk v1.4.0/go.mod h1:2tuAxaYLctfqfr5jvyqSrxxEQGMwYPm3yJGWSj85YVQ=
github.com/IBM/go-sdk-core v1.1.0 h1:pV73lZqr9r1xKb3h08c1uNG3AphwoV5KzUzhS+pfEqY=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func DataSourceIBMEnCFDestination() *schema.Resource {
},
},
},
DeprecationMessage: "The IBM Cloud Function destination has been deprectated",
DeprecationMessage: "datasource is depreacted due to cloud function servie depreaction",
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ func DataSourceIBMEnSlackDestination() *schema.Resource {
Computed: true,
Description: "Slack webhook url",
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "The Slack Destination type incoming_webhook/direct_message",
},
"token": {
Type: schema.TypeString,
Computed: true,
Description: "Slack Bot token. Required in case of type is direct_message",
},
},
},
},
Expand Down Expand Up @@ -184,5 +194,13 @@ func enSlackDestinationConfigParamsToMap(paramsItem en.DestinationConfigOneOfInt
if params.URL != nil {
paramsMap["url"] = params.URL
}

if params.Type != nil {
paramsMap["type"] = params.Type
}

if params.Token != nil {
paramsMap["token"] = params.Token
}
return paramsMap
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func testAccCheckIBMEnSlackDestinationDataSourceConfigBasic(instanceName, name,
description = "%s"
config {
params {
type = "incoming_webhook"
url = "https://hooks.slack.com/services/G0gyhsush/TYodsjhs/GHTbfidsimkk"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ func DataSourceIBMEnSlackSubscription() *schema.Resource {
Optional: true,
Description: "attachment color code",
},
"channels": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "List of channels.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "channel id.",
},
},
},
},
},
},
},
Expand Down Expand Up @@ -137,5 +151,23 @@ func enSlackSubscriptionToMap(attributeItem *en.SubscriptionAttributes) (attribu
attributeMap["attachment_color"] = attributeItem.AttachmentColor
}

if attributeItem.Channels != nil {
channels := []map[string]interface{}{}
for _, channelsItem := range attributeItem.Channels {
channelsItemMap, err := dataSourceIBMEnSubscriptionChannelCreateAttributesToMap(&channelsItem)
if err != nil {
return attributeMap
}
channels = append(channels, channelsItemMap)
}
attributeMap["channels"] = channels
}

return attributeMap
}

func dataSourceIBMEnSubscriptionChannelCreateAttributesToMap(model *en.ChannelCreateAttributes) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["id"] = model.ID
return modelMap, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func testAccCheckIBMEnSlackSubscriptionDataSourceConfig(instanceName, name, desc
description = "tf_destinatios_description_0364"
config {
params {
url = "https://hooks.slack.com/services/G0gyhsush/TYodsjhs/GHTbfidsimkk"
type = "direct_message"
token = "dsgdewgfpwfgpewweeffewsgcvsaksdksff"
}
}
}
Expand All @@ -76,7 +77,10 @@ func testAccCheckIBMEnSlackSubscriptionDataSourceConfig(instanceName, name, desc
topic_id = ibm_en_topic.en_topic_resource_4.topic_id
destination_id = ibm_en_destination_slack.en_destination_resource_4.destination_id
attributes {
attachment_color = "#0000FF"
channels {
id = "GFDRTUIIOBVD"
}
}
}
Expand Down
38 changes: 34 additions & 4 deletions ibm/service/eventnotification/resource_ibm_en_destination_slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,19 @@ func ResourceIBMEnSlackDestination() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Optional: true,
Description: "Slack webhook url. Required in case of type is incoming_webhook",
},
"type": {
Type: schema.TypeString,
Required: true,
Description: "Slack webhook url.",
Description: "The Slack Destination type incoming_webhook/direct_message",
},
"token": {
Type: schema.TypeString,
Optional: true,
Description: "Slack Bot token. Required in case of type is direct_message",
},
},
},
Expand Down Expand Up @@ -182,7 +192,7 @@ func resourceIBMEnSlackDestinationRead(context context.Context, d *schema.Resour
}

if result.Config != nil {
err = d.Set("config", enWebhookDestinationFlattenConfig(*result.Config))
err = d.Set("config", enSlackDestinationFlattenConfig(*result.Config))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting config %s", err))
}
Expand Down Expand Up @@ -279,8 +289,28 @@ func resourceIBMEnSlackDestinationDelete(context context.Context, d *schema.Reso

func SlackdestinationConfigMapToDestinationConfig(configParams map[string]interface{}, destinationtype string) en.DestinationConfig {
params := new(en.DestinationConfigOneOf)
if configParams["url"] != nil {
params.URL = core.StringPtr(configParams["url"].(string))

params.Type = core.StringPtr(configParams["type"].(string))

if *params.Type == "incoming_webhook" {

if configParams["url"] != nil {
params.URL = core.StringPtr(configParams["url"].(string))
}

if configParams["type"] != nil {
params.Type = core.StringPtr(configParams["type"].(string))
}

} else {

if configParams["token"] != nil {
params.Token = core.StringPtr(configParams["token"].(string))
}

if configParams["type"] != nil {
params.Type = core.StringPtr(configParams["type"].(string))
}
}

destinationConfig := new(en.DestinationConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func testAccCheckIBMEnSlackDestinationConfig(instanceName, name, description str
description = "%s"
config {
params {
type = "incoming_webhook"
url = "https://hooks.slack.com/services/G0gyhsush/TYodsjhs/GHTbfidsimkk"
}
}
Expand Down
97 changes: 86 additions & 11 deletions ibm/service/eventnotification/resource_ibm_en_subscription_slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ func ResourceIBMEnSlackSubscription() *schema.Resource {
Optional: true,
Description: "The templete id for notification",
},
"channels": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Description: "List of channels.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "channel id.",
},
"operation": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The channel operation type. The values are add/remove",
},
},
},
},
},
},
},
Expand Down Expand Up @@ -235,7 +254,10 @@ func resourceIBMEnSlackSubscriptionUpdate(context context.Context, d *schema.Res
options.SetDescription(d.Get("description").(string))
}

_, attributes := slackattributesMapToAttributes(d.Get("attributes.0").(map[string]interface{}))
attributes, err := resourceIBMEnSubscriptionMapToSubscriptionUpdateAttributes(d.Get("attributes.0").(map[string]interface{}))
if err != nil {
return diag.FromErr(err)
}
options.SetAttributes(&attributes)

_, response, err := enClient.UpdateSubscriptionWithContext(context, options)
Expand Down Expand Up @@ -279,19 +301,72 @@ func resourceIBMEnSlackSubscriptionDelete(context context.Context, d *schema.Res
return nil
}

func slackattributesMapToAttributes(attributeMap map[string]interface{}) (en.SubscriptionCreateAttributes, en.SubscriptionUpdateAttributesSlackAttributes) {
attributesCreate := en.SubscriptionCreateAttributes{}
attributesUpdate := en.SubscriptionUpdateAttributesSlackAttributes{}
func slackattributesMapToAttributes(modelMap map[string]interface{}) (en.SubscriptionCreateAttributes, error) {
model := en.SubscriptionCreateAttributes{}

if modelMap["template_id_notification"] != nil && modelMap["template_id_notification"].(string) != "" {
model.TemplateIDNotification = core.StringPtr(modelMap["template_id_notification"].(string))
}
if modelMap["template_id_invitation"] != nil && modelMap["template_id_invitation"].(string) != "" {
model.TemplateIDInvitation = core.StringPtr(modelMap["template_id_invitation"].(string))
}

if modelMap["attachment_color"] != nil && modelMap["attachment_color"].(string) != "" {
model.AttachmentColor = core.StringPtr(modelMap["attachment_color"].(string))
}
if modelMap["channels"] != nil {
channels := []en.ChannelCreateAttributes{}
for _, channelsItem := range modelMap["channels"].([]interface{}) {
channelsItemModel, err := resourceIBMEnSubscriptionMapToChannelCreateAttributes(channelsItem.(map[string]interface{}))
if err != nil {
return model, err
}
channels = append(channels, *channelsItemModel)
}
model.Channels = channels
}
return model, nil
}

func resourceIBMEnSubscriptionMapToChannelCreateAttributes(modelMap map[string]interface{}) (*en.ChannelCreateAttributes, error) {
model := &en.ChannelCreateAttributes{}
model.ID = core.StringPtr(modelMap["id"].(string))
return model, nil
}

func resourceIBMEnSubscriptionMapToSubscriptionUpdateAttributes(modelMap map[string]interface{}) (en.SubscriptionUpdateAttributes, error) {
model := en.SubscriptionUpdateAttributes{}

if attributeMap["attachment_color"] != nil {
attributesCreate.AttachmentColor = core.StringPtr(attributeMap["attachment_color"].(string))
attributesUpdate.AttachmentColor = core.StringPtr(attributeMap["attachment_color"].(string))
if modelMap["template_id_notification"] != nil && modelMap["template_id_notification"].(string) != "" {
model.TemplateIDNotification = core.StringPtr(modelMap["template_id_notification"].(string))
}

if attributeMap["template_id_notification"] != nil {
attributesCreate.TemplateIDNotification = core.StringPtr(attributeMap["template_id_notification"].(string))
attributesUpdate.TemplateIDNotification = core.StringPtr(attributeMap["template_id_notification"].(string))
if modelMap["attachment_color"] != nil && modelMap["attachment_color"].(string) != "" {
model.AttachmentColor = core.StringPtr(modelMap["attachment_color"].(string))
}
if modelMap["channels"] != nil {
channels := []en.ChannelUpdateAttributes{}
for _, channelsItem := range modelMap["channels"].([]interface{}) {
channelsItemModel, err := resourceIBMEnSubscriptionMapToChannelUpdateAttributes(channelsItem.(map[string]interface{}))
if err != nil {
return model, err
}
channels = append(channels, *channelsItemModel)
}
model.Channels = channels
}
return model, nil
}

func resourceIBMEnSubscriptionChannelCreateAttributesToMap(model *en.ChannelCreateAttributes) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["id"] = model.ID
return modelMap, nil
}

return attributesCreate, attributesUpdate
func resourceIBMEnSubscriptionMapToChannelUpdateAttributes(modelMap map[string]interface{}) (*en.ChannelUpdateAttributes, error) {
model := &en.ChannelUpdateAttributes{}
model.ID = core.StringPtr(modelMap["id"].(string))
model.Operation = core.StringPtr(modelMap["operation"].(string))
return model, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func testAccCheckIBMEnSlackSubscriptionConfig(instanceName, name, description st
description = "tf_destinatios_description_0364"
config {
params {
"url = "https://hooks.slack.com/services/G0gyhsush/TYodsjhs/GHTbfidsimkk""
type = "incoming_webhook"
url = "https://hooks.slack.com/services/G0gyhsush/TYodsjhs/GHTbfidsimkk"
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/en_destination_slack.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ In addition to all argument references listed, you can access the following attr

Nested scheme for **params**:

- `type` - (String) The type of slack destination. The supported values are incoming_webhook and direct_message
- `url` - (String) Slack Webhook url.
- `token`- (String) Token of slack application

- `updated_at` - (String) Last updated time.
2 changes: 2 additions & 0 deletions website/docs/d/en_subscription_slack.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ In addition to all argument references listed, you can access the following attr
- `attributes` - (Required, List)

- `attachment_color` - (String) The color code for slack attachment .
- `template_id_notification` - (String) The templete id for notification
- `channels` - (List) List of channels

- `updated_at` - (String) Last updated time.
Loading

0 comments on commit 44face1

Please sign in to comment.