Skip to content

Commit

Permalink
Add Postgres flex data source (#41)
Browse files Browse the repository at this point in the history
* add acceptance test to resource + fix found issues

* change to v1.2.1

* add postgres flex data source

Co-authored-by: Dean Oren <deangili.oren@mail.schwarz>
  • Loading branch information
do87 and Dean Oren authored Nov 14, 2022
1 parent 5e63f2b commit 86e849f
Show file tree
Hide file tree
Showing 15 changed files with 659 additions and 57 deletions.
56 changes: 56 additions & 0 deletions docs/data-sources/postgres_flex_instance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackit_postgres_flex_instance Data Source - stackit"
subcategory: ""
description: |-
Data source for Postgres Flex instance
~> Note: Postgres Flex is in 'alpha' stage in STACKIT
---

# stackit_postgres_flex_instance (Data Source)

Data source for Postgres Flex instance

~> **Note:** Postgres Flex is in 'alpha' stage in STACKIT

## Example Usage

```terraform
data "stackit_postgres_flex_instance" "example" {
name = "example"
project_id = "example"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Specifies the instance name
- `project_id` (String) The project ID

### Optional

- `acl` (List of String) Access Control rules to whitelist IP addresses
- `labels` (Map of String) Instance Labels
- `options` (Map of String) Specifies postgres instance options

### Read-Only

- `backup_schedule` (String) Specifies the backup schedule (cron style)
- `id` (String) Specifies the resource ID
- `machine_type` (String) The Machine Type
- `replicas` (Number) Number of replicas
- `storage` (Attributes) A signle `storage` block as defined below (see [below for nested schema](#nestedatt--storage))
- `version` (String) Postgres version

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

Read-Only:

- `class` (String) Specifies the storage class. Available option: `premium-perf6-stackit`
- `size` (Number) The storage size in GB


18 changes: 8 additions & 10 deletions docs/resources/postgres_flex_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ Manages Postgres Flex instances

```terraform
resource "stackit_postgres_flex_instance" "example" {
name = "example"
project_id = var.project_id
version = "7"
plan = "stackit-elasticsearch-single-small"
name = "example"
project_id = "example"
machine_type = "c1.2"
version = "14"
replicas = 1
}
```

Expand All @@ -31,7 +32,7 @@ resource "stackit_postgres_flex_instance" "example" {

- `machine_type` (String) The Machine Type. Available options: `c1.2` `m1.2`, `c1.3`, `m1.3`, `c1.4`, `c1.5`, `m1.5`
- `name` (String) Specifies the instance name. Changing this value requires the resource to be recreated.
- `project_id` (String) The project ID the cluster runs in. Changing this value requires the resource to be recreated.
- `project_id` (String) The project ID the instance runs in. Changing this value requires the resource to be recreated.

### Optional

Expand All @@ -41,22 +42,19 @@ resource "stackit_postgres_flex_instance" "example" {
- `options` (Map of String) Specifies postgres instance options
- `replicas` (Number) How many replicas of the database should exist. Changing this value requires the resource to be recreated.
- `storage` (Attributes) A signle `storage` block as defined below. Changing this value requires the resource to be recreated. (see [below for nested schema](#nestedatt--storage))
- `user` (Attributes) The databse admin user (see [below for nested schema](#nestedatt--user))
- `version` (String) Postgres version. Options: `13`, `14`. Changing this value requires the resource to be recreated.

### Read-Only

- `id` (String) Specifies the resource ID
- `user` (Attributes) The databse admin user (see [below for nested schema](#nestedatt--user))

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

Required:

- `class` (String) Specifies the storage class. Available option: `premium-perf6-stackit`

Optional:

- `class` (String) Specifies the storage class. Available option: `premium-perf6-stackit`
- `size` (Number) The storage size in GB


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "stackit_postgres_flex_instance" "example" {
name = "example"
project_id = "example"
}
9 changes: 5 additions & 4 deletions examples/resources/stackit_postgres_flex_instance/resource.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
resource "stackit_postgres_flex_instance" "example" {
name = "example"
project_id = var.project_id
version = "7"
plan = "stackit-elasticsearch-single-small"
name = "example"
project_id = "example"
machine_type = "c1.2"
version = "14"
replicas = 1
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/Masterminds/semver v1.5.0
github.com/SchwarzIT/community-stackit-go-client v1.2.0
github.com/SchwarzIT/community-stackit-go-client v1.2.1
github.com/hashicorp/terraform-plugin-framework v0.15.0
github.com/hashicorp/terraform-plugin-go v0.14.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.21.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ github.com/SchwarzIT/community-stackit-go-client v1.1.3 h1:KKYeHdhEYJ7oVFbdW2qlS
github.com/SchwarzIT/community-stackit-go-client v1.1.3/go.mod h1:f78uJlfuNQmS5qZrYtJknfqAp2MWuqXJZNWP3fentyE=
github.com/SchwarzIT/community-stackit-go-client v1.2.0 h1:mJkGJkk5uVjhE3WKiOxexKhuVhc3H09FuW8Ho+HrNU8=
github.com/SchwarzIT/community-stackit-go-client v1.2.0/go.mod h1:f78uJlfuNQmS5qZrYtJknfqAp2MWuqXJZNWP3fentyE=
github.com/SchwarzIT/community-stackit-go-client v1.2.1 h1:VBqR2gawk3ULYRhiA8ZDm5JHnir8uk56mKCXB6U7U9Y=
github.com/SchwarzIT/community-stackit-go-client v1.2.1/go.mod h1:f78uJlfuNQmS5qZrYtJknfqAp2MWuqXJZNWP3fentyE=
github.com/SchwarzIT/community-stackit-go-client v1.2.2-0.20221114094552-29f6c39e7854 h1:mNFi/79HP/uO4zbp2mSr7w6DL73w5fyjZWPCBZ2u+jg=
github.com/SchwarzIT/community-stackit-go-client v1.2.2-0.20221114094552-29f6c39e7854/go.mod h1:f78uJlfuNQmS5qZrYtJknfqAp2MWuqXJZNWP3fentyE=
github.com/SchwarzIT/community-stackit-go-client v1.2.2-0.20221114094829-0936703384cd h1:ssmWJsCgG1LKKReMNUgbCcqvdDV/QbWUpjt6c0bNZPY=
github.com/SchwarzIT/community-stackit-go-client v1.2.2-0.20221114094829-0936703384cd/go.mod h1:f78uJlfuNQmS5qZrYtJknfqAp2MWuqXJZNWP3fentyE=
github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk=
github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE=
Expand Down
89 changes: 89 additions & 0 deletions stackit/internal/data-sources/postgres-flex/instance/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package instance

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Read - lifecycle function
func (r DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
c := r.client.PostgresFlex
var config Instance
diags := req.Config.Get(ctx, &config)

resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

list, err := c.Instances.List(ctx, config.ProjectID.Value)
if err != nil {
resp.Diagnostics.AddError("failed to list instances", err.Error())
return
}

found := -1
existing := ""
for i, instance := range list.Items {
if instance.Name == config.Name.Value {
found = i
break
}
if existing == "" {
existing = "\navailable instances in the project are:"
}
existing = fmt.Sprintf("%s\n- %s", existing, instance.Name)
}

if found == -1 {
resp.State.RemoveResource(ctx)
diags.AddError("couldn't find instance", "instance could not be found."+existing)
resp.Diagnostics.Append(diags...)
return
}

// set found instance
instance := list.Items[found]
ires, err := c.Instances.Get(ctx, config.ProjectID.Value, instance.ID)
if err != nil {
resp.Diagnostics.AddError("failed to get instances", err.Error())
return
}
i := ires.Item
config.ID = types.String{Value: instance.ID}
config.ACL = types.List{ElemType: types.StringType}
for _, v := range i.ACL.Items {
config.ACL.Elems = append(config.ACL.Elems, types.String{Value: v})
}
config.BackupSchedule = types.String{Value: i.BackupSchedule}
config.MachineType = types.String{Value: i.Flavor.ID}
config.Name = types.String{Value: i.Name}
config.Replicas = types.Int64{Value: int64(i.Replicas)}

storage, d := types.ObjectValue(
map[string]attr.Type{
"class": types.StringType,
"size": types.Int64Type,
},
map[string]attr.Value{
"class": types.String{Value: i.Storage.Class},
"size": types.Int64{Value: int64(i.Storage.Size)},
})

resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}

config.Storage = storage
config.Version = types.String{Value: i.Version}

resp.Diagnostics.Append(resp.State.Set(ctx, &config)...)
if resp.Diagnostics.HasError() {
return
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package instance

import (
"context"
"fmt"

client "github.com/SchwarzIT/community-stackit-go-client"
"github.com/hashicorp/terraform-plugin-framework/datasource"
)

// New returns a new configured data source
func New() datasource.DataSource {
return &DataSource{}
}

// DataSource is the exported data source
type DataSource struct {
client *client.Client
}

var _ = datasource.DataSource(&DataSource{})

// Metadata returns data resource metadata
func (r *DataSource) Metadata(_ context.Context, req datasource.MetadataRequest, res *datasource.MetadataResponse) {
res.TypeName = "stackit_postgres_flex_instance"
}

// Configure configures the data source client
func (d *DataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*client.Client)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.client = client
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package instance_test

import (
"fmt"
"testing"

"github.com/SchwarzIT/terraform-provider-stackit/stackit"
"github.com/SchwarzIT/terraform-provider-stackit/stackit/internal/common"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const run_this_test = true

func TestAcc_PostgresInstance(t *testing.T) {
if !common.ShouldAccTestRun(run_this_test) {
t.Skip()
return
}

name := "odjtest-" + acctest.RandStringFromCharSet(7, acctest.CharSetAlpha)
version := "14"

resource.ParallelTest(t, resource.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"stackit": providerserver.NewProtocol6WithError(stackit.New("test")()),
},
Steps: []resource.TestStep{
// check minimal configuration
{
Config: config(name, version),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.stackit_postgres_flex_instance.example", "name", name),
resource.TestCheckResourceAttr("data.stackit_postgres_flex_instance.example", "project_id", common.ACC_TEST_PROJECT_ID),
resource.TestCheckResourceAttr("data.stackit_postgres_flex_instance.example", "version", version),
resource.TestCheckResourceAttr("stackit_postgres_flex_instance.example", "machine_type", "c1.2"),
resource.TestCheckResourceAttr("stackit_postgres_flex_instance.example", "replicas", "1"),
resource.TestCheckResourceAttr("stackit_postgres_flex_instance.example", "storage.class", "premium-perf6-stackit"),
resource.TestCheckResourceAttr("stackit_postgres_flex_instance.example", "storage.size", "30"),
resource.TestCheckResourceAttrSet("stackit_postgres_flex_instance.example", "id"),
),
},
},
})
}

func config(name, version string) string {
return fmt.Sprintf(`
resource "stackit_postgres_flex_instance" "example" {
name = "%s"
project_id = "%s"
machine_type = "c1.2"
version = "%s"
replicas = 1
storage = {
class = "premium-perf6-stackit"
size = 30
}
}
data "stackit_postgres_flex_instance" "example" {
depends_on = [stackit_postgres_flex_instance.example]
name = "%s"
project_id = "%s"
}
`,
name,
common.ACC_TEST_PROJECT_ID,
version,
name,
common.ACC_TEST_PROJECT_ID,
)
}
Loading

0 comments on commit 86e849f

Please sign in to comment.