-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresource_programmatic_user.go
104 lines (84 loc) · 2.59 KB
/
resource_programmatic_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
package main
import (
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceProgrammaticUser() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
userResourceAccountIDAttrKey: &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
userResourceNameAttrKey: &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
userResourceDescriptionAttrKey: &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "",
},
userResourceAccessTokenAttrKey: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
Create: resourceProgrammaticUserCreate,
Read: resourceProgrammaticUserRead,
// Update: resourceProgrammaticUserUpdate,
Delete: resourceProgrammaticUserDelete,
}
}
func resourceProgrammaticUserRead(d *schema.ResourceData, m interface{}) error {
usersService := m.(*Meta).usersService
accountID := d.Get(userResourceAccountIDAttrKey).(string)
log.Println(accountID)
userName := d.Id()
log.Printf("IN_RESOURCE_READ: %v-%v\n", userName, accountID)
obj, err := usersService.Get(userName, accountID)
if err != nil {
d.SetId("")
return err
}
log.Println(obj)
actualName := strings.ToLower(obj.CoreUser.FirstName)
if actualName == d.Id() {
d.SetId(actualName)
return d.Set(userResourceAccountIDAttrKey, obj.AccountID)
}
d.SetId("")
return nil
}
// func resourceProgrammaticUserUpdate(d *schema.ResourceData, m interface{}) error {
// }
func resourceProgrammaticUserCreate(d *schema.ResourceData, m interface{}) error {
usersService := m.(*Meta).usersService
username := d.Get(userResourceNameAttrKey).(string)
description := d.Get(userResourceDescriptionAttrKey).(string)
accountID := d.Get(userResourceAccountIDAttrKey).(string)
log.Printf("IN_RESOURCE_CREATE: %v\n", accountID)
user, err := usersService.Create(username, description, accountID)
if err != nil {
return err
}
d.SetId(strings.ToLower(user.CoreUser.FirstName))
if err := d.Set(userResourceAccessTokenAttrKey, user.AccessToken); err != nil {
return err
}
if err := d.Set(userResourceAccountIDAttrKey, accountID); err != nil {
return err
}
return resourceProgrammaticUserRead(d, m)
}
func resourceProgrammaticUserDelete(d *schema.ResourceData, m interface{}) error {
usersService := m.(*Meta).usersService
username := d.Get(userResourceNameAttrKey).(string)
accountID := d.Get(userResourceAccountIDAttrKey).(string)
return usersService.Delete(username, accountID)
}