-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
15 changed files
with
659 additions
and
57 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,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 | ||
|
||
|
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
4 changes: 4 additions & 0 deletions
4
examples/data-sources/stackit_postgres_flex_instance/data-source.tf
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,4 @@ | ||
data "stackit_postgres_flex_instance" "example" { | ||
name = "example" | ||
project_id = "example" | ||
} |
9 changes: 5 additions & 4 deletions
9
examples/resources/stackit_postgres_flex_instance/resource.tf
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 |
---|---|---|
@@ -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 | ||
} |
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
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
89 changes: 89 additions & 0 deletions
89
stackit/internal/data-sources/postgres-flex/instance/actions.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,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 | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
stackit/internal/data-sources/postgres-flex/instance/data_source.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,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 | ||
} |
78 changes: 78 additions & 0 deletions
78
stackit/internal/data-sources/postgres-flex/instance/data_source_test.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,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, | ||
) | ||
} |
Oops, something went wrong.