/
resource_credential.go
178 lines (152 loc) · 4.54 KB
/
resource_credential.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Create a credential.
Example Usage
```hcl
resource "awx_credential" "default" {
name = "acc-test"
organization_id = 1
credential_type_id = 1
inputs = <<YAML
...
YAML
}
```
*/
package awx
import (
"context"
"encoding/json"
"fmt"
"strconv"
awx "github.com/denouche/goawx/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceCredential() *schema.Resource {
return &schema.Resource{
CreateContext: resourceCredentialCreate,
ReadContext: resourceCredentialRead,
UpdateContext: resourceCredentialUpdate,
DeleteContext: CredentialsServiceDeleteByID,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"organization_id": {
Type: schema.TypeInt,
Required: true,
},
"credential_type_id": {
Type: schema.TypeInt,
Required: true,
Description: "Specify the type of credential you want to create. Refer to the Ansible Tower documentation for details on each type",
},
"inputs": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
},
}
}
func resourceCredentialCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
var err error
inputs := d.Get("inputs").(string)
inputs_map := make(map[string]interface{})
jsonerr := json.Unmarshal([]byte(inputs), &inputs_map)
if jsonerr != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to create new credential",
Detail: fmt.Sprintf("Unable to create new credential: %s", jsonerr.Error()),
})
return diags
}
newCredential := map[string]interface{}{
"name": d.Get("name").(string),
"description": d.Get("description").(string),
"organization": d.Get("organization_id").(int),
"credential_type": d.Get("credential_type_id").(int),
"inputs": inputs_map,
}
client := m.(*awx.AWX)
cred, err := client.CredentialsService.CreateCredentials(newCredential, map[string]string{})
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to create new credential",
Detail: fmt.Sprintf("Unable to create new credential: %s", err.Error()),
})
return diags
}
d.SetId(strconv.Itoa(cred.ID))
resourceCredentialRead(ctx, d, m)
return diags
}
func resourceCredentialRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*awx.AWX)
id, _ := strconv.Atoi(d.Id())
cred, err := client.CredentialsService.GetCredentialsByID(id, map[string]string{})
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to fetch credential",
Detail: fmt.Sprintf("Unable to credential with id %d: %s", id, err.Error()),
})
return diags
}
d.Set("name", cred.Name)
d.Set("description", cred.Description)
d.Set("organization_id", cred.OrganizationID)
d.Set("inputs", cred.Inputs)
return diags
}
func resourceCredentialUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
keys := []string{
"name",
"description",
"organization_id",
"inputs",
}
if d.HasChanges(keys...) {
var err error
inputs := d.Get("inputs").(string)
inputs_map := make(map[string]interface{})
jsonerr := json.Unmarshal([]byte(inputs), &inputs_map)
if jsonerr != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to create new credential",
Detail: fmt.Sprintf("Unable to create new credential: %s", jsonerr.Error()),
})
return diags
}
id, _ := strconv.Atoi(d.Id())
updatedCredential := map[string]interface{}{
"name": d.Get("name").(string),
"description": d.Get("description").(string),
"organization": d.Get("organization_id").(int),
"credential_type": d.Get("credential_type_id"),
"inputs": inputs_map,
}
client := m.(*awx.AWX)
_, err = client.CredentialsService.UpdateCredentialsByID(id, updatedCredential, map[string]string{})
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to update existing credentials",
Detail: fmt.Sprintf("Unable to update existing credentials with id %d: %s", id, err.Error()),
})
return diags
}
}
return resourceCredentialSCMRead(ctx, d, m)
}