/
resource_credential_google_compute_engine.go
162 lines (140 loc) · 4.28 KB
/
resource_credential_google_compute_engine.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
/*
Create a GCE credential.
Example Usage
```hcl
resource "awx_credential_google_compute_engine" "default" {
name = "acc-test"
organization_id = 1
username = "..."
project = "..."
ssh_key_data = "..."
}
*/
package awx
import (
"context"
"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 resourceCredentialGoogleComputeEngine() *schema.Resource {
return &schema.Resource{
CreateContext: resourceCredentialGoogleComputeEngineCreate,
ReadContext: resourceCredentialGoogleComputeEngineRead,
UpdateContext: resourceCredentialGoogleComputeEngineUpdate,
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,
},
"username": {
Type: schema.TypeString,
Required: true,
},
"project": {
Type: schema.TypeString,
Required: true,
},
"ssh_key_data": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
},
}
}
func resourceCredentialGoogleComputeEngineCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
var err error
newCredential := map[string]interface{}{
"name": d.Get("name").(string),
"description": d.Get("description").(string),
"organization": d.Get("organization_id").(int),
"credential_type": 10, // Google Compute Engine
"inputs": map[string]interface{}{
"username": d.Get("username").(string),
"project": d.Get("project").(string),
"ssh_key_data": d.Get("ssh_key_data").(string),
},
}
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 credentials",
Detail: fmt.Sprintf("Unable to create new credentials: %s", err.Error()),
})
return diags
}
d.SetId(strconv.Itoa(cred.ID))
resourceCredentialGoogleComputeEngineRead(ctx, d, m)
return diags
}
func resourceCredentialGoogleComputeEngineRead(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 credentials",
Detail: fmt.Sprintf("Unable to credentials 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("username", cred.Inputs["username"])
d.Set("project", cred.Inputs["project"])
return diags
}
func resourceCredentialGoogleComputeEngineUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
keys := []string{
"name",
"description",
"username",
"project",
"ssh_key_data",
}
if d.HasChanges(keys...) {
var err error
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": 10, // Google Compute Engine
"inputs": map[string]interface{}{
"username": d.Get("username").(string),
"project": d.Get("project").(string),
"ssh_key_data": d.Get("ssh_key_data").(string),
},
}
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 resourceCredentialGoogleComputeEngineRead(ctx, d, m)
}