From bf646f64cfa816ef331457dec4716bd063c5ce2c Mon Sep 17 00:00:00 2001 From: Paul BESRET Date: Fri, 8 Nov 2024 14:59:26 +0100 Subject: [PATCH] Added data source 'cloudtemple_backup_iaas_opensource_policy' --- .../backup_iaas_opensource_policy.md | 54 ++++++++ internal/client/backup_openiaas.go | 9 ++ internal/client/backup_openiaas_policy.go | 69 ++++++++++ .../data_source_backup_openiaas_policy.go | 120 ++++++++++++++++++ internal/provider/provider.go | 3 + 5 files changed, 255 insertions(+) create mode 100644 docs/data-sources/backup_iaas_opensource_policy.md create mode 100644 internal/client/backup_openiaas.go create mode 100644 internal/client/backup_openiaas_policy.go create mode 100644 internal/provider/data_source_backup_openiaas_policy.go diff --git a/docs/data-sources/backup_iaas_opensource_policy.md b/docs/data-sources/backup_iaas_opensource_policy.md new file mode 100644 index 0000000..66100f4 --- /dev/null +++ b/docs/data-sources/backup_iaas_opensource_policy.md @@ -0,0 +1,54 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "cloudtemple_backup_iaas_opensource_policy Data Source - terraform-provider-cloudtemple" +subcategory: "Backup" +description: |- + Used to retrieve a specific backup policy from an Open IaaS infrastructure. + To query this datasource you will need the backup_iaas_opensource_read role. +--- + +# cloudtemple_backup_iaas_opensource_policy (Data Source) + +Used to retrieve a specific backup policy from an Open IaaS infrastructure. + +To query this datasource you will need the `backup_iaas_opensource_read` role. + + + + +## Schema + +### Optional + +- `machine_manager_id` (String) +- `name` (String) + +### Read-Only + +- `id` (String) The ID of this resource. +- `internal_id` (String) +- `machine_manager` (List of Object) (see [below for nested schema](#nestedatt--machine_manager)) +- `mode` (String) +- `running` (Boolean) +- `schedulers` (List of Object) (see [below for nested schema](#nestedatt--schedulers)) + + +### Nested Schema for `machine_manager` + +Read-Only: + +- `id` (String) +- `name` (String) + + + +### Nested Schema for `schedulers` + +Read-Only: + +- `cron` (String) +- `retention` (Number) +- `temporarily_disabled` (Boolean) +- `timezone` (String) + + diff --git a/internal/client/backup_openiaas.go b/internal/client/backup_openiaas.go new file mode 100644 index 0000000..3a51c71 --- /dev/null +++ b/internal/client/backup_openiaas.go @@ -0,0 +1,9 @@ +package client + +type BackupOpenIaasClient struct { + c *BackupClient +} + +func (c *BackupClient) OpenIaaS() *BackupOpenIaasClient { + return &BackupOpenIaasClient{c} +} diff --git a/internal/client/backup_openiaas_policy.go b/internal/client/backup_openiaas_policy.go new file mode 100644 index 0000000..3cb8aad --- /dev/null +++ b/internal/client/backup_openiaas_policy.go @@ -0,0 +1,69 @@ +package client + +import "context" + +type BackupOpenIaasPolicyClient struct { + c *Client +} + +func (c *BackupOpenIaasClient) Policy() *BackupOpenIaasPolicyClient { + return &BackupOpenIaasPolicyClient{c.c.c} +} + +type BackupOpenIaasPolicy struct { + ID string `terraform:"id"` + Name string `terraform:"name"` + InternalID string `terraform:"internal_id"` + Running bool `terraform:"running"` + Mode string `terraform:"mode"` + MachineManager struct { + ID string `terraform:"id"` + Name string `terraform:"name"` + } `terraform:"machine_manager"` + Schedulers []struct { + TemporarilyDisabled bool `terraform:"temporarily_disabled"` + Retention int `terraform:"retention"` + Cron string `terraform:"cron"` + Timezone string `terraform:"timezone"` + } `terraform:"schedulers"` +} + +func (v *BackupOpenIaasPolicyClient) Read(ctx context.Context, id string) (*BackupOpenIaasPolicy, error) { + r := v.c.newRequest("GET", "/backup/v1/open_iaas/policies/%s", id) + resp, err := v.c.doRequest(ctx, r) + if err != nil { + return nil, err + } + defer closeResponseBody(resp) + found, err := requireNotFoundOrOK(resp, 403) + if err != nil || !found { + return nil, err + } + + var out BackupOpenIaasPolicy + if err := decodeBody(resp, &out); err != nil { + return nil, err + } + + return &out, nil +} + +func (v *BackupOpenIaasPolicyClient) List(ctx context.Context) ([]*BackupOpenIaasPolicy, error) { + r := v.c.newRequest("GET", "/backup/v1/open_iaas/policies") + resp, err := v.c.doRequest(ctx, r) + if err != nil { + return nil, err + } + defer closeResponseBody(resp) + found, err := requireNotFoundOrOK(resp, 403) + if err != nil || !found { + return nil, err + } + + var out []*BackupOpenIaasPolicy + if err := decodeBody(resp, &out); err != nil { + return nil, err + } + + return out, nil +} diff --git a/internal/provider/data_source_backup_openiaas_policy.go b/internal/provider/data_source_backup_openiaas_policy.go new file mode 100644 index 0000000..91d0f39 --- /dev/null +++ b/internal/provider/data_source_backup_openiaas_policy.go @@ -0,0 +1,120 @@ +package provider + +import ( + "context" + "fmt" + + "github.com/cloud-temple/terraform-provider-cloudtemple/internal/client" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +func dataSourceOpenIaasBackupPolicy() *schema.Resource { + return &schema.Resource{ + Description: "Used to retrieve a specific backup policy from an Open IaaS infrastructure.", + + ReadContext: readFullResource(func(ctx context.Context, c *client.Client, d *schema.ResourceData, sw *stateWriter) (interface{}, error) { + id := d.Get("id").(string) + if id != "" { + policy, err := c.Backup().OpenIaaS().Policy().Read(ctx, id) + if err == nil && policy == nil { + return nil, fmt.Errorf("failed to find backup policy with id %q", id) + } + return policy, err + } + + name := d.Get("name").(string) + if name != "" { + policies, err := c.Backup().OpenIaaS().Policy().List(ctx) + if err != nil { + return nil, fmt.Errorf("failed to list backup policies: %s", err) + } + for _, policy := range policies { + if policy.Name == name { + return policy, nil + } + } + return nil, fmt.Errorf("failed to find backup policy named %q", name) + } + + return nil, fmt.Errorf("either id or name must be specified") + }), + + Schema: map[string]*schema.Schema{ + // In + "id": { + Type: schema.TypeString, + Optional: true, + ConflictsWith: []string{"name"}, + AtLeastOneOf: []string{"id", "name"}, + ValidateFunc: validation.IsUUID, + }, + "name": { + Type: schema.TypeString, + Optional: true, + ConflictsWith: []string{"id"}, + AtLeastOneOf: []string{"id", "name"}, + }, + "machine_manager_id": { + Type: schema.TypeString, + Optional: true, + ConflictsWith: []string{"id"}, + RequiredWith: []string{"name"}, + }, + + // Out + "internal_id": { + Type: schema.TypeString, + Computed: true, + }, + "running": { + Type: schema.TypeBool, + Computed: true, + }, + "mode": { + Type: schema.TypeString, + Computed: true, + }, + "machine_manager": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + "schedulers": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "temporarily_disabled": { + Type: schema.TypeBool, + Computed: true, + }, + "retention": { + Type: schema.TypeInt, + Computed: true, + }, + "cron": { + Type: schema.TypeString, + Computed: true, + }, + "timezone": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 15187a9..6472f53 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -120,6 +120,9 @@ func New(version string) func() *schema.Provider { "cloudtemple_compute_machine_manager": documentDatasource(dataSourceWorker(), "compute_iaas_vmware_read"), "cloudtemple_compute_machine_managers": documentDatasource(dataSourceWorkers(), "compute_iaas_vmware_read"), + // Backup - Open IaaS + "cloudtemple_backup_iaas_opensource_policy": documentDatasource(dataSourceOpenIaasBackupPolicy(), "backup_iaas_opensource_read"), + // Compute - Open IaaS "cloudtemple_compute_iaas_opensource_host": documentDatasource(dataSourceOpenIaasHost(), "compute_iaas_opensource_infrastructure_read"), "cloudtemple_compute_iaas_opensource_storage_repository": documentDatasource(dataSourceOpenIaasStorageRepository(), "compute_iaas_opensource_infrastructure_read"),