Skip to content

Commit

Permalink
feat: Add data source for Snapshots, add documentation, fix version i…
Browse files Browse the repository at this point in the history
…n user agent
  • Loading branch information
adeatcu-ionos committed Jul 23, 2024
1 parent 0a04ba4 commit f961eb0
Show file tree
Hide file tree
Showing 62 changed files with 606 additions and 424 deletions.
2 changes: 2 additions & 0 deletions docs/data-sources/dbaas_redisdb_replica_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ data "ionoscloud_redis_replicaset" "example" {
}
```

## Argument Reference

* `id` - (Optional) The ID of the RedisDB Replica Set.
* `display_name` - (Optional) The display name of the RedisDB Replica Set.
* `location` - (Required) The location of the RedisDB Replica Set.
Expand Down
35 changes: 35 additions & 0 deletions docs/data-sources/dbaas_redisdb_snapshot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
subcategory: "Database as a Service - RedisDB"
layout: "ionoscloud"
page_title: "IonosCloud: ionoscloud_redis_snapshot"
sidebar_current: "docs-datasource-redis_snapshot"
description: |-
Gets information about an existing RedisDB Snapshot.
---

# ionoscloud_redis_snapshot

The `ionoscloud_redis_snapshot` data source can be used to retrieve information about an existing RedisDB Snapshot.

## Example Usage

```hcl
data "ionoscloud_redis_snapshot" "example" {
id = "snapshot-id"
location = "de/txl"
}
```

## Argument Reference

* `id` - (Required) The ID of the RedisDB Snapshot.
* `location` - (Required) The location of the RedisDB Snapshot.

## Attributes Reference

* `metadata` - Metadata of the snapshot.
* `created_date` - The ISO 8601 creation timestamp.
* `datacenter_id` - The ID of the datacenter in which the snapshot is located.
* `last_modified_date` - The ISO 8601 modified timestamp.
* `replica_set_id` - The ID of the replica set from which the snapshot was created.
* `snapshot_time` - The time at which the snapshot was taken.
1 change: 1 addition & 0 deletions ionoscloud/data_source_dbaas_redisdb_replicaset.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func dataSourceDBaaSRedisDBReplicaSet() *schema.Resource {
},
},
},
Timeouts: &resourceDefaultTimeouts,
}
}

Expand Down
80 changes: 80 additions & 0 deletions ionoscloud/data_source_dbaas_redisdb_snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package ionoscloud

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils/constant"
)

func dataSourceDBaaSRedisDBSnapshot() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDBaaSRedisDBSnapshotRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "The ID of the snapshot.",
Required: true,
},
"location": {
Type: schema.TypeString,
Description: "The location of the snapshot.",
Required: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(constant.DBaaSClusterLocations, false)),
},
"metadata": {
Type: schema.TypeList,
Description: "The metadata of the snapshot.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"created_date": {
Type: schema.TypeString,
Description: "The ISO 8601 creation timestamp.",
Computed: true,
},
"last_modified_date": {
Type: schema.TypeString,
Description: "The ISO 8601 modified timestamp.",
Computed: true,
},
"replica_set_id": {
Type: schema.TypeString,
Description: "The ID of the Redis replica set the snapshot is taken from.",
Computed: true,
},
"snapshot_time": {
Type: schema.TypeString,
Description: "The time the snapshot was dumped from the replica set.",
Computed: true,
},
"datacenter_id": {
Type: schema.TypeString,
Description: "The ID of the datacenter the snapshot was created in. Please mind, that the snapshot is not available in other datacenters.",
Computed: true,
},
},
},
},
},
Timeouts: &resourceDefaultTimeouts,
}
}

func dataSourceDBaaSRedisDBSnapshotRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(services.SdkBundle).RedisDBClient
id := d.Get("id").(string)
location := d.Get("location").(string)

snapshot, _, err := client.GetSnapshot(ctx, id, location)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occurred while fetching the Redis snapshot with ID: %v", id, err))
}
if err := client.SetSnapshotData(d, snapshot); err != nil {
return diag.FromErr(err)
}
return nil
}
1 change: 1 addition & 0 deletions ionoscloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func Provider() *schema.Provider {
constant.TargetGroupResource: dataSourceTargetGroup(),
constant.DBaasMongoUserResource: dataSourceDbaasMongoUser(),
constant.DBaaSRedisDBReplicaSetResource: dataSourceDBaaSRedisDBReplicaSet(),
constant.DBaaSRedisDBSnapshotResource: dataSourceDBaaSRedisDBSnapshot(),
constant.CertificateResource: dataSourceCertificate(),
constant.ContainerRegistryResource: dataSourceContainerRegistry(),
constant.ContainerRegistryTokenResource: dataSourceContainerRegistryToken(),
Expand Down
3 changes: 1 addition & 2 deletions services/dbaas/redisdb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package redisdb
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/meta"
psql "github.com/ionos-cloud/sdk-go-dbaas-postgres"
redis "github.com/ionos-cloud/sdk-go-dbaas-redis"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/utils/constant"
Expand All @@ -28,7 +27,7 @@ func NewRedisDBClient(username, password, token, url, version, terraformVersion
newConfigDbaas.HTTPClient = &http.Client{Transport: utils.CreateTransport()}
newConfigDbaas.UserAgent = fmt.Sprintf(
"terraform-provider/%s_ionos-cloud-sdk-go-dbaas-redisdb/%s_hashicorp-terraform/%s_terraform-plugin-sdk/%s_os/%s_arch/%s",
version, psql.Version, terraformVersion, meta.SDKVersionString(), runtime.GOOS, runtime.GOARCH)
version, redis.Version, terraformVersion, meta.SDKVersionString(), runtime.GOOS, runtime.GOARCH)

return &RedisDBClient{
sdkClient: redis.NewAPIClient(newConfigDbaas),
Expand Down
39 changes: 39 additions & 0 deletions services/dbaas/redisdb/replica_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ func (c *RedisDBClient) GetReplicaSet(ctx context.Context, replicaSetID, locatio
return replicaSet, apiResponse, err
}

func (c *RedisDBClient) GetSnapshot(ctx context.Context, snapshotID, location string) (redisdb.SnapshotRead, *redisdb.APIResponse, error) {
c.modifyConfigURL(location)
snapshot, apiResponse, err := c.sdkClient.SnapshotApi.SnapshotsFindById(ctx, snapshotID).Execute()
apiResponse.LogInfo()
return snapshot, apiResponse, err
}

func (c *RedisDBClient) ListReplicaSets(ctx context.Context, filterName, location string) (redisdb.ReplicaSetReadList, *redisdb.APIResponse, error) {
c.modifyConfigURL(location)
request := c.sdkClient.ReplicaSetApi.ReplicasetsGet(ctx)
Expand Down Expand Up @@ -266,6 +273,38 @@ func (c *RedisDBClient) SetRedisDBReplicaSetData(d *schema.ResourceData, replica
return nil
}

func (c *RedisDBClient) SetSnapshotData(d *schema.ResourceData, snapshot redisdb.SnapshotRead) error {
if snapshot.Id == nil {
return fmt.Errorf("expected a valid ID for RedisDB snapshot, but got 'nil' instead")
}
d.SetId(*snapshot.Id)
if snapshot.Metadata == nil {
return fmt.Errorf("response metadata should not be empty for RedisDB snapshot with ID: %v", *snapshot.Id)
}
var metadata []interface{}
metadataEntry := make(map[string]interface{})
if snapshot.Metadata.CreatedDate != nil {
metadataEntry["created_date"] = (*snapshot.Metadata.CreatedDate).Time.Format("2006-01-02T15:04:05Z")
}
if snapshot.Metadata.LastModifiedDate != nil {
metadataEntry["last_modified_date"] = (*snapshot.Metadata.LastModifiedDate).Time.Format("2006-01-02T15:04:05Z")
}
if snapshot.Metadata.ReplicasetId != nil {
metadataEntry["replica_set_id"] = *snapshot.Metadata.ReplicasetId
}
if snapshot.Metadata.SnapshotTime != nil {
metadataEntry["snapshot_time"] = (*snapshot.Metadata.SnapshotTime).Time.Format("2006-01-02T15:04:05Z")
}
if snapshot.Metadata.DatacenterId != nil {
metadataEntry["datacenter_id"] = *snapshot.Metadata.DatacenterId
}
metadata = append(metadata, metadataEntry)
if err := d.Set("metadata", metadata); err != nil {
return utils.GenerateSetError(constant.DBaaSRedisDBSnapshotResource, "metadata", err)
}
return nil
}

// getResources returns information about the 'resources' attribute defined in the tf configuration
// for the ReplicaSet resource, this information will be latter used to populate the request.
func getResources(d *schema.ResourceData) *redisdb.Resources {
Expand Down
1 change: 1 addition & 0 deletions utils/constant/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ const (
DBaaSReplicaSetTestDataSourceByID = "test_dbaas_replicaset_id"
DBaaSReplicaSetTestDataSourceByName = "test_dbaas_replicaset_name"
DBaaSRedisDBReplicaSetResource = "ionoscloud_redis_replicaset"
DBaaSRedisDBSnapshotResource = "ionoscloud_redis_snapshot"
)

// DBaaSClusterLocations slice represents the locations in which DBaaS clusters can be created
Expand Down
4 changes: 2 additions & 2 deletions vendor/github.com/ProtonMail/go-crypto/openpgp/keys.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions vendor/github.com/ProtonMail/go-crypto/openpgp/s2k/s2k.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/github.com/google/uuid/dce.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/github.com/google/uuid/hash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/google/uuid/node_js.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f961eb0

Please sign in to comment.