Skip to content

Commit

Permalink
Merge pull request #70 from cloudsigma/feat/migrate-tags-resource-to-…
Browse files Browse the repository at this point in the history
…framework

feat: migrate tags resource to framework
  • Loading branch information
pavel-github authored Jul 3, 2024
2 parents d26ae4d + f956a13 commit 726c516
Show file tree
Hide file tree
Showing 17 changed files with 781 additions and 235 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,40 @@ jobs:
- name: Run acceptance tests
run: make testacc

sweeper:
name: sweeper cleanup
runs-on: ubuntu-latest
needs: acceptance-tests
timeout-minutes: 15
env:
CLOUDSIGMA_USERNAME: ${{ secrets.CLOUDSIGMA_USERNAME }}
CLOUDSIGMA_PASSWORD: ${{ secrets.CLOUDSIGMA_PASSWORD }}
CLOUDSIGMA_LOCATION: ${{ secrets.CLOUDSIGMA_LOCATION }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: "go.mod"

- name: Fix permissions for cache directories
run: |
chmod -R 0755 ~/.cache/go-build ~/go/pkg/mod || true
- name: Set up cache
uses: actions/cache@v4
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('go.sum', 'tools/go.sum') }}
restore-keys: ${{ runner.os }}-go-

- name: Run sweeper cleanup
run: make sweep

docs:
name: documentation
runs-on: ubuntu-latest
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ testacc:
@mkdir -p $(BUILD_DIR)
TF_ACC=1 go test -count=1 -v -cover -coverprofile=$(BUILD_DIR)/coverage-with-acceptance.out -timeout 120m ./...

## sweep: Run sweepers to cleanup leftover infrastructure after acceptance tests.
.PHONY: sweep
sweep:
@echo "==> Running sweepers to cleanup leftover infrastructure..."
@echo " WARNING: This will destroy infrastructure. Use only in development accounts."
@echo ""
@go test -count=1 -v ./internal/provider -sweep=testacc

## build: Build binary for default local system's operating system and architecture.
.PHONY: build
build:
Expand Down
1 change: 0 additions & 1 deletion cloudsigma/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func Provider() *schema.Provider {
"cloudsigma_server": resourceCloudSigmaServer(),
"cloudsigma_snapshot": resourceCloudSigmaSnapshot(),
"cloudsigma_ssh_key": resourceCloudSigmaSSHKey(),
"cloudsigma_tag": resourceCloudSigmaTag(),
},
}

Expand Down
63 changes: 48 additions & 15 deletions cloudsigma/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
package cloudsigma

import (
"context"
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"

var testAccProvider *schema.Provider
var testAccProviders map[string]*schema.Provider
var testAccProviderFactories map[string]func() (*schema.Provider, error)
"github.com/cloudsigma/cloudsigma-sdk-go/cloudsigma"
"github.com/cloudsigma/terraform-provider-cloudsigma/internal/provider"
)

func init() {
testAccProvider = Provider()
testAccProviders = map[string]*schema.Provider{
"cloudsigma": testAccProvider,
}
testAccProviderFactories = map[string]func() (*schema.Provider, error){
"cloudsigma": func() (*schema.Provider, error) {
return testAccProvider, nil
},
}
var testAccProto6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"cloudsigma": func() (tfprotov6.ProviderServer, error) {
ctx := context.Background()
upgradedSDKProvider, err := tf5to6server.UpgradeServer(ctx, Provider().GRPCProvider)
if err != nil {
return nil, err
}
providers := []func() tfprotov6.ProviderServer{
func() tfprotov6.ProviderServer { return upgradedSDKProvider },
providerserver.NewProtocol6(provider.New("testacc")()),
}
muxServer, err := tf6muxserver.NewMuxServer(ctx, providers...)
if err != nil {
return nil, err
}
return muxServer.ProviderServer(), nil
},
}

func TestProvider(t *testing.T) {
Expand All @@ -42,3 +53,25 @@ func testAccPreCheck(t *testing.T) {
t.Fatal("CLOUDSIGMA_PASSWORD must be set for acceptance tests")
}
}

func sharedClient() (*cloudsigma.Client, error) {
location := os.Getenv("CLOUDSIGMA_LOCATION")
if location == "" {
return nil, fmt.Errorf("empty CLOUDSIGMA_LOCATION")
}

username := os.Getenv("CLOUDSIGMA_USERNAME")
if username == "" {
return nil, fmt.Errorf("CLOUDSIGMA_USERNAME must be set for acceptance tests")
}

password := os.Getenv("CLOUDSIGMA_PASSWORD")
if password == "" {
return nil, fmt.Errorf("CLOUDSIGMA_PASSWORD must be set for acceptance tests")
}

opts := []cloudsigma.ClientOption{cloudsigma.WithUserAgent("terraform-provider-cloudsigma/sweeper")}
opts = append(opts, cloudsigma.WithLocation(location))
creds := cloudsigma.NewUsernamePasswordCredentialsProvider(username, password)
return cloudsigma.NewClient(creds, opts...), nil
}
47 changes: 27 additions & 20 deletions cloudsigma/resource_cloudsigma_drive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"

"github.com/cloudsigma/cloudsigma-sdk-go/cloudsigma"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccCloudSigmaDrive_basic(t *testing.T) {
Expand All @@ -18,9 +19,9 @@ func TestAccCloudSigmaDrive_basic(t *testing.T) {
tagName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaDriveConfig_basic(driveName),
Expand Down Expand Up @@ -53,9 +54,9 @@ func TestAccCloudSigmaDrive_basic(t *testing.T) {

func TestAccCloudSigmaDrive_emptyTag(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaDriveConfig_emptyTag(),
Expand All @@ -70,13 +71,13 @@ func TestAccCloudSigmaDrive_changeSize(t *testing.T) {
driveName := fmt.Sprintf("tf-acc-test--%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaDriveConfig_basic(driveName),
Check: resource.ComposeAggregateTestCheckFunc(
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudSigmaDriveExists("cloudsigma_drive.test", &drive),
resource.TestCheckResourceAttr("cloudsigma_drive.test", "media", "disk"),
resource.TestCheckResourceAttr("cloudsigma_drive.test", "name", driveName),
Expand All @@ -87,7 +88,7 @@ func TestAccCloudSigmaDrive_changeSize(t *testing.T) {
},
{
Config: testAccCloudSigmaDriveConfig_changeSize(driveName),
Check: resource.ComposeAggregateTestCheckFunc(
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudSigmaDriveExists("cloudsigma_drive.test", &drive),
resource.TestCheckResourceAttr("cloudsigma_drive.test", "size", "16106127360"),
),
Expand All @@ -101,13 +102,13 @@ func TestAccCloudSigmaDrive_changeStorageType(t *testing.T) {
driveName := fmt.Sprintf("tf-acc-test--%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaDriveConfig_storageType(driveName),
Check: resource.ComposeAggregateTestCheckFunc(
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudSigmaDriveExists("cloudsigma_drive.test", &drive),
resource.TestCheckResourceAttr("cloudsigma_drive.test", "storage_type", "dssd"),
),
Expand All @@ -121,7 +122,10 @@ func TestAccCloudSigmaDrive_changeStorageType(t *testing.T) {
}

func testAccCheckCloudSigmaDriveDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*cloudsigma.Client)
client, err := sharedClient()
if err != nil {
return err
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "cloudsigma_drive" {
Expand All @@ -148,7 +152,10 @@ func testAccCheckCloudSigmaDriveExists(n string, drive *cloudsigma.Drive) resour
return fmt.Errorf("no drive ID is set")
}

client := testAccProvider.Meta().(*cloudsigma.Client)
client, err := sharedClient()
if err != nil {
return err
}
retrievedDrive, _, err := client.Drives.Get(context.Background(), rs.Primary.ID)
if err != nil {
return err
Expand Down
58 changes: 32 additions & 26 deletions cloudsigma/resource_cloudsigma_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"

"github.com/cloudsigma/cloudsigma-sdk-go/cloudsigma"
)
Expand All @@ -20,9 +20,9 @@ func TestAccCloudSigmaServer_basic(t *testing.T) {
tagName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaServerConfig_basic(serverName),
Expand Down Expand Up @@ -54,9 +54,9 @@ func TestAccCloudSigmaServer_basic(t *testing.T) {

func TestAccCloudSigmaServer_emptySSH(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaServerConfig_emptySSHKey(),
Expand All @@ -68,9 +68,9 @@ func TestAccCloudSigmaServer_emptySSH(t *testing.T) {

func TestAccCloudSigmaServer_emptyTag(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaServerConfig_emptyTag(),
Expand All @@ -85,9 +85,9 @@ func TestAccCloudSigmaServer_smp(t *testing.T) {
serverName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaServerConfig_basic(serverName),
Expand All @@ -112,9 +112,9 @@ func TestAccCloudSigmaServer_smp(t *testing.T) {

func TestAccCloudSigmaServer_invalidSMP(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaServerConfig_invalidSMP(),
Expand All @@ -131,9 +131,9 @@ func TestAccCloudSigmaServer_withDrive(t *testing.T) {
driveName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaServerConfig_withDrive(serverName, driveName),
Expand Down Expand Up @@ -167,9 +167,9 @@ func TestAccCloudSigmaServer_withMeta(t *testing.T) {
serverName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaServerConfig_withMeta(serverName),
Expand All @@ -192,7 +192,10 @@ func TestAccCloudSigmaServer_withMeta(t *testing.T) {
}

func testAccCheckCloudSigmaServerDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*cloudsigma.Client)
client, err := sharedClient()
if err != nil {
return err
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "cloudsigma_server" {
Expand All @@ -219,7 +222,10 @@ func testAccCheckCloudSigmaServerExists(n string, server *cloudsigma.Server) res
return fmt.Errorf("no server ID is set")
}

client := testAccProvider.Meta().(*cloudsigma.Client)
client, err := sharedClient()
if err != nil {
return err
}
retrievedServer, _, err := client.Servers.Get(context.Background(), rs.Primary.ID)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 726c516

Please sign in to comment.