Skip to content

Commit

Permalink
Refactored to handle remote entity deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
micahlmartin committed Sep 9, 2021
1 parent 8dec8a7 commit b294572
Show file tree
Hide file tree
Showing 60 changed files with 1,785 additions and 850 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
TEST?=$$(go list ./... | grep -v 'vendor')
SWEEP_DIR?=./internal/provider
SWEEP?=all
SWEEP_ARGS?=
HOSTNAME=hashicorp.com
NAMESPACE=micahlmartin
NAMESPACE=harness-io
NAME=harness
VERSION=0.2
BINARY=terraform-provider-${NAME}
Expand All @@ -29,4 +28,4 @@ test:

sweep:
@echo "WARNING: This will destroy infrastructure. Use only in development accounts."
go test $(SWEEP_DIR) -v -sweep=$(SWEEP) $(SWEEPARGS) -timeout 60m
go test $(SWEEP_DIR) -v -sweep=all $(SWEEPARGS) -timeout 60m
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ description: |-
terraform {
required_providers {
harness = {
source = "micahlmartin/harness"
source = "harness-io/harness"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/data-sources/data-source.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
harness = {
source = "micahlmartin/harness"
source = "harness-io/harness"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/provider/provider.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
harness = {
source = "micahlmartin/harness"
source = "harness-io/harness"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/resources/application_resource/resource.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
harness = {
source = "micahlmartin/harness"
source = "harness-io/harness"
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions internal/provider/data_source_encrypted_text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ func testAccDataSourceEncryptedTextByName(name string) string {
application_filter_type = "ALL"
environment_filter_type = "NON_PRODUCTION_ENVIRONMENTS"
}
lifecycle {
ignore_changes = [secret_manager_id]
}
}
data "harness_encrypted_text" "test" {
Expand Down
4 changes: 0 additions & 4 deletions internal/provider/data_source_git_connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ func testAccDataSourceGitConnector(name string) string {
name = "%[1]s"
value = "foo"
secret_manager_id = data.harness_secret_manager.test.id
lifecycle {
ignore_changes = [secret_manager_id]
}
}
resource "harness_git_connector" "test" {
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/infrastructure_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ func infraDetailsAwsLambda() *schema.Resource {
"iam_role": {
Description: "The IAM role to use.",
Type: schema.TypeString,
Required: true,
Optional: true,
},
"region": {
Description: "The region to deploy to.",
Expand Down
5 changes: 0 additions & 5 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,6 @@ func New(version string) func() *schema.Provider {
}
}

type HarnessClient struct {
GraphQLClient *api.Client
CaacClient *api.ConfigAsCodeClient
}

// Setup the client for interacting with the Harness API
func configure(version string, p *schema.Provider) func(context.Context, *schema.ResourceData) (interface{}, diag.Diagnostics) {
return func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
Expand Down
23 changes: 20 additions & 3 deletions internal/provider/resource_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ func resourceApplicationRead(ctx context.Context, d *schema.ResourceData, meta i
return diag.FromErr(err)
}

if app == nil {
d.SetId("")
d.MarkNewResource()
return nil
}

applicationRead(d, app)

return nil
}

func applicationRead(d *schema.ResourceData, app *graphql.Application) {
if app == nil {
return
}

d.Set("id", app.Id)
d.Set("name", app.Name)
d.Set("description", app.Description)
d.Set("is_manual_trigger_authorized", app.IsManualTriggerAuthorized)
Expand All @@ -94,8 +111,6 @@ func resourceApplicationRead(ctx context.Context, d *schema.ResourceData, meta i
d.Set("git_sync_connector_id", app.GitSyncConfig.GitConnector.Id)
}
}

return nil
}

func resourceApplicationUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand All @@ -108,11 +123,13 @@ func resourceApplicationUpdate(ctx context.Context, d *schema.ResourceData, meta
Name: d.Get("name").(string),
}

_, err := c.Applications().UpdateApplication(input)
app, err := c.Applications().UpdateApplication(input)
if err != nil {
return diag.FromErr(err)
}

applicationRead(d, app)

return nil
}

Expand Down
41 changes: 41 additions & 0 deletions internal/provider/resource_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package provider

import (
"fmt"
"log"
"strings"
"testing"

"github.com/harness-io/harness-go-sdk/harness/api"
"github.com/harness-io/harness-go-sdk/harness/api/graphql"
"github.com/harness-io/harness-go-sdk/harness/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -50,6 +52,43 @@ func TestAccResourceApplication(t *testing.T) {
})
}

func TestAccResourceApplication_DeleteUnderlyingResource(t *testing.T) {

expectedName := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(12))
resourceName := "harness_application.test"

resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccApplicationDestroy(resourceName),
Steps: []resource.TestStep{
{
Config: testAccResourceApplication(expectedName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", expectedName),
resource.TestCheckResourceAttr(resourceName, "description", "my description"),
testAccApplicationCreation(t, resourceName, expectedName),
),
},
{
PreConfig: func() {
testAccConfigureProvider()
c := testAccProvider.Meta().(*api.Client)
app, err := c.Applications().GetApplicationByName(expectedName)
require.NoError(t, err)
require.NotNil(t, app)

err = c.Applications().DeleteApplication(app.Id)
require.NoError(t, err)
},
PlanOnly: true,
ExpectNonEmptyPlan: true,
Config: testAccResourceApplication(expectedName),
},
},
})
}

func testAccApplicationCreation(t *testing.T, resourceName string, appName string) resource.TestCheckFunc {
return func(state *terraform.State) error {
app, err := testAccGetApplication(resourceName, state)
Expand Down Expand Up @@ -103,6 +142,8 @@ func testSweepApplications(r string) error {
return err
}

log.Printf("[INFO] Deleting %d applications", len(apps))

for _, app := range apps {
// Only delete applications that start with 'Test'
if strings.HasPrefix(app.Name, "Test") {
Expand Down
68 changes: 32 additions & 36 deletions internal/provider/resource_cloudprovider_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ func resourceCloudProviderAws() *schema.Resource {

return &schema.Resource{
Description: "Resource for creating a physical data center cloud provider",
CreateContext: resourceCloudProviderAwsCreate,
CreateContext: resourceCloudProviderAwsCreateOrUpdate,
ReadContext: resourceCloudProviderAwsRead,
UpdateContext: resourceCloudProviderAwsUpdate,
UpdateContext: resourceCloudProviderAwsCreateOrUpdate,
DeleteContext: resourceCloudProviderDelete,

Schema: providerSchema,
Expand All @@ -131,36 +131,41 @@ func resourceCloudProviderAwsRead(ctx context.Context, d *schema.ResourceData, m
name := d.Get("name").(string)

cp := &cac.AwsCloudProvider{}
err := c.ConfigAsCode().GetCloudProviderByName(name, cp)
if err != nil {
if err := c.ConfigAsCode().GetCloudProviderByName(name, cp); err != nil {
return diag.FromErr(err)
} else if cp.IsEmpty() {
d.SetId("")
d.MarkNewResource()
return nil
}

d.SetId(cp.Id)
d.Set("name", cp.Name)

scope, err := flattenUsageRestrictions(c, cp.UsageRestrictions)
if err != nil {
return diag.FromErr(err)
}
d.Set("usage_scope", scope)

return nil
return readCloudProviderAws(c, d, cp)
}

func resourceCloudProviderAwsCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func resourceCloudProviderAwsCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*api.Client)

input := cac.NewEntity(cac.ObjectTypes.AwsCloudProvider).(*cac.AwsCloudProvider)
var input *cac.AwsCloudProvider
var err error

if d.IsNewResource() {
input = cac.NewEntity(cac.ObjectTypes.AwsCloudProvider).(*cac.AwsCloudProvider)
} else {
input = &cac.AwsCloudProvider{}
if err = c.ConfigAsCode().GetCloudProviderById(d.Id(), input); err != nil {
return diag.FromErr(err)
} else if input.IsEmpty() {
d.SetId("")
d.MarkNewResource()
return nil
}
}

input.Name = d.Get("name").(string)

restrictions, err := expandUsageRestrictions(c, d.Get("usage_scope").(*schema.Set).List())
if err != nil {
if err := expandUsageRestrictions(c, d.Get("usage_scope").(*schema.Set).List(), input.UsageRestrictions); err != nil {
return diag.FromErr(err)
}
if restrictions != nil {
input.UsageRestrictions = restrictions
}

expandAwsCloudProviderCredentials(d.Get("credentials").([]interface{}), input)

Expand All @@ -169,27 +174,18 @@ func resourceCloudProviderAwsCreate(ctx context.Context, d *schema.ResourceData,
return diag.FromErr(err)
}

d.SetId(cp.Id)

return nil
return readCloudProviderAws(c, d, cp)
}

func resourceCloudProviderAwsUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*api.Client)

cp := cac.NewEntity(cac.ObjectTypes.AwsCloudProvider).(*cac.AwsCloudProvider)
cp.Name = d.Get("name").(string)

usageRestrictions, err := expandUsageRestrictions(c, d.Get("usage_scope").(*schema.Set).List())
if err != nil {
return diag.FromErr(err)
}
cp.UsageRestrictions = usageRestrictions
func readCloudProviderAws(c *api.Client, d *schema.ResourceData, cp *cac.AwsCloudProvider) diag.Diagnostics {
d.SetId(cp.Id)
d.Set("name", cp.Name)

_, err = c.ConfigAsCode().UpsertAwsCloudProvider(cp)
scope, err := flattenUsageRestrictions(c, cp.UsageRestrictions)
if err != nil {
return diag.FromErr(err)
}
d.Set("usage_scope", scope)

return nil
}
Expand Down
44 changes: 38 additions & 6 deletions internal/provider/resource_cloudprovider_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,39 @@ import (
"fmt"
"testing"

"github.com/harness-io/harness-go-sdk/harness/api"
"github.com/harness-io/harness-go-sdk/harness/api/cac"
"github.com/harness-io/harness-go-sdk/harness/helpers"
"github.com/harness-io/harness-go-sdk/harness/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/stretchr/testify/require"
)

func init() {
resource.AddTestSweepers("harness_cloudprovider_aws", &resource.Sweeper{
Name: "harness_cloudprovider_aws",
F: testSweepCloudProviders,
func TestAccResourceAwsCloudProvider(t *testing.T) {

var (
name = fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(4))
resourceName = "harness_cloudprovider_aws.test"
)

resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCloudProviderDestroy(resourceName),
Steps: []resource.TestStep{
{
Config: testAccResourceAwsCloudProvider(name, true),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", name),
testAccCheckAwsCloudProviderExists(t, resourceName, name),
),
},
},
})
}

func TestAccResourceAwsCloudProvider(t *testing.T) {
func TestAccResourceAwsCloudProvider_DeleteUnderlyingResource(t *testing.T) {

var (
name = fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(4))
Expand All @@ -28,7 +46,6 @@ func TestAccResourceAwsCloudProvider(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCloudProviderDestroy(resourceName),
Steps: []resource.TestStep{
{
Config: testAccResourceAwsCloudProvider(name, true),
Expand All @@ -37,6 +54,21 @@ func TestAccResourceAwsCloudProvider(t *testing.T) {
testAccCheckAwsCloudProviderExists(t, resourceName, name),
),
},
{
PreConfig: func() {
testAccConfigureProvider()
c := testAccProvider.Meta().(*api.Client)
cp, err := c.CloudProviders().GetAwsCloudProviderByName(name)
require.NoError(t, err)
require.NotNil(t, cp)

err = c.CloudProviders().DeleteCloudProvider(cp.Id)
require.NoError(t, err)
},
Config: testAccResourceAwsCloudProvider(name, true),
PlanOnly: true,
ExpectNonEmptyPlan: true,
},
},
})
}
Expand Down
Loading

0 comments on commit b294572

Please sign in to comment.