-
Notifications
You must be signed in to change notification settings - Fork 65
/
resource_sshkey_account.go
109 lines (86 loc) · 3.1 KB
/
resource_sshkey_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
101
102
103
104
105
106
107
108
109
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 resourceSSHKeyAccount() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSSHKeyAccountCreate,
DeleteContext: resourceSSHKeyAccountDelete,
Description: "This resource manages SSH key accounts in Octopus Deploy.",
Importer: getImporter(),
ReadContext: resourceSSHKeyAccountRead,
Schema: getSSHKeyAccountSchema(),
UpdateContext: resourceSSHKeyAccountUpdate,
}
}
func resourceSSHKeyAccountCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
account := expandSSHKeyAccount(d)
log.Printf("[INFO] creating SSH key account: %#v", account)
client := m.(*octopusdeploy.Client)
createdAccount, err := client.Accounts.Add(account)
if err != nil {
return diag.FromErr(err)
}
if err := setSSHKeyAccount(ctx, d, createdAccount.(*octopusdeploy.SSHKeyAccount)); err != nil {
return diag.FromErr(err)
}
d.SetId(createdAccount.GetID())
log.Printf("[INFO] SSH key account created (%s)", d.Id())
return nil
}
func resourceSSHKeyAccountDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] deleting SSH key 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] SSH key account deleted")
return nil
}
func resourceSSHKeyAccountRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] reading SSH key 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] SSH key account (%s) not found; deleting from state", d.Id())
d.SetId("")
return nil
}
}
return diag.FromErr(err)
}
accountResource, err = octopusdeploy.ToAccount(accountResource.(*octopusdeploy.AccountResource))
if err != nil {
return diag.FromErr(err)
}
if err := setSSHKeyAccount(ctx, d, accountResource.(*octopusdeploy.SSHKeyAccount)); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] SSH key account read (%s)", d.Id())
return nil
}
func resourceSSHKeyAccountUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] updating SSH key account (%s)", d.Id())
account := expandSSHKeyAccount(d)
client := m.(*octopusdeploy.Client)
updatedAccount, err := client.Accounts.Update(account)
if err != nil {
return diag.FromErr(err)
}
accountResource, err := octopusdeploy.ToAccount(updatedAccount.(*octopusdeploy.AccountResource))
if err != nil {
return diag.FromErr(err)
}
if err := setSSHKeyAccount(ctx, d, accountResource.(*octopusdeploy.SSHKeyAccount)); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] SSH key account updated (%s)", d.Id())
return nil
}