Skip to content

Commit e696479

Browse files
feat: pre-defined templates and code engine templates support (IBM-Cloud#6398)
* feat: pre-defined templates and cpde engine templates support * fix: topic resource update to mark starts_at and ends_at parameters as optional
1 parent 0f4ad1d commit e696479

22 files changed

+2046
-7
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/IBM/configuration-aggregator-go-sdk v0.0.2
1616
github.com/IBM/container-registry-go-sdk v1.1.0
1717
github.com/IBM/continuous-delivery-go-sdk/v2 v2.0.3
18-
github.com/IBM/event-notifications-go-admin-sdk v0.15.0
18+
github.com/IBM/event-notifications-go-admin-sdk v0.18.0
1919
github.com/IBM/eventstreams-go-sdk v1.4.0
2020
github.com/IBM/go-sdk-core/v5 v5.20.1
2121
github.com/IBM/ibm-backup-recovery-sdk-go v1.0.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ github.com/IBM/container-registry-go-sdk v1.1.0 h1:sYyknIod8R4RJZQqAheiduP6wbSTp
116116
github.com/IBM/container-registry-go-sdk v1.1.0/go.mod h1:4TwsCnQtVfZ4Vkapy/KPvQBKFc3VOyUZYkwRU4FTPrs=
117117
github.com/IBM/continuous-delivery-go-sdk/v2 v2.0.3 h1:5oyqzqwywzVef30h1qjNEBcgxOvdDiX43ppsZyCTwik=
118118
github.com/IBM/continuous-delivery-go-sdk/v2 v2.0.3/go.mod h1:Iibf+4gRasEZHakDgnMm7TOX87xex0L+DfghUclcmcg=
119-
github.com/IBM/event-notifications-go-admin-sdk v0.15.0 h1:5rmEbZmGhJiS+WZOK+ABp83nXmlIG5nLL0b1dCswO1Y=
120-
github.com/IBM/event-notifications-go-admin-sdk v0.15.0/go.mod h1:9fG6k1gYRqiChZvmn1iRIjaKCXuDBCOlPH6xPJN+mMU=
119+
github.com/IBM/event-notifications-go-admin-sdk v0.18.0 h1:G4230wYVCjHnHFEcingfMVznjSJeUoSWzyEMsEFYCmo=
120+
github.com/IBM/event-notifications-go-admin-sdk v0.18.0/go.mod h1:9fG6k1gYRqiChZvmn1iRIjaKCXuDBCOlPH6xPJN+mMU=
121121
github.com/IBM/eventstreams-go-sdk v1.4.0 h1:yS/Ns29sBOe8W2tynQmz9HTKqQZ0ckse4Py5Oy/F2rM=
122122
github.com/IBM/eventstreams-go-sdk v1.4.0/go.mod h1:2tuAxaYLctfqfr5jvyqSrxxEQGMwYPm3yJGWSj85YVQ=
123123
github.com/IBM/go-sdk-core/v5 v5.0.0/go.mod h1:vyNdbFujJtdTj9HbihtvKwwS3k/GKSKpOx9ZIQ6MWDY=

ibm/provider/provider.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ func Provider() *schema.Provider {
984984
"ibm_en_destination_sn": eventnotification.DataSourceIBMEnServiceNowDestination(),
985985
"ibm_en_subscription_sn": eventnotification.DataSourceIBMEnFCMSubscription(),
986986
"ibm_en_destination_ce": eventnotification.DataSourceIBMEnCodeEngineDestination(),
987-
"ibm_en_subscription_ce": eventnotification.DataSourceIBMEnFCMSubscription(),
987+
"ibm_en_subscription_ce": eventnotification.DataSourceIBMEnCodeEngineSubscription(),
988988
"ibm_en_destination_cos": eventnotification.DataSourceIBMEnCOSDestination(),
989989
"ibm_en_subscription_cos": eventnotification.DataSourceIBMEnFCMSubscription(),
990990
"ibm_en_destination_huawei": eventnotification.DataSourceIBMEnHuaweiDestination(),
@@ -1010,6 +1010,9 @@ func Provider() *schema.Provider {
10101010
"ibm_en_destination_event_streams": eventnotification.DataSourceIBMEnEventStreamsDestination(),
10111011
"ibm_en_subscription_event_streams": eventnotification.DataSourceIBMEnEventStreamsSubscription(),
10121012
"ibm_en_event_streams_template": eventnotification.DataSourceIBMEnEventStreamsTemplate(),
1013+
"ibm_en_pre_defined_template": eventnotification.DataSourceIBMEnPreDefinedTemplate(),
1014+
"ibm_en_pre_defined_templates": eventnotification.DataSourceIBMEnPreDefinedTemplates(),
1015+
"ibm_en_code_engine_template": eventnotification.DataSourceIBMEnCodeEngineTemplate(),
10131016

10141017
// Added for Toolchain
10151018
"ibm_cd_toolchain": cdtoolchain.DataSourceIBMCdToolchain(),
@@ -1662,7 +1665,7 @@ func Provider() *schema.Provider {
16621665
"ibm_en_destination_sn": eventnotification.ResourceIBMEnServiceNowDestination(),
16631666
"ibm_en_subscription_sn": eventnotification.ResourceIBMEnFCMSubscription(),
16641667
"ibm_en_destination_ce": eventnotification.ResourceIBMEnCodeEngineDestination(),
1665-
"ibm_en_subscription_ce": eventnotification.ResourceIBMEnFCMSubscription(),
1668+
"ibm_en_subscription_ce": eventnotification.ResourceIBMEnCodeEngineSubscription(),
16661669
"ibm_en_destination_cos": eventnotification.ResourceIBMEnCOSDestination(),
16671670
"ibm_en_subscription_cos": eventnotification.ResourceIBMEnFCMSubscription(),
16681671
"ibm_en_destination_huawei": eventnotification.ResourceIBMEnHuaweiDestination(),
@@ -1684,6 +1687,7 @@ func Provider() *schema.Provider {
16841687
"ibm_en_destination_event_streams": eventnotification.ResourceIBMEnEventStreamsDestination(),
16851688
"ibm_en_subscription_event_streams": eventnotification.ResourceIBMEnEventStreamsSubscription(),
16861689
"ibm_en_event_streams_template": eventnotification.ResourceIBMEnEventStreamsTemplate(),
1690+
"ibm_en_code_engine_template": eventnotification.ResourceIBMEnCodeEngineTemplate(),
16871691

16881692
// Added for Toolchain
16891693
"ibm_cd_toolchain": cdtoolchain.ResourceIBMCdToolchain(),
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// Copyright IBM Corp. 2021 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package eventnotification
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"log"
10+
11+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
12+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
14+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
15+
16+
en "github.com/IBM/event-notifications-go-admin-sdk/eventnotificationsv1"
17+
)
18+
19+
func DataSourceIBMEnCodeEngineSubscription() *schema.Resource {
20+
return &schema.Resource{
21+
ReadContext: dataSourceIBMEnCodeEngineSubscriptionRead,
22+
23+
Schema: map[string]*schema.Schema{
24+
"instance_guid": {
25+
Type: schema.TypeString,
26+
Required: true,
27+
Description: "Unique identifier for IBM Cloud Event Notifications instance.",
28+
},
29+
"subscription_id": {
30+
Type: schema.TypeString,
31+
Required: true,
32+
Description: "Unique identifier for result.",
33+
},
34+
"name": {
35+
Type: schema.TypeString,
36+
Computed: true,
37+
Description: "Subscription name.",
38+
},
39+
"description": {
40+
Type: schema.TypeString,
41+
Computed: true,
42+
Description: "Subscription description.",
43+
},
44+
"destination_id": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
Description: "The destination ID.",
48+
},
49+
"topic_id": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
Description: "Topic ID.",
53+
},
54+
"attributes": {
55+
Type: schema.TypeList,
56+
Computed: true,
57+
Elem: &schema.Resource{
58+
Schema: map[string]*schema.Schema{
59+
"template_id_notification": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
Description: "The templete id for notification",
63+
},
64+
},
65+
},
66+
},
67+
"updated_at": {
68+
Type: schema.TypeString,
69+
Computed: true,
70+
Description: "Last updated time.",
71+
},
72+
},
73+
}
74+
}
75+
76+
func dataSourceIBMEnCodeEngineSubscriptionRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
77+
enClient, err := meta.(conns.ClientSession).EventNotificationsApiV1()
78+
if err != nil {
79+
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_en_subscription_ce", "read")
80+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
81+
return tfErr.GetDiag()
82+
}
83+
84+
getSubscriptionOptions := &en.GetSubscriptionOptions{}
85+
86+
getSubscriptionOptions.SetInstanceID(d.Get("instance_guid").(string))
87+
getSubscriptionOptions.SetID(d.Get("subscription_id").(string))
88+
89+
result, _, err := enClient.GetSubscriptionWithContext(context, getSubscriptionOptions)
90+
if err != nil {
91+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetIntegrationWithContext failed: %s", err.Error()), "(Data) ibm_en_subscription_ce", "read")
92+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
93+
return tfErr.GetDiag()
94+
}
95+
96+
d.SetId(fmt.Sprintf("%s/%s", *getSubscriptionOptions.InstanceID, *getSubscriptionOptions.ID))
97+
98+
if err = d.Set("name", result.Name); err != nil {
99+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting name: %s", err), "(Data) ibm_en_subscription_ce", "read")
100+
return tfErr.GetDiag()
101+
}
102+
103+
if result.Description != nil {
104+
if err = d.Set("description", result.Description); err != nil {
105+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting description: %s", err), "(Data) ibm_en_subscription_ce", "read")
106+
return tfErr.GetDiag()
107+
}
108+
}
109+
if err = d.Set("updated_at", result.UpdatedAt); err != nil {
110+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting updated_at: %s", err), "(Data) ibm_en_subscription_ce", "read")
111+
return tfErr.GetDiag()
112+
}
113+
114+
if err = d.Set("destination_id", result.DestinationID); err != nil {
115+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting destination_id: %s", err), "(Data) ibm_en_subscription_ce", "read")
116+
return tfErr.GetDiag()
117+
}
118+
119+
if err = d.Set("topic_id", result.TopicID); err != nil {
120+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting topic_id: %s", err), "(Data) ibm_en_subscription_ce", "read")
121+
return tfErr.GetDiag()
122+
}
123+
124+
if result.Attributes != nil {
125+
if err = d.Set("attributes", enCodeEngineSubscriptionFlattenAttributes(result.Attributes)); err != nil {
126+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting attributes: %s", err), "(Data) ibm_en_subscription_ce", "read")
127+
return tfErr.GetDiag()
128+
}
129+
}
130+
131+
return nil
132+
}
133+
134+
func enCodeEngineSubscriptionFlattenAttributes(result en.SubscriptionAttributesIntf) (finalList []map[string]interface{}) {
135+
finalList = []map[string]interface{}{}
136+
137+
attributes := result.(*en.SubscriptionAttributes)
138+
139+
finalMap := enCodeEngineSubscriptionToMap(attributes)
140+
finalList = append(finalList, finalMap)
141+
142+
return finalList
143+
}
144+
145+
func enCodeEngineSubscriptionToMap(attributeItem *en.SubscriptionAttributes) (attributeMap map[string]interface{}) {
146+
attributeMap = map[string]interface{}{}
147+
148+
if attributeItem.TemplateIDNotification != nil {
149+
attributeMap["template_id_notification"] = attributeItem.TemplateIDNotification
150+
}
151+
152+
return attributeMap
153+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright IBM Corp. 2021 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package eventnotification_test
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
11+
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
14+
)
15+
16+
func TestAccIBMEnCodeEngineSubscriptionDataSourceAllArgs(t *testing.T) {
17+
instanceName := fmt.Sprintf("tf_instance_%d", acctest.RandIntRange(10, 100))
18+
name := fmt.Sprintf("tf_name_%d", acctest.RandIntRange(10, 100))
19+
description := fmt.Sprintf("tf_description_%d", acctest.RandIntRange(10, 100))
20+
21+
resource.Test(t, resource.TestCase{
22+
PreCheck: func() { acc.TestAccPreCheck(t) },
23+
Providers: acc.TestAccProviders,
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testAccCheckIBMEnCodeEngineSubscriptionDataSourceConfig(instanceName, name, description),
27+
Check: resource.ComposeTestCheckFunc(
28+
resource.TestCheckResourceAttrSet("data.ibm_en_subscription_ce.data_subscription_1", "id"),
29+
resource.TestCheckResourceAttrSet("data.ibm_en_subscription_ce.data_subscription_1", "instance_guid"),
30+
resource.TestCheckResourceAttrSet("data.ibm_en_subscription_ce.data_subscription_1", "subscription_id"),
31+
resource.TestCheckResourceAttrSet("data.ibm_en_subscription_ce.data_subscription_1", "name"),
32+
resource.TestCheckResourceAttrSet("data.ibm_en_subscription_ce.data_subscription_1", "description"),
33+
resource.TestCheckResourceAttrSet("data.ibm_en_subscription_ce.data_subscription_1", "updated_at"),
34+
resource.TestCheckResourceAttrSet("data.ibm_en_subscription_ce.data_subscription_1", "destination_type"),
35+
resource.TestCheckResourceAttrSet("data.ibm_en_subscription_ce.data_subscription_1", "destination_id"),
36+
resource.TestCheckResourceAttrSet("data.ibm_en_subscription_ce.data_subscription_1", "destination_name"),
37+
resource.TestCheckResourceAttrSet("data.ibm_en_subscription_ce.data_subscription_1", "topic_id"),
38+
resource.TestCheckResourceAttrSet("data.ibm_en_subscription_ce.data_subscription_1", "topic_name"),
39+
),
40+
},
41+
},
42+
})
43+
}
44+
45+
func testAccCheckIBMEnCodeEngineSubscriptionDataSourceConfig(instanceName, name, description string) string {
46+
return fmt.Sprintf(`
47+
resource "ibm_resource_instance" "en_subscription_datasource" {
48+
name = "%s"
49+
location = "us-south"
50+
plan = "standard"
51+
service = "event-notifications"
52+
}
53+
54+
resource "ibm_en_topic" "en_topic_resource_4" {
55+
instance_guid = ibm_resource_instance.en_subscription_datasource.guid
56+
name = "tf_topic_name_0664"
57+
description = "tf_topic_description_0455"
58+
}
59+
60+
resource "ibm_en_destination_ce" "en_destination_resource_2" {
61+
instance_guid = ibm_resource_instance.en_subscription_resource.guid
62+
name = "code_engine_destination"
63+
type = "ibmce"
64+
description = "code engine destination tf"
65+
config {
66+
params {
67+
type = "application"
68+
verb = "POST"
69+
url = "https://test.codetestcodeengine.com"
70+
}
71+
}
72+
}
73+
resource "ibm_en_subscription_ce" "en_subscription_resource_1" {
74+
name = "%s"
75+
description = "%s"
76+
instance_guid = ibm_resource_instance.en_subscription_resource.guid
77+
topic_id = ibm_en_topic.en_topic_resource_2.topic_id
78+
destination_id = ibm_en_destination_ce.en_destination_resource_2.destination_id
79+
}
80+
81+
data "ibm_en_subscription_ce" "data_subscription_1" {
82+
instance_guid = ibm_resource_instance.en_subscription_datasource.guid
83+
subscription_id = ibm_en_subscription_ce.en_subscription_resource_4.subscription_id
84+
}
85+
86+
`, instanceName, name, description)
87+
}

0 commit comments

Comments
 (0)