-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathresource_feed.go
74 lines (60 loc) · 1.8 KB
/
resource_feed.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
package octopusdeploy
import (
"context"
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceFeed() *schema.Resource {
return &schema.Resource{
CreateContext: resourceFeedCreate,
DeleteContext: resourceFeedDelete,
Importer: getImporter(),
ReadContext: resourceFeedRead,
Schema: getFeedSchema(),
UpdateContext: resourceFeedUpdate,
}
}
func resourceFeedCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
feedResource := expandFeed(d)
client := m.(*octopusdeploy.Client)
createdFeed, err := client.Feeds.Add(feedResource)
if err != nil {
return diag.FromErr(err)
}
d.SetId(createdFeed.GetID())
return resourceFeedRead(ctx, d, m)
}
func resourceFeedDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*octopusdeploy.Client)
err := client.Feeds.DeleteByID(d.Id())
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
}
func resourceFeedRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*octopusdeploy.Client)
feed, err := client.Feeds.GetByID(d.Id())
if err != nil {
apiError := err.(*octopusdeploy.APIError)
if apiError.StatusCode == 404 {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
feedResource := feed.(*octopusdeploy.FeedResource)
setFeed(ctx, d, feedResource)
return nil
}
func resourceFeedUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
feedResource := expandFeed(d)
client := m.(*octopusdeploy.Client)
_, err := client.Feeds.Update(feedResource)
if err != nil {
return diag.FromErr(err)
}
return resourceFeedRead(ctx, d, m)
}