Skip to content

Commit 4e06748

Browse files
committed
feat: support IAM policies
Signed-off-by: Nicolas FOURNIER <[email protected]>
1 parent 77cde4c commit 4e06748

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1164
-31
lines changed

ovh/config.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"log"
66
"sync"
7-
"time"
87

98
cleanhttp "github.com/hashicorp/go-cleanhttp"
109
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
@@ -14,6 +13,8 @@ import (
1413
var providerVersion, providerCommit string
1514

1615
type Config struct {
16+
Account string
17+
Plate string
1718
Endpoint string
1819
ApplicationKey string
1920
ApplicationSecret string
@@ -24,17 +25,6 @@ type Config struct {
2425
lockAuth *sync.Mutex
2526
}
2627

27-
type OvhAuthCurrentCredential struct {
28-
OvhSupport bool `json:"ovhSupport"`
29-
Status string `json:"status"`
30-
ApplicationId int64 `json:"applicationId"`
31-
CredentialId int64 `json:"credentialId"`
32-
Rules []ovh.AccessRule `json:"rules"`
33-
Expiration time.Time `json:"expiration"`
34-
LastUse time.Time `json:"lastUse"`
35-
Creation time.Time `json:"creation"`
36-
}
37-
3828
func clientDefault(c *Config) (*ovh.Client, error) {
3929
client, err := ovh.NewClient(
4030
c.Endpoint,
@@ -63,15 +53,21 @@ func (c *Config) loadAndValidate() error {
6353
}
6454

6555
if !c.authenticated {
66-
var cred OvhAuthCurrentCredential
67-
if err := c.OVHClient.Get("/auth/currentCredential", &cred); err != nil {
56+
var details OvhAuthDetails
57+
if err := c.OVHClient.Get("/auth/details", &details); err != nil {
6858
c.authFailed = fmt.Errorf("OVH client seems to be misconfigured: %q\n", err)
6959
return c.authFailed
7060
}
7161

7262
log.Printf("[DEBUG] Logged in on OVH API")
63+
c.Account = details.Account
7364
c.authenticated = true
7465
}
66+
67+
if c.Plate == "" {
68+
c.Plate = plateFromEndpoint(c.Endpoint)
69+
}
70+
7571
return nil
7672
}
7773

@@ -106,3 +102,17 @@ func (c *Config) load() error {
106102

107103
return nil
108104
}
105+
106+
var plateMapping map[string]string = map[string]string{
107+
"ovh-eu": "eu",
108+
"ovh-ca": "ca",
109+
"ovh-us": "us",
110+
"kimsufi-eu": "eu",
111+
"kimsufi-ca": "ca",
112+
"soyoustart-eu": "eu",
113+
"soyoustart-ca": "ca",
114+
}
115+
116+
func plateFromEndpoint(endpoint string) string {
117+
return plateMapping[endpoint]
118+
}

ovh/data_dbaas_logs_cluster.go

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/url"
77

88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
910
)
1011

1112
func dataSourceDbaasLogsCluster() *schema.Resource {
@@ -20,6 +21,10 @@ func dataSourceDbaasLogsCluster() *schema.Resource {
2021
Required: true,
2122
},
2223
// Computed
24+
"urn": {
25+
Type: schema.TypeString,
26+
Computed: true,
27+
},
2328
"cluster_type": {
2429
Type: schema.TypeString,
2530
Description: "Cluster type",
@@ -114,6 +119,7 @@ func dataSourceDbaasLogsClusterRead(d *schema.ResourceData, meta interface{}) er
114119
}
115120

116121
d.SetId(cluster_id)
122+
d.Set("urn", helpers.ServiceURN(config.Plate, "ldp", serviceName))
117123

118124
endpoint := fmt.Sprintf(
119125
"/dbaas/logs/%s/cluster/%s",

ovh/data_dedicated_ceph.go

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ func dataSourceDedicatedCeph() *schema.Resource {
2020
Type: schema.TypeString,
2121
},
2222
},
23+
"urn": {
24+
Type: schema.TypeString,
25+
Computed: true,
26+
},
2327
"ceph_version": {
2428
Type: schema.TypeString,
2529
Optional: true,
@@ -84,6 +88,7 @@ func dataSourceDedicatedCephRead(d *schema.ResourceData, meta interface{}) error
8488
}
8589
log.Printf("[DEBUG] CEPH is %v", ceph.CephMonitors)
8690
d.SetId(ceph.ServiceName)
91+
d.Set("urn", helpers.ServiceURN(config.Plate, "dedicatedCeph", ceph.ServiceName))
8792
d.Set("service_name", ceph.ServiceName)
8893
d.Set("ceph_mons", ceph.CephMonitors)
8994
d.Set("ceph_version", ceph.CephVersion)

ovh/data_dedicated_nasha.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package ovh
33
import (
44
"context"
55
"fmt"
6-
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
76
"net/url"
87

8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
10+
911
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1012
)
1113

@@ -20,6 +22,10 @@ func dataSourceDedicatedNasha() *schema.Resource {
2022
},
2123

2224
// Computed
25+
"urn": {
26+
Type: schema.TypeString,
27+
Computed: true,
28+
},
2329
"can_create_partition": {
2430
Type: schema.TypeBool,
2531
Computed: true,
@@ -86,6 +92,7 @@ func dataSourceDedicatedNashaRead(c context.Context, d *schema.ResourceData, met
8692
}
8793

8894
d.SetId(ds.ServiceName)
95+
d.Set("urn", helpers.ServiceURN(config.Plate, "nasHA", ds.ServiceName))
8996
d.Set("service_name", ds.ServiceName)
9097
d.Set("monitored", ds.Monitored)
9198
d.Set("zpool_size", ds.ZpoolSize)

ovh/data_dedicated_server.go

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/url"
77

88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
910
)
1011

1112
func dataSourceDedicatedServer() *schema.Resource {
@@ -18,6 +19,10 @@ func dataSourceDedicatedServer() *schema.Resource {
1819
},
1920

2021
// Computed
22+
"urn": {
23+
Type: schema.TypeString,
24+
Computed: true,
25+
},
2126
"boot_id": {
2227
Type: schema.TypeInt,
2328
Computed: true,
@@ -196,6 +201,7 @@ func dataSourceDedicatedServerRead(d *schema.ResourceData, meta interface{}) err
196201
}
197202

198203
d.SetId(ds.Name)
204+
d.Set("urn", helpers.ServiceURN(config.Plate, "dedicatedServer", ds.Name))
199205
d.Set("boot_id", ds.BootId)
200206
d.Set("commercial_range", ds.CommercialRange)
201207
d.Set("datacenter", ds.Datacenter)

ovh/data_domain_zone.go

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
78
)
89

910
func dataSourceDomainZone() *schema.Resource {
@@ -16,6 +17,10 @@ func dataSourceDomainZone() *schema.Resource {
1617
},
1718

1819
// Computed
20+
"urn": {
21+
Type: schema.TypeString,
22+
Computed: true,
23+
},
1924
"has_dns_anycast": {
2025
Type: schema.TypeBool,
2126
Computed: true,
@@ -55,5 +60,7 @@ func dataSourceDomainZoneRead(d *schema.ResourceData, meta interface{}) error {
5560
d.Set("last_update", dz.LastUpdate)
5661
d.Set("name_servers", dz.NameServers)
5762

63+
d.Set("urn", helpers.ServiceURN(config.Plate, "dnsZone", zoneName))
64+
5865
return nil
5966
}

ovh/data_hosting_privatedatabase.go

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/url"
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
89
)
910

1011
func dataSourceHostingPrivateDatabase() *schema.Resource {
@@ -17,6 +18,10 @@ func dataSourceHostingPrivateDatabase() *schema.Resource {
1718
},
1819

1920
// Computed
21+
"urn": {
22+
Type: schema.TypeString,
23+
Computed: true,
24+
},
2025
"cpu": {
2126
Type: schema.TypeInt,
2227
Computed: true,
@@ -138,6 +143,7 @@ func dataSourceHostingPrivateDatabaseRead(d *schema.ResourceData, meta interface
138143
}
139144
}
140145
d.SetId(ds.ServiceName)
146+
d.Set("urn", helpers.ServiceURN(config.Plate, "webCloudDatabases", ds.ServiceName))
141147

142148
return nil
143149
}

ovh/data_iam_policies.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package ovh
2+
3+
import (
4+
"context"
5+
"sort"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
10+
)
11+
12+
func dataSourceIamPolicies() *schema.Resource {
13+
return &schema.Resource{
14+
Schema: map[string]*schema.Schema{
15+
"policies": {
16+
Type: schema.TypeSet,
17+
Computed: true,
18+
Elem: &schema.Schema{
19+
Type: schema.TypeString,
20+
},
21+
},
22+
},
23+
ReadContext: datasourceIamPoliciesRead,
24+
}
25+
}
26+
27+
func datasourceIamPoliciesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
28+
config := meta.(*Config)
29+
30+
var policies []IamPolicy
31+
err := config.OVHClient.GetWithContext(ctx, "/v2/iam/policy", &policies)
32+
if err != nil {
33+
return diag.FromErr(err)
34+
}
35+
36+
var polIDs []string
37+
for _, p := range policies {
38+
polIDs = append(polIDs, p.Id)
39+
}
40+
41+
d.Set("policies", polIDs)
42+
43+
sort.Strings(polIDs)
44+
d.SetId(hashcode.Strings(polIDs))
45+
return nil
46+
}

ovh/data_iam_policy.go

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package ovh
2+
3+
import (
4+
"context"
5+
"net/url"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceIamPolicy() *schema.Resource {
12+
return &schema.Resource{
13+
Schema: map[string]*schema.Schema{
14+
"id": {
15+
Type: schema.TypeString,
16+
Required: true,
17+
},
18+
"name": {
19+
Type: schema.TypeString,
20+
Computed: true,
21+
},
22+
"description": {
23+
Type: schema.TypeString,
24+
Optional: true,
25+
},
26+
"identities": {
27+
Type: schema.TypeSet,
28+
Computed: true,
29+
Elem: &schema.Schema{
30+
Type: schema.TypeString,
31+
},
32+
},
33+
"resources": {
34+
Type: schema.TypeSet,
35+
Computed: true,
36+
Elem: &schema.Schema{
37+
Type: schema.TypeString,
38+
},
39+
},
40+
"allow": {
41+
Type: schema.TypeSet,
42+
Optional: true,
43+
Elem: &schema.Schema{
44+
Type: schema.TypeString,
45+
},
46+
},
47+
"except": {
48+
Type: schema.TypeSet,
49+
Optional: true,
50+
Elem: &schema.Schema{
51+
Type: schema.TypeString,
52+
},
53+
},
54+
"owner": {
55+
Type: schema.TypeString,
56+
Computed: true,
57+
},
58+
"created_at": {
59+
Type: schema.TypeString,
60+
Computed: true,
61+
},
62+
"updated_at": {
63+
Type: schema.TypeString,
64+
Computed: true,
65+
},
66+
"read_only": {
67+
Type: schema.TypeBool,
68+
Computed: true,
69+
},
70+
},
71+
ReadContext: datasourceIamPolicyRead,
72+
}
73+
}
74+
75+
func datasourceIamPolicyRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
76+
config := meta.(*Config)
77+
id := d.Get("id").(string)
78+
79+
var pol IamPolicy
80+
err := config.OVHClient.GetWithContext(ctx, "/v2/iam/policy/"+url.PathEscape(id), &pol)
81+
if err != nil {
82+
return diag.FromErr(err)
83+
}
84+
85+
for k, v := range pol.ToMap() {
86+
err := d.Set(k, v)
87+
if err != nil {
88+
return diag.Errorf("key: %s; value: %v; err: %v", k, v, err)
89+
}
90+
}
91+
d.SetId(id)
92+
return nil
93+
}

0 commit comments

Comments
 (0)