-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added data source 'cloudtemple_backup_iaas_opensource_policy'
- Loading branch information
Showing
5 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
120
internal/provider/data_source_backup_openiaas_policy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters