Skip to content

Commit 013c48c

Browse files
authored
Merge pull request #166 from matejvelikonja/identity-user
feat: add identity user data source and resource
2 parents 4be4958 + 1a7336a commit 013c48c

13 files changed

+739
-0
lines changed
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceMeIdentityUser() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceMeIdentityUserRead,
13+
Schema: map[string]*schema.Schema{
14+
"user": {
15+
Type: schema.TypeString,
16+
Required: true,
17+
Description: "User's login",
18+
},
19+
"login": {
20+
Type: schema.TypeString,
21+
Computed: true,
22+
Description: "User's login suffix",
23+
},
24+
"creation": {
25+
Type: schema.TypeString,
26+
Computed: true,
27+
Description: "Creation date of this user",
28+
},
29+
"description": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
Description: "User description",
33+
},
34+
"email": {
35+
Type: schema.TypeString,
36+
Computed: true,
37+
Description: "User's email",
38+
},
39+
"group": {
40+
Type: schema.TypeString,
41+
Computed: true,
42+
Description: "User's group",
43+
},
44+
"last_update": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
Description: "Last update of this user",
48+
},
49+
"password_last_update": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
Description: "When the user changed his password for the last time",
53+
},
54+
"status": {
55+
Type: schema.TypeString,
56+
Computed: true,
57+
Description: "Current user's status",
58+
},
59+
},
60+
}
61+
}
62+
63+
func dataSourceMeIdentityUserRead(d *schema.ResourceData, meta interface{}) error {
64+
config := meta.(*Config)
65+
66+
identityUser := &MeIdentityUserResponse{}
67+
68+
user := d.Get("user").(string)
69+
err := config.OVHClient.Get(
70+
fmt.Sprintf("/me/identity/user/%s", user),
71+
identityUser,
72+
)
73+
if err != nil {
74+
return fmt.Errorf("Unable to find identity user %s:\n\t %q", user, err)
75+
}
76+
log.Printf("[DEBUG] identity user for %s: %+v", user, identityUser)
77+
78+
d.SetId(user)
79+
d.Set("login", identityUser.Login)
80+
d.Set("creation", identityUser.Creation)
81+
d.Set("description", identityUser.Description)
82+
d.Set("email", identityUser.Email)
83+
d.Set("group", identityUser.Group)
84+
d.Set("last_update", identityUser.LastUpdate)
85+
d.Set("password_last_update", identityUser.PasswordLastUpdate)
86+
d.Set("status", identityUser.Status)
87+
88+
return nil
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package ovh
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
)
11+
12+
func TestAccMeIdentityUserDataSource_basic(t *testing.T) {
13+
desc := "Identity user created by Terraform Acc."
14+
email := "[email protected]"
15+
group := "DEFAULT"
16+
login := acctest.RandomWithPrefix(test_prefix)
17+
password := base64.StdEncoding.EncodeToString([]byte(acctest.RandomWithPrefix(test_prefix)))
18+
config := fmt.Sprintf(testAccMeIdentityUserDatasourceConfig, desc, email, group, login, password)
19+
20+
resource.Test(t, resource.TestCase{
21+
PreCheck: func() { testAccPreCheckCredentials(t) },
22+
Providers: testAccProviders,
23+
Steps: []resource.TestStep{
24+
{
25+
Config: config,
26+
Check: resource.ComposeTestCheckFunc(
27+
checkIdentityUserResourceAttr("data.ovh_me_identity_user.user_1", desc, email, group, login)...,
28+
),
29+
},
30+
},
31+
})
32+
}
33+
34+
const testAccMeIdentityUserDatasourceConfig = `
35+
resource "ovh_me_identity_user" "user_1" {
36+
description = "%s"
37+
email = "%s"
38+
group = "%s"
39+
login = "%s"
40+
password = "%s"
41+
}
42+
43+
data "ovh_me_identity_user" "user_1" {
44+
user = ovh_me_identity_user.user_1.login
45+
depends_on = [ovh_me_identity_user.user_1]
46+
}
47+
`
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"sort"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/terraform-providers/terraform-provider-ovh/ovh/helpers/hashcode"
10+
)
11+
12+
func dataSourceMeIdentityUsers() *schema.Resource {
13+
return &schema.Resource{
14+
Read: dataSourceMeIdentityUsersRead,
15+
Schema: map[string]*schema.Schema{
16+
"users": {
17+
Type: schema.TypeSet,
18+
Computed: true,
19+
Elem: &schema.Schema{Type: schema.TypeString},
20+
Set: schema.HashString,
21+
},
22+
},
23+
}
24+
}
25+
26+
func dataSourceMeIdentityUsersRead(d *schema.ResourceData, meta interface{}) error {
27+
config := meta.(*Config)
28+
29+
users := make([]string, 0)
30+
31+
err := config.OVHClient.Get(
32+
"/me/identity/user",
33+
&users,
34+
)
35+
if err != nil {
36+
return fmt.Errorf("Unable to get identity users:\n\t %q", err)
37+
}
38+
log.Printf("[DEBUG] identity users: %+v", users)
39+
40+
sort.Strings(users)
41+
d.SetId(hashcode.Strings(users))
42+
d.Set("users", users)
43+
44+
return nil
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package ovh
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
)
11+
12+
func TestAccMeIdentityUsersDataSource_basic(t *testing.T) {
13+
desc := "Identity user created by Terraform Acc."
14+
email1 := "[email protected]"
15+
email2 := "[email protected]"
16+
group := "DEFAULT"
17+
login1 := acctest.RandomWithPrefix(test_prefix)
18+
login2 := acctest.RandomWithPrefix(test_prefix)
19+
password := base64.StdEncoding.EncodeToString([]byte(acctest.RandomWithPrefix(test_prefix)))
20+
21+
preSetup := fmt.Sprintf(
22+
testAccMeIdentityUsersDatasourceConfig_preSetup,
23+
desc,
24+
email1,
25+
group,
26+
login1,
27+
password,
28+
desc,
29+
email2,
30+
group,
31+
login2,
32+
password,
33+
)
34+
config := fmt.Sprintf(
35+
testAccMeIdentityUsersDatasourceConfig_keys,
36+
desc,
37+
email1,
38+
group,
39+
login1,
40+
password,
41+
desc,
42+
email2,
43+
group,
44+
login2,
45+
password,
46+
)
47+
48+
checks := checkIdentityUserResourceAttr("ovh_me_identity_user.user_1", desc, email1, group, login1)
49+
checks = append(checks, checkIdentityUserResourceAttr("ovh_me_identity_user.user_1", desc, email1, group, login1)...)
50+
51+
resource.Test(t, resource.TestCase{
52+
PreCheck: func() { testAccPreCheckCredentials(t) },
53+
Providers: testAccProviders,
54+
Steps: []resource.TestStep{
55+
{
56+
Config: preSetup,
57+
Check: resource.ComposeTestCheckFunc(checks...),
58+
}, {
59+
Config: config,
60+
Check: resource.ComposeTestCheckFunc(
61+
resource.TestCheckOutput(
62+
"keys_present", "true"),
63+
),
64+
},
65+
},
66+
})
67+
}
68+
69+
func checkIdentityUserResourceAttr(name, desc, email, group, login string) []resource.TestCheckFunc {
70+
return []resource.TestCheckFunc{
71+
resource.TestCheckResourceAttr(name, "description", desc),
72+
resource.TestCheckResourceAttr(name, "email", email),
73+
resource.TestCheckResourceAttr(name, "group", group),
74+
resource.TestCheckResourceAttr(name, "login", login),
75+
}
76+
}
77+
78+
const testAccMeIdentityUsersDatasourceConfig_preSetup = `
79+
resource "ovh_me_identity_user" "user_1" {
80+
description = "%s"
81+
email = "%s"
82+
group = "%s"
83+
login = "%s"
84+
password = "%s"
85+
}
86+
87+
resource "ovh_me_identity_user" "user_2" {
88+
description = "%s"
89+
email = "%s"
90+
group = "%s"
91+
login = "%s"
92+
password = "%s"
93+
}
94+
`
95+
96+
const testAccMeIdentityUsersDatasourceConfig_keys = `
97+
resource "ovh_me_identity_user" "user_1" {
98+
description = "%s"
99+
email = "%s"
100+
group = "%s"
101+
login = "%s"
102+
password = "%s"
103+
}
104+
105+
resource "ovh_me_identity_user" "user_2" {
106+
description = "%s"
107+
email = "%s"
108+
group = "%s"
109+
login = "%s"
110+
password = "%s"
111+
}
112+
113+
data "ovh_me_identity_users" "users" {}
114+
115+
output "keys_present" {
116+
value = tostring(contains(data.ovh_me_identity_users.users.users, ovh_me_identity_user.user_1.login) && contains(data.ovh_me_identity_users.users.users, ovh_me_identity_user.user_2.login))
117+
}
118+
`

ovh/import_me_identity_user_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ovh
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
)
11+
12+
func TestAccMeIdentityUser_importBasic(t *testing.T) {
13+
resourceName := "ovh_me_identity_user.user_1"
14+
login := acctest.RandomWithPrefix(test_prefix)
15+
password := base64.StdEncoding.EncodeToString([]byte(acctest.RandomWithPrefix(test_prefix)))
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheckCredentials(t) },
19+
Providers: testAccProviders,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: fmt.Sprintf(testAccMeIdentityUserConfig_import, login, password),
23+
},
24+
{
25+
ResourceName: resourceName,
26+
ImportState: true,
27+
ImportStateVerify: true,
28+
},
29+
},
30+
})
31+
}
32+
33+
const testAccMeIdentityUserConfig_import = `
34+
resource "ovh_me_identity_user" "user_1" {
35+
description = "tf acc import test"
36+
37+
group = "DEFAULT"
38+
login = "%s"
39+
password = "%s"
40+
}
41+
`

ovh/provider.go

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ func Provider() *schema.Provider {
5151
"ovh_iploadbalancing": dataSourceIpLoadbalancing(),
5252
"ovh_iploadbalancing_vrack_network": dataSourceIpLoadbalancingVrackNetwork(),
5353
"ovh_iploadbalancing_vrack_networks": dataSourceIpLoadbalancingVrackNetworks(),
54+
"ovh_me_identity_user": dataSourceMeIdentityUser(),
55+
"ovh_me_identity_users": dataSourceMeIdentityUsers(),
5456
"ovh_me_installation_template": dataSourceMeInstallationTemplate(),
5557
"ovh_me_installation_templates": dataSourceMeInstallationTemplates(),
5658
"ovh_me_ipxe_script": dataSourceMeIpxeScript(),
@@ -95,6 +97,7 @@ func Provider() *schema.Provider {
9597
"ovh_me_installation_template_partition_scheme_hardware_raid": resourceMeInstallationTemplatePartitionSchemeHardwareRaid(),
9698
"ovh_me_installation_template_partition_scheme_partition": resourceMeInstallationTemplatePartitionSchemePartition(),
9799
"ovh_me_ipxe_script": resourceMeIpxeScript(),
100+
"ovh_me_identity_user": resourceMeIdentityUser(),
98101
"ovh_me_ssh_key": resourceMeSshKey(),
99102
"ovh_vrack_cloudproject": resourceVrackCloudProject(),
100103
"ovh_vrack_dedicated_server": resourceVrackDedicatedServer(),

0 commit comments

Comments
 (0)