Skip to content

Commit

Permalink
Added data source 'cloudtemple_backup_iaas_opensource_policy'
Browse files Browse the repository at this point in the history
  • Loading branch information
pbesret committed Nov 8, 2024
1 parent dab97b3 commit bf646f6
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/data-sources/backup_iaas_opensource_policy.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## 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))

<a id="nestedatt--machine_manager"></a>
### Nested Schema for `machine_manager`

Read-Only:

- `id` (String)
- `name` (String)


<a id="nestedatt--schedulers"></a>
### Nested Schema for `schedulers`

Read-Only:

- `cron` (String)
- `retention` (Number)
- `temporarily_disabled` (Boolean)
- `timezone` (String)


9 changes: 9 additions & 0 deletions internal/client/backup_openiaas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package client

type BackupOpenIaasClient struct {
c *BackupClient
}

func (c *BackupClient) OpenIaaS() *BackupOpenIaasClient {
return &BackupOpenIaasClient{c}
}
69 changes: 69 additions & 0 deletions internal/client/backup_openiaas_policy.go
Original file line number Diff line number Diff line change
@@ -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
}
120 changes: 120 additions & 0 deletions internal/provider/data_source_backup_openiaas_policy.go
Original file line number Diff line number Diff line change
@@ -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,
},
},
},
},
},
}
}
3 changes: 3 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit bf646f6

Please sign in to comment.