Skip to content

Commit

Permalink
fix(sonatypeiq_source_control): Handle the attributes correctly
Browse files Browse the repository at this point in the history
In all types of resource it is possible that the attributes can be
nil, this represents that they are inherited from a parent.

Signed-off-by: grahamhar <[email protected]>
  • Loading branch information
grahamhar committed Jun 20, 2024
1 parent d1c3ab9 commit 3e819cb
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 15 deletions.
26 changes: 19 additions & 7 deletions internal/provider/source_control_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"io"
"strings"

Expand Down Expand Up @@ -184,12 +185,16 @@ func (r *sourceControlResource) Read(ctx context.Context, req resource.ReadReque
// Get refreshed Source Control from IQ
ownerType := strings.Trim(state.OwnerType.String(), "\"")
ownerId := strings.Trim(state.ID.String(), "\"")
tflog.Debug(ctx, fmt.Sprintf("Importing Source Control with owner_type: %s", ownerType))
tflog.Debug(ctx, fmt.Sprintf("Importing Source Control with owner_id: %s", ownerId))
sourceControl, apiResponse, err := r.client.SourceControlAPI.GetSourceControl1(ctx, ownerType, ownerId).Execute()
if err != nil {
if apiResponse.StatusCode == 404 {
tflog.Debug(ctx, "Remove resource from state as no longer exists")
resp.State.RemoveResource(ctx)
return
} else {
tflog.Error(ctx, "Error Reading IQ Source Control")
resp.Diagnostics.AddError(
"Error Reading IQ Source Control",
"Could not read Source Control for ID "+state.ID.ValueString()+": "+err.Error(),
Expand All @@ -198,14 +203,23 @@ func (r *sourceControlResource) Read(ctx context.Context, req resource.ReadReque
return
} else {
// Overwrite items with refreshed state
tflog.Debug(ctx, fmt.Sprintf("Setting imported attributes in state %s", *sourceControl.OwnerId))
state.ID = types.StringValue(*sourceControl.OwnerId)
if ownerType == "application" {
state.RepositoryUrl = types.StringValue(*sourceControl.RepositoryUrl)
}
state.BaseBranch = types.StringValue(*sourceControl.BaseBranch)
state.RemediationPullRequestsEnabled = types.BoolValue(*sourceControl.RemediationPullRequestsEnabled)
state.PullRequestCommentingEnabled = types.BoolValue(*sourceControl.PullRequestCommentingEnabled)
state.SourceControlEvaluationsEnabled = types.BoolValue(*sourceControl.SourceControlEvaluationsEnabled)
if sourceControl.BaseBranch != nil {
state.BaseBranch = types.StringValue(*sourceControl.BaseBranch)
}
if sourceControl.RemediationPullRequestsEnabled != nil {
state.RemediationPullRequestsEnabled = types.BoolValue(*sourceControl.RemediationPullRequestsEnabled)
}
if sourceControl.PullRequestCommentingEnabled != nil {
state.PullRequestCommentingEnabled = types.BoolValue(*sourceControl.PullRequestCommentingEnabled)
}
if sourceControl.SourceControlEvaluationsEnabled != nil {
state.SourceControlEvaluationsEnabled = types.BoolValue(*sourceControl.SourceControlEvaluationsEnabled)
}
if sourceControl.Provider != nil {
state.ScmProvider = types.StringValue(*sourceControl.Provider)
}
Expand Down Expand Up @@ -260,8 +274,6 @@ func (r *sourceControlResource) Update(ctx context.Context, req resource.UpdateR
return
}
// Set the state to fully populated data
resp.Diagnostics.AddWarning(
"what", "what"+*sourceControl.Id+"XXXXXXX"+*sourceControl.OwnerId)
plan.ID = types.StringValue(*sourceControl.OwnerId)
resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -310,7 +322,7 @@ func (r *sourceControlResource) ImportState(ctx context.Context, req resource.Im
)
return
}

tflog.Debug(ctx, fmt.Sprintf("Importing Source Control with owner_type: %s and owner_id: %s", idParts[0], idParts[1]))
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("owner_type"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("owner_id"), idParts[1])...)
}
71 changes: 63 additions & 8 deletions internal/provider/source_control_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,38 @@ import (
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func TestAccSourceControlApplicationResourceMinimumConfig(t *testing.T) {
rand := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
resourceName := "sonatypeiq_source_control.test"
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccSourceControlApplicationResourceMinimumConfig(rand),
Check: resource.ComposeTestCheckFunc(
resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "repository_url", "https://github.com/sonatype-nexus-community/terraform-provider-sonatypeiq.git"),
resource.TestCheckNoResourceAttr(resourceName, "remediation_pull_requests_enabled"),
resource.TestCheckNoResourceAttr(resourceName, "pull_request_commenting_enabled"),
resource.TestCheckNoResourceAttr(resourceName, "source_control_evaluation_enabled"),
resource.TestCheckNoResourceAttr(resourceName, "base_branch"),
),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIdentifierAttribute: "owner_id",
ImportStateIdFunc: func(s *terraform.State) (string, error) {
id := s.RootModule().Resources[resourceName].Primary.Attributes["owner_id"]
return fmt.Sprintf("application:%s", id), nil
},
},
},
})
}

func TestAccSourceControlApplicationResource(t *testing.T) {
rand := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
firstState := "true"
Expand All @@ -38,8 +70,9 @@ func TestAccSourceControlApplicationResource(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "remediation_pull_requests_enabled", firstState),
resource.TestCheckResourceAttr(resourceName, "remediation_pull_requests_enabled", firstState),
resource.TestCheckResourceAttr(resourceName, "remediation_pull_requests_enabled", firstState),
resource.TestCheckResourceAttr(resourceName, "pull_request_commenting_enabled", firstState),
resource.TestCheckResourceAttr(resourceName, "source_control_evaluation_enabled", firstState),
resource.TestCheckResourceAttr(resourceName, "base_branch", "my-cool-branch"),
),
),
},
Expand All @@ -58,8 +91,9 @@ func TestAccSourceControlApplicationResource(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "remediation_pull_requests_enabled", secondState),
resource.TestCheckResourceAttr(resourceName, "remediation_pull_requests_enabled", secondState),
resource.TestCheckResourceAttr(resourceName, "remediation_pull_requests_enabled", secondState),
resource.TestCheckResourceAttr(resourceName, "pull_request_commenting_enabled", secondState),
resource.TestCheckResourceAttr(resourceName, "source_control_evaluation_enabled", secondState),
resource.TestCheckResourceAttr(resourceName, "base_branch", "my-cool-branch"),
),
),
},
Expand All @@ -80,8 +114,9 @@ func TestAccSourceControlOrganizationResource(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "remediation_pull_requests_enabled", firstState),
resource.TestCheckResourceAttr(resourceName, "remediation_pull_requests_enabled", firstState),
resource.TestCheckResourceAttr(resourceName, "remediation_pull_requests_enabled", firstState),
resource.TestCheckResourceAttr(resourceName, "pull_request_commenting_enabled", firstState),
resource.TestCheckResourceAttr(resourceName, "source_control_evaluation_enabled", firstState),
resource.TestCheckResourceAttr(resourceName, "base_branch", "my-cool-branch"),
),
),
},
Expand All @@ -100,8 +135,9 @@ func TestAccSourceControlOrganizationResource(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "remediation_pull_requests_enabled", secondState),
resource.TestCheckResourceAttr(resourceName, "remediation_pull_requests_enabled", secondState),
resource.TestCheckResourceAttr(resourceName, "remediation_pull_requests_enabled", secondState),
resource.TestCheckResourceAttr(resourceName, "pull_request_commenting_enabled", secondState),
resource.TestCheckResourceAttr(resourceName, "source_control_evaluation_enabled", secondState),
resource.TestCheckResourceAttr(resourceName, "base_branch", "my-cool-branch"),
),
),
},
Expand Down Expand Up @@ -132,6 +168,25 @@ resource "sonatypeiq_source_control" "test" {
}`, rand, rand, enabled, enabled, enabled)
}

func testAccSourceControlApplicationResourceMinimumConfig(rand string) string {
return fmt.Sprintf(providerConfig+`
data "sonatypeiq_organization" "sandbox" {
name = "Sandbox Organization"
}
resource "sonatypeiq_application" "app_by_public_id" {
name = "app-%s"
public_id = "app-%s"
organization_id = data.sonatypeiq_organization.sandbox.id
}
resource "sonatypeiq_source_control" "test" {
owner_type = "application"
owner_id = sonatypeiq_application.app_by_public_id.id
repository_url = "https://github.com/sonatype-nexus-community/terraform-provider-sonatypeiq.git"
}`, rand, rand)
}

func testAccSourceControlOrganizationResource(rand string, enabled string) string {
return fmt.Sprintf(providerConfig+`
data "sonatypeiq_organization" "sandbox" {
Expand Down

0 comments on commit 3e819cb

Please sign in to comment.