Skip to content

Commit 93ff1ce

Browse files
authored
feat: Add Sysdig User datasource (#78)
Adds the `sysdig_user` data source that retrieves information from the user via their email.
1 parent c26f36b commit 93ff1ce

File tree

8 files changed

+203
-22
lines changed

8 files changed

+203
-22
lines changed

sysdig/data_source_sysdig_user.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"strconv"
6+
"time"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func dataSourceSysdigUser() *schema.Resource {
13+
timeout := 30 * time.Second
14+
15+
return &schema.Resource{
16+
ReadContext: dataSourceSysdigUserRead,
17+
18+
Timeouts: &schema.ResourceTimeout{
19+
Read: schema.DefaultTimeout(timeout),
20+
},
21+
22+
Schema: map[string]*schema.Schema{
23+
"email": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
},
27+
"system_role": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
},
31+
"first_name": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
"last_name": {
36+
Type: schema.TypeString,
37+
Computed: true,
38+
},
39+
"version": {
40+
Type: schema.TypeInt,
41+
Computed: true,
42+
},
43+
},
44+
}
45+
}
46+
47+
func dataSourceSysdigUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
48+
client, err := meta.(SysdigClients).sysdigCommonClient()
49+
if err != nil {
50+
return diag.FromErr(err)
51+
}
52+
53+
u, err := client.GetUserByEmail(ctx, d.Get("email").(string))
54+
55+
if err != nil {
56+
return diag.FromErr(err)
57+
}
58+
59+
d.SetId(strconv.Itoa(u.ID))
60+
d.Set("version", u.Version)
61+
d.Set("system_role", u.SystemRole)
62+
d.Set("first_name", u.FirstName)
63+
d.Set("last_name", u.LastName)
64+
65+
return nil
66+
67+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package sysdig_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
10+
"github.com/draios/terraform-provider-sysdig/sysdig"
11+
)
12+
13+
func TestAccDataUser(t *testing.T) {
14+
resource.ParallelTest(t, resource.TestCase{
15+
PreCheck: func() {
16+
monitor := os.Getenv("SYSDIG_MONITOR_API_TOKEN")
17+
secure := os.Getenv("SYSDIG_SECURE_API_TOKEN")
18+
if monitor == "" && secure == "" {
19+
t.Fatal("either SYSDIG_MONITOR_API_TOKEN or SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
20+
}
21+
},
22+
ProviderFactories: map[string]func() (*schema.Provider, error){
23+
"sysdig": func() (*schema.Provider, error) {
24+
return sysdig.Provider(), nil
25+
},
26+
},
27+
Steps: []resource.TestStep{
28+
{
29+
Config: getUser(),
30+
},
31+
},
32+
})
33+
}
34+
35+
func getUser() string {
36+
return `
37+
resource "sysdig_user" "sample" {
38+
39+
}
40+
41+
data "sysdig_user" "me" {
42+
depends_on = ["sysdig_user.sample"]
43+
email = sysdig_user.sample.email
44+
}
45+
`
46+
}

sysdig/internal/client/common/client.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import (
1212
)
1313

1414
type SysdigCommonClient interface {
15-
CreateUser(context.Context, User) (User, error)
16-
GetUserById(context.Context, int) (User, error)
15+
CreateUser(context.Context, *User) (*User, error)
16+
GetUserById(context.Context, int) (*User, error)
17+
GetUserByEmail(context.Context, string) (*User, error)
1718
DeleteUser(context.Context, int) error
18-
UpdateUser(context.Context, User) (User, error)
19-
GetCurrentUser(context.Context) (User, error)
19+
UpdateUser(context.Context, *User) (*User, error)
20+
GetCurrentUser(context.Context) (*User, error)
2021
}
2122

2223
func WithExtraHeaders(client SysdigCommonClient, extraHeaders map[string]string) SysdigCommonClient {

sysdig/internal/client/common/users.go

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package common
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"io/ioutil"
89
"net/http"
910
)
1011

11-
func (client *sysdigCommonClient) GetUserById(ctx context.Context, id int) (u User, err error) {
12+
func (client *sysdigCommonClient) GetUserById(ctx context.Context, id int) (u *User, err error) {
1213
response, err := client.doSysdigCommonRequest(ctx, http.MethodGet, client.GetUserUrl(id), nil)
1314
if err != nil {
1415
return
@@ -22,12 +23,43 @@ func (client *sysdigCommonClient) GetUserById(ctx context.Context, id int) (u Us
2223
return
2324
}
2425

25-
u = UserFromJSON(body)
26+
user := UserFromJSON(body)
27+
return &user, nil
28+
}
29+
30+
func (client *sysdigCommonClient) GetUserByEmail(ctx context.Context, email string) (u *User, err error) {
31+
response, err := client.doSysdigCommonRequest(ctx, http.MethodGet, client.GetUsersUrl(), nil)
32+
if err != nil {
33+
return
34+
}
35+
defer response.Body.Close()
36+
37+
body, _ := ioutil.ReadAll(response.Body)
38+
39+
if response.StatusCode != http.StatusOK {
40+
err = errors.New(response.Status)
41+
return
42+
}
43+
44+
var userList struct {
45+
Users []User `json:"users"`
46+
}
47+
48+
err = json.Unmarshal(body, &userList)
49+
if err != nil {
50+
return
51+
}
52+
53+
for _, user := range userList.Users {
54+
if user.Email == email {
55+
return &user, nil
56+
}
57+
}
2658

27-
return
59+
return nil, fmt.Errorf("user not found for the given email")
2860
}
2961

30-
func (client *sysdigCommonClient) CreateUser(ctx context.Context, uRequest User) (u User, err error) {
62+
func (client *sysdigCommonClient) CreateUser(ctx context.Context, uRequest *User) (u *User, err error) {
3163
response, err := client.doSysdigCommonRequest(ctx, http.MethodPost, client.GetUsersUrl(), uRequest.ToJSON())
3264

3365
if err != nil {
@@ -42,11 +74,11 @@ func (client *sysdigCommonClient) CreateUser(ctx context.Context, uRequest User)
4274
return
4375
}
4476

45-
u = UserFromJSON(body)
46-
return
77+
user := UserFromJSON(body)
78+
return &user, nil
4779
}
4880

49-
func (client *sysdigCommonClient) UpdateUser(ctx context.Context, uRequest User) (u User, err error) {
81+
func (client *sysdigCommonClient) UpdateUser(ctx context.Context, uRequest *User) (u *User, err error) {
5082
response, err := client.doSysdigCommonRequest(ctx, http.MethodPut, client.GetUserUrl(uRequest.ID), uRequest.ToJSON())
5183
if err != nil {
5284
return
@@ -60,8 +92,8 @@ func (client *sysdigCommonClient) UpdateUser(ctx context.Context, uRequest User)
6092
return
6193
}
6294

63-
u = UserFromJSON(body)
64-
return
95+
user := UserFromJSON(body)
96+
return &user, nil
6597
}
6698

6799
func (client *sysdigCommonClient) DeleteUser(ctx context.Context, id int) error {
@@ -77,7 +109,7 @@ func (client *sysdigCommonClient) DeleteUser(ctx context.Context, id int) error
77109
return nil
78110
}
79111

80-
func (client *sysdigCommonClient) GetCurrentUser(ctx context.Context) (u User, err error) {
112+
func (client *sysdigCommonClient) GetCurrentUser(ctx context.Context) (u *User, err error) {
81113
response, err := client.doSysdigCommonRequest(ctx, http.MethodGet, client.GetCurrentUserUrl(), nil)
82114
if err != nil {
83115
return
@@ -90,8 +122,8 @@ func (client *sysdigCommonClient) GetCurrentUser(ctx context.Context) (u User, e
90122
}
91123
body, _ := ioutil.ReadAll(response.Body)
92124

93-
u = UserFromJSON(body)
94-
return
125+
user := UserFromJSON(body)
126+
return &user, nil
95127
}
96128

97129
func (client *sysdigCommonClient) GetUsersUrl() string {

sysdig/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func Provider() *schema.Provider {
8787
DataSourcesMap: map[string]*schema.Resource{
8888
"sysdig_secure_notification_channel": dataSourceSysdigSecureNotificationChannel(),
8989
"sysdig_current_user": dataSourceSysdigCurrentUser(),
90+
"sysdig_user": dataSourceSysdigUser(),
9091
},
9192
ConfigureContextFunc: providerConfigure,
9293
}

sysdig/resource_sysdig_user.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ func resourceSysdigUserUpdate(ctx context.Context, d *schema.ResourceData, meta
102102
return diag.FromErr(err)
103103
}
104104

105-
u := userFromResourceData(d)
105+
user := userFromResourceData(d)
106106

107-
u.Version = d.Get("version").(int)
108-
u.ID, _ = strconv.Atoi(d.Id())
107+
user.Version = d.Get("version").(int)
108+
user.ID, _ = strconv.Atoi(d.Id())
109109

110-
_, err = client.UpdateUser(ctx, u)
110+
_, err = client.UpdateUser(ctx, user)
111111
if err != nil {
112112
return diag.FromErr(err)
113113
}
@@ -130,8 +130,8 @@ func resourceSysdigUserDelete(ctx context.Context, d *schema.ResourceData, meta
130130
return nil
131131
}
132132

133-
func userFromResourceData(d *schema.ResourceData) (u common.User) {
134-
u = common.User{
133+
func userFromResourceData(d *schema.ResourceData) (u *common.User) {
134+
u = &common.User{
135135
SystemRole: d.Get("system_role").(string),
136136
Email: d.Get("email").(string),
137137
FirstName: d.Get("first_name").(string),

website/docs/d/sysdig_user.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
layout: "sysdig"
3+
page_title: "Sysdig: sysdig_user"
4+
sidebar_current: "docs-sysdig-user-ds"
5+
description: |-
6+
Retrieves information about a user from their email
7+
---
8+
9+
# sysdig\_user
10+
11+
Retrieves information about a user from their email
12+
13+
`~> **Note:** Sysdig Terraform Provider is under rapid development at this point. If you experience any issue or discrepancy while using it, please make sure you have the latest version. If the issue persists, or you have a Feature Request to support an additional set of resources, please open a [new issue](https://github.com/sysdiglabs/terraform-provider-sysdig/issues/new) in the GitHub repository.`
14+
15+
## Example usage
16+
17+
```hcl
18+
data "sysdig_user" "user" {
19+
20+
}
21+
```
22+
23+
## Attributes Reference
24+
25+
* `id` - The user's ID.
26+
27+
* `first_name` - The user's first name.
28+
29+
* `last_name` - The user's last name.
30+
31+
* `system_role` - The user's system role.

website/sysdig.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@
146146
<li<%= sidebar_current("docs-sysdig-current-user-ds") %>>
147147
<a href="/docs/providers/sysdig/d/sysdig_current_user.html">sysdig_current_user</a>
148148
</li>
149+
<li<%= sidebar_current("docs-sysdig-user-ds") %>>
150+
<a href="/docs/providers/sysdig/d/sysdig_user.html">sysdig_user</a>
151+
</li>
149152
</ul>
150153
</li>
151154
</ul>

0 commit comments

Comments
 (0)