/
resource_channel.go
104 lines (80 loc) · 2.76 KB
/
resource_channel.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/channels"
"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 resourceChannel() *schema.Resource {
return &schema.Resource{
CreateContext: resourceChannelCreate,
DeleteContext: resourceChannelDelete,
Description: "This resource manages channels in Octopus Deploy.",
Importer: getImporter(),
ReadContext: resourceChannelRead,
Schema: getChannelSchema(),
UpdateContext: resourceChannelUpdate,
}
}
func resourceChannelCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()
channel := expandChannel(d)
tflog.Info(ctx, fmt.Sprintf("creating channel: %#v", channel))
client := m.(*client.Client)
createdChannel, err := channels.Add(client, channel)
if err != nil {
return diag.FromErr(err)
}
if err := setChannel(ctx, d, createdChannel); err != nil {
return diag.FromErr(err)
}
d.SetId(createdChannel.GetID())
tflog.Info(ctx, fmt.Sprintf("channel created (%s)", d.Id()))
return nil
}
func resourceChannelDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()
tflog.Info(ctx, fmt.Sprintf("deleting channel (%s)", d.Id()))
client := m.(*client.Client)
if err := channels.DeleteByID(client, d.Get("space_id").(string), d.Id()); err != nil {
return diag.FromErr(err)
}
d.SetId("")
tflog.Info(ctx, "channel deleted")
return nil
}
func resourceChannelRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
tflog.Info(ctx, fmt.Sprintf("reading channel (%s)", d.Id()))
client := m.(*client.Client)
channel, err := channels.GetByID(client, d.Get("space_id").(string), d.Id())
if err != nil {
return errors.ProcessApiError(ctx, d, err, "channel")
}
if err := setChannel(ctx, d, channel); err != nil {
return diag.FromErr(err)
}
tflog.Info(ctx, fmt.Sprintf("channel read (%s)", d.Id()))
return nil
}
func resourceChannelUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()
tflog.Info(ctx, fmt.Sprintf("updating channel (%s)", d.Id()))
channel := expandChannel(d)
client := m.(*client.Client)
updatedChannel, err := channels.Update(client, channel)
if err != nil {
return diag.FromErr(err)
}
if err := setChannel(ctx, d, updatedChannel); err != nil {
return diag.FromErr(err)
}
tflog.Info(ctx, fmt.Sprintf("channel updated (%s)", d.Id()))
return nil
}