-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathresource_username_password_account.go
100 lines (78 loc) · 3.09 KB
/
resource_username_password_account.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
package octopusdeploy
import (
"context"
"log"
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceUsernamePasswordAccount() *schema.Resource {
return &schema.Resource{
CreateContext: resourceUsernamePasswordAccountCreate,
DeleteContext: resourceUsernamePasswordAccountDelete,
Description: "This resource manages username-password accounts in Octopus Deploy.",
Importer: getImporter(),
ReadContext: resourceUsernamePasswordAccountRead,
Schema: getUsernamePasswordAccountSchema(),
UpdateContext: resourceUsernamePasswordAccountUpdate,
}
}
func resourceUsernamePasswordAccountCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
account := expandUsernamePasswordAccount(d)
log.Printf("[INFO] creating username-password account: %#v", account)
client := m.(*octopusdeploy.Client)
createdAccount, err := client.Accounts.Add(account)
if err != nil {
return diag.FromErr(err)
}
if err := setUsernamePasswordAccount(ctx, d, createdAccount.(*octopusdeploy.UsernamePasswordAccount)); err != nil {
return diag.FromErr(err)
}
d.SetId(createdAccount.GetID())
log.Printf("[INFO] username-password account created (%s)", d.Id())
return nil
}
func resourceUsernamePasswordAccountDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] deleting username-password account (%s)", d.Id())
client := m.(*octopusdeploy.Client)
if err := client.Accounts.DeleteByID(d.Id()); err != nil {
return diag.FromErr(err)
}
d.SetId("")
log.Printf("[INFO] username-password account deleted")
return nil
}
func resourceUsernamePasswordAccountRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] reading username-password account (%s)", d.Id())
client := m.(*octopusdeploy.Client)
accountResource, err := client.Accounts.GetByID(d.Id())
if err != nil {
if apiError, ok := err.(*octopusdeploy.APIError); ok {
if apiError.StatusCode == 404 {
log.Printf("[INFO] username-password account (%s) not found; deleting from state", d.Id())
d.SetId("")
return nil
}
}
return diag.FromErr(err)
}
if err := setUsernamePasswordAccount(ctx, d, accountResource.(*octopusdeploy.UsernamePasswordAccount)); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] username-password account read: (%s)", d.Id())
return nil
}
func resourceUsernamePasswordAccountUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
account := expandUsernamePasswordAccount(d)
log.Printf("[INFO] updating username-password account: %#v", account)
client := m.(*octopusdeploy.Client)
updatedAccount, err := client.Accounts.Update(account)
if err != nil {
return diag.FromErr(err)
}
if err := setUsernamePasswordAccount(ctx, d, updatedAccount.(*octopusdeploy.UsernamePasswordAccount)); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] username-password account updated (%s)", d.Id())
return nil
}