-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/stack: add new stack resource for Stacks API integration
This PR adds a "scylladbcloud_stack" resource which is used for accounting managed resources with the Stacks API. It uses new client, as the payloads for Stacks API are HMAC signed.
- Loading branch information
Showing
7 changed files
with
435 additions
and
49 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package stack | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/scylladb/terraform-provider-scylladbcloud/internal/scylla" | ||
"github.com/scylladb/terraform-provider-scylladbcloud/internal/scylla/model" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
const ( | ||
stackTimeout = 60 * time.Second | ||
) | ||
|
||
func ResourceStack() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceStackCreate, | ||
ReadContext: resourceStackRead, | ||
UpdateContext: resourceStackUpdate, | ||
DeleteContext: resourceStackDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(stackTimeout), | ||
Update: schema.DefaultTimeout(stackTimeout), | ||
Delete: schema.DefaultTimeout(stackTimeout), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"attributes": { | ||
Description: "List of managed resources", | ||
Required: true, | ||
Type: schema.TypeMap, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceStackCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var ( | ||
c = meta.(*scylla.Client) | ||
s = &model.Stack{ | ||
RequestType: "Create", | ||
ResourceProperties: d.Get("attributes").(map[string]interface{}), | ||
} | ||
) | ||
|
||
id, err := sendStack(ctx, c, s) | ||
if err != nil { | ||
return diag.Errorf("failed to create stack: %s", err) | ||
} | ||
|
||
d.SetId(id) | ||
|
||
return nil | ||
} | ||
|
||
func resourceStackRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
return nil | ||
} | ||
|
||
func resourceStackUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var ( | ||
c = meta.(*scylla.Client) | ||
s = &model.Stack{ | ||
RequestType: "Update", | ||
ResourceProperties: d.Get("attributes").(map[string]interface{}), | ||
} | ||
) | ||
|
||
_, err := sendStack(ctx, c, s) | ||
if err != nil { | ||
return diag.Errorf("failed to update stack: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceStackDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var ( | ||
c = meta.(*scylla.Client) | ||
s = &model.Stack{ | ||
RequestType: "Delete", | ||
ResourceProperties: d.Get("attributes").(map[string]interface{}), | ||
} | ||
) | ||
|
||
_, err := sendStack(ctx, c, s) | ||
if err != nil { | ||
return diag.Errorf("failed to delete stack: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func sendStack(ctx context.Context, c *scylla.Client, s *model.Stack) (string, error) { | ||
auth := strings.Split(c.Token, ":") | ||
|
||
if len(auth) != 2 { | ||
return "", errors.New("invalid token format") | ||
} | ||
|
||
req := c.V2.Request(ctx, "POST", s, "/") | ||
|
||
req.Header.Set("X-Scylla-Cloud-Stack-Flavor", "tf") | ||
|
||
if err := c.V2.BasicSign(req, auth[0], []byte(auth[1])); err != nil { | ||
return "", fmt.Errorf("failed to sign request: %w", err) | ||
} | ||
|
||
if _, err := c.V2.Do(req, s); err != nil { | ||
return "", fmt.Errorf("failed to create stack: %w", err) | ||
} | ||
|
||
return auth[0], nil | ||
} |
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
Oops, something went wrong.