-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathresource_netapp_cloudmanager_nss_account.go
110 lines (102 loc) · 3.05 KB
/
resource_netapp_cloudmanager_nss_account.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
package cloudmanager
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceCVONssAccount() *schema.Resource {
return &schema.Resource{
Create: resourceCVONssAccountCreate,
Read: resourceCVONssAccountRead,
Delete: resourceCVONssAccountDelete,
Exists: resourceCVONssAccountExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"password": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
ForceNew: true,
},
"client_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceCVONssAccountCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("Creating nss account: %s", d.Get("username").(string))
client := meta.(*Client)
clientID := d.Get("client_id").(string)
nssAcc := nssAccountRequest{}
nssAcc.VsaList = make([]string, 0, 0)
if v, ok := d.GetOk("username"); ok {
nssAcc.AccountCredentials.Username = v.(string)
} else {
return fmt.Errorf("username is required to create nss account")
}
if v, ok := d.GetOk("password"); ok {
nssAcc.AccountCredentials.Password = v.(string)
} else {
return fmt.Errorf("password is required to create nss account")
}
res, err := client.createNssAccount(nssAcc, clientID)
if err != nil {
log.Printf("Error creating nss account: %s", d.Get("username").(string))
return err
}
d.SetId(res["publicId"].(string))
return resourceCVONssAccountRead(d, meta)
}
func resourceCVONssAccountRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("Getting nss account: %s", d.Get("username").(string))
client := meta.(*Client)
clientID := d.Get("client_id").(string)
res, err := client.getNssAccount(d.Get("username").(string), clientID)
if err != nil {
log.Printf("Error getting nss account: %s", d.Get("username").(string))
return err
}
if res == nil {
return fmt.Errorf("Failed to find account: %s ", d.Get("username"))
}
if _, ok := d.GetOk("username"); ok {
d.Set("username", res["nssUserName"])
}
return nil
}
func resourceCVONssAccountDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("Deleting nss account: %s", d.Get("username").(string))
client := meta.(*Client)
clientID := d.Get("client_id").(string)
err := client.deleteNssAccount(d.Id(), clientID)
if err != nil {
log.Printf("Error deleting nss account: %s", d.Get("username").(string))
return err
}
d.SetId("")
return nil
}
func resourceCVONssAccountExists(d *schema.ResourceData, meta interface{}) (bool, error) {
log.Printf("Checking existence of nss account: %s", d.Get("username").(string))
client := meta.(*Client)
clientID := d.Get("client_id").(string)
res, err := client.getNssAccount(d.Get("username").(string), clientID)
if err != nil {
log.Printf("Error checking existence of nss account: %s", d.Get("username").(string))
return false, err
}
if res == nil {
return false, nil
}
return true, nil
}