-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
341a9ef
commit 3c27bac
Showing
9 changed files
with
123 additions
and
18 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
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
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 |
---|---|---|
@@ -1,9 +1,15 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"crypto/ed25519" | ||
"encoding/base64" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"terraform-provider-binarylane/internal/binarylane" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
|
@@ -20,7 +26,7 @@ func TestSshKeyResource(t *testing.T) { | |
{ | ||
Config: providerConfig + ` | ||
resource "binarylane_ssh_key" "test" { | ||
name = "tf_ssh_key_resource_test" | ||
name = "tf-test-key-resource-test" | ||
public_key = "` + publicKey + `" | ||
} | ||
|
@@ -32,13 +38,13 @@ data "binarylane_ssh_key" "test" { | |
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
// Verify resource values | ||
resource.TestCheckResourceAttr("binarylane_ssh_key.test", "name", "tf_ssh_key_resource_test"), | ||
resource.TestCheckResourceAttr("binarylane_ssh_key.test", "name", "tf-test-key-resource-test"), | ||
resource.TestCheckResourceAttr("binarylane_ssh_key.test", "public_key", publicKey), | ||
resource.TestCheckResourceAttr("binarylane_ssh_key.test", "default", "false"), | ||
resource.TestCheckResourceAttrSet("binarylane_ssh_key.test", "id"), | ||
|
||
// Verify data source values | ||
resource.TestCheckResourceAttr("data.binarylane_ssh_key.test", "name", "tf_ssh_key_resource_test"), | ||
resource.TestCheckResourceAttr("data.binarylane_ssh_key.test", "name", "tf-test-key-resource-test"), | ||
resource.TestCheckResourceAttrSet("data.binarylane_ssh_key.test", "public_key"), // Ideally would check this is identical, but whitespace is not preserved | ||
resource.TestCheckResourceAttr("data.binarylane_ssh_key.test", "default", "false"), | ||
resource.TestCheckResourceAttrSet("data.binarylane_ssh_key.test", "id"), | ||
|
@@ -62,14 +68,14 @@ data "binarylane_ssh_key" "test" { | |
// { | ||
// Config: providerConfig + ` | ||
// resource "binarylane_ssh_key" "test" { | ||
// name = "tf_ssh_key_resource_test" | ||
// name = "tf-test-key-resource-test" | ||
// public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJCsuosklP0T4fJcQDgkeVh7dQu+eV+vev1CfwdUkj7h [email protected]" | ||
// default = true | ||
// } | ||
// `, | ||
// Check: resource.ComposeAggregateTestCheckFunc( | ||
// // Verify resource values | ||
// resource.TestCheckResourceAttr("binarylane_ssh_key.test", "name", "tf_ssh_key_resource_test"), | ||
// resource.TestCheckResourceAttr("binarylane_ssh_key.test", "name", "tf-test-key-resource-test"), | ||
// resource.TestCheckResourceAttr("data.binarylane_ssh_key.test", "default", "true"), | ||
// ), | ||
// }, | ||
|
@@ -106,3 +112,70 @@ func ImportByFingerprint(state *terraform.State) (fingerprint string, err error) | |
|
||
return rawState["fingerprint"], nil | ||
} | ||
|
||
func init() { | ||
resource.AddTestSweepers("ssh_key", &resource.Sweeper{ | ||
Name: "ssh_key", | ||
F: func(_ string) error { | ||
endpoint := os.Getenv("BINARYLANE_API_ENDPOINT") | ||
if endpoint == "" { | ||
endpoint = "https://api.binarylane.com.au/v2" | ||
} | ||
token := os.Getenv("BINARYLANE_API_TOKEN") | ||
|
||
client, err := binarylane.NewClientWithAuth( | ||
endpoint, | ||
token, | ||
) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error creating Binary Lane API client: %w", err) | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
var page int32 = 1 | ||
perPage := int32(200) | ||
nextPage := true | ||
|
||
for nextPage { | ||
params := binarylane.GetAccountKeysParams{ | ||
Page: &page, | ||
PerPage: &perPage, | ||
} | ||
|
||
keyResp, err := client.GetAccountKeysWithResponse(ctx, ¶ms) | ||
if err != nil { | ||
return fmt.Errorf("Error getting SSH keys for test sweep: %w", err) | ||
} | ||
|
||
if keyResp.StatusCode() != http.StatusOK { | ||
return fmt.Errorf("Unexpected status code getting SSH keys for test sweep: %s", keyResp.Body) | ||
} | ||
|
||
keys := keyResp.JSON200.SshKeys | ||
for _, k := range keys { | ||
if strings.HasPrefix(*k.Name, "tf-test-") { | ||
|
||
keyResp, err := client.DeleteAccountKeysKeyIdWithResponse(ctx, int(*k.Id)) | ||
if err != nil { | ||
return fmt.Errorf("Error deleting SSH key %d for test sweep: %w", *k.Id, err) | ||
} | ||
if keyResp.StatusCode() != http.StatusNoContent { | ||
return fmt.Errorf("Unexpected status %d deleting SSH key %d for test sweep: %s", keyResp.StatusCode(), *k.Id, keyResp.Body) | ||
} | ||
log.Println("Deleted SSH key for test sweep:", *k.Id) | ||
} | ||
} | ||
if keyResp.JSON200.Links == nil || keyResp.JSON200.Links.Pages == nil || keyResp.JSON200.Links.Pages.Next == nil { | ||
nextPage = false | ||
break | ||
} | ||
|
||
page++ | ||
} | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package provider | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
resource.TestMain(m) | ||
} |