/
resource_runbook.go
104 lines (81 loc) · 2.91 KB
/
resource_runbook.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
package octopusdeploy
import (
"context"
"fmt"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/runbooks"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/internal/errors"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceRunbook() *schema.Resource {
return &schema.Resource{
CreateContext: resourceRunbookCreate,
DeleteContext: resourceRunbookDelete,
Description: "This resource manages runbooks in Octopus Deploy.",
Importer: getImporter(),
ReadContext: resourceRunbookRead,
Schema: getRunbookSchema(),
UpdateContext: resourceRunbookUpdate,
}
}
func resourceRunbookCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
runbook := expandRunbook(ctx, d)
tflog.Info(ctx, fmt.Sprintf("creating runbook (%s)", runbook.Name))
client := m.(*client.Client)
createdRunbook, err := runbooks.Add(client, runbook)
if err != nil {
return diag.FromErr(err)
}
if err := setRunbook(ctx, d, createdRunbook); err != nil {
return diag.FromErr(err)
}
d.SetId(createdRunbook.GetID())
tflog.Info(ctx, fmt.Sprintf("runbook created (%s)", d.Id()))
return nil
}
func resourceRunbookDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
tflog.Info(ctx, fmt.Sprintf("deleting runbook (%s)", d.Id()))
client := m.(*client.Client)
if err := runbooks.DeleteByID(client, d.Get("space_id").(string), d.Id()); err != nil {
return diag.FromErr(err)
}
tflog.Info(ctx, fmt.Sprintf("runbook deleted (%s)", d.Id()))
d.SetId("")
return nil
}
func resourceRunbookRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
tflog.Info(ctx, fmt.Sprintf("reading runbook (%s)", d.Id()))
client := m.(*client.Client)
runbook, err := runbooks.GetByID(client, d.Get("space_id").(string), d.Id())
if err != nil {
return errors.ProcessApiError(ctx, d, err, "runbook")
}
if err := setRunbook(ctx, d, runbook); err != nil {
return diag.FromErr(err)
}
tflog.Info(ctx, fmt.Sprintf("runbook read (%s)", d.Id()))
return nil
}
func resourceRunbookUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
tflog.Info(ctx, fmt.Sprintf("updating runbook (%s)", d.Id()))
client := m.(*client.Client)
runbook := expandRunbook(ctx, d)
var updatedRunbook *runbooks.Runbook
var err error
runbookLinks, err := runbooks.GetByID(client, d.Get("space_id").(string), d.Id())
if err != nil {
return diag.FromErr(err)
}
runbook.Links = runbookLinks.Links
updatedRunbook, err = runbooks.Update(client, runbook)
if err != nil {
return diag.FromErr(err)
}
if err := setRunbook(ctx, d, updatedRunbook); err != nil {
return diag.FromErr(err)
}
tflog.Info(ctx, fmt.Sprintf("runbook updated (%s)", d.Id()))
return nil
}