generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 44
/
resource_triggers.go
130 lines (107 loc) · 3.71 KB
/
resource_triggers.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
package triggers
import (
"context"
"net/http"
"github.com/antihax/optional"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceTriggers() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating triggers in Harness.",
ReadContext: resourceTriggersRead,
UpdateContext: resourceTriggersCreateOrUpdate,
CreateContext: resourceTriggersCreateOrUpdate,
DeleteContext: resourceTriggersDelete,
Importer: helpers.TriggerResourceImporter,
Schema: map[string]*schema.Schema{
"target_id": {
Description: "Identifier of the target pipeline",
Type: schema.TypeString,
Required: true,
},
"ignore_error": {
Description: "ignore error default false",
Type: schema.TypeBool,
Optional: true,
},
"yaml": {
Description: "trigger yaml." + helpers.Descriptions.YamlText.String(),
Type: schema.TypeString,
Required: true,
},
"if_match": {
Description: "if-Match",
Type: schema.TypeString,
Optional: true,
},
},
}
helpers.SetProjectLevelResourceSchema(resource.Schema)
return resource
}
func resourceTriggersRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
id := d.Id()
resp, httpResp, err := c.TriggersApi.GetTrigger(ctx, c.AccountId,
d.Get("org_id").(string),
d.Get("project_id").(string), d.Get("target_id").(string), id)
if httpResp.StatusCode == 404 {
d.SetId("")
d.MarkNewResource()
return nil
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readTriggers(d, resp.Data)
return nil
}
func resourceTriggersCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
var err error
var resp nextgen.ResponseDtongTriggerResponse
var httpResp *http.Response
id := d.Id()
if id == "" {
resp, httpResp, err = c.TriggersApi.CreateTrigger(ctx, d.Get("yaml").(string), c.AccountId,
d.Get("org_id").(string),
d.Get("project_id").(string),
d.Get("target_id").(string), &nextgen.TriggersApiCreateTriggerOpts{
WithServiceV2: optional.NewBool(true),
})
} else {
resp, httpResp, err = c.TriggersApi.UpdateTrigger(ctx, d.Get("yaml").(string), c.AccountId, d.Get("org_id").(string),
d.Get("project_id").(string),
d.Get("target_id").(string), id, &nextgen.TriggersApiUpdateTriggerOpts{
IfMatch: helpers.BuildField(d, "if_match"),
})
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readTriggers(d, resp.Data)
return nil
}
func resourceTriggersDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
_, httpResp, err := c.TriggersApi.DeleteTrigger(ctx, c.AccountId, d.Get("org_id").(string), d.Get("project_id").(string), d.Get("target_id").(string), d.Id(), &nextgen.TriggersApiDeleteTriggerOpts{
IfMatch: helpers.BuildField(d, "if_match"),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
return nil
}
func readTriggers(d *schema.ResourceData, trigger *nextgen.NgTriggerResponse) {
d.SetId(trigger.Identifier)
d.Set("identifier", trigger.Identifier)
d.Set("name", trigger.Name)
d.Set("org_id", trigger.OrgIdentifier)
d.Set("project_id", trigger.ProjectIdentifier)
d.Set("target_id", trigger.TargetIdentifier)
d.Set("yaml", trigger.Yaml)
}