-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresource_client.go
86 lines (71 loc) · 1.88 KB
/
resource_client.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
package main
import (
"log"
"github.com/DelineaXPM/dsv-sdk-go/v2/vault"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceClientCreate(d *schema.ResourceData, meta interface{}) error {
role := d.Get("role").(string)
dsv, err := vault.New(meta.(vault.Configuration))
if err != nil {
log.Print("[DEBUG] configuration error", err)
return err
}
client := new(vault.Client)
client.RoleName = role
err = dsv.New(client)
if err != nil {
log.Printf("[DEBUG] unable to create client for role %s: %s", role, err)
return err
}
d.SetId(client.ID)
d.Set("client_id", client.ClientID)
d.Set("client_secret", client.ClientSecret)
d.Set("role", client.RoleName)
return nil
}
func resourceClientDelete(d *schema.ResourceData, meta interface{}) error {
clientID := d.Get("client_id").(string)
dsv, err := vault.New(meta.(vault.Configuration))
if err != nil {
log.Print("[DEBUG] configuration error", err)
return err
}
log.Printf("[DEBUG] getting client %s", clientID)
client, err := dsv.Client(clientID)
if err != nil {
log.Printf("[DEBUG] unable to delete client %s: %s", clientID, err)
return err
}
err = client.Delete()
if err != nil {
log.Printf("[DEBUG] unable to delete client %s: %s", clientID, err)
return err
}
return nil
}
func resourceClient() *schema.Resource {
return &schema.Resource{
Create: resourceClientCreate,
Delete: resourceClientDelete,
Read: dataSourceClientRead,
Schema: map[string]*schema.Schema{
"role": {
Description: "the role of the client",
ForceNew: true,
Required: true,
Type: schema.TypeString,
},
"client_id": {
Computed: true,
Description: "the client_id of the client",
Type: schema.TypeString,
},
"client_secret": {
Computed: true,
Description: "the client_secret of the client",
Type: schema.TypeString,
},
},
}
}