Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for IAM policies (beta) #424

Merged
merged 3 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions ovh/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"log"
"sync"
"time"

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

type Config struct {
Account string
Plate string
Endpoint string
ApplicationKey string
ApplicationSecret string
Expand All @@ -24,17 +25,6 @@ type Config struct {
lockAuth *sync.Mutex
}

type OvhAuthCurrentCredential struct {
OvhSupport bool `json:"ovhSupport"`
Status string `json:"status"`
ApplicationId int64 `json:"applicationId"`
CredentialId int64 `json:"credentialId"`
Rules []ovh.AccessRule `json:"rules"`
Expiration time.Time `json:"expiration"`
LastUse time.Time `json:"lastUse"`
Creation time.Time `json:"creation"`
}

func clientDefault(c *Config) (*ovh.Client, error) {
client, err := ovh.NewClient(
c.Endpoint,
Expand Down Expand Up @@ -63,15 +53,21 @@ func (c *Config) loadAndValidate() error {
}

if !c.authenticated {
var cred OvhAuthCurrentCredential
if err := c.OVHClient.Get("/auth/currentCredential", &cred); err != nil {
var details OvhAuthDetails
if err := c.OVHClient.Get("/auth/details", &details); err != nil {
c.authFailed = fmt.Errorf("OVH client seems to be misconfigured: %q\n", err)
return c.authFailed
}

log.Printf("[DEBUG] Logged in on OVH API")
c.Account = details.Account
c.authenticated = true
}

if c.Plate == "" {
c.Plate = plateFromEndpoint(c.Endpoint)
}

return nil
}

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

return nil
}

var plateMapping map[string]string = map[string]string{
"ovh-eu": "eu",
"ovh-ca": "ca",
"ovh-us": "us",
"kimsufi-eu": "eu",
"kimsufi-ca": "ca",
"soyoustart-eu": "eu",
"soyoustart-ca": "ca",
}

func plateFromEndpoint(endpoint string) string {
return plateMapping[endpoint]
}
6 changes: 6 additions & 0 deletions ovh/data_dbaas_logs_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)

func dataSourceDbaasLogsCluster() *schema.Resource {
Expand All @@ -20,6 +21,10 @@ func dataSourceDbaasLogsCluster() *schema.Resource {
Required: true,
},
// Computed
"urn": {
Type: schema.TypeString,
Computed: true,
},
"cluster_type": {
Type: schema.TypeString,
Description: "Cluster type",
Expand Down Expand Up @@ -114,6 +119,7 @@ func dataSourceDbaasLogsClusterRead(d *schema.ResourceData, meta interface{}) er
}

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

endpoint := fmt.Sprintf(
"/dbaas/logs/%s/cluster/%s",
Expand Down
5 changes: 5 additions & 0 deletions ovh/data_dedicated_ceph.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func dataSourceDedicatedCeph() *schema.Resource {
Type: schema.TypeString,
},
},
"urn": {
Type: schema.TypeString,
Computed: true,
},
"ceph_version": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -84,6 +88,7 @@ func dataSourceDedicatedCephRead(d *schema.ResourceData, meta interface{}) error
}
log.Printf("[DEBUG] CEPH is %v", ceph.CephMonitors)
d.SetId(ceph.ServiceName)
d.Set("urn", helpers.ServiceURN(config.Plate, "dedicatedCeph", ceph.ServiceName))
d.Set("service_name", ceph.ServiceName)
d.Set("ceph_mons", ceph.CephMonitors)
d.Set("ceph_version", ceph.CephVersion)
Expand Down
9 changes: 8 additions & 1 deletion ovh/data_dedicated_nasha.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package ovh
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

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

// Computed
"urn": {
Type: schema.TypeString,
Computed: true,
},
"can_create_partition": {
Type: schema.TypeBool,
Computed: true,
Expand Down Expand Up @@ -86,6 +92,7 @@ func dataSourceDedicatedNashaRead(c context.Context, d *schema.ResourceData, met
}

d.SetId(ds.ServiceName)
d.Set("urn", helpers.ServiceURN(config.Plate, "nasHA", ds.ServiceName))
d.Set("service_name", ds.ServiceName)
d.Set("monitored", ds.Monitored)
d.Set("zpool_size", ds.ZpoolSize)
Expand Down
6 changes: 6 additions & 0 deletions ovh/data_dedicated_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)

func dataSourceDedicatedServer() *schema.Resource {
Expand All @@ -18,6 +19,10 @@ func dataSourceDedicatedServer() *schema.Resource {
},

// Computed
"urn": {
Type: schema.TypeString,
Computed: true,
},
"boot_id": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -196,6 +201,7 @@ func dataSourceDedicatedServerRead(d *schema.ResourceData, meta interface{}) err
}

d.SetId(ds.Name)
d.Set("urn", helpers.ServiceURN(config.Plate, "dedicatedServer", ds.Name))
d.Set("boot_id", ds.BootId)
d.Set("commercial_range", ds.CommercialRange)
d.Set("datacenter", ds.Datacenter)
Expand Down
7 changes: 7 additions & 0 deletions ovh/data_domain_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)

func dataSourceDomainZone() *schema.Resource {
Expand All @@ -16,6 +17,10 @@ func dataSourceDomainZone() *schema.Resource {
},

// Computed
"urn": {
Type: schema.TypeString,
Computed: true,
},
"has_dns_anycast": {
Type: schema.TypeBool,
Computed: true,
Expand Down Expand Up @@ -55,5 +60,7 @@ func dataSourceDomainZoneRead(d *schema.ResourceData, meta interface{}) error {
d.Set("last_update", dz.LastUpdate)
d.Set("name_servers", dz.NameServers)

d.Set("urn", helpers.ServiceURN(config.Plate, "dnsZone", zoneName))

return nil
}
6 changes: 6 additions & 0 deletions ovh/data_hosting_privatedatabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)

func dataSourceHostingPrivateDatabase() *schema.Resource {
Expand All @@ -17,6 +18,10 @@ func dataSourceHostingPrivateDatabase() *schema.Resource {
},

// Computed
"urn": {
Type: schema.TypeString,
Computed: true,
},
"cpu": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -138,6 +143,7 @@ func dataSourceHostingPrivateDatabaseRead(d *schema.ResourceData, meta interface
}
}
d.SetId(ds.ServiceName)
d.Set("urn", helpers.ServiceURN(config.Plate, "webCloudDatabases", ds.ServiceName))

return nil
}
46 changes: 46 additions & 0 deletions ovh/data_iam_policies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ovh

import (
"context"
"sort"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
)

func dataSourceIamPolicies() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
fehrnah marked this conversation as resolved.
Show resolved Hide resolved
"policies": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
ReadContext: datasourceIamPoliciesRead,
}
}

func datasourceIamPoliciesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
config := meta.(*Config)

var policies []IamPolicy
err := config.OVHClient.GetWithContext(ctx, "/v2/iam/policy", &policies)
if err != nil {
return diag.FromErr(err)
}

var polIDs []string
for _, p := range policies {
polIDs = append(polIDs, p.Id)
}

d.Set("policies", polIDs)

sort.Strings(polIDs)
d.SetId(hashcode.Strings(polIDs))
return nil
}
93 changes: 93 additions & 0 deletions ovh/data_iam_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package ovh

import (
"context"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceIamPolicy() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"identities": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"resources": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"allow": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"except": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"owner": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
"read_only": {
Type: schema.TypeBool,
Computed: true,
},
},
ReadContext: datasourceIamPolicyRead,
}
}

func datasourceIamPolicyRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
config := meta.(*Config)
id := d.Get("id").(string)

var pol IamPolicy
err := config.OVHClient.GetWithContext(ctx, "/v2/iam/policy/"+url.PathEscape(id), &pol)
if err != nil {
return diag.FromErr(err)
}

for k, v := range pol.ToMap() {
err := d.Set(k, v)
if err != nil {
return diag.Errorf("key: %s; value: %v; err: %v", k, v, err)
}
}
d.SetId(id)
return nil
}
Loading