forked from scaleway/terraform-provider-scaleway
/
resource_token.go
162 lines (146 loc) · 3.78 KB
/
resource_token.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
package scaleway
import (
"github.com/hashicorp/terraform/helper/schema"
api "github.com/nicolai86/scaleway-sdk"
)
func resourceScalewayToken() *schema.Resource {
return &schema.Resource{
Create: resourceScalewayTokenCreate,
Read: resourceScalewayTokenRead,
Update: resourceScalewayTokenUpdate,
Delete: resourceScalewayTokenDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"email": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The account email. Defaults to registered user.",
},
"user_id": {
Type: schema.TypeString,
Computed: true,
Description: "The userid of the associated user.",
},
"access_key": {
Type: schema.TypeString,
Computed: true,
Description: "The access_key.",
},
"secret_key": {
Type: schema.TypeString,
Computed: true,
Description: "The secret_key.",
Sensitive: true,
},
"creation_ip": {
Type: schema.TypeString,
Computed: true,
Description: "The ip used to create the key.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The token description.",
},
"password": {
Type: schema.TypeString,
Optional: true,
Description: "User password, in case a login is require",
},
"expires": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Defines if the token is set to expire",
},
"expiration_date": {
Type: schema.TypeString,
Computed: true,
Description: "The tokens expiration date",
},
},
}
}
func resourceScalewayTokenCreate(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Client).scaleway
email := ""
if mail, ok := d.GetOk("email"); ok {
email = mail.(string)
} else {
user, err := scaleway.GetUser()
if err != nil {
return err
}
email = user.Email
}
token, err := scaleway.CreateToken(&api.CreateTokenRequest{
Email: email,
Password: d.Get("password").(string),
Expires: d.Get("expires").(bool),
})
if err != nil {
return err
}
d.SetId(token.ID)
// the secret_key is not present in read operations
d.Set("secret_key", token.SecretKey)
return resourceScalewayTokenUpdate(d, m)
}
func resourceScalewayTokenRead(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Client).scaleway
token, err := scaleway.GetToken(d.Id())
if err != nil {
if serr, ok := err.(api.APIError); ok {
if serr.StatusCode == 404 {
d.SetId("")
return nil
}
}
return err
}
d.Set("description", token.Description)
d.Set("expiration_date", token.Expires)
d.Set("expires", token.Expires != "")
d.Set("user_id", token.UserID)
d.Set("creation_ip", token.CreationIP)
d.Set("access_key", token.AccessKey)
// this is compatibilty to old tokens: the secret key is the id
if d.Get("secret_key") == "" {
d.Set("secret_key", token.ID)
}
user, err := scaleway.GetUser()
if err != nil {
return err
}
if user.ID == token.UserID {
d.Set("email", user.Email)
}
return nil
}
func resourceScalewayTokenUpdate(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Client).scaleway
if d.HasChange("description") || d.HasChange("expires") {
_, err := scaleway.UpdateToken(&api.UpdateTokenRequest{
ID: d.Id(),
Expires: d.Get("expires").(bool),
Description: d.Get("description").(string),
})
if err != nil {
return err
}
}
return resourceScalewayTokenRead(d, m)
}
func resourceScalewayTokenDelete(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Client).scaleway
err := scaleway.DeleteToken(d.Id())
if err != nil {
return err
}
d.SetId("")
return nil
}