-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathdata_current_account.go
More file actions
108 lines (89 loc) · 2.47 KB
/
data_current_account.go
File metadata and controls
108 lines (89 loc) · 2.47 KB
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
package codefresh
import (
"fmt"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceCurrentAccount() *schema.Resource {
return &schema.Resource{
Description: "Returns the current account (owner of the token) and its users.",
Read: dataSourceCurrentAccountRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
},
"_id": {
Type: schema.TypeString,
Optional: true,
},
"users": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"email": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
func dataSourceCurrentAccountRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cfclient.Client)
var currentAccount *cfclient.CurrentAccount
var err error
currentAccount, err = client.GetCurrentAccount()
if err != nil {
return err
}
if currentAccount == nil {
return fmt.Errorf("data.codefresh_current_account - failed to get current_account")
}
return mapDataCurrentAccountToResource(currentAccount, d)
}
func mapDataCurrentAccountToResource(currentAccount *cfclient.CurrentAccount, d *schema.ResourceData) error {
if currentAccount == nil || currentAccount.ID == "" {
return fmt.Errorf("data.codefresh_current_account - failed to mapDataCurrentAccountToResource")
}
d.SetId(currentAccount.ID)
err := d.Set("_id", currentAccount.ID)
if err != nil {
return err
}
err = d.Set("name", currentAccount.Name)
if err != nil {
return err
}
// users := make(map[string](map[string]interface{}))
// for n, user := range currentAccount.Users {
// users[n] = make(map[string]interface{})
// users[n]["name"] = user.UserName
// users[n]["email"] = user.Email
// users[n]["id"] = user.ID
// }
// d.Set("users", []map[string](map[string]interface{}){users})
users := make([](map[string]interface{}), len(currentAccount.Users))
for n, user := range currentAccount.Users {
users[n] = make(map[string]interface{})
users[n]["name"] = user.UserName
users[n]["email"] = user.Email
users[n]["id"] = user.ID
}
err = d.Set("users", users)
if err != nil {
return err
}
return nil
}