-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cloud region resource #535
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "materialize_region Resource - terraform-provider-materialize" | ||
subcategory: "" | ||
description: |- | ||
The region resource allows you to manage regions in Materialize. When a new region is created, it automatically includes an 'xsmall' quickstart cluster as part of the initialization process. Users are billed for this quickstart cluster from the moment the region is created. To avoid unnecessary charges, you can connect to the new region and drop the quickstart cluster if it is not needed. Please note that disabling a region cannot be achieved directly through this provider. If you need to disable a region, contact Materialize support for assistance. This process ensures that any necessary cleanup and billing adjustments are handled properly. | ||
--- | ||
|
||
# materialize_region (Resource) | ||
|
||
The region resource allows you to manage regions in Materialize. When a new region is created, it automatically includes an 'xsmall' quickstart cluster as part of the initialization process. Users are billed for this quickstart cluster from the moment the region is created. To avoid unnecessary charges, you can connect to the new region and drop the quickstart cluster if it is not needed. Please note that disabling a region cannot be achieved directly through this provider. If you need to disable a region, contact Materialize support for assistance. This process ensures that any necessary cleanup and billing adjustments are handled properly. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "materialize_region" "example" { | ||
region_id = "aws/eu-west-2" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `region_id` (String) The ID of the region to manage. Example: aws/us-west-2 | ||
|
||
### Read-Only | ||
|
||
- `enabled_at` (String) The timestamp when the region was enabled. | ||
- `http_address` (String) The HTTP address of the region. | ||
- `id` (String) The ID of this resource. | ||
- `region_state` (Boolean) The state of the region. True if enabled, false otherwise. | ||
- `resolvable` (Boolean) Indicates if the region is resolvable. | ||
- `sql_address` (String) The SQL address of the region. | ||
|
||
## Import | ||
|
||
Import is supported using the following syntax: | ||
|
||
```shell | ||
# Regions can be imported using the `terraform import` command | ||
terraform import materialize_region.example_region aws/us-east-1 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Regions can be imported using the `terraform import` command | ||
terraform import materialize_region.example_region aws/us-east-1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
resource "materialize_region" "example" { | ||
region_id = "aws/eu-west-2" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package clients | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
@@ -115,6 +116,39 @@ func (c *CloudAPIClient) GetRegionDetails(ctx context.Context, provider CloudPro | |
return ®ion, nil | ||
} | ||
|
||
// EnableRegion sends a PATCH request to enable a region and polls until the region is enabled or a timeout is reached. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like this comment describes the region resource, this function doesn't appear to do any polling? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes, I initially had it setup that way, but a better approach is to do the polling in the resource itself rather than the client lib, so I moved the |
||
func (c *CloudAPIClient) EnableRegion(ctx context.Context, provider CloudProvider) (*CloudRegion, error) { | ||
endpoint := fmt.Sprintf("%s/api/region", provider.Url) | ||
emptyJSONPayload := bytes.NewBuffer([]byte("{}")) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, endpoint, emptyJSONPayload) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating request to enable region: %v", err) | ||
} | ||
|
||
req.Header.Add("Content-Type", "application/json") | ||
|
||
resp, err := c.FronteggClient.HTTPClient.Do(req) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this use the frontegg client? This is the cloud region api, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I think that this has been flawed from the initial implementation during the large refactor a few months ago. As far as I can tell, the main idea seems to have been to reuse the FronteggClient's HTTPClient which already includes the Authorization token. I'll have to refactor this. I've created an issue to track this and will submit a PR tomorrow! #543 Thanks for pointing it out! |
||
if err != nil { | ||
return nil, fmt.Errorf("error sending request to enable region: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read response body: %v", err) | ||
} | ||
return nil, fmt.Errorf("cloud API returned non-200/201 status code: %d, body: %s", resp.StatusCode, string(body)) | ||
} | ||
|
||
var region CloudRegion | ||
if err := json.NewDecoder(resp.Body).Decode(®ion); err != nil { | ||
return nil, err | ||
} | ||
|
||
return ®ion, nil | ||
} | ||
|
||
// GetHost retrieves the SQL address for a specified region | ||
func (c *CloudAPIClient) GetHost(ctx context.Context, regionID string) (string, error) { | ||
providers, err := c.ListCloudProviders(ctx) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package provider | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func TestAccResourceRegion_basic(t *testing.T) { | ||
resourceName := "materialize_region.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: testAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccResourceRegionConfig(), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceName, "region_id", "aws/us-east-1"), | ||
resource.TestCheckResourceAttrSet(resourceName, "sql_address"), | ||
resource.TestCheckResourceAttr(resourceName, "sql_address", "materialized:6877"), | ||
resource.TestCheckResourceAttrSet(resourceName, "http_address"), | ||
resource.TestCheckResourceAttr(resourceName, "http_address", "materialized:6875"), | ||
resource.TestCheckResourceAttr(resourceName, "resolvable", "true"), | ||
resource.TestCheckResourceAttrSet(resourceName, "enabled_at"), | ||
resource.TestCheckResourceAttr(resourceName, "region_state", "true"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccResourceRegion_update(t *testing.T) { | ||
resourceName := "materialize_region.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: testAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccResourceRegionConfig(), | ||
Check: resource.TestCheckResourceAttr(resourceName, "region_id", "aws/us-east-1"), | ||
}, | ||
{ | ||
Config: testAccResourceRegionConfig(), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceName, "region_id", "aws/us-east-1"), | ||
resource.TestCheckResourceAttrSet(resourceName, "sql_address"), | ||
resource.TestCheckResourceAttr(resourceName, "sql_address", "materialized:6877"), | ||
resource.TestCheckResourceAttrSet(resourceName, "http_address"), | ||
resource.TestCheckResourceAttr(resourceName, "http_address", "materialized:6875"), | ||
resource.TestCheckResourceAttr(resourceName, "resolvable", "true"), | ||
resource.TestCheckResourceAttrSet(resourceName, "enabled_at"), | ||
resource.TestCheckResourceAttr(resourceName, "region_state", "true"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccResourceRegionConfig() string { | ||
return ` | ||
resource "materialize_region" "test" { | ||
region_id = "aws/us-east-1" | ||
} | ||
` | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems weird to have a region that doesn't exist in our example?