generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
trigger_data_source.go
158 lines (146 loc) · 4.34 KB
/
trigger_data_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package trigger
import (
"context"
"strings"
"github.com/harness/harness-go-sdk/harness/cd/graphql"
"github.com/harness/terraform-provider-harness/internal"
"github.com/harness/terraform-provider-harness/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceTrigger() *schema.Resource {
return &schema.Resource{
Description: "Data source for retrieving a Harness trigger.",
ReadContext: dataSourceTriggerRead,
Schema: map[string]*schema.Schema{
"id": {
Description: "Unique identifier of the trigger.",
Type: schema.TypeString,
Optional: true,
},
"name": {
Description: "The name of the trigger.",
Type: schema.TypeString,
Optional: true,
},
"app_id": {
Description: "The id of the application.",
Type: schema.TypeString,
Optional: true,
},
"description": {
Description: "The trigger description.",
Type: schema.TypeString,
Optional: true,
},
"condition": {
Description: "The condition that will execute the Trigger: On new artifact, On pipeline completion, On Cron schedule, On webhook, On New Manifest.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"trigger_condition_type": {
Description: "Trigger condition.",
Type: schema.TypeString,
Computed: true,
},
"on_webhook": {
Description: "On webhook.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"webhook_details": {
Description: "Webhook details.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"header": {
Description: "Header.",
Type: schema.TypeString,
Computed: true,
},
"method": {
Description: "Method.",
Type: schema.TypeString,
Computed: true,
},
"payload": {
Description: "Payload.",
Type: schema.TypeString,
Computed: true,
},
"webhook_url": {
Description: "Webhook URL.",
Type: schema.TypeString,
Computed: true,
},
"webhook_token": {
Description: "Webhook token.",
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
},
},
},
},
}
}
func dataSourceTriggerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).CDClient
if c == nil {
return diag.Errorf(utils.CDClientAPIKeyError)
}
var trigger *graphql.Trigger
var err error
if id, ok := d.GetOk("id"); ok {
trigger, err = c.TriggerClient.GetTriggerById(id.(string))
} else if name, ok := d.GetOk("name"); ok {
trigger, err = c.TriggerClient.GetTriggerByName(name.(string), d.Get("app_id").(string))
} else {
return diag.Errorf("Must specify either `id` or `name`.")
}
if err != nil {
return diag.FromErr(err)
}
d.SetId(trigger.Id)
d.Set("name", trigger.Name)
d.Set("description", trigger.Description)
d.Set("condition", []map[string]interface{}{
{
"trigger_condition_type": trigger.Condition.TriggerConditionType,
"on_webhook": []map[string]interface{}{
{
"webhook_details": []map[string]interface{}{
{
"payload": trigger.Condition.WebhookDetails.Payload,
"header": trigger.Condition.WebhookDetails.Header,
"method": trigger.Condition.WebhookDetails.Method,
"webhook_url": trigger.Condition.WebhookDetails.WebhookUrl,
"webhook_token": getWebhookToken(trigger.Condition.WebhookDetails.WebhookUrl),
},
},
},
},
},
})
return nil
}
func getWebhookToken(webhookurl string) string {
first := strings.LastIndex(webhookurl, "webhooks/")
last := strings.LastIndex(webhookurl, "?accountId")
result := ""
for c, ch := range webhookurl {
if c > first+8 && c < last {
result = result + string(ch)
}
}
return result
}