Skip to content

Commit cbc9fbd

Browse files
committed
feat: support identity groups
Signed-off-by: Nicolas FOURNIER <[email protected]>
1 parent c02b921 commit cbc9fbd

14 files changed

+663
-0
lines changed

ovh/data_me_identity_group.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package ovh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func dataSourceMeIdentityGroup() *schema.Resource {
13+
return &schema.Resource{
14+
ReadContext: dataSourceMeIdentityGroupRead,
15+
Schema: map[string]*schema.Schema{
16+
"name": {
17+
Type: schema.TypeString,
18+
Required: true,
19+
},
20+
"default_group": {
21+
Type: schema.TypeBool,
22+
Computed: true,
23+
},
24+
"creation": {
25+
Type: schema.TypeString,
26+
Computed: true,
27+
},
28+
"last_update": {
29+
Type: schema.TypeString,
30+
Computed: true,
31+
},
32+
"description": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
},
36+
"role": {
37+
Type: schema.TypeString,
38+
Computed: true,
39+
},
40+
},
41+
}
42+
}
43+
44+
func dataSourceMeIdentityGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
45+
config := meta.(*Config)
46+
47+
group := d.Get("name").(string)
48+
49+
endpoint := fmt.Sprintf("/me/identity/group/%s", group)
50+
51+
var resp MeIdentityGroupResponse
52+
err := config.OVHClient.GetWithContext(
53+
ctx,
54+
endpoint,
55+
&resp,
56+
)
57+
if err != nil {
58+
return diag.Errorf("Unable to get identity group detail:\n\t %q", err)
59+
}
60+
61+
log.Printf("[DEBUG] identity groups: %+v", resp)
62+
63+
d.SetId(group)
64+
65+
d.Set("name", resp.Name)
66+
d.Set("default_group", resp.DefaultGroup)
67+
d.Set("last_update", resp.LastUpdate)
68+
d.Set("creation", resp.Creation)
69+
d.Set("description", resp.Description)
70+
d.Set("role", resp.Role)
71+
72+
return nil
73+
}

ovh/data_me_identity_group_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccMeIdentityGroupDataSource_basic(t *testing.T) {
12+
groupName := acctest.RandomWithPrefix(test_prefix)
13+
desc := "Identity group created by Terraform Acc."
14+
role := "NONE"
15+
16+
config := fmt.Sprintf(testAccMeIdentityGroupDatasourceConfig, desc, groupName, role)
17+
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheckCredentials(t) },
20+
Providers: testAccProviders,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: config,
24+
Check: resource.ComposeTestCheckFunc(
25+
checkIdentityGroupResourceAttr("data.ovh_me_identity_group.group_1", groupName, desc, role)...,
26+
),
27+
},
28+
},
29+
})
30+
}
31+
32+
const testAccMeIdentityGroupDatasourceConfig = `
33+
resource "ovh_me_identity_group" "group_1" {
34+
description = "%s"
35+
name = "%s"
36+
role = "%s"
37+
}
38+
39+
data "ovh_me_identity_group" "group_1" {
40+
name = ovh_me_identity_group.group_1.name
41+
depends_on = [ovh_me_identity_group.group_1]
42+
}
43+
`

ovh/data_me_identity_groups.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package ovh
2+
3+
import (
4+
"context"
5+
"log"
6+
"sort"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
11+
)
12+
13+
func dataSourceMeIdentityGroups() *schema.Resource {
14+
return &schema.Resource{
15+
ReadContext: dataSourceMeIdentityGroupsRead,
16+
Schema: map[string]*schema.Schema{
17+
"groups": {
18+
Type: schema.TypeSet,
19+
Elem: &schema.Schema{Type: schema.TypeString},
20+
Computed: true,
21+
},
22+
},
23+
}
24+
}
25+
26+
func dataSourceMeIdentityGroupsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
27+
config := meta.(*Config)
28+
29+
var groups []string
30+
31+
err := config.OVHClient.GetWithContext(
32+
ctx,
33+
"/me/identity/group",
34+
&groups,
35+
)
36+
if err != nil {
37+
return diag.Errorf("Unable to get identity groups:\n\t %q", err)
38+
}
39+
log.Printf("[DEBUG] identity groups: %+v", groups)
40+
41+
sort.Strings(groups)
42+
d.SetId(hashcode.Strings(groups))
43+
d.Set("groups", groups)
44+
45+
return nil
46+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccMeIdentityGroupsDataSource_basic(t *testing.T) {
12+
desc := "Identity group created by Terraform Acc."
13+
role1 := "NONE"
14+
role2 := "ADMIN"
15+
groupName1 := acctest.RandomWithPrefix(test_prefix)
16+
groupName2 := acctest.RandomWithPrefix(test_prefix)
17+
18+
preSetup := fmt.Sprintf(
19+
testAccMeIdentityGroupsDatasourceConfig_preSetup,
20+
desc,
21+
groupName1,
22+
role1,
23+
desc,
24+
groupName2,
25+
role2,
26+
)
27+
config := fmt.Sprintf(
28+
testAccMeIdentityGroupsDatasourceConfig_keys,
29+
desc,
30+
groupName1,
31+
role1,
32+
desc,
33+
groupName2,
34+
role2,
35+
)
36+
37+
checks := checkIdentityGroupResourceAttr("ovh_me_identity_group.group_1", groupName1, desc, role1)
38+
checks = append(checks, checkIdentityGroupResourceAttr("ovh_me_identity_group.group_2", groupName2, desc, role2)...)
39+
40+
resource.Test(t, resource.TestCase{
41+
PreCheck: func() { testAccPreCheckCredentials(t) },
42+
Providers: testAccProviders,
43+
Steps: []resource.TestStep{
44+
{
45+
Config: preSetup,
46+
Check: resource.ComposeTestCheckFunc(checks...),
47+
}, {
48+
Config: config,
49+
Check: resource.ComposeTestCheckFunc(
50+
resource.TestCheckOutput(
51+
"keys_present", "true"),
52+
),
53+
},
54+
},
55+
})
56+
}
57+
58+
func checkIdentityGroupResourceAttr(name, g_name, desc, role string) []resource.TestCheckFunc {
59+
return []resource.TestCheckFunc{
60+
resource.TestCheckResourceAttr(name, "name", g_name),
61+
resource.TestCheckResourceAttr(name, "description", desc),
62+
resource.TestCheckResourceAttr(name, "role", role),
63+
}
64+
}
65+
66+
const testAccMeIdentityGroupsDatasourceConfig_preSetup = `
67+
resource "ovh_me_identity_group" "group_1" {
68+
description = "%s"
69+
name = "%s"
70+
role = "%s"
71+
}
72+
73+
resource "ovh_me_identity_group" "group_2" {
74+
description = "%s"
75+
name = "%s"
76+
role = "%s"
77+
}
78+
`
79+
80+
const testAccMeIdentityGroupsDatasourceConfig_keys = `
81+
resource "ovh_me_identity_group" "group_1" {
82+
description = "%s"
83+
name = "%s"
84+
role = "%s"
85+
}
86+
87+
resource "ovh_me_identity_group" "group_2" {
88+
description = "%s"
89+
name = "%s"
90+
role = "%s"
91+
}
92+
93+
data "ovh_me_identity_groups" "groups" {}
94+
95+
output "keys_present" {
96+
value = tostring(contains(data.ovh_me_identity_groups.groups.groups, ovh_me_identity_group.group_1.name) && contains(data.ovh_me_identity_groups.groups.groups, ovh_me_identity_group.group_2.name))
97+
}
98+
`
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccMeIdentityGroup_importBasic(t *testing.T) {
12+
resourceName := "ovh_me_identity_group.group_1"
13+
groupName := acctest.RandomWithPrefix(test_prefix)
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheckCredentials(t) },
17+
Providers: testAccProviders,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: fmt.Sprintf(testAccMeIdentityGroupConfig_import, groupName),
21+
},
22+
{
23+
ResourceName: resourceName,
24+
ImportState: true,
25+
ImportStateVerify: true,
26+
},
27+
},
28+
})
29+
}
30+
31+
const testAccMeIdentityGroupConfig_import = `
32+
resource "ovh_me_identity_group" "group_1" {
33+
description = "tf acc import test"
34+
name = "%s"
35+
role = "ADMIN"
36+
}
37+
`

ovh/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ func Provider() *schema.Provider {
111111
"ovh_iploadbalancing_vrack_network": dataSourceIpLoadbalancingVrackNetwork(),
112112
"ovh_iploadbalancing_vrack_networks": dataSourceIpLoadbalancingVrackNetworks(),
113113
"ovh_me": dataSourceMe(),
114+
"ovh_me_identity_group": dataSourceMeIdentityGroup(),
115+
"ovh_me_identity_groups": dataSourceMeIdentityGroups(),
114116
"ovh_me_identity_user": dataSourceMeIdentityUser(),
115117
"ovh_me_identity_users": dataSourceMeIdentityUsers(),
116118
"ovh_me_installation_template": dataSourceMeInstallationTemplate(),
@@ -195,6 +197,7 @@ func Provider() *schema.Provider {
195197
"ovh_iploadbalancing_tcp_route": resourceIPLoadbalancingTcpRoute(),
196198
"ovh_iploadbalancing_tcp_route_rule": resourceIPLoadbalancingTcpRouteRule(),
197199
"ovh_iploadbalancing_vrack_network": resourceIPLoadbalancingVrackNetwork(),
200+
"ovh_me_identity_group": resourceMeIdentityGroup(),
198201
"ovh_me_identity_user": resourceMeIdentityUser(),
199202
"ovh_me_installation_template": resourceMeInstallationTemplate(),
200203
"ovh_me_installation_template_partition_scheme": resourceMeInstallationTemplatePartitionScheme(),

0 commit comments

Comments
 (0)