Skip to content

Commit

Permalink
fix: correct setting nested attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
acch committed Dec 21, 2024
1 parent 9a6ed51 commit 74eb6b4
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 33 deletions.
18 changes: 18 additions & 0 deletions examples/resources/forgejo_repository/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ resource "forgejo_repository" "personal_non_defaults" {
readme = "Default"
trust_model = "collaborator"
archived = true

internal_tracker = {
enable_time_tracker = false
allow_only_contributors_to_track_time = false
enable_issue_dependencies = false
}
}

resource "forgejo_repository" "org_defaults" {
Expand All @@ -44,6 +50,12 @@ resource "forgejo_repository" "org_non_defaults" {
readme = "Default"
trust_model = "collaborator"
archived = true

internal_tracker = {
enable_time_tracker = false
allow_only_contributors_to_track_time = false
enable_issue_dependencies = false
}
}

resource "forgejo_repository" "user_defaults" {
Expand All @@ -63,6 +75,12 @@ resource "forgejo_repository" "user_non_defaults" {
readme = "Default"
trust_model = "collaborator"
archived = true

internal_tracker = {
enable_time_tracker = false
allow_only_contributors_to_track_time = false
enable_issue_dependencies = false
}
}

output "personal_debug_defaults" {
Expand Down
44 changes: 22 additions & 22 deletions internal/provider/repository_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (m *repositoryResourceModel) internalTrackerFrom(ctx context.Context, it *f
}

// internalTrackerTo is a helper function to save Terraform data model into an API struct.
func (m *repositoryResourceModel) internalTrackerTo(ctx context.Context, it *forgejo.InternalTracker) diag.Diagnostics {
func (m *repositoryResourceModel) internalTrackerTo(ctx context.Context, o *forgejo.EditRepoOption) diag.Diagnostics {
if m.InternalTracker.IsNull() || m.InternalTracker.IsUnknown() {
return nil
}
Expand All @@ -272,12 +272,12 @@ func (m *repositoryResourceModel) internalTrackerTo(ctx context.Context, it *for
diags := m.InternalTracker.As(ctx, &intTracker, basetypes.ObjectAsOptions{})

if !diags.HasError() {
if it == nil {
it = new(forgejo.InternalTracker)
if o.InternalTracker == nil {
o.InternalTracker = new(forgejo.InternalTracker)
}
it.EnableTimeTracker = intTracker.EnableTimeTracker.ValueBool()
it.AllowOnlyContributorsToTrackTime = intTracker.AllowOnlyContributorsToTrackTime.ValueBool()
it.EnableIssueDependencies = intTracker.EnableIssueDependencies.ValueBool()
o.InternalTracker.EnableTimeTracker = intTracker.EnableTimeTracker.ValueBool()
o.InternalTracker.AllowOnlyContributorsToTrackTime = intTracker.AllowOnlyContributorsToTrackTime.ValueBool()
o.InternalTracker.EnableIssueDependencies = intTracker.EnableIssueDependencies.ValueBool()
}

return diags
Expand Down Expand Up @@ -327,7 +327,7 @@ func (m *repositoryResourceModel) externalTrackerFrom(ctx context.Context, et *f
}

// externalTrackerTo is a helper function to save Terraform data model into an API struct.
func (m *repositoryResourceModel) externalTrackerTo(ctx context.Context, et *forgejo.ExternalTracker) diag.Diagnostics {
func (m *repositoryResourceModel) externalTrackerTo(ctx context.Context, o *forgejo.EditRepoOption) diag.Diagnostics {
if m.ExternalTracker.IsNull() || m.ExternalTracker.IsUnknown() {
return nil
}
Expand All @@ -336,12 +336,12 @@ func (m *repositoryResourceModel) externalTrackerTo(ctx context.Context, et *for
diags := m.ExternalTracker.As(ctx, &extTracker, basetypes.ObjectAsOptions{})

if !diags.HasError() {
if et == nil {
et = new(forgejo.ExternalTracker)
if o.ExternalTracker == nil {
o.ExternalTracker = new(forgejo.ExternalTracker)
}
et.ExternalTrackerURL = extTracker.ExternalTrackerURL.ValueString()
et.ExternalTrackerFormat = extTracker.ExternalTrackerFormat.ValueString()
et.ExternalTrackerStyle = extTracker.ExternalTrackerStyle.ValueString()
o.ExternalTracker.ExternalTrackerURL = extTracker.ExternalTrackerURL.ValueString()
o.ExternalTracker.ExternalTrackerFormat = extTracker.ExternalTrackerFormat.ValueString()
o.ExternalTracker.ExternalTrackerStyle = extTracker.ExternalTrackerStyle.ValueString()
}

return diags
Expand Down Expand Up @@ -385,7 +385,7 @@ func (m *repositoryResourceModel) externalWikiFrom(ctx context.Context, ew *forg
}

// externalWikiTo is a helper function to save Terraform data model into an API struct.
func (m *repositoryResourceModel) externalWikiTo(ctx context.Context, ew *forgejo.ExternalWiki) diag.Diagnostics {
func (m *repositoryResourceModel) externalWikiTo(ctx context.Context, o *forgejo.EditRepoOption) diag.Diagnostics {
if m.ExternalWiki.IsNull() || m.ExternalWiki.IsUnknown() {
return nil
}
Expand All @@ -394,10 +394,10 @@ func (m *repositoryResourceModel) externalWikiTo(ctx context.Context, ew *forgej
diags := m.ExternalWiki.As(ctx, &extWiki, basetypes.ObjectAsOptions{})

if !diags.HasError() {
if ew == nil {
ew = new(forgejo.ExternalWiki)
if o.ExternalWiki == nil {
o.ExternalWiki = new(forgejo.ExternalWiki)
}
ew.ExternalWikiURL = extWiki.ExternalWikiURL.ValueString()
o.ExternalWiki.ExternalWikiURL = extWiki.ExternalWikiURL.ValueString()
}

return diags
Expand Down Expand Up @@ -971,9 +971,9 @@ func (r *repositoryResource) Create(ctx context.Context, req resource.CreateRequ
// Generate API request body from plan
eopts := forgejo.EditRepoOption{}
data.to(&eopts)
diags = data.internalTrackerTo(ctx, eopts.InternalTracker)
diags.Append(data.externalTrackerTo(ctx, eopts.ExternalTracker)...)
diags.Append(data.externalWikiTo(ctx, eopts.ExternalWiki)...)
diags = data.internalTrackerTo(ctx, &eopts)
diags.Append(data.externalTrackerTo(ctx, &eopts)...)
diags.Append(data.externalWikiTo(ctx, &eopts)...)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -1158,9 +1158,9 @@ func (r *repositoryResource) Update(ctx context.Context, req resource.UpdateRequ
// Generate API request body from plan
opts := forgejo.EditRepoOption{}
data.to(&opts)
diags = data.internalTrackerTo(ctx, opts.InternalTracker)
diags.Append(data.externalTrackerTo(ctx, opts.ExternalTracker)...)
diags.Append(data.externalWikiTo(ctx, opts.ExternalWiki)...)
diags = data.internalTrackerTo(ctx, &opts)
diags.Append(data.externalTrackerTo(ctx, &opts)...)
diags.Append(data.externalWikiTo(ctx, &opts)...)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand Down
89 changes: 78 additions & 11 deletions internal/provider/repository_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func TestAccRepositoryResource(t *testing.T) {
{
Config: providerConfig + `
resource "forgejo_repository" "test" {
owner = {}
name = "tftest"
}
`,
Expand Down Expand Up @@ -83,10 +82,8 @@ resource "forgejo_repository" "test" {
{
Config: providerConfig + `
resource "forgejo_repository" "test" {
owner = {
login = "test_org_1"
}
name = "tftest"
owner = "test_org_1"
name = "tftest"
}
`,
ConfigStateChecks: []statecheck.StateCheck{
Expand Down Expand Up @@ -150,10 +147,79 @@ resource "forgejo_repository" "test" {
{
Config: providerConfig + `
resource "forgejo_repository" "test" {
owner = {
login = "test_user_1"
owner = "test_user_1"
name = "tftest"
}
`,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("allow_merge_commits"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("allow_rebase"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("allow_rebase_explicit"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("allow_squash_merge"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("archived"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("auto_init"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("avatar_url"), knownvalue.StringExact("")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("clone_url"), knownvalue.StringExact("http://localhost:3000/test_user_1/tftest.git")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("created_at"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("default_branch"), knownvalue.StringExact("main")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("default_merge_style"), knownvalue.StringExact("merge")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("description"), knownvalue.StringExact("")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("empty"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("external_tracker"), knownvalue.Null()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("external_wiki"), knownvalue.Null()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("fork"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("forks_count"), knownvalue.Int64Exact(0)),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("full_name"), knownvalue.StringExact("test_user_1/tftest")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("gitignores"), knownvalue.StringExact("")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("has_actions"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("has_issues"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("has_packages"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("has_projects"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("has_pull_requests"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("has_releases"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("has_wiki"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("html_url"), knownvalue.StringExact("http://localhost:3000/test_user_1/tftest")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("id"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("ignore_whitespace_conflicts"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("internal"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("internal_tracker"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("issue_labels"), knownvalue.StringExact("")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("license"), knownvalue.StringExact("")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("mirror"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("mirror_interval"), knownvalue.StringExact("")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("mirror_updated"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("name"), knownvalue.StringExact("tftest")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("open_issues_count"), knownvalue.Int64Exact(0)),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("open_pr_counter"), knownvalue.Int64Exact(0)),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("original_url"), knownvalue.StringExact("")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("owner"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("parent_id"), knownvalue.Null()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("permissions"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("private"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("readme"), knownvalue.StringExact("")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("release_counter"), knownvalue.Int64Exact(0)),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("size"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("ssh_url"), knownvalue.StringExact("git@localhost:test_user_1/tftest.git")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("stars_count"), knownvalue.Int64Exact(0)),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("template"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("trust_model"), knownvalue.StringExact("")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("updated_at"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("watchers_count"), knownvalue.Int64Exact(1)),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("website"), knownvalue.StringExact("")),
},
},
// Update and Read testing
{
Config: providerConfig + `
resource "forgejo_repository" "test" {
owner = "test_user_1"
name = "tftest"
internal_tracker = {
enable_time_tracker = false
allow_only_contributors_to_track_time = false
enable_issue_dependencies = false
}
name = "tftest"
}
`,
ConfigStateChecks: []statecheck.StateCheck{
Expand Down Expand Up @@ -188,6 +254,9 @@ resource "forgejo_repository" "test" {
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("ignore_whitespace_conflicts"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("internal"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("internal_tracker"), knownvalue.NotNull()),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("internal_tracker").AtMapKey("enable_time_tracker"), knownvalue.Bool(false)),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("internal_tracker").AtMapKey("allow_only_contributors_to_track_time"), knownvalue.Bool(false)),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("internal_tracker").AtMapKey("enable_issue_dependencies"), knownvalue.Bool(false)),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("issue_labels"), knownvalue.StringExact("")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("license"), knownvalue.StringExact("")),
statecheck.ExpectKnownValue("forgejo_repository.test", tfjsonpath.New("mirror"), knownvalue.NotNull()),
Expand Down Expand Up @@ -217,9 +286,7 @@ resource "forgejo_repository" "test" {
{
Config: providerConfig + `
resource "forgejo_repository" "test" {
owner = {
login = "test_user_1"
}
owner = "test_user_1"
name = "tftest1"
allow_merge_commits = false
allow_rebase = false
Expand Down

0 comments on commit 74eb6b4

Please sign in to comment.