-
Notifications
You must be signed in to change notification settings - Fork 16
/
resource_transip_openstack_user.go
150 lines (129 loc) · 3.88 KB
/
resource_transip_openstack_user.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
package main
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/transip/gotransip/v6/openstack"
"github.com/transip/gotransip/v6/repository"
)
func resourceOpenstackUser() *schema.Resource {
return &schema.Resource{
Create: resourceOpenstackUserCreate,
Read: resourceOpenstackUserRead,
Update: resourceOpenstackUserUpdate,
Delete: resourceOpenstackUserDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"projectid": {
Type: schema.TypeString,
Description: "Grant user access to a project",
Required: true,
ForceNew: false,
},
"email": {
Type: schema.TypeString,
Description: "Email address",
Required: true,
ForceNew: false,
},
"username": {
Type: schema.TypeString,
Description: "Username",
Required: true,
ForceNew: true,
},
"password": {
Type: schema.TypeString,
Description: "Password",
Required: true,
ForceNew: false,
},
"description": {
Type: schema.TypeString,
Description: "Description",
Required: false,
Optional: true,
ForceNew: false,
},
},
}
}
func resourceOpenstackUserCreate(d *schema.ResourceData, m interface{}) error {
client := m.(repository.Client)
repository := openstack.UserRepository{Client: client}
err := repository.Create(openstack.CreateUserRequest{
ProjectID: d.Get("projectid").(string),
Username: d.Get("username").(string),
Password: d.Get("password").(string),
Description: d.Get("description").(string),
Email: d.Get("email").(string),
})
if err != nil {
return fmt.Errorf("failed to create openstack user %q: %s", d.Get("username").(string), err)
}
// Return with retryable error, as the user is not found instantly.
return resource.Retry(d.Timeout(schema.TimeoutCreate)-15*time.Minute, func() *resource.RetryError {
users, err := repository.GetAll()
if err != nil {
return resource.NonRetryableError(fmt.Errorf("failed to list openstack users: %s", err))
}
var found bool
// get ID of openstack project
log.Printf("[DEBUG] listing all users from openstack %v", users)
for _, user := range users {
if user.Username == d.Get("username") {
log.Printf("[DEBUG] found id %v for user %v\n", user.Username, user.ID)
d.SetId(user.ID)
found = true
break
}
}
if !found {
return resource.RetryableError(fmt.Errorf("UserId not available yet due to OpenStack delay, retrying"))
}
err = resourceOpenstackUserRead(d, m)
if err != nil {
return resource.NonRetryableError(err)
} else {
return nil
}
})
}
func resourceOpenstackUserRead(d *schema.ResourceData, m interface{}) error {
id := d.Id()
client := m.(repository.Client)
repository := openstack.UserRepository{Client: client}
i, err := repository.GetByID(id)
if err != nil {
return fmt.Errorf("failed to get openstack users %q: %s", id, err)
}
d.Set("email", i.Email)
d.Set("username", i.Username)
d.Set("description", i.Description)
return nil
}
func resourceOpenstackUserUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(repository.Client)
repository := openstack.UserRepository{Client: client}
repository.Update(openstack.User{
ID: d.Id(),
Username: d.Get("username").(string),
Email: d.Get("email").(string),
Description: d.Get("description").(string),
})
return resourceOpenstackUserRead(d, m)
}
func resourceOpenstackUserDelete(d *schema.ResourceData, m interface{}) error {
client := m.(repository.Client)
repository := openstack.UserRepository{Client: client}
err := repository.Delete(d.Id())
if err != nil {
return fmt.Errorf("failed to delete openstack user %q: %s", d.Get("username"), err)
}
d.SetId("")
return nil
}